- Home /
Why is Mathf.Pow( <some float value>, <some value less than 1> ) returning NaN on iOS 4.2 beta?
I'm having a weird issue with people running iOS 4.2 beta (presumably on iPads).
The long and short of it is because this line of code that worked before now is returning zero:
percentageToWin = Mathf.Lerp( ( 1.0f / 3.0f ), .75f, Mathf.Pow( levelNumber / 85.0f, .333333f ) );
(Where percentageToWin
is a float.)
I broke it down to this bit
Mathf.Pow( levelNumber / 85.0f, .333333f )
returning NaN
. levelNumber
is positive. I've searched for anything regarding Pow returning NaN, and haven't found any similar issues. This math is somewhat important to the balancing of our game, and I'm not sure how to work around it.
For reference, the game was compiled against Unity iPhone 1.7. I'm going to try it with Unity 3 to see if it goes away, but somehow I doubt it.
Has anybody seen any similar issues or have any solutions?
If this problem only occurs on this beta OS, it's likely because it's a beta. Is it only this line that presents the problem or does it happen elsewhere? You're sure levelNumber is being assigned? You could always write your own Pow function. If you just want to do a root (yth root of x = x^(1.0/y)), you could just write a root function with Newton's $$anonymous$$ethod or something, hardcoding whatever you can.
If the Pow function is working correctly then I think that your Pow function will return NaN only when the first argument < 0 So, you should check and make sure that levelNumber is properly assigned. You may want to check if your variable is properly initialized too.
@slovacs1 there's another line of code that does a similar cube root calculation that also returns NaN for the pow. I'm sure my variable is being initialized; on pre-4.2 versions of the OS the math works as expected.
I tested Pow through all relevant combinations, and as loginjjobby suggested, Pow returns NaN when the base is negative, but specifically only when the base is negative AND the exponent is a fraction. Since you are certain that levelNumber is positive and it only seems to happen on this beta OS, I would still look to the OS as the source. At this point, if you are 100% certain that levelNumber is positive, I can only suggest checking with Apple or writing your own cube root function (if you are only doing cube roots).
Answer by Tetrad · Oct 01, 2010 at 10:05 PM
So I made a test project with my current Unity iPhone 1.7 as well as the latest Unity 3.0 iPhone that is nothing more than a scene with the default camera and a script that does this:
void OnGUI()
{
GUILayout.Label( Mathf.Pow( 8 / 35.0f, .333333f ).ToString() );
}
Running the app built with Unity 3.0 spits out the correct number. Running the app built with Unity iPhone 1.7 spits out NaN.
For now I'm just going to file a bug report and try to get our project built with the new version of Unity (which is going to be a pain because of all the project settings, etc).