Live data from Hacker News

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

lemire.me

181–190 of 219 posts

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

#181

Earlier quoted context omitted.

Floats are essential for machine learning, scientific computing, and any other application where the goal is to model mathematical processes involving real numbers. The idea that there is something out there that could do the job better than floats is almost certainly wrong. I'm fascinated by how common it is for programmers to hate floats. Yes, if you write business software for a living you may not have much use fo…

> Floats are essential for Not necessarily true. Theoretically you can replace floats by fixed-point representations given enough bits, and in practice this also works often. It's a pity languages/hardware don't have good built-in support for fixed-point numbers though.

Fixed point is obsolete on the vast majority of compute devices today. It is only a reasonable alternative in ultra low power scenarios or on tech stacks that haven't migrated to modern architectures, like some DSPs (which are also really only applicable in ultra low power devices).

On modern CPUs, fixed point is slower and error prone (both in usage and the computations themselves) relative to floating point.

I don't think it's a pity that fixed point support is out the door. It sucks. Floats minus subnormals are the least surprising numerical representation out there, and the most versatile.

Do you really want to worry about whether your numbers are saturating or wrapping around? Keeping track of what your max/min are? Tracking decimal places? Implementing division by hand? Screw all that. Fixed point is awful and is essentially an academic exercise today.

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

#182

Earlier quoted context omitted.

> Floats are essential for Not necessarily true. Theoretically you can replace floats by fixed-point representations given enough bits, and in practice this also works often. It's a pity languages/hardware don't have good built-in support for fixed-point numbers though.

Okay, so now, relative to floating point arithmetic, you've sacrificed an enormous amount of dynamic range and have to worry about overflow and underflow much more than before. What did you gain from this, exactly? I'm sure there are some special situations where fixed point is better, but for general purpose scientific and technical computing, floating point is obviously the right choice.

On some low power architectures without FPUs it is significantly more power/compute efficient to use fixed point arithmetic. But that's really it.

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

#183

Earlier quoted context omitted.

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.

division, yes, but multiplication is faster, but it's not huge. The limiting factor for multiplication is that it's O(N^2) in multiplied digits; so a 32-bit fixed point has 32x32 and IEEE fp32 multiplication has 23x23.

That's assuming that you're implementing multiplication in software though, right? Since CPUs have dedicated multipliers, aren't both around a single cycle?

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

#184

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…

What better alternative is there for handling currencies? Multiplying the original amount by 100 and casting to an int? ($3.50 -> 350)?

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

#185
post #30

Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.

When does this need arise? Well, otherwise inverting a value can change it's sign and in particular inverting -∞ twice will give you +∞, and being off by "2∞" is a pretty large error for a lot of computations ;) You can end up with zeros and infinities pretty easily because you overflow or underflow the range of floating point precision, and generally you want something sensible to happen in typical cases, even if so…

So no then? As you say, epsilon should be used in this case.

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

#186

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.

It depends on which 0 you are talking about. The 1-1=0 or the 1/infinity zero. Somewhere deep in a physics department there is an equation where the distinction either confirms or denies the multiverse.

1/infinity is not 0, it's infinitesimal.

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

#187
post #169

Earlier quoted context omitted.

For some reason many programmers love to hate technologies that are old. Hate floats, love big decimals Hate OOP, love functional Hate RDBMS, love nosql Hate html web pages, love SPA In my opinion it's a confluence of influences. 1) the same social media attitudes poisoning society generally. "I've got an opinion, and it's worth just as much as yours". News flash - opinions are like arseholes, everyone has one. 2) In…

Not disputing your other points, but functional programming is by most measures older than OOP. The FP hype now is definitely more a matter of rediscovering the wisdom of the past, and I think that's a good thing.

That's an excellent point.

Although to go down that road, schemaless databases preceded RDBMS by decades, and then were replaced for good reasons.

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

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

"To a first approximation, floating-point numbers behave like real numbers"

This is incorrect. To count as an approximation, there have to be some accuracy bounds. This is impossible to define, as the reals don't have the same cardinality as any floating point number system.

Now, for many interesting and useful real-valued functions, a float-valued function can be defined that is an approximation. But there is no general way to map a function defined on the reals to a function defined on the floats with a known accuracy.

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

#189

Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.

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.

You could still use the bitwise -0 value to represent another real number. Do you gain anything?

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

#190

Earlier quoted context omitted.

division, yes, but multiplication is faster, but it's not huge. The limiting factor for multiplication is that it's O(N^2) in multiplied digits; so a 32-bit fixed point has 32x32 and IEEE fp32 multiplication has 23x23.

That's assuming that you're implementing multiplication in software though, right? Since CPUs have dedicated multipliers, aren't both around a single cycle?

They are not in general (iirc 64b mult is down to 3 cycles now), but usually in an x86 cpu it can run microinstructions out of order enough so that the multiplication looks like it's finished in one cycle.
Post reply on HN