Game Development Community

Console float calculation returns wrong result

by Thomas Huehn · in Torque Game Engine · 06/03/2007 (12:45 pm) · 2 replies

Hi, i do a simple calculation, but it returns the wrong result:
I used also a clean 1.51 TGE to check it.

$money = 10014.10;
$tmp = mFloor(($money - mFloor($money)) * 100);
echo ($tmp);

==> return 9 - but should be 10.


$money = 1014.10;
$tmp = mFloor(($money - mFloor($money)) * 100);
echo ($tmp);

==> also return 9 - but should be 10.


$money = 14.10;
$tmp = mFloor(($money - mFloor($money)) * 100);
echo ($tmp);

==> return 10

Are the numbers to big ? - Any idees how i can solve this ?

greetings thomas

edit: typo

#1
06/04/2007 (11:46 am)
In the first example, presumably $money - mFloor($money) returns 0.09999999 or similar.
If you want to round to the nearest hundredth you will have to do it more directly.

e.g.:
$money = 10014.10;
$tmp = mFloor($money*100.0) - mFloor($money) * 100.0;
echo ($tmp);

$tmp = mFloor($money*100.0) - mFloor($money) * 100.0;

.. should work for awhile. But as you mentioned, you may run into precision limitations if your numbers get much larger. Single precision will only give so many significant figures. One way of getting around this limitation might involve keeping track of dollars and cents in two separate (integer) variables.
#2
06/05/2007 (1:49 am)
Hi Matthew,

you are right, its better to work with integers since i cant set the precision in the script ;)

I modified the source a little, so i can display numbers up to 9 digits, i only transfer the "cents" now
and use getsubstr to cut it in parts. Not the perfect way but it works.