Shop OBEX P1 Docs P2 Docs Learn Events
Trig Float VS Integer ?'s — Parallax Forums

Trig Float VS Integer ?'s

ShawnaShawna Posts: 508
edited 2013-05-18 16:29 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-18 11:55
    You can't use ">", "<", "=>", or "=<" with floating point numbers. They don't work the way you want. Similarly, "+", "-", "*", "/", "//" won't work. These all are integer operators.

    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.
  • ShawnaShawna Posts: 508
    edited 2013-05-18 12:20
    Thanks Mike
    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.:smile:

    Thanks
    Shawn
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-18 12:27
    Yes, the floating point values are unpacked to work on, then re-packed later. The values are still kept in exponent / mantissa form, not integer values although these parts are manipulated as integers using integer arithmetic. For addition (and subtraction), the mantissas have to be aligned properly by computing the exponent difference and shifting one mantissa or the other by the exponent difference. At that point, you can add the two mantissas and use the larger exponent for the result (then normalize that). Multiplication and division are, in some sense, easier since you handle the mantissas (multiply them) and exponents (add them) separately before normalizing the result.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-05-18 12:48
    The sine tables are entered with 12 bits, with a 16 bit result. Interpolation brings the 12 bits up to roughly 16. F32 uses a 24 bit CORDIC for ATAN. The development thread for F32 is here.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-05-18 13:17
    If you know all your numbers are non-negative, >, <, and == will actually work correctly with floats. You can also do comparisons of == 0 or > 0, because 0 is the same bit pattern in float and integer.

    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.
  • max72max72 Posts: 1,155
    edited 2013-05-18 14:08
    Beau posted a pasm object doing cordic atan2 with integers a couple of years ago.
    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-18 15:00
    Shawna wrote: »
    Can I do this without converting them back to an integer?

    If A>B
    do something
    Else
    do something different

    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.
    Shawna wrote: »
    Also is the F32 obj in the obex, the patched version that fixes the ATan2 bug?

    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.
  • Heater.Heater. Posts: 21,230
    edited 2013-05-18 16:29
    As it happens you can compare floats "<", ">",etc using normal integer operations provided you do a little preparation.
    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.
Sign In or Register to comment.