Cosine/Sine lookup tables floating point exception

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
mpellegr
Halfling
Posts: 73
Joined: Thu Jan 10, 2013 4:29 pm
x 1

Cosine/Sine lookup tables floating point exception

Post by mpellegr »

I'm getting a floating point exception when using Ogre::Math::Cos and Ogre::Math::Sin with lookup tables set to true, but not when it's false. Does anyone know why this would happen? Are their certain limits to the value input?
User avatar
Daixiwen
Greenskin
Posts: 105
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 16

Re: Cosine/Sine lookup tables floating point exception

Post by Daixiwen »

Here is the code that is called when you use a lookup table:

Code: Select all

Real Math::SinTable (Real fValue)
{
  // Convert range to index values, wrap if required
  int idx;
  if (fValue >= 0)
  {
    idx = int(fValue * mTrigTableFactor) % mTrigTableSize;
  }
  else
  {
    idx = mTrigTableSize - (int(-fValue * mTrigTableFactor) % mTrigTableSize) - 1;
  }
	
  return mSinTable[idx];
}
To me the only ways you'd get a floating point exception is if you call the function with a NaN or if the result of fValue*mTrigTableFactor is too big to fit into an integer. (i.e. fValue is really huge).
The function uses an integer modulo operation so using it with angles outside of -π / π is safe.

By default mTrigTableSize is 4096 so mTrigTableFactor should be about 651.9
Hardware, n.: part of the computer you can kick
mpellegr
Halfling
Posts: 73
Joined: Thu Jan 10, 2013 4:29 pm
x 1

Re: Cosine/Sine lookup tables floating point exception

Post by mpellegr »

I'm using the Ogre::Math::Cos(Real fvalue, bool useTables) version. The values I tested with were 0 and numbers near zero, like 0.00006. Does Ogre need to be built with the option to use trig tables?
User avatar
Daixiwen
Greenskin
Posts: 105
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 16

Re: Cosine/Sine lookup tables floating point exception

Post by Daixiwen »

the SinTable function is the one called by Cos when you ask to use tables.
The tables are automatically generated when the Ogre::Math() constructor is called. I don't know when or by what this object is constructed though. You could also get an exception if you use this functions and the Math() object hasn't been constructed.
Hardware, n.: part of the computer you can kick
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Cosine/Sine lookup tables floating point exception

Post by Kojack »

Daixiwen wrote:the SinTable function is the one called by Cos when you ask to use tables.
The tables are automatically generated when the Ogre::Math() constructor is called. I don't know when or by what this object is constructed though. You could also get an exception if you use this functions and the Math() object hasn't been constructed.
The Math object isn't normally constructed. The only place that does create it is in the ogrexmlconverter. Ogre itself doesn't create it.
So if you want the table based sin/cos to work, you need to construct an instance of the Math class first.
mpellegr
Halfling
Posts: 73
Joined: Thu Jan 10, 2013 4:29 pm
x 1

Re: Cosine/Sine lookup tables floating point exception

Post by mpellegr »

Oh I see. I noticed the buildTrigTables() function and it does seem you have to create an instance of the Ogre::Math class. I did that and now I'm no longer getting a crash, but by using the tables I'm getting incorrect values. For instance, with Ogre::Math::Cos(theta, true) I'm getting the following output for the given thetas without using tables on the left and with on the right:

Code: Select all

