neslib / FastMath

Fast Math Library for Delphi
Other
165 stars 36 forks source link

FastPower does not work #5

Closed iga2iga closed 1 year ago

iga2iga commented 2 years ago

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.

SkybuckFlying commented 1 year ago

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

iga2iga commented 1 year ago

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

neslib commented 1 year ago

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.