Live data from Hacker News

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

lemire.me

121–130 of 219 posts

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

#121

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…

yes, and the PS1 had a vector unit built in to the CPU that was used for world transform and it was only 24 bits.

Early PS1 games fit the entire world in to a 24 bit space, and you can tell that there is steping if you drive very slow in a game like Ridge Racer. Later games moved the world around in the coordinate system to manage precision.

Another reason why the graphics looks extra "popy" on PS1 (And early DirectX games) is that vertices where computed at a pixel accuracy. OpenGL, Doom, Quake and more modern hardware have sub pixel accuracy.

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

#122

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…

> When they do it's often a mistake, like for currency. The most beautiful FP bug I remember was a denial of service in some webservers where by setting the header "Accepted Language: en-gb;q=0.3 en;q=0.8 ..." to a specific value you could send the official Java floating-point parser in an infinite loop (and this affected several Java webservers). So at each webpage request, you were sending one CPU core of the webse…

Meh, it's not like there can't be a bug in whatever other parsing code a webserver uses.

Maybe using a float is overkill in this case, since you're never going to hit more than a hundred languages. But it's at least not setting any limitations.

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

#123

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.

They all follow IEEE 754. But that defines the representation of the bits in a floating point number, not what happens when you operate on them. Different implementations do rounding differently, and there are arguments about how to do this correctly. There can be multiple ways to represent the same number even (this is a reason not to ever do a == compare on floating point number that have been operated on). Not only have there been differences between CPU makers, but also between different CPU of the same vendor.

A separate, complicating factor is that floating point math often don't yield the same results with different levels of optimization turned on. The reason to use integer math for games is because you want it to be deterministic so can be an issue.

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

#124

Earlier quoted context omitted.

Computers (obviously) have to approximate the majority of actual mathematical numbers, as they do not have infinite storage. If you've got two numbers, +0.0000001 and -0.0000001, but you can't represent that precision, can you see how it's less bad to round to +0.0000 and -0.0000 rather than to just 0.0000? It's encoding strictly more information.

Reading your comment gave me a realization that transformed my understanding of floats. What just clicked for me was that in any system where we use finite precision to store exponents, we can't actually have zero as a normal value... we'll always underflow the exponent before we get to actual mathematical zero. So +0/-0 is actually a convenient misnomer. They really are +/- epsilon. The only way to have zero is if i…

The same is true on the other end of the limit. Infinity in floating point really just means "some value larger than can be represented".

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

#125
post #84

Earlier quoted context omitted.

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

In signal processing on constrained domains (constrained including the lack of an FPU), the usual alternative to floating point is "fixed point" arithmethic. Basically you use the platform's native types (e.g. uint32) and decide which of the bits are for the integer part and which are for the fractional. uint32 can be interpreted as 20/12 for example. Your register went from representing "units" to representing "2^-1…

The Wikipedia page explains the operations: https://en.wikipedia.org/wiki/Q_(number_format)

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

#126
post #93

Earlier quoted context omitted.

That's right. It's a symbol. When you see it in an expression, you're probably expected to interpret it in light of a limit of some kind. It's just convenient to write it into an expression rather than use cumbersome limit notation all over the place. Like many notational shortcuts, it's a hack supported by proof. ;-)

This explanation feels off, to me. Aren't all numbers symbols? Pi, e, 10, 0xA, 9.99999...?

In my view, numbers are numbers, and symbols are symbols. There's an agreement that a symbol represents a number, but there's not a one-to-one relationship between available symbols and available numbers. Normally this isn't a problem, and we treat them interchangeably. And indeed the distinction may only be a philosophical oddity or a matter for mathematicians. But I believe nonetheless that there is a distinction.

Now I was merely an undergrad math major, which means I topped out before learning this stuff in a formal way. But at my primitive level of understanding, I think of a number as something that behaves like a number within a given system. What I learned in my courses was how different kinds of numbers behaved: Whole numbers, reals, complex, vectors and tensors, etc. I remember reading a definition of "tensor" that was to the effect of: A tensor is something that behaves like a tensor, meaning that the important thing is the behavior.

Another post in this page expressed that we should be particularly cautious when dealing with numbers, symbols, and IEEE floats, notably to beware that IEEE floats and real numbers don't always behave the same. That was treated in one of my math courses, "Numerical Analysis." You could also get CS credit for that course, suggesting its practical importance.

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

#127
post #94

Earlier quoted context omitted.

Floats have a totally different algebra than the reals, though.

Only in the strictly mathematical sense. To a first approximation, floating-point numbers behave like real numbers, and that's good enough for many use-cases (not all, though). Similarly, fixed-width integers have a totally different algebra than true integers, and yet they're immensely useful.

fixed-width integers have exactly the same semantics as true integers bar overflow, which is a very easy to understand concept.

Floating-point numbers behave like a physicist would do calculations rounding at every step, but doing everything in binary. Or hexadecimal if you prefer, it's equivalent, but still difficult to get your head around to. Then there additionally is + and - 0 and + and - infinity, and several settings for how rounding is done exactly.

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

#128
post #91
post #85

Earlier quoted context omitted.

Lots of the math in games and ML assumes you’re operating in the reals. For example, the optimization in neural networks. Integers and fixed point won’t work.

Doesn't ML work fine with fixed point numbers? I don't think there is a particular use case for very large or very small numbers.

It's the exact opposite. It really loves large and small numbers, the precision of a number doesn't matter that much

That's the reason why they introduced https://en.wikipedia.org/wiki/Bfloat16_floating-point_format

It's similar to normal half precision float but the exponent is the same as with 32bit float and mantissa has been sacrificed to get room for it.

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

#129
post #93

Earlier quoted context omitted.

That's right. It's a symbol. When you see it in an expression, you're probably expected to interpret it in light of a limit of some kind. It's just convenient to write it into an expression rather than use cumbersome limit notation all over the place. Like many notational shortcuts, it's a hack supported by proof. ;-)

This explanation feels off, to me. Aren't all numbers symbols? Pi, e, 10, 0xA, 9.99999...?

But not all symbols are numbers.

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

#130

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…

No. The DS uses only fixed point numbers and does not wobble.
Post reply on HN