Blog about (Web-) Programming, Graphics and Games.


Danger on the floor

In programming languages (tested in javascript and perl) you will get:
6*1.9*5 == 6*5*1.9 -> false
Because of calculations errors during floating point operations, the expression values differ by a very small amount. Here we get 57 and 56.9999... Floating point operations are not associative. One can work around this imprecission by never directly comparing floating point numbers, but always calculating with a small tolerance. Like this:
Math.abs(6*1.9*5-6*5*1.9)<0.00001 -> true
Now, when you use the floor function, you will get this:
Math.floor(6*5*1.9)-Math.floor(6*1.9*5) -> 1
The small floating point value difference has grown to 1. The difference is now substantial and might make a program do unexpected things. So, how to handle the floor-function here ? One could add a small value before applying floor. But then the value could grow too big and the floor-result is flawed again. The problem is, that the floor function cannot known if a value like 56.9999.. is a calculation error or deliberately constructed. In my opinion there is a systematic danger with using floor-function, because floating point numbers are imprecise, while the floor function has a big result-difference, dependent on a precise number. So in dealing with floor-functions there are this conclusions:
  • One should try to avoid imprecise floating point operations. Maybe calculations can be done with fixed point or fractional numbers.
  • If you have imprecise floating point numbers, dont use the floor function.

Keine Kommentare: