-2

In my program I have a running total of a particular number, declared as a float before the main so as to be universal, and on every iteration I add and subtract floats from it.

These floats are always numbers between 0 and 10, to one decimal place. However, the total deviates occasionally (very infrequently, but i'm dealing with billions of iterations) from this 1.d.p. accuracy, by 0.01 (i.e. I add 2.4 to 15.9 and get 18.31)

This minor deviation can lead to the program crashing, so is there any way to alleviate it?

zephyr
  • 215
  • 2
  • 13

2 Answers2

4

If you always have 1 decimal place multiply all your numbers by 10 and use integer arithmetic! Binary floating points cannot, in general, represent decimal fractional values. Doing computations with binary floating point numbers causes the small errors to be magnified. Doing the computations with integers is exact.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • @EricPostpischil: yes, it is an overstatement: additive operations and multiplications are exact until they overflow. Divisions may be exact depending on the values involves but are, in general, not exact. – Dietmar Kühl Oct 01 '13 at 19:54
3

0.1 is a repeating decimal in binary, so can't be represented exactly. Best to use integers and multiples of 10 for your calculations.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54