Floating point math and the propeller
bprager
Posts: 22
I'm looking at a application that needs to do a lot of high precision trig calculations.
I know there are math objects that will help with this, but I was also wondering about the FPU chip that is offered in the Parallax store.
Does this chip help any with the propeller, or is it only useful with the stamp chipset?
And any suggestions as to the best way to use the propeller for this kind of application?
Ignoring the FPU issue, my initial thought was to dedicate a propeller chip to handling only the trig calculations to maximize available code space and speed.
Any thoughts on this would be gratefully accepted.
Thanks!
I know there are math objects that will help with this, but I was also wondering about the FPU chip that is offered in the Parallax store.
Does this chip help any with the propeller, or is it only useful with the stamp chipset?
And any suggestions as to the best way to use the propeller for this kind of application?
Ignoring the FPU issue, my initial thought was to dedicate a propeller chip to handling only the trig calculations to maximize available code space and speed.
Any thoughts on this would be gratefully accepted.
Thanks!
Comments
Remember that both the FPU and the Prop's FP library are limited to single precision floating point. You're limited to about 7 significant digits. If you need greater precision, you may have to use some kind of fixed point (scaled) arithmetic and you may need to use CORDIC algorithms for your transcendentals. I think there's a CORDIC object in the Object Exchange that you might look at.
These are to translate coordinates between different coor systems.
There is of course, some basic addition/division etc, however, the bulk of the calcs are trig, with the results of those being added or multiplied together at the end.
Also, there are some calcs doing basic multiplication, but the numbers need to be held at high precision due to the use of large multipliers in parts of the formulas. If I would round, that rounding would cause a significant answer change.
So between the inverse trig and the precision calcs, the question popped up, would the use of the FPU either gain us, speed, precision, or additional code space?
It sounds like speed would be similar, what are your thoughts on precision and/or code space?
Thanks!
I wasn't sure if the FPU would either speed up the use of inverse trig functions, or offload the propeller enough to be worth using.
In my experience, floating point math is almost never really necessary with proper scaling and an understanding of the ** (return high order long of integer multiply) function. In fact, integer math with add and subtract 2's complement wraparound works much better than float for angular functions when you normalize 1 360-degree rotation to 2^32 bits.
This is a new application, where the prior was implemented in a chip where FP math and trig functions were easily available as was space, so there was no issue.
I haven't had an opportunity to use the ** function and will have to investigate it to see how we might be able to use it to both yield the accuracy and speed we need, while keep out space requirements to a minimum.
My understanding of the trig tables in ROM, is that they do not support the inverse functions, for those you have to load the trig math object.
I have used linear interpolation before and in fact, one of our routines relies on it in order to calculate positions to the required accuracy from speciality tables.
I will have to investigate your suggestion of the ** function along with wraparound to see if we can bypass much if not all the need for FP math.
Thank you for the suggestions.
Bruce
I use this coupled with planar cartesian projection and it works well.
I started writing a page on the propeller wikispaces, but I had no time to finish the page yet.
On obex there are other integer implementation of navigation routines.
It takes a little bit to move from float to integer (because it sounds odd), but at the end it is worth the effort.
Massimo
The value of ** is that it lets you multiply a number by a fraction between 0 and 1 (scaled from 0 to 2^32) very finely, with a single integer multiply and no shifts. If you are using an assembly language multiply (I use the one from the PASM interpreter) you'll get this result automatically along with the lower 32 bits. Since the Prop has no hardware multiply you still have a multiply routine, but you avoid all the shifting and positioning and normalization of float math so it's still much faster.
Could you possible give me an example of how I would do a inverse sin using the method you recommend?
If this is outside the scope of this forum, you could reply to bprager@att.net and we could take this offline.
I truly appreciate your time and knowledge and thank you very much.
I'm attaching a demo of binary inverse search in the hub sine table, to find the inverse sine. The demo does not implement the interpolation that localroger mentioned, but that is not hard to add in order to improve angular resolution.
Should you decide you still need to use some floating point I should mention that there is now a new floating point object by Lonesock in the object exchange. This version puts all the functions of the older version into a single COG and speeds them up a bit as well. I think it's called "F32".
Attached is a spin I used while learning about this chip. It needs two other objects that can be found in the OBEX
gave me my answer (and then some!). I was wondering about the FPU having used such chips before, but with the prop, it was a bit of an unknown as to if it really helped or not. Looks like
I'll have to try each of the ideas presented to see which matches best to my calcs.
Heater - thanks for the OBEX reference. I didn't know about the newer object and COGS are at a bit of a premium so that helps a lot.
Tracey, I assume the interpolation I would use would be the standard math interpolation formula for estimating between data points. I am using that now to interpolate between two hourly values to derive
the value at the minute requested.
Again, thanks to all for a ton of information. I hope I can contribute back.
Bruce
By the way, I don't work for micromega, and like others, I do 99.9% of my math in the integer domain. After the BASIC Stamp, the Prop's 32 bit signed integers are close to infinity.
You mention loading it at runtime, can the FPU can "loaded" dynamically, Ie; User wants to know a location, so I get the current coordinates, load the required calcs into the FPU and get back the answer?
That would be ideal for what we want to do, as sunrise/sunset calcs etc are effectively what we are doing, in some cases, even more complex (planetary coordinates based on current time etc)
Thanks for this info, I had never heard of this chip and in the future, we intend to add a GPS system so it might match up well.
Bruce
Jonathan
This does not exactly make sense.
Working with floating point allows you to have huge numbers of course but the precision is limited to the 24 bits of mantissa. Straight 32 bit ints limits your number range to "only" plus/minus 2 billion but then you have a superior precision of 32 bits. So working with suitably scaled ints may give a better precision over the required range than floats.
Of course using a ready made float system, hardware or software, allows you to get on with development more quickly. One just needs to be aware of it's limitations.
Sounds like you have an interesting project on your hands.