Remix.run Logo
calibas 2 days ago

> They are not exact

It's not exactly a myth, as the article mentions, they're only exact for certain ranges of values.

> NaN and INF are indication of an error

This is somewhat semantic, but dividing by zero typically does create a hardware exception. However, it's all handled behind the scenes, and you get "Inf" as the result.

You can make it so dividing by zero is explicitly an error, see the "ftrapping-math" flag.

GuB-42 2 days ago | parent | next [-]

I think the myth about exactness is that you can't use strict equality with floating point numbers because they are somehow fuzzy. They are not.

Some operations involve rounding, notably conversion from decimal, but the (rounded) result is an exact number that can be stored and recovered without loss of precision and equality will work as expected. Converting from floating point binary to decimal is exact though, given enough digits (can be >1000 for very small or very large numbers).

Joker_vD 2 days ago | parent [-]

> you can't use strict equality with floating point numbers because they are somehow fuzzy. They are not.

They are though. All arithmetic operations involve rounding, so e.g. (7.0 / 1234 + 0.5) * 1234 is not equal to 7.0 + 617 (it differs in 1 ULP). On the other hand, (9.0 / 1234 + 0.5) * 1234 is equal to 9.0 + 617, so the end result is sometimes exact and sometimes is not. How can you know beforehand which one is the case in your specific case? Generally, you can't, any arithmetic operation can potentially give you 1 ULP of error, and it can (and likely, will) slowly accumulate.

2 days ago | parent | prev [-]
[deleted]