Float32 object question
Bits
Posts: 414
Is there a better way to do this? Or is there a feature in the object that will do this?
I want to use a floating point number but only with a precision of 2 decimal places.
using float32 object ...
Then out would be 23.36
I want to use a floating point number but only with a precision of 2 decimal places.
using float32 object ...
out := 23.3699 out := Math.Fmul(out,100.0) out := Math.Fround(out) out := Math.Ftrunc(out) Out := Math.Fdiv(out,100.0)
Then out would be 23.36
Comments
But be aware that floating point is done in binary, not decimal, and 0.37 has no exact representation. When you eventually print out your result, you may well get 23.369998 or somesuch, instead of 23.37.
-Phil
Again thanks.
If it is about displaying the numbers it is better to use fixed point arithmetics. The value can easily be calculated without float which is slower and which needs an object, so extra RAM.
Rule of three is your friend here. Let's say your reference voltage of the ADC is 3.3V which means that you get an ADC value of $0000 for 0V and $ffff for 3.3V. You simply calculate
330 * ADCvalue / $ffff. Now your result is in the range of 0 - 330. The last 2 digits are the fractional part of your number. When converting this number to a string you simply use a function which puts the point between digit 1 and 2 (counted starting with 0 and from right to left).
If you want to round up at >=x.xx5 and down at <x.xx5 you simply add another digit to the result and add 5
3300 * ADCvalue / $ffff + 5. Now you either use the same function as above and feed it with result/10 or you tweak the function to put the point between digit 2 and 3 and skipping digit 0 at all.