Questions tagged [negative-number]

A negative number is a number that is less than 0. It is signified by a preceding hyphen (i.e. -12,345).

This tag should be used for any questions dealing specifically with negative numbers.

599 questions
284
votes
13 answers

Modulo operation with negative numbers

In a C program I was trying the below operations (Just to check the behavior) x = 5 % (-3); y = (-5) % (3); z = (-5) % (-3); printf("%d ,%d ,%d", x, y, z); It gave me output as (2, -2 , -2) in gcc. I was expecting a positive result every…
Alva
  • 2,882
  • 3
  • 14
  • 8
235
votes
19 answers

Why prefer two's complement over sign-and-magnitude for signed numbers?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1…
Ray
  • 3,468
  • 8
  • 26
  • 27
188
votes
23 answers

Make a negative number positive

I have a Java method in which I'm summing a set of numbers. However, I want any negatives numbers to be treated as positives. So (1)+(2)+(1)+(-1) should equal 5. I'm sure there is very easy way of doing this - I just don't know how.
Tray
  • 3,105
  • 8
  • 26
  • 25
153
votes
12 answers

How does the modulo (%) operator work on negative numbers in Python?

Exactly how does the % operator work in Python, particularly when negative numbers are involved? For example, why does -5 % 4 evaluate to 3, rather than, say, -1?
facha
  • 11,862
  • 14
  • 59
  • 82
133
votes
7 answers

Best way to make Java's modulus behave like it should with negative numbers?

In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ? b + a : a % b
fent
  • 17,861
  • 15
  • 87
  • 91
132
votes
9 answers

How to replace negative numbers in Pandas Data Frame by zero

I would like to know if there is someway of replacing all DataFrame negative numbers by zeros?
Hangon
  • 2,449
  • 7
  • 23
  • 31
111
votes
8 answers

Javascript Math Object Methods - negatives to zero

in Javascript I can't seem to find a method to set negatives to zero? -90 becomes 0 -45 becomes 0 0 becomes 0 90 becomes 90 Is there anything like that? I have just rounded numbers.
FFish
  • 10,964
  • 34
  • 95
  • 136
104
votes
15 answers

How does java do modulus calculations with negative numbers?

Am I doing modulus wrong? Because in Java -13 % 64 evaluates to -13 but I want to get 51.
Jakir00
  • 2,023
  • 4
  • 20
  • 32
99
votes
7 answers

How to check if a number is negative?

I want to check if a number is negative. I’m searching for the easiest way, so a predefined JavaScript function would be the best, but I didn’t find anything yet. Here is what I have so far, but I don’t think that this is a good way: function…
Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
93
votes
7 answers

Is it possible to differentiate between 0 and -0?

I know that the integer values 0 and -0 are essentially the same. But, I am wondering if it is possible to differentiate between them. For example, how do I know if a variable was assigned -0? bool IsNegative(int num) { // How ? } int num =…
Filip Minx
  • 2,438
  • 1
  • 17
  • 32
92
votes
3 answers

Why does the most negative int value cause an error about ambiguous function overloads?

I'm learning about function overloading in C++ and came across this: void display(int a) { cout << "int" << endl; } void display(unsigned a) { cout << "unsigned" << endl; } int main() { int i = -2147483648; cout << i << endl;…
infinite loop
  • 1,309
  • 9
  • 19
73
votes
6 answers

Advantage of 2's complement over 1's complement?

What is the advantage of 2's complement over 1's complement in negative number representation in binary number system? How does it affect the range of values stored in a certain bit representation of number in binary system?
pranphy
  • 1,787
  • 4
  • 16
  • 22
56
votes
3 answers

Loop starting at -1 doesn't print anything

This program is supposed to print out the elements of array, but when it is run, no output is shown. #include #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = { 23, 34, 12, 17, 204, 99, 16 }; int main() { int…
rohit kumar
  • 739
  • 5
  • 8
49
votes
2 answers

Are there any non-twos-complement implementations of C?

As we all no doubt know, the ISO C standard (and C++ as well, I think, though I'm more interested on the C side) allows three underlying representations of signed numbers: two's complement; ones' complement; and sign/magnitude. Wikipedia's entry…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
47
votes
8 answers

In Python, what is a good way to round towards zero in integer division?

1/2 gives 0 as it should. However, -1/2 gives -1 , but I want it to round towards 0 (i.e. I want -1/2 to be 0), regardless of whether it's positive or negative. What is the best way to do that?
blacktrance
  • 905
  • 2
  • 11
  • 25
1
2 3
39 40