Strange result of dividing by C

6 answers

The problem is that the .575 value does not have an exact representation in floating point encoding. Correction depends on what you do with the value, as well as the effect of inaccuracy.

If this is just a display problem, use rounding to 3 decimal places and you will get 0.575.

If this is due to an inaccuracy in the calculation, try storing the value as a fraction, which will be accurate. You will need to store and process two floating point values, but that will be for sure. Postpone effective division until the last moment when you need a result.

Check if the difference between the two values ​​matches your problem. For example, subtract sqrt epsilon for the value and check how the changes affect the result of the final calculation and compare it with the required or desired accuracy.

Unfortunately, we have to live with the limitation of the representation of real value in float. Using 128-bit precision floats will make the error much smaller, but not zero.

+4
source

This is an artifact on how floating point numbers are stored.

Either you can bypass the sprintf answer ("%. 3f", result), or use some Decimal processing package.

+4
source

Take a look at this - it should tell you everything you need to know about why computers suck at math

+4
source

I suggest you do an online search for “floating point precision” or look at the absolute dozens of other questions on SO, what is your duplicate.

+2
source

This is normal with floating point arithmetic . You can change the way how to print float and double using% .3g, for example

+2
source

You cannot fix it. Not all numbers can be represented as a float, see "How it is stored with a floating point, when it matters . "

+2
source

All Articles