RomanZhu / FixedPoint-Sharp

Fixed point math with 48.16 precision (based on lib by https://github.com/fholm)
MIT License
168 stars 40 forks source link

Algorithm in fixmath.Sqrt #5

Closed hyamw closed 3 years ago

hyamw commented 3 years ago

When I try to use fixmath.Sqrt, I found that the difference between returns value and the expected value is much bigger than I expected. So I checked the source code and found the source code is confused me. As I known that in Newton’s Method, the guess value should be: Xn+1=(Xn+V/Xn)/2

but in fixmath, the guess value is: var c = (b + (num.value / b)) >> 1;

I think the fixed point division should be applied here. var c = (b + ((num.value << fixlut.PRECISION)/b)) >> 1;

Sorry if I misunderstood the code

RomanZhu commented 3 years ago

FP has a usable range of approximately -65000 to 65000 Since you can't sqrt on a negative number, then we have a range of 0 to 65000

On that range, the max deviation of the result is 0.01f at the end of the range The deviation is unnoticeably small at the beginning of the range

image