negative-number
In Python, what is a good way to round towards zero in integer division?
Do floating point division then convert to an int. No extra modules needed. Python 3: >>> int(-1 / 2) 0 >>> int(-3 / 2) -1 >>> int(1 / 2) 0 >>> int(3 / 2) 1 Python 2: >>> int(float(-1) / 2) 0 >>> int(float(-3) / 2) -1 >>> int(float(1) / 2) 0 >>> int(float(3) / … Read more
How does the modulo (%) operator work on negative numbers in Python?
Unlike C or C++, Python’s modulo operator (%) always return a number having the same sign as the denominator (divisor). Your expression yields 3 because (-5) / 4 = -1.25 –> floor(-1.25) = -2 (-5) % 4 = (-2 × 4 + 3) % 4 = 3. It is chosen over the C behavior because … Read more
processing negative number in “accounting” format
If you create an “as.acntngFmt” method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses(“acnt”). setClass(“acntngFmt”) # [1] “acntngFmt” setAs(“character”, “acntngFmt”, function(from) as.numeric( gsub(“\\)”, “”, gsub(“\\(“, “-“, from)))) Input <- “A, B, C (1.76), 1%, 3.50€ 2.00, 2%, 4.77€ 3.000, 3% , €5.68” DF <- read.csv(textConnection(Input), header = … Read more
2’s complement hex number to decimal in java
This seems to trick java into converting the number without forcing a positive result: Integer.valueOf(“FFFF”,16).shortValue(); // evaluates to -1 (short) Of course this sort of thing only works for 8, 16, 32, and 64-bit 2’s complement: Short.valueOf(“FF”,16).byteValue(); // -1 (byte) Integer.valueOf(“FFFF”,16).shortValue(); // -1 (short) Long.valueOf(“FFFFFFFF”,16).intValue(); // -1 (int) new BigInteger(“FFFFFFFFFFFFFFFF”,16).longValue(); // -1 (long) Example here.
Why does the most negative int value cause an error about ambiguous function overloads?
This is a very subtle error. What you are seeing is a consequence of there being no negative integer literals in C++. If we look at [lex.icon] we get that a integer-literal, integer-literal decimal-literal integer-suffixopt […] can be a decimal-literal, decimal-literal: nonzero-digit decimal-literal ’ opt digit where digit is [0-9] and nonzero-digit is [1-9] and … Read more
Integer division & modulo operation with negative operands in Python
The integer division there is just taking the floor of the number obtained at the end. 10/3 -> floor(3.33) -> 3 -10/3 -> floor(-3.33) -> -4 (Why it floors) The modulo operation on the other hand is following the mathematical definition.
Java: right shift on negative number
Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>>. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Arithmetic shift >> will keep the sign bit. Unsigned shift >>> will not keep the sign bit (thus filling 0s). (images from Wikipedia) By the way, both arithmetic left shift and logical … Read more
How to replace negative numbers in Pandas Data Frame by zero
If all your columns are numeric, you can use boolean indexing: In [1]: import pandas as pd In [2]: df = pd.DataFrame({‘a’: [0, -1, 2], ‘b’: [-3, 2, 1]}) In [3]: df Out[3]: a b 0 0 -3 1 -1 2 2 2 1 In [4]: df[df < 0] = 0 In [5]: df Out[5]: … Read more
Representation of negative numbers in C?
ISO C (C99 section 6.2.6.2/2 in this case but it carries forward to later iterations of the standard(a)) states that an implementation must choose one of three different representations for integral data types, two’s complement, ones’ complement or sign/magnitude (although it’s incredibly likely that the two’s complement implementations far outweigh the others). In all those … Read more