flatmush / dingoo-sdk

Automatically exported from code.google.com/p/dingoo-sdk
2 stars 3 forks source link

fix16_sqrt returns wrong results #44

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Trying to calculate distance between two points, I found that fix16_sqrt() 
function returns too big results.
For example, fix16_sqrt(980) returns 8014, but correct result is 31.

Original issue reported on code.google.com by nzeemin on 14 Jun 2010 at 1:34

GoogleCodeExporter commented 9 years ago
That's my fault, I translated a floating point algorithm from somewhere into 
fixed point and never tested it, if I get time then I may re-write the 
algorithm but if you're feeling brave then your best bet is to google for an 
algorithm and implement it (and then add it to the SDK of-course :) ).

Original comment by Flatmush@googlemail.com on 17 Jun 2010 at 12:48

GoogleCodeExporter commented 9 years ago
Hmm, I've tested it and the library is correct, your use of it is wrong.

This library does maths as fixed point 16.16 (look up Q number format on 
wikipedia).

sqrt(980) actually works out the sqrt of (980 / 65536) which is 
0.01495361328125.
This equates to 0.12228496751951974902237668500874 which when multiplied by 
65536 is 8014.

If you want to use this library just for integer square roots then you can do:
fix_to_int(fix16_sqrt(fix16_from_int(980)))

Which would actually return 31 as you expect.

Original comment by Flatmush@googlemail.com on 17 Jun 2010 at 1:59

GoogleCodeExporter commented 9 years ago
Oh! Thanks, my fault. Sorry for bothering.

Original comment by nzeemin on 17 Jun 2010 at 2:53

GoogleCodeExporter commented 9 years ago

Original comment by Flatmush@googlemail.com on 17 Jun 2010 at 3:52