https://godbolt.org/z/1qjxvW3KK
I found this out the hard way several years ago and still have my own implementations of these functions laying about in case I want to use -ffast-math.
float
unit_price (float volume, int count) {
return volume / count;
}
is only called where count > 0 if (count > 0)
total += unit_price(volume, count);
Are you confusing it with the opposite scenario: total += unit_price(volume, count);
if (count == 0)
printf("oops\n");
That test might be optimized out because divide by zero is undefined behavior.Unfortunately most compilers have too permissive default options.
C and C++ programs should always be compiled, at least during development, with strict options, e.g.:
-fsanitize=undefined -fsanitize-undefined-trap-on-error
or equivalent options, which eliminate all the horror stories with unexpected behavior caused by integer divide-by-zero or overflow, pointer overflow, out-of-bounds access and many others.
Because unlike integer operations the FP operations are standardized, there is no need for compiler options, the behavior for exceptions can be controlled from the program with the functions defined in <fenv.h> in C or <cfenv> in C++.