Live data from Hacker News

Examples of floating point problems

jvns.ca

141–150 of 179 posts

Re: Examples of floating point problems

#141
post #135

Earlier quoted context omitted.

> The result of any operation on a NaN is a NaN. That's not true! maxNum(nan, x) = x.

The standard for comparison with NaNs is to always return false [0]. So that entirely depends on how max() is implemented. A naive implementation of max() might just as easily instead return NaN for that. Or if max(NaN, x) is x, then it may give NaN for max(x, NaN). Note that the fact that comparisons always return false also means that sorting an array of floating point values that contain NaNs can very easily break…

maxNum is (was - as another poster mentioned it is now deprecated) a specific function defined in the IEEE specification. This isn't a language level comparison function but a specific operation on floating points that has to behave the way I described. It is not a comparison function.

maximumMagnitude (and a few similar ones) replace it but behave similarly.

Re: Examples of floating point problems

#142
post #123

Earlier quoted context omitted.

> The result of any operation on a NaN is a NaN. That's not true! maxNum(nan, x) = x.

It's complicated. https://754r.ucbtest.org/background/minNum_maxNum_Removal_De...

Looks like maximum and maximumMagnitude have the same relevant property (NaN is not viral).

Re: Examples of floating point problems

#143

Earlier quoted context omitted.

> Both C and C++. Seems C++ only added it in C++11. Surprising. > That's why I said in the second part that we need to do away with them. Do away entirely with fast math calculations? That would be horrible. They exist for a very good reason: Some applications are too slow without them. Enabling subnormal numbers can really slow things down. I'd wager that for the majority of programs written, the fast math is as goo…

> Some applications are too slow without them. I fully expect the intersection between programs where floating point instructions are a speed bottleneck and programs where the numerical instability that fast_math can cause is not a problem is the empty set.

IIRC we used -ffast-math for numerical simulations of radar cross sections. Fast math was a decent perf win but didn't have any negative effects on the results.

Most programs don't care about the difference between ((a+b)+c) vs (a+(b+c)). Why bother with NaNs if you know you can't get them? Etc.

Re: Examples of floating point problems

#144

in computer graphics, some people try to accumulate the transformations in 1 matrix. So A_{n+1} = T_{n} * A_{n} where T_{n} is a small transformation like a rotation around an axis They learn by experience that they also slowly accumulate errors and end up with a transformation matrix A that's no longer orthogonal and will skew the image. Or people try to solve large lineair systems of floating points with a naive Ga…

moreover.... floating point addition is not commutative, nor is is associative.

What would be an example of a floating-point addition operation that doesn't commute?

Re: Examples of floating point problems

#145

> NaN/infinity values can propagate and cause chaos NaN is the most misunderstood feature of IEEE floating point. Most people react to a NaN like they'd react to the dentist telling them they need a root canal. But NaN is actually a very valuable and useful tool! NaN is just a value that represents an invalid floating point value. The result of any operation on a NaN is a NaN. This means that NaNs propagate from the…

[deleted]

Re: Examples of floating point problems

#146

> NaN/infinity values can propagate and cause chaos NaN is the most misunderstood feature of IEEE floating point. Most people react to a NaN like they'd react to the dentist telling them they need a root canal. But NaN is actually a very valuable and useful tool! NaN is just a value that represents an invalid floating point value. The result of any operation on a NaN is a NaN. This means that NaNs propagate from the…

> What do you return for an index into the array? None in an Optional/Maybe, and Error in a Result, Null in a nullable type, an exception,

I concur. I would use an `Option`, `Result`, or enum for all of those scenarios, depending on the details.

Re: Examples of floating point problems

#148
post #47

I had one issue where pdftotext would produce different output on different machines (Linux vs Mac). It broke some of our tests. I tracked down where it was happening (involving an ==), but it magically stopped when I added print statements or looked at it in the debugger. It turns out the x86 was running the math at a higher precision and truncating when it moved values out of registers - as soon as it hit memory, t…

macOS doesn't use -ffloat-store, it uses SSE for floats instead of x87 because it only supports machines with SSSE3. You wanted -mfpmath=sse or -march=native.

Re: Examples of floating point problems

#150
post #60
post #43

Example 4 mentions that the result might be different with the same code. Here is an example that is particularly counter-intuitive. Some CPU have the instruction FMA(a,b,c) = ab + c and it is guaranteed to be rounded to the nearest float. You might think that using FMA will lead to more accurate results, which is true most of the time. However, assume that you want to compute a dot product between 2 orthogonal vecto…

Yes, and this is not just a theoretical concern: There was an article here [1] in 2021 claiming that Apple M1's FMA implementation had "flaws". There was actually no such flaw. Instead, the author was caught off guard by the very phenomenon you are describing. [1] https://news.ycombinator.com/item?id=27880461

Although funnily enough the Windows fma implementation is significantly flawed.
Post reply on HN