$a = 7 / 3; ==>$b = $a; ==>echo( $a == $b ); 0 But maybe my compiler just messed things up... Anyway there is an easy workaround for "> RC1: TorqueScript precision issue | Torque Game Builder | Forums | Community | GarageGames.com

Game Development Community

RC1: TorqueScript precision issue

by A. Saratow · in Torque Game Builder · 06/04/2006 (3:07 pm) · 6 replies

It looks like there is a precision issue with either the comparsion operator "==" or the assign operator "=":
==>$a = 7 / 3;
==>$b = $a;
==>echo( $a == $b );
0
But maybe my compiler just messed things up... Anyway there is an easy workaround for this problem:
==>$a = 7 / 3;
==>$b = $a;
==>echo( $a $= $b );
1

#1
06/05/2006 (12:39 pm)
Oh boy this is true... anyway my paranoid side doesn't allow me to compare equality in floats
But I have code that compares integers in switchs and ifs, I'm going to think about the risk of doing this...
#2
06/06/2006 (1:23 pm)
Wow- that's a good one to be aware of!
#3
06/06/2006 (1:48 pm)
Note that this isn't necessarily just TorqueScript precision issues... it's all computer precision issues. TS may have given up some precision for speed, but no matter what you do, there are some numbers that cannot be represented in computers by current standards. The number 0.2 is impossible to store in a floating point variable, for example. No number of bits will do it, either. The best practice is to allow a small error threshold when testing for equality. For example,

%difference = %num1 - %num2;

if( mAbs(%difference) < 0.00001 ) {
   echo("The numbers are equal enough");
}
#4
06/07/2006 (2:43 am)
You are correct about this point, but no matter if a certain number can be exactly represented or not - at least a direct copy of the representation has to be equal to the representation itself, e.g. in your case
==>%differenceCopy = %difference;
==>echo( %differenceCopy == %difference );
1
Seems like there is someone chopping a bit out of numbers somewhere...
#5
06/07/2006 (6:49 am)
A. Saratow is correct. Here is a C program that shows the correct behavior of his example 1

main()
{
  float a = 7.0/ 3.0;  // edit: 7/3 rounds to integer value, 7.0/3.0 keeps precision
  float b = a;
  printf("a == b => %s\n", (a == b) ? "true" : "false");
}

Quote:alexr@cyclone:~$ gcc test.c -o test
alexr@cyclone:~$ ./test
a == b => true
#6
06/07/2006 (8:28 am)
Ah yes. I see it now. Sorry for missing the point.