Float and integer arithmetic follow two different paradigms
blog.pkh.me
blog.pkh.me
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.
Can you chain these operations in a way that will propagate the error to the end result while simultaneously have a compiler produce code without branches after every operation. Safe integer operations alone are not enough for that.
The branching does not cost as much as you think, I'm pretty sure the compiler marks the erroring path as cold, which the cpu can use to elide most of the branch cost through specex
I'd say the reason that programming languages don't have built-in support for checking the CPU's sticky status flags is precisely because of a lack of x86 support. Plenty of languages do have support for checking overflow on each individual operation, e.g. Rust's `overflowing_foo` methods, which return a tuple whose second member is a boolean indicating overflow: https://doc.rust-lang.org/std/primitive.i32.html#method.over...
>I sometimes wish that signed integers were symmetrical. i8 would represent the range of [-127 to 127] with 0xFF representing NaN. Any operation which can not be computed (division by zero, overflows, operation with another NaN, etc.) would result in NaN. For further symmetry we could do the same for signed integers as well.
There's an interesting "special case" of that in the Alesis HR16 and SR16 drum machines. They store 16-bit sample data in an 8-bit ROM. Okay, you say, nothing unusual about that, it's int16 pairs, right?
Nope.
It's int8 data, that gets scaled. Drum sounds tend to decay, so you start off with the 8 bits of sample data representing the eight highest bits of the 16 bit DAC value. At some point the maximum level has decayed by a half so all subsequent values will fit in 7 bits. So there's a "tick" to -128 in the data that makes the sound ASIC "change up a gear" and shift the 8-bit data right one place with the data scaled to suit. Once it decays enough again, another gearchange, another scaling, another "notch" on the Christmas tree-shaped sample.
At the very end of the sample to signify that the note is to stop, you just have a tail of -128s until the shifter has "run off the end" - from memory (it was around 2010 that I last looked at this) there are eight or nine "ticks" in total which are necessary for the sample to stop before it goes right on into the start of the following sample.
So, yeah, nothing new under the sun. Signed 8-bit audio with -128 considered a special value.
Would an int type with three special values +inf, -inf and NaN be useful? In addition to handling overflow, it would be symmetrical; the relation
INT_MIN == -INT_MAX
would hold.And `-ffinite-math-only` is enabled by `-ffast-math` which in turn is enabled by `-Ofast`.
Presumably the airtight implementation assuming Annex F is as follows:
#pragma STDC FENV_ACCESS ON
int my_div(float x, float y, float* r) {
if(!r) {
return -1;
}
fenv_t environment;
if(feholdexcept(&environment) != 0) {
return -1;
}
float result = x / y;
int failed = fetestexcept(
FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
if(fesetenv(&environment) != 0 || failed != 0) {
return -1;
}
*r = result;
return 0;
}
When I asked about the author's implementation I got the response that the author's implementation is deficient in several areas: - Underflow produces a finite result and passes this check.
- Overflow can produce a finite maximum value under some rounding modes.
- Quiet NaNs and valid infinite results fail this check, even without an arithmetic exception.
It also assumes floating-point traps are disabled. Annex F defines the arithmetic behavior; your function still needs a chosen definition of “failure.”
So the answer really is... safely dividing two floats really depends on what trade-offs you're willing to make, and the original reply of just checking for a 0 denominator is honestly the most sensible, portable, and safest.> These eleven functions were defined in C99, and describe the handling of floating-point rounding and exceptions (overflow, zero-divide, etc.).