neslib/FastMath

FastPower does not work

iga2iga opened this issue · 3 comments

FastPower(Single, Single) does not work with Base = 0,474733531475067 eg and Exponent = 150, should return zero.
The same checks as in rtl before Result := FastExp2(AExponent * FastLog2(ABase)); make it work as it should.

Are you sure it's not just a typical rounding error/floating point inaccuracy ?

Actually I don't remember where i faced the problem. But rtl definitely returns zero.
System.Math.Power(0.474733531475067, 150) = 0
while
neslib.FastMath.FastPower(0.474733531475067, 150) returns -3,35626519513237e+28

I can see several reasons for this:

  1. System.Math.Power(0.474733531475067, 150) uses Extended precision by default, while FastMath uses Single precision. An exponent of 150 may be too big to be handled using Single precision.
  2. Even if you use the specific Single version of System.Math.Power, the it will store the intermediate results using Double precision, while FastMath stores all intermediate results in Single precision.
  3. The Fast* versions focus on speed, not accuracy. With edge cases like this, accuracy may suffer a lot.

But I think the issue is mostly related to reasons 1 and 2 above. If precision is important, then don't use the Fast* versions and use Delphi version that offer at least Double precision.