Download

Weird behavior of FMath::Square and FMath::Pow

I want to calculate x^16 with x being a float. Since this is a time critical calculation, I used four successive calls of
x = FMath::Square(x);
with 0 <= x <= 1
This works for the first 3 calls, but in the fourth it often results in x=inf. Typical initial values of x are 0.99, so this should not cause any numerical instabilities. FMath::Pow(x, 16.0f); also returns inf.

However using standard pow() from math.h instead of FMath works, same for the explicit calculation x = x * x * …] * x.
What could this be?

I forgot to mention: using double instead of float works.

Hi!

FMath::Pow looks like this:
static FORCEINLINE float Pow( float A, float B ) { return powf(A,B); }
Standard pow() uses double type, standard powf() uses float type.

I think you problem is numeric limits of float type.
https://en.cppreference.com/w/c/types/limits

FLT_MIN = 1.175494e-38
FLT_MAX = 3.402823e+38
FLT_EPSILON = 1.192093e-07