Questions tagged [negative-zero]

Negative zero is a value that occurs in several representations of signed numbers, including IEEE 754 floating point formats. It usually compares equal to positive zero but may result in different results under certain operations.

Negative zero is a value that occurs in several representations of signed numbers, including IEEE 754 floating point formats and integers using ones' complement or sign-and-magnitude representation. It usually compares equal to positive zero but may result in different results under certain operations, for example with IEEE 754 floating point 1. / 0. = ∞ while 1. / -0. = -∞.

Negative zero does not exist in the common two's complement representation for signed integers.

Further details can be found in Wikipedia's article about signed zero.

17 questions
72
votes
4 answers

Why do we have 0.0 and -0.0 in Ruby?

In ruby why can I assign a negative sign to 0.0 float, is this feature useful in any way? Could someone explain this one to me? -0.0 #=> -0.0 -0.0 * -1 #=> 0.0
32
votes
10 answers

Does float have a negative zero? (-0f)

IEEE floating point numbers have a bit assigned to indicate the sign, which means you can technically have different binary representations of zero (+0 and -0). Is there an arithmetic operation I could do for example in C which result in a negative…
tenfour
  • 33,679
  • 12
  • 73
  • 135
10
votes
1 answer

In String.prototype.slice(), should .slice(0,-0) and .slice(0,+0) output the same result?

I came across this quirk while trying to optimise string pluralisation in a game of code golf. I had the idea to write strings as plurals and then use substr to cut the last character off, conditionally: var counter = 1; var myText = counter + "…
jamesinc
  • 493
  • 4
  • 11
8
votes
4 answers

Is it possible to test whether a type supports negative zero in C++ at compile time?

Is there a way to write a type trait to determine whether a type supports negative zero in C++ (including integer representations such as sign-and-magnitude)? I don't see anything that directly does that, and std::signbit doesn't appear to be…
user9723177
5
votes
2 answers

How do I seperate negative zero from positive zero?

I want to be able to test if zero is positive or negative in swift. let neg: CGFloat = -0 let pos: CGFloat = +0 if pos == neg { // this gets executed, but I don't want this } The code above does not work like I need it to, can someone help…
swift-lynx
  • 1,674
  • 1
  • 8
  • 26
2
votes
2 answers

Does R have "negative zero"?

My console tells me that -0 returns 0 and that both c(1:5)[0] and c(1:5)[-0] return integer(0). Does R this mean that R has no concept of "negative zero"?
J. Mini
  • 1,288
  • 4
  • 23
2
votes
1 answer

How to change from negative number to zero in Swift

I have Double data type, cause I need result with floating number, but if my result is negative, it broke all my algorithm. Is there maybe a unsigned data type with floating point?
netatmoBench
  • 55
  • 1
  • 8
1
vote
2 answers

Negative zero int

I require a vector of integers, where I can distinguish between 0 and -0. So far i've come up with the idea of defining a new class called zero_int for this special case.. However, now I can't push both normal integers and the zero_int into the same…
Adam
  • 388
  • 3
  • 8
1
vote
1 answer

Float division returning -0.0 instead of 0.0

I'm working on the exercises from Chapter 5 of How to Think Like a Computer Scientist, and am trying to solve the 3rd exercise: Write a function slope(x1, y1, x2, y2) that returns the slope of the line through the points (x1, y1) and (x2, y2). Be…
dan
  • 319
  • 1
  • 3
  • 9
1
vote
1 answer

Java int argument not negative

I'm writting a method in Java with 3 argumens, the last one (int step) cannot be 0 or negative. At the moment I have it like this: public static int[] createMonotoneIncreasingArray(int start, int end, int step) { if (step <= 0) throw new…
Belen
  • 440
  • 6
  • 21
1
vote
4 answers

Can I print -0.000 as 0.000?

printf("%.3lf\n", -0.0001); This outputs -0.000, but shouldn't it be 0.000? How can I make this print without the minus sign, i.e. 0.000 ?
0
votes
2 answers

Casting '0' to double precision, then multiple by -1 will result in negative zero

I'm trying to negate a double precision value inside jsonb data. I had to cast it to double since PG will return it as text. I found that zero value are printed as -0 Is that fine? how to fix it without "CASE WHEN"
Mahmoud
  • 117
  • 1
  • 8
0
votes
1 answer

When working with financial data in Google Sheets, why are some of my "0.00" values reporting as negative or positive?

I'm currently working with financial data and there's a lot of manual data entry. I'm using a dedicated SUM column combined with conditional formatting to double check my work. Using this number formatting: #,##0.00;(#,##0.00) I'm working with…
0
votes
2 answers

Any examples / scenarios where negative zero in JavaScript make a big difference?

I know that JavaScript has both a normal zero 0 (known as a positive zero +0) and a negative zero -0, however I have never come across a situation where I had to use -0. There are some existing posts on stack overflow about how positive and…
Aaditya Sharma
  • 2,125
  • 1
  • 19
  • 34
0
votes
2 answers

Handle -0 from Database Value in C++

At present I am working on piece of C++ code in which I need to read data from a database, and if database value is non-zero, then I need to apply some further logic. But in the database there are values which are being calculated and can come out…
WENzER
  • 195
  • 1
  • 4
  • 15
1
2