Help please with very large numbers

ferrymanr

Member
In previous projects I have manged to do multiplication and division wiith 31bit numbers as described elsewhere on this site. However I now have a requirement to calculate the data string required to be sent via SPI to a DDS synthesiser. The frequency control data is a 32 bit string (4 bytes) calculated from the required frequency in MHz. The number to be sent (n) is calculated from

f = n X 180 / 2^32
or
n = f * 2^32 /180

where f can be anything up to 70.000000 MHz in 1Hz (0.000001 MHz) steps.

The numbers look huge, particularly when in integer form! What is likely to be the best approach to this calculation (using a 20X2)?

Thanks
Dick
 

nick12ab

Senior Member
PICAXE doesn't support decimal values, and

2^32/180 can be simplified to 23860929.422222222222222222222222, but the decimals would have to be eliminated losing a bit of accuracy or you'll have to use a massive number again. Even then, you'll still need far more than 32 bits if you want to have 1Hz steps.

How much accuracy do you need? 23860929.422222222222222222222222 can be represented by 2386093 but you'll also need to compromise on how small the frequency steps are to do it with 32-bit maths.
 

ferrymanr

Member
Unfortunately I want to use 1Hz steps in my application. Thank goodness I don't have to go to practical limit of 0.0419Hz steps.....
I'm almost coming to the conclusion that it might be easier (and little more expensive) to use a dedicated Raspberry Pi as the controller instead of a PicAxe chip but a challenge is fun!!
It strikes me that the 2^32 really involves bit shifting in binary which should simplify things.
Dick
 

AllyCat

Senior Member
Hi Dick,

I don't know if you're referring to the 32-bit routines that I posted in the code snippetts section, but in principle they could be extended to 48 or 64 bits (but no I'm not offering). :)

However, IMHO the simplest solution might be as I suggested in #5 of this very recent thread. Store each 10 digit number (for example) as separate decimal digits in an array of 10 consecutive bytes. Then write generic routines to process the individual pairs of digits (i.e. units, then tens, hundreds, ... etc.) in the same way as you would (or should have done at school) calculate with a pencil and paper. Addition and subtraction are fairly straightforward (just a potential "1" to carry/borrow for the next stage) and multiplication and division can be reduced to repeated addition and subtraction (likely to be rather slow) or by using decimal lookup tables.

Cheers, Alan.
 
Top