logoalt Hacker News

Float and integer arithmetic follow two different paradigms

47 points • by ibobev • last Monday at 2:09 PM • 9 comments • view on HN

Comments

a_e_k • today at 5:08 PM

For the float stuff, beware of `-ffinite-math-only`. If enabled, `isfinite()` and may compile out as assumed true (with similar assumptions around `isnan()` and `isinf()`).

And `-ffinite-math-only` is enabled by `-ffast-math` which in turn is enabled by `-Ofast`.

weinzierl • today at 2:19 PM

The "integer arithmetic paradigm" would mean either checking after (almost) every operation or living with potentially incorrect results.

The first is terribly inefficient, the second is just wrong (even if insanely common).

This is sad because there is no reason at all why integers couldn’t follow what OP calls the "float paradigm". It doesn’t even have to be slow. Most modern processors (if we ignore x86) support some form of sticky arithmetic flags. Unfortunately, programming languages don’t support them, so they aren’t used.

There is also something to be said about the special treatment division by zero gets.

➕ show 2 replies
dooglius • today at 2:47 PM

FWIW one can configure floating point exceptions at runtime at no overhead, see `man 3 fenv`

➕ show 3 replies
cowlevel • today at 1:58 PM

[flagged]