Why is 24.0000 not equal to 24.0000 in MATLAB?

The problem you’re having relates to how floating-point numbers are represented on a computer. A more detailed discussion of floating-point representations appears towards the end of my answer (The “Floating-point representation” section). The TL;DR version: because computers have finite amounts of memory, numbers can only be represented with finite precision. Thus, the accuracy of floating-point … Read more

Why are floating point numbers inaccurate?

In most programming languages, floating point numbers are represented a lot like scientific notation: with an exponent and a mantissa (also called the significand). A very simple number, say 9.2, is actually this fraction: 5179139571476070 * 2 -49 Where the exponent is -49 and the mantissa is 5179139571476070. The reason it is impossible to represent … Read more

Why are these numbers not equal?

General (language agnostic) reason Since not all numbers can be represented exactly in IEEE floating point arithmetic (the standard that almost all computers use to represent decimal numbers and do math with them), you will not always get what you expected. This is especially true because some values which are simple, finite decimals (such as … Read more

Why doesn’t GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

Because Floating Point Math is not Associative. The way you group the operands in floating point multiplication has an effect on the numerical accuracy of the answer. As a result, most compilers are very conservative about reordering floating point calculations unless they can be sure that the answer will stay the same, or unless you … Read more