Question

I have a quirky problem where if I input say 720.60 into

sscanf("%f", &amount);

where amount is of type float, the value in the variable changes to 720.59967.

How can I bypass this and prevent it from changing my value?

Was it helpful?

Solution 2

There is an alternative to using floating point if you need a fixed number of digits after the decimal point, such as 2.

Split the input string at the ".", and treat "720.60" as two integers, 720 and 60. Multiply 720 by 100 and add 60, getting 72060. Do all your arithmetic in terms of integer numbers of hundredths. To display, print x/100 and x%100, separated by a decimal point with the second number printed as two digits with a possible leading zero.

If you prefer to work in a floating point type, double is much more precise than float. The closest float to 720.60 is 720.5999755859375. The closest double is 720.6000000000000227373675443232059478759765625. Both would round to "720.60" if printed with only two digits after the decimal point, but rounding error can build as you do arithmetic.

OTHER TIPS

You can print fewer digits, or use double. A single precision IEEE-754 float value has a 23 bit mantissa, which means it has about 7 decimal digits of precision. The value you see is as close as it can get: there is no float value closer to 720.60 than 720.59967.

In fact, the exact values of the two float value closest to 720.60 are:

720.5999755859375      (slightly less than 720.60)
720.60003662109375     (slightly more than 720.60)

You cannot do that. It is precision problem, i.e. the floating point format used in C cannot represent 720.60 exactly. You can try double type for amount, that will increase your precision but still you will not get exact value.

See this link for more discussion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top