Live data from Hacker News

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

lemire.me

111–120 of 219 posts

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

#111
post #36

Earlier quoted context omitted.

∞ is not a number. Using it as a number is a hack invented by mathematicians.

∞ is not used as a number by mathematicians. Maybe by engineers.

Ehh.

https://en.wikipedia.org/wiki/Extended_real_number_line

NB this is hardly nonstandard analysis.

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

#112

Earlier quoted context omitted.

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.

Isn't that what caused the PS1 graphics "wobble"? I remember you could just stare at a wall, rotate the camera, and the textures would slightly jitter and move. It's really obvious in some the Metal Gear cutscenes, especially this conversation with Donald Anderson where the camera pans up from the floor (at 15m) or the sweep(15m31s): https://www.youtube.com/watch?v=VLTqIXZ1jQQ#t=15m00s I never had a PS1 but it always…

> I never had a PS1 but it always bugged me when playing on someone elses.

Conversely I like the fact a lot that it does. And some others that probably have fond memories of PS1 too recreated this on modern computers using shaders.

https://github.com/dsoft20/psx_retroshader

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

#113

Earlier quoted context omitted.

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.

Isn't that what caused the PS1 graphics "wobble"? I remember you could just stare at a wall, rotate the camera, and the textures would slightly jitter and move. It's really obvious in some the Metal Gear cutscenes, especially this conversation with Donald Anderson where the camera pans up from the floor (at 15m) or the sweep(15m31s): https://www.youtube.com/watch?v=VLTqIXZ1jQQ#t=15m00s I never had a PS1 but it always…

This is because of two things: the affine texture mapping was incorrect, and there was no sub-pixel precision. The sub-pixel part is kinda float-related (though you could make use of fixed point too).

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

#114
post #84

Earlier quoted context omitted.

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.

Only integers? Wow. Were int pairs used to represent numbers with decimals or what?

The GPU made use of fixed point numbers. So you have a set number of decimal places for a number.

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

#115
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.

Which modern general-purpose CPUs do not follow IEEE 754? I don't think what you're saying is true.

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

#116
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.

It might be a little faster for addition and subtraction in synthetic benchmarks -- and with the use of pipelining might end up being slower overall -- but multiplication and division are considerably faster with floating point.

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

#117
post #83

Earlier quoted context omitted.

>And computing integers IS faster than computing floats, at least on a CPU. In the abstract yes. In reality not so much. A lot of what floats are used for in games is vector math (you are often in a 2D/3D world), and vector math in fixed point requires a lot more work in integer math since you constantly need to shift down things in order to avoid overflow. Overflow bugs are a constant problem when doing dot products…

I never use the square root in games, I always compare squared distances (since the properties are the same). If you try to do with Fixed Point Arithmetic what was intended to be done with Floating Point, you're the problem. A 32 bits integer can hold values up to 4 billions, if I have that kind of value in a simple game, then yes i will switch to Floating Point Arithmetic, but when does that use case happen if you'r…

> A 32 bits integer can hold values up to 4 billions, if I have that kind of value in a simple game, then yes i will switch to Floating Point Arithmetic, but when does that use case happen if you're not writing a physic simulation game ?

All the time. Let me give you an example from a game I made. During early R&D I used 32 bit numbers and a fixed point of 11 bits. That means that a normalized vector is between 1024 and -1023. You can do 2 dot products between vectors before it breaks it breaks. (10 + 10 + 10 bits plus one sign bit). That means that you have to do a lot of shifting down. the world can only be 20 bits large, because you need to be able to multiply world coordinates with vectors without getting overflow.

20 bis is very low resolution for a real-time world. You get problems with things not being able to move slow enough at high frame rates. (this was in 2D). Switching to 64 bit was the right move.

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

#118

Earlier quoted context omitted.

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.

Which modern general-purpose CPUs do not follow IEEE 754? I don't think what you're saying is true.

[deleted]

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

#119
post #61

Ruby seems to handle this correctly: >> 1 / -0.0 => -Infinity Although you have to put the 0.0, otherwise it treats it as an integer and makes it 0.

Dividing by zero is "undefined" not "Infinity" because there is not a "well defined limit." https://en.wikipedia.org/wiki/Division_by_zero

From the article you link:

> Depending on the programming environment and the type of number (e.g. floating point, integer) being divided by zero, it may generate positive or negative infinity by the IEEE 754 floating point standard.

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

#120

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.

Did you use macros/operator overloading for all that bit shifting or just hardcoded it everywhere?

We made a simple 3d demo for uni using fixed math (and we were clueless then and weren't using macros because sometimes you could refactor some operations out that way and we wanted all the speed). There were lots of bugs caused by this.

Can't imagine how painful writing a whole game this way would be.

Post reply on HN