Trig Float VS Integer ?'s
Shawna
Posts: 508
Hey guys,
Time to start another thread that may meander off into a million different directions.
I am writing a program that uses ATAN2 trig functions, it will eventually need cos, sin, Asin, and Acos also. I have(want) to run these calculations as fast as prop possible using spin and pasm. I have tried the Full Float 32 object and it works great, but it is to slow. I like the F32 object, but I have some concerns about how to use floating point numbers outside of the F32 routines. For example, lets say I have variables A and B. They are both floating point numbers. Can I do this without converting them back to an integer?
If A>B
do something
Else
do something different
Next example would be like this. And I am pretty sure this would not work because 0 is not a floating point value.
If A<0
do something
So I am doing all the math now with integers and I would like to stay with integers. Is there an interger math obj like F32, that is fast or faster? I have been looking for one.
Also is the F32 obj in the obex, the patched version that fixes the ATan2 bug?
I would like it to be as accurate as possible, I think the 24 bits you get out of the F32 is more than enough for what I am doing. There probably has to be a compromise between speed and precision, I would imagine.
Thanks
Shawn
Time to start another thread that may meander off into a million different directions.
I am writing a program that uses ATAN2 trig functions, it will eventually need cos, sin, Asin, and Acos also. I have(want) to run these calculations as fast as prop possible using spin and pasm. I have tried the Full Float 32 object and it works great, but it is to slow. I like the F32 object, but I have some concerns about how to use floating point numbers outside of the F32 routines. For example, lets say I have variables A and B. They are both floating point numbers. Can I do this without converting them back to an integer?
If A>B
do something
Else
do something different
Next example would be like this. And I am pretty sure this would not work because 0 is not a floating point value.
If A<0
do something
So I am doing all the math now with integers and I would like to stay with integers. Is there an interger math obj like F32, that is fast or faster? I have been looking for one.
Also is the F32 obj in the obex, the patched version that fixes the ATan2 bug?
I would like it to be as accurate as possible, I think the 24 bits you get out of the F32 is more than enough for what I am doing. There probably has to be a compromise between speed and precision, I would imagine.
Thanks
Shawn
Comments
The Prop 1's ROM has tables for Log2, Exp2, and Sine/Cosine. See the attached PDF for sample code. You can do reverse table lookup to get Arc-sine and Arc-cosine.
There's a fixed-point math object in the ObEx (here) that includes Atan2. It's all written in Spin. You'd have to convert to PASM if you need high speed.
I will play with that, it looks like the PDF says the Prop sine table is only 16 bits. I was looking at the PASM section of the F32 function and it looks like all the math routines are preformed with integers. It looks like the obj unpacks the floating point variables at the beginning of each routine, does the math and then packs the integers back to floating point variables. I can't believe that someone hasn't modified this obj to use just integers. If this has not been done how hard would it be to do. My PASM skills are limited, but good enough to mess stuff up.
Thanks
Shawn
The moment you throw negative numbers in there it messes things up, because (sort of) the sign bit is handled differently in negative ints and negative floats.
This would probably be the fastest solution.
If you only need distance Lonesock posted an integer hypotenuse approx that is insanely fast, and with a very small error.
Massimo
As others have mentioned, you can't use these comparisons directly with floating point numbers (unless you're sure they're not negative), however F32 and other floating point objects have a "Cmp" method to let you compare two floating point numbers.
I haven't checked to see if the patched version is in the OBEX yet, but there is a patched version in the thread Tracy linked to.
F32 is pretty fast. I've used in a couple of hexapod robots to compute the IK positions of all 18 servos. I could complete all the necessary calculations (with lots of ATan2 calls) at the 50Hz without any problem.
See post here:
http://forums.parallax.com/showthread.php/146761-Algorithm-debugging-and-floating-point-help?p=1171084&viewfull=1#post1171084
The creator of the floating point format went to some lengths to make this so.