As has been said, using
float or
double is a bad idea since they're slow as hell to do math on. Microcontrollers have no hardware resources to do floating point ops and performing them is like 1000x slower than integer ops.
Generally when we need to work with decimals on a controller, it is best to simply multiply it so the desired accuracy is achieved. For example
unsigned int can store values 0-65535. You could deal with Pi as Pi*10,000, Pi*100, Pi*2^14, or Pi*2^8, etc. As always, you will need to evaluate your math carefully to ensure that nowhere will a math operation overflow the storage type being used. This is true regardless of whether a multiplier is used or not. Also research "
integer promotion" which is how C normally does math, this will directly affect when a calculation will overflow.
Your display code will need to do some tricks to take 314159 and display as 3.14159. A string processing task could be told to make a string out of the number and add a decimal 5 places over.
Code:
void sprintdec(char *ptr, unsigned int number, unsigned char shift);
(of course you may also want
signed integer,
signed long, or
unsigned long)
When stating this in your code you will probably want to take advantage of
constant folding. For example:
Code:
#define DEC_MULT 10000
unsigned int myInt= 3.14159f * DEC_MULT;
The interesting part here is that the compiler will realize that myInt is just supposed to get assigned 31415 and will only produce assembly code that assigns this value; it will not create code that does a multiplication or floating point op. Note if there is a variable in this equation- such as if DEC_MULT were a variable- this will not be the case.