View previous topic :: View next topic |
Author |
Message |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
floating point printf with suppressed decimal point |
Posted: Mon Jun 21, 2010 2:30 pm |
|
|
In an instrumentation project that is already working as expected,
I am reading a number of channels with an ADS8371 A/D, with pic 18f4620 @ 14.7456mhz oscillator and baud rate of 460800.
The system generates a LOT of pipelined data (doing calcs and sending previous reading while new data settles for next reading)
that is corrected and is comma delimited in blocks of 8 readings/output string:
now as (typically)
123.45,12.34,1234.56,12345.66 ......<cr>
and terminate with a naked carriage return
Usually data is in the range of 9999 or less but sometimes there is a value over 10000 - we ALWAYS Output only 2 decimal points resolution.
newfloat holds the floating point result of the
present adc 16 bit ( unsigned int16) sample,
multiplied by a FLOAT correction adjustment factor, generally in the range of about .975 to 1.25
The formatting is done with printf (%.2g,",newfloat);
I've been asked to compress the output data and try to squeeze out more data points per unit time. I know it is a SMALL gain but
the question is:
Does anyone know of any efficient easy EASY way to suppress the
printf() output of the DECIMAL point ?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Jun 21, 2010 2:36 pm |
|
|
The obvious way, is just to multiply the value by 100, and put it into an int32.
Also has the advantage that the output maths will be faster to...
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Jun 21, 2010 2:39 pm |
|
|
Why not multiply by 100?
Instead of a correction factor 0.975 to 1.25 you use 97.5 to 125
Or multiply by 1000 and you can get rid of all the slow float arithmetic at all, only using integers for the calculations. In the presentation of the data you insert the dot at the right location (this technique is called 'fixed point floating point'). |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Jun 21, 2010 3:07 pm |
|
|
thanks for the jolt in the
new direction
much appreciated |
|
|
|