Advertisement

Problems when using floats in for loops

Started by October 07, 2006 07:58 AM
9 comments, last by WitchLord 18 years ago
Hi In certain circumstances when using floats in for loops the termination value is not calculated correctly. Example

float[] myArray(70);

float aSize = 8;

float MyFunction(float a)
{
	return a;
}

void Test()
{
  for (float k = 0; k< aSize; k++)
  {
      myArray[MyFunction(k*aSize)] = k;
  }

  for (int i = 0; i< aSize*aSize; i++)
  {
	Print("i = " + i + "\n");
      myArray = i;
  }
}

In the above example the 2nd for loop should terminate at 64 but it keeps going until it eventually crashes with an out of bounds exception when it hits 70. Remove the 1st for loop and the 2nd for loop stops at 64 as expected. Remove the call to MyFunction and the 2nd loop works. Remove the *aSize in the 1st for loop and the 2nd loop works. Compiled Bytecode

   31   8 *    SUSPEND
   32   8 *    CpyVtoV4 v4, v1
   34   8 *    iTOf     v4
   35   8 *    CpyGtoV4 v5, 1
   37   8 *    CpyGtoV4 v4, 1
   39   8 *    MULf     v5, v5, v4
   41   8 *    CMPf     v4, v5
   43   8 *    JNS      +55              (d:100)

I don't fully understand the above but it looks like v4 is getting overwritten before it is used. I am using the svn version of AngelScript from today but this problem is also in version 2.5.0b. Thanks.
It looks like a bug with the compiler reusing temporary variables before they have been freed.

I'll try to fix this as soon as possible.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
I've fixed this bug now. Please download revision 77 in the SVN for the fix.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

If you want to get technical, floats (IEEE standard single or double) really shouldn't be used as for-loop control variables anyway...even if the answer seems easy and obvious, anytime you perform an operation ( + - * / ) on a float you are only getting an approximation of the answer, and the approximation is not consistent.

Rather then try and explain here, just refer to example number 3 here: http://www2.hursley.ibm.com/decimal/decifaq1.html (Hursley is IBM's R&D division in Winchester).

And yes, I'm a database guy, and recently rekindled my hatred of float after seeing some large financial stuff being tossed around with them.
That is true.

We can often see the same thing in comparisons of floats, common in collision detection for eaxmple. In many algorithms we have to take extra care to add a certain tolerance for difference, i.e. if the difference is smaller than a certain number we say the values are equal.

Perhaps I'll write a class for AngelScript to handle decimal arithmetics. :)

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Yeah, I agree with the float thing. However this was a user script that I was debugging to find out why it was crashing, so not guilty!

Anyway, thanks for the fix and such a great library.

You may be interested to know that I am currently using AngelScript in a music visualization plugin for the Xbox Media Center called Vortex.

A couple of screenshots of it in action.

Cheers!
MrC

SpaceHarrier

Specturm

Awakenings
Advertisement
Cool! I wish I had an XBox to try it out with.

Can I link to the Vortex page as a user of AngelScript?

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Yeah, certainly! And its worth getting yourself an Xbox just for Vortex ;-)

Cheers!
MrC
Are you guys working on something similar for XBox 360? It would be interesting to know if AngelScript works on the 360 as well.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

XB360 hasn't been properly hacked yet, so that's not possible.

This topic is closed to new replies.

Advertisement