integer

Can the input() function in Python dynamically detect the input's data type?

被刻印的时光 ゝ 提交于 2021-02-05 06:39:05
问题 So I was making a console application using Python 3.7; that heavily depends on input (wowz). The application's function is to "swap" between two integer variables' values. And that is not where the problem is at, the problem is when I try to validate the user's input by checking the data-type for the input using a couple "if statements", and no matter what the user inputs using the " input() " function; the input's data-type will always be defined as "" I just want this little piece of ART

How do I use an uint8_t with I/O streams while avoiding the char behavior?

喜你入骨 提交于 2021-02-05 05:59:26
问题 Consider this simple C++ program: #include <cstdint> #include <iostream> int main() { uint8_t var; std::cin >> var; std::cout << "var = " << var << '\n'; if (var == 1) { std::cout << "var is 1\n"; } else { std::cout << "var is not 1\n"; } } Running it shows some surprising behavior. When the input is 1 , the output is var = 1 var is not 1 which is clearly absurd! After quite a few varied tests, I realized what is happening—it's reading and writing a char! Still, it's not the behavior I want—I

Why I am getting -2147483648 and -1's multiplication, negative i.e. -2147483648, instead it should be +2147483648 [duplicate]

这一生的挚爱 提交于 2021-02-04 21:32:46
问题 This question already has answers here : How are integers internally represented at a bit level in Java? (10 answers) Closed 3 months ago . This is the code snippet I had written for this question in Leetcode. public static int quotient(int dividend,int divisor){ int n=Math.abs(divisor),count=0,ans=Integer.MAX_VALUE-1; if(Math.abs(dividend)==1 && Math.abs(divisor)==1){ return dividend*divisor; } else if(Math.abs(divisor)==1){ if(dividend<0 && divisor<0) return Math.abs(dividend); return

-9'223'372'036'854'775'808LL is unsigned

时间秒杀一切 提交于 2021-02-04 05:56:50
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

-9'223'372'036'854'775'808LL is unsigned

╄→尐↘猪︶ㄣ 提交于 2021-02-04 05:56:19
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

Drop un-needed decimal “.0”

心不动则不痛 提交于 2021-02-02 09:54:25
问题 I'm making a simple calculator and I am having an issue with it showing a decimal on what I want to just be a whole number. For example, if the entered expression is "50 + 50" the answer will come out "100.0". I understand that it's happening because my output is set as a double, but I can't figure out how to convert those numbers to integer only when the answer is ".0". My output answer code: fun equal (view: View) { secondnum = editText.text.toString() decpressed = 0 var sum = 0.0 when (op)

Mypy doesn't throw an error when mixing booleans with integers

亡梦爱人 提交于 2021-02-01 20:41:48
问题 I am trying to use mypy to check a Python 3 project. In the example below, I want mypy to flag the construction of the class MyClass as an error, but it doesn't. class MyClass: def __init__(self, i:int) -> None: pass obj = MyClass(False) Can anyone explain this, please? I.e. explain why mypy does not report an error? 回答1: It’s because — unfortunately! — booleans in Python are integers. As in, bool is a subclass of int : In [1]: issubclass(bool, int) Out[1]: True Hence the code typechecks, and

Mypy doesn't throw an error when mixing booleans with integers

ⅰ亾dé卋堺 提交于 2021-02-01 20:41:26
问题 I am trying to use mypy to check a Python 3 project. In the example below, I want mypy to flag the construction of the class MyClass as an error, but it doesn't. class MyClass: def __init__(self, i:int) -> None: pass obj = MyClass(False) Can anyone explain this, please? I.e. explain why mypy does not report an error? 回答1: It’s because — unfortunately! — booleans in Python are integers. As in, bool is a subclass of int : In [1]: issubclass(bool, int) Out[1]: True Hence the code typechecks, and

Convert integers to strings to create output filenames at run time

感情迁移 提交于 2021-01-29 17:25:00
问题 I have a program in Fortran that saves the results to a file. At the moment I open the file using OPEN (1, FILE = 'Output.TXT') However, I now want to run a loop, and save the results of each iteration to the files 'Output1.TXT' , 'Output2.TXT' , 'Output3.TXT' , and so on. Is there an easy way in Fortran to constuct filenames from the loop counter i ? 回答1: you can write to a unit, but you can also write to a string program foo character(len=1024) :: filename write (filename, "(A5,I2)") "hello

Float is not converting to integer pandas

纵然是瞬间 提交于 2021-01-29 11:17:58
问题 I used this code to convert my float numbers into an integer, however, it does not work. Here are all step I gone through so far: Step 1: I converted timestamp1 and timestamp2 to datetime in order subtract and get days: a=pd.to_datetime(df['timestamp1'], format='%Y-%m-%dT%H:%M:%SZ') b=pd.to_datetime(df['timestamp2'], format='%Y-%m-%dT%H:%M:%SZ') df['delta'] = (b-a).dt.days Step 2: Converted the strings into integers as the day: df['delta'] = pd.to_datetime(df['delta'], format='%Y-%m-%d',