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.

28 questions
78
votes
4 answers

Why do floating-point numbers have signed zeros?

Why do doubles have -0 as well as +0? What is the background and significance?
Ken Russell
  • 2,348
  • 5
  • 24
  • 39
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
65
votes
2 answers

Why does MSVS not optimize away +0?

This question demonstrates a very interesting phenomenon: denormalized floats slow down the code more than an order of magnitude. The behavior is well explained in the accepted answer. However, there is one comment, with currently 153 upvotes, that…
Vorac
  • 8,726
  • 11
  • 58
  • 101
59
votes
4 answers

Why does Math.min() return -0 from [+0, 0, -0]

I know (-0 === 0) comes out to be true. I am curious to know why -0 < 0 happens? When I run this code in stackoverflow execution context, it returns 0. const arr = [+0, 0, -0]; console.log(Math.min(...arr)); But when I run the same code in the…
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
47
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
  • 36,141
  • 15
  • 83
  • 142
13
votes
4 answers

What is (+0)+(-0) by IEEE floating point standard?

Am I right that any arithmetic operation on any floating numbers is unambiguously defined by IEEE floating point standard? If yes, just for curiosity, what is (+0)+(-0)? And is there a way to check such things in practice, in C++ or other commonly…
se0808
  • 556
  • 3
  • 18
11
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
  • 568
  • 4
  • 13
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
6
votes
2 answers

How can I separate 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 me?
swift-lynx
  • 3,219
  • 3
  • 26
  • 45
6
votes
4 answers

How to convert −0 to string with sign preserved?

x = -0 >> -0 typeof(x) >> "number" x.toString() >> "0" console.log(x) >> -0 How can I convert Javascript's −0 (number zero with the sign bit set rather than clear) to a two character string ("-0") in the same way that console.log does before…
Ivan
  • 4,383
  • 36
  • 27
5
votes
1 answer

In IEEE 754, why does adding negative zero result in a no-op but adding positive zero does not?

I'm toying with some algorithm in Rust (though the language doesn't really matter for my question). Consider the code: #[no_mangle] pub fn test(x: f32) -> f32 { let m = 0.; x + m } fn main() { test(2.); } It produces the following LLVM…
4
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,868
  • 1
  • 9
  • 38
4
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
  • 75
  • 1
  • 8
2
votes
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
  • 673
  • 2
  • 10
  • 25
1
vote
0 answers

How to make a constexpr that equals -0.0

Any way for me to set a constexpr to negative 0? A legacy library uses negative 0 to indicate the calculation failed so I want to create a constexpr float fNEGZERO = -0.0. Normally I would create this using a union between a uint32 and a float and…
Rocinante8
  • 13
  • 3
1
2