theta 0.000000
costheta 1.000000 0.000000
theta 0.062832
costheta 0.998027 0.500000
theta 0.125664
costheta 0.992115 0.000000
theta 0.188496
costheta 0.982287 -54.826492
theta 0.251327
costheta 0.968583 72.052948
theta 0.314159
costheta 0.951057 0.000000
theta 0.376991
costheta 0.929776 0.000000
theta 0.439823
costheta 0.904827 -9.709031
theta 0.502655
costheta 0.876307 0.000000
theta 0.565487
costheta 0.844328 1.000000
theta 0.628319
costheta 0.809017 0.000000
theta 0.691150
costheta 0.770513 0.000000
theta 0.753982
costheta 0.728969 0.000000
theta 0.816814
costheta 0.684547 0.000000
theta 0.879646
costheta 0.637424 0.000000
theta 0.942478
costheta 0.587785 0.000000
theta 1.005310
costheta 0.535827 0.000000
theta 1.068142
costheta 0.481754 0.000000
theta 1.130973
costheta 0.425779 0.000000
theta 1.193805
costheta 0.368125 0.000000
theta 1.256637
costheta 0.309017 0.000000
theta 1.319469
costheta 0.248690 0.000000
theta 1.382301
costheta 0.187381 0.000000
theta 1.445133
costheta 0.125333 0.000000
theta 1.507965
costheta 0.062790 0.000000
theta 1.570796
costheta -0.000000 -0.000000
theta 1.633628
costheta -0.062791 0.000000
theta 1.696460
costheta -0.125333 0.000000
theta 1.759292
costheta -0.187381 0.000000
theta 1.822124
costheta -0.248690 0.000000
theta 1.884956
costheta -0.309017 0.000000
theta 1.947787
costheta -0.368125 0.000000
theta 2.010619
costheta -0.425779 0.000000
theta 2.073451
costheta -0.481754 0.000000
theta 2.136283
costheta -0.535827 0.000000
theta 2.199115
costheta -0.587785 0.000000
theta 2.261947
costheta -0.637424 0.000000
theta 2.324779
costheta -0.684547 0.000000
theta 2.387610
costheta -0.728969 0.000000
theta 2.450442
costheta -0.770513 0.000000
theta 2.513274
costheta -0.809017 0.000000
theta 2.576106
costheta -0.844328 0.000000
theta 2.638938
costheta -0.876307 0.000000
theta 2.701770
costheta -0.904827 0.000000
theta 2.764602
costheta -0.929777 0.000000
theta 2.827433
costheta -0.951057 0.000000
theta 2.890265
costheta -0.968583 0.000000
theta 2.953097
costheta -0.982287 0.000000
theta 3.015929
costheta -0.992115 0.000000
theta 3.078761
costheta -0.998027 0.000000
theta 3.141593
costheta -1.000000 0.000000
theta 3.204425
costheta -0.998027 0.000000
theta 3.267256
costheta -0.992115 0.000000
theta 3.330088
costheta -0.982287 0.000000
theta 3.392920
costheta -0.968583 0.000000
theta 3.455752
costheta -0.951057 0.000000
theta 3.518584
costheta -0.929776 0.000000
theta 3.581416
costheta -0.904827 0.000000
theta 3.644248
costheta -0.876307 0.000000
theta 3.707079
costheta -0.844328 0.000000
theta 3.769911
costheta -0.809017 0.000000
theta 3.832743
costheta -0.770513 0.000000
theta 3.895575
costheta -0.728969 0.000000
theta 3.958407
costheta -0.684547 0.000000
theta 4.021239
costheta -0.637424 0.000000
theta 4.084071
costheta -0.587785 0.000000
theta 4.146902
costheta -0.535827 -0.536293
theta 4.209734
costheta -0.481753 -0.482184
theta 4.272566
costheta -0.425779 -0.426168
theta 4.335398
costheta -0.368124 -0.368467
theta 4.398230
costheta -0.309017 -0.309309
theta 4.461062
costheta -0.248690 -0.248928
theta 4.523894
costheta -0.187381 -0.187562
theta 4.586725
costheta -0.125333 -0.125455
theta 4.649557
costheta -0.062791 -0.062852
theta 4.712389
costheta 0.000000 0.000000
theta 4.775221
costheta 0.062791 0.000000
theta 4.838053
costheta 0.125333 0.123932
theta 4.900885
costheta 0.187381 0.000000
theta 4.963717
costheta 0.248690 0.247442
theta 5.026548
costheta 0.309017 0.307850
theta 5.089380
costheta 0.368125 0.367040
theta 5.152212
costheta 0.425779 0.000000
theta 5.215044
costheta 0.481754 0.000000
theta 5.277876
costheta 0.535827 0.000000
theta 5.340708
costheta 0.587785 0.000000
theta 5.403540
costheta 0.637424 0.000000
theta 5.466371
costheta 0.684547 0.000000
theta 5.529203
costheta 0.728969 0.000000
theta 5.592035
costheta 0.770513 0.000000
theta 5.654867
costheta 0.809017 0.000000
theta 5.717699
costheta 0.844328 0.000000
theta 5.780531
costheta 0.876307 0.000000
theta 5.843362
costheta 0.904827 0.000000
theta 5.906194
costheta 0.929776 0.929641
theta 5.969026
costheta 0.951057 0.950962
theta 6.031858
costheta 0.968583 0.968522
theta 6.094690
costheta 0.982287 0.000000
theta 6.157522
costheta 0.992115 0.000000
theta 6.220354
costheta 0.998027 0.000000
User avatar
Daixiwen
Greenskin
Posts: 105
Joined: Fri Feb 08, 2013 11:30 am
Location: Oslo
x 16

Re: Cosine/Sine lookup tables floating point exception

Post by Daixiwen »

Are you sure the Math object isn't destroyed before you call the Cos function? It could be the case for example if you constructed it as a local variable in another function.
The ~Math() destructor frees the allocated tables but doesn't change the static members so it could explain what you are seeing.
Hardware, n.: part of the computer you can kick
mpellegr
Halfling
Posts: 73
Joined: Thu Jan 10, 2013 4:29 pm
x 1

Re: Cosine/Sine lookup tables floating point exception

Post by mpellegr »

Oh okay. I fixed it by creating a global Ogre::Math instance for the life of the application. That seems to have fixed the issue. Thanks!
Post Reply