|
|
| Author |
Message |
mwf
Joined: 06 Oct 2006
Posts: 27
Location: Boulder, CO
|
|
Posted: 11 November 2006, 21:05 PM Post subject: precision arithmetic |
|
|
| I have a project coming up that needs some high precision math. A fixed point format like 32bit.32bit (integer.fraction) would work as would double precision floating point. Does anyone have software/experience with this that they are willing to share? The hope is to do this with ZX-24 hardware. |
|
| Back to top |
|
 |
spamiam
Joined: 13 Nov 2005
Posts: 665
|
|
Posted: 12 November 2006, 0:17 AM Post subject: |
|
|
Well, it is definitely a ZBasic question, but I suppose not specifically ZX-24.
I am glad that you suggested fixed point math. It will be easier than trying to re-create 64 bit floating point math.
You must have need some considerable precision and/or large numbers to need these sorts of variables.
When I took my chemistry courses in college, they tried to pound into my head the principles of significant digits.
I do not think I ever really got it. I saw in your other post that you want to interface to some sensors. When you deal with sensors, you definitely need to be aware of the precision and accuracy.
Precision is the number of significant digits it will send to you. The accuracy is different. This is the error in the reading. They are somewhat related.
If you have a temp sensor which reads to 1/1000 degree, but the readings vary by 1/10 degree from one measurement to the next, then you really are only sure of the temp to 1/10 degree or so. The added precision is ficticious. These extra numbers do not represent real information.
The other issue is how to combine large and small numbers when adding.
If you have only a certain number of significant digits, (e.g. 3 sig. digits), then how do you add 1000 (1.00e3) and 1.00? The answer is still 1000.
This issue of significant digits might reduce your need to such high precision, and still result in calculations that are just as meaningful as those with more digits.
-Tony |
|
| Back to top |
|
 |
GTBecker
Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 12 November 2006, 4:43 AM Post subject: Re: precision arithmetic |
|
|
| GTBecker wrote: | Has anyone tried the Micromega uM-FPU V3 Floating Point Coprocessor?
| I've seen this one, or perhaps another like it, advertised in Nuts 'n' Volts and elsewhere. The page at the link that you provided seems to indicate that it provides 32-bit floating point and 32-bit integer operations. That portion is equivalent to the math support in ZBasic. It goes further, though, in that it supports matrix operations.
In answer to the original post, you may want to search using the phrases "fixed point math" and/or "arbitrary precision". Depending on what operators you need, it can be fairly simple to implement the fixed-point or higher precision operations. |
|
| Back to top |
|
 |
stevech
Joined: 23 Feb 2006
Posts: 657
|
|
Posted: 12 November 2006, 5:22 AM Post subject: Re: precision arithmetic |
|
|
| mwf wrote: | | I have a project coming up that needs some high precision math. A fixed point format like 32bit.32bit (integer.fraction) would work as would double precision floating point. Does anyone have software/experience with this that they are willing to share? The hope is to do this with ZX-24 hardware. |
there are C versions of fixed point libraries around. And also C versions of "double" (64 bit) floating point. It could be coded in ZBasic - but it would run fairly slowly. Fixed point is always faster (no normalization) but the burden falls on you, the programmer, to mind the decimal point.
As said before, step one is a bit of anaysis on how many digits of precision you need. |
|
| Back to top |
|
 |
GTBecker
Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral
|
|
Posted: 12 November 2006, 5:39 AM Post subject: precision arithmetic |
|
|
> ... equivalent to the math support in ZBasic...
Certainly the common math must be equivalent except, perhaps for speed;
and its matrix functions are very inviting. It goes beyond just math,
though, in that it can be programmed to run up to 64 user functions,
each a subroutine, macro or autonomous program.
It has two 10kHz 12-bit ADCs, a few timers, external trigger. It seems
to me that it could be programmed to be a smart sensor front-end,
implementing stuff like least-squares or Kalmans without burdening the
main process after initialization.
A little off-thread, but interesting, I think. |
|
| Back to top |
|
 |
mwf
Joined: 06 Oct 2006
Posts: 27
Location: Boulder, CO
|
|
Posted: 12 November 2006, 18:19 PM Post subject: |
|
|
A little more on why I want this for the curious. I am computing the frequency for a direct digital synthesizer which needs a 32bit fraction (of its clock frequency). There are several steps in the calculation and intermediate values can range from 32bit integers to numbers with the binary point somewhere in the middle. The final answer is 27bits of precision with the useful bits in the middle of the 32 bit fraction. A few high bits should be zero and a few low bits are don't care. I will loose a couple bits in inexact divisions in the algorithm
This is a few more bits than FP can manage unless it is double precision. I could do the calculation in 32bit fixed point since I could predetermine the binary point throughout the calculation. Keeping track of the binary point is why they invented floating point. All the intermediate values have to fit the format if it is going to be the same for the entire calculation. Everything will fit in a 32.32 format. There are DDS chips that want 48 bits for the frequency fraction. Double precision floating point is barely adequate for these.
Absolute frequency is known to whatever precision your time base allows - maybe 20 to 24 bits. Relative frequency - is the synthesizer higher or lower than some other signal source - can easily be determined to millihertz. I'm not going that far....but every bit of resolution is useful if you are matching frequency between sources. |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX
|
|
Posted: 12 November 2006, 18:41 PM Post subject: Re: precision arithmetic |
|
|
| mwf wrote: | | I have a project coming up that needs some high precision math. A fixed point format like 32bit.32bit (integer.fraction) would work as would double precision floating point. Does anyone have software/experience with this that they are willing to share? The hope is to do this with ZX-24 hardware. |
Strictly speaking single precision floating point numbers only have 24 bits of precision and double precision floating point numbers have 53 bits of precision. I have previously suggested that to add double precision math to ZBasic but up to this point there hasn't been a strong requirement for it.
As mentioned by Tony you first of all really need to decide on what range of numbers is required and what precision is required. Possible variations on 32:32 include 8:24, 16:16, 48:16, 32:16 etc. You didn't mention the requirement for this extra precision math but that might help us give you more ideas.
Next I would suggest you work on the interface to the math package. This is a set of constants, types, empty-body functions and subroutines that is contained in a separate module. For example do you need
- To initialize these fixed point numbers from integers longs, singles
- Only a subset of math functions (e.g. no mult and no div)
- A superset of math functions including sin, cos, abs etc
- Conversion functions back to ZBasic types
- Output functions that format fixed point numbers
I would recommend storing the fixed point number in a structure with two parts for the integer and fractional part. You can use the alias function to map that to other types of data formats.
I spent some time Googling around for a good implementation that could be converted to ZBasic. Here are some links that might be worth reading at least for their design content:
http://pixwiki.bafsoft.com/wiki/index.php/Portable_Fixed_Point
http://jet.ro/files/The_neglected_art_of_Fixed_Point_arithmetic_20060913.pdf
http://ww1.microchip.com/downloads/en/AppNotes/00617.pdf
http://wwwold.dkuug.dk/jtc1/sc22/open/n3574.pdf
http://www.emesystems.com/BS2index.htm#math (good implementation notes from Basic Stamp math wizard)
There are some common design points contained in these papers including how to deal with overflow, how to convert divison operation to multiplication, and dealing with an internal representation that is larger to prevent loss of precision. The implementation of the interface is the most work but don't forget to add a test suite to verify that your code works correctly. |
|
| Back to top |
|
 |
|