Live data from Hacker News

How does your programming language handle “minus zero” (-0.0)?

lemire.me

61–70 of 219 posts

Re: How does your programming language handle “minus zero” (-0.0)?

#63
post #45

There are still (a few) computers in the world (Sperry Univac legacy support) using one's complement arithmetic, hence having a positive and negative zero in the architecture. https://en.wikipedia.org/wiki/Ones%27_complement

Well all computers supporting IEEE 754 floating point numbers have a positive and a negative 0. Thats quite a few. I prefer to store all my most important data in the sign bit of floats set to zero.

Its also the key to solving this:

How is the result of:

a = 0;

different from:

a = -0;

:-)

Re: How does your programming language handle “minus zero” (-0.0)?

#64

I feel like floating point is a completely separate branch of programming that a lot of coders never use. When they do it's often a mistake, like for currency. Yet floating point is very useful for scientific calculations and simulations, which is what computers were all about for the first couple of decades. I have a suspicion that on a fundamental level, floating point isn't actually good for games or machine learn…

In some game engines, Fixed Point Arithmetic is used instead of Floating Point, because it's faster, and the approximation is generally good enough for a game.

Re: How does your programming language handle “minus zero” (-0.0)?

#65

I feel like floating point is a completely separate branch of programming that a lot of coders never use. When they do it's often a mistake, like for currency. Yet floating point is very useful for scientific calculations and simulations, which is what computers were all about for the first couple of decades. I have a suspicion that on a fundamental level, floating point isn't actually good for games or machine learn…

As someone who has started out making games on Playstation 1 that didn't have floating point hardware, I can with authority say that developing games with only integers suck. Floating point number are far easier and robust to use.

Re: How does your programming language handle “minus zero” (-0.0)?

#66
post #64

I feel like floating point is a completely separate branch of programming that a lot of coders never use. When they do it's often a mistake, like for currency. Yet floating point is very useful for scientific calculations and simulations, which is what computers were all about for the first couple of decades. I have a suspicion that on a fundamental level, floating point isn't actually good for games or machine learn…

In some game engines, Fixed Point Arithmetic is used instead of Floating Point, because it's faster, and the approximation is generally good enough for a game.

I dont think this is true. Integer math is used in some game engines because they need to be deterministic, between networked computers (and different CPUs rounds floating point numbers differently). I have written an engine like that, and i know that Starcraft 2 is all integer for the same reason. No one does it because its faster or easier. Its a pain.

Re: How does your programming language handle “minus zero” (-0.0)?

#67
post #6

Earlier quoted context omitted.

Isn't that true for all correct implementations of negative zero? It's still a neutral for addition

Not quite neutral. You return -0.0 only when needed. >>> 0.0+0.0 0.0 >>> 0.0+(-0.0) 0.0 >>> (-0.0)+0.0 0.0 >>> (-0.0)+(-0.0) -0.0

heh, looks like abs(-0.0) is slightly less than abs(+0.0)!

Re: How does your programming language handle “minus zero” (-0.0)?

#70
post #64

Earlier quoted context omitted.

In some game engines, Fixed Point Arithmetic is used instead of Floating Point, because it's faster, and the approximation is generally good enough for a game.

I dont think this is true. Integer math is used in some game engines because they need to be deterministic, between networked computers (and different CPUs rounds floating point numbers differently). I have written an engine like that, and i know that Starcraft 2 is all integer for the same reason. No one does it because its faster or easier. Its a pain.

I never said it was easier and I never said ALL game engines.

And computing integers IS faster than computing floats, at least on a CPU.

Post reply on HN