gcc -O0 still optimizes out “unused” code that should raise an FP exception. Is there a compile flag to change that?

Why does gcc not emit the specified instruction? A compiler produces code that must have the observable behavior specified by the Standard. Anything that is not observable can be changed (and optimized) at will, as it does not change the behavior of the program (as specified). How can you beat it into submission? The trick … Read more

What is the difference between std::min/std::max and fmin/fmax?

fmin and fmax are specifically for use with floating point numbers (hence the “f”). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform. std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a … Read more

How can I deal with floating point number precision in JavaScript? [duplicate]

From the Floating-Point Guide: What can I do to avoid this problem? That depends on what kind of calculations you’re doing. If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype. If you just don’t want to see all those extra decimal places: simply … Read more

what does NaN mean for doubles?

From Wikipedia : In computing, NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities. And from MSDN : … Read more

Why aren’t rational numbers implemented and stored as fractions with zero loss of information? [closed]

The reason they are not stored this way by default is that the range of valid values that can fit in a fixed set of bits is smaller. Your float class can store numbers between 1/MAXINT and MAXINT (plus or minus). A C/C++ float can represent numbers between 1E+37 and 1E-37 (plus or minus). In … Read more

What’s the relative speed of floating point add vs. floating point multiply

It also depends on instruction mix. Your processor will have several computation units standing by at any time, and you’ll get maximum throughput if all of them are filled all the time. So, executing a loop of mul’s is just as fast as executing a loop or adds – but the same doesn’t hold if … Read more