integer-only microcontrollers

bobsmith1492

Diamond Member
Feb 21, 2004
3,875
3
81
Has anyone tried programming microcontrollers such that, in order to avoid floating-point operations (which they essentially can't do), you simply multiply any decimal numbers you use by 10^x power to make it a large enough integer to do the job and just run everything as if it were at 10^-X power? I tried that on a simple program where it needed a constant of like 0.323 or something and just used 323; the numbers were large enough that when the actual value was needed you could just divide by 1000 and the remainder was insignificant. Does that hold any promise for more complicated code?
 

PottedMeat

Lifer
Apr 17, 2002
12,363
475
126
yeah its a common thing to do - like if you're using a 8 bit mcu and you can get the results as you said ( ex. multiply 8.25 * 1.375 -> convert to 8250 * 13750 with just plain integers ), what do you mean 'hold any promise for more complicated code' ? you can do floats i believe, but it will take a very large number of cycles compared to integer computation.
 

bobsmith1492

Diamond Member
Feb 21, 2004
3,875
3
81
Well, I'm wanting to output a waveform to a DAC to generate an AC signal of some sort according to a particular equation, say sin(x). The problem is: how to do that without using floats? I know you theoretically CAN use floats, but it takes a TON of processing and code (we accidentally included the aforementioned float in a program and the program jumped from 10K to 30K when compiled.) Therefore, I want to avoid them. Or maybe we'll need a controller with a floating-point unit? :p
 

PottedMeat

Lifer
Apr 17, 2002
12,363
475
126
damn that aint too bad - some options for you

1. Can you simulate a SPI/I2C port on the MCU or does it have those peripherials?
If you can, get a direct digital synthesizer sample from http://www.analog.com
I've used the AD9834/9858 DDSs for 0.1Hz to 200MHz in 0.1Hz increments ( depends on DDS resolution and input frequency/PLL )

2. Use a look up table ( what the DDSs do )
Store a table of Sine values in EEPROM( from 0 to 2pi maybe ), load these into the DAC at a specific rate. You can reduce the size of the table through symmetry - i.e. store only values from 0 to pi/2. You will need an output filter on the DAC to smooth out the stair-step appearance. Again Analog Devices has a bunch of datasheets/online books that can help.
 

SagaLore

Elite Member
Dec 18, 2001
24,036
21
81
Originally posted by: PottedMeat
but it will take a very large number of cycles compared to integer computation.

That is not the only problem. When you need to deal with exact and continuously reused numbers from solutions, floating point becomes very unreliable.
 

Peter

Elite Member
Oct 15, 1999
9,640
1
0
That's why there's the fine art of Numerical Math in your proper CS degree :)

This covers everything from arranging your algorithms such that error and loss of precision are minimized, all the way down to iteration and approximation strategies for math with limited precision - be that finite precision float, or even integer.
 

bobsmith1492

Diamond Member
Feb 21, 2004
3,875
3
81
Ahhh.... I like the table idea; that would fit in perfectly. This thing will probably be getting the required formula from the user via a PC, in which case the PC could do the necessary calculations and multiply by some factor, say 10^4 to keep some accuracy when converting over to integers for the table.

I'm not sure about the SPI - we haven't chosen a microcontroller yet, so that's all up in the air. The DDS looks interesting though; I'll have to look into that.

Peter: That's why I'm not studying CS. :p
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
That is called fixed point arithmetic or q-point. You should use base 2 instead of base 10. Using base two avoids the division need in base 10 and perserves more accuracy.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: SagaLore
Originally posted by: PottedMeat
but it will take a very large number of cycles compared to integer computation.

That is not the only problem. When you need to deal with exact and continuously reused numbers from solutions, floating point becomes very unreliable.

That effect has little to do with floating point and more to do with trying to save 80 bits of data with 64 bits. The only ways to increase the accuracy is to decrease the range or increase the number of bits.
 

bobsmith1492

Diamond Member
Feb 21, 2004
3,875
3
81
Originally posted by: smack Down
That is called fixed point arithmetic or q-point. You should use base 2 instead of base 10. Using base two avoids the division need in base 10 and perserves more accuracy.


Yeah, that makes sense because then you can just bit-shift anyway; that's one of the quickest operations if I remember correctly.