I am writing a program that will be using a few single precision floating point variables. I will need to print these variables to an LCD module. I have been using the LCDOUT command in the following manner to do this:
Code:
main:
Dim a1 As Single
Dim b1 As Single
Dim c1 As Single
Gosub setup
Lcdinit
Lcdcmdout LcdClear
a1 = 3.1415
b1 = 1.5
c1 = 2 * a1 * b1
Lcdout "i= ", #c1
The LCD displays this "i= 9.424". What I need/want is to have 9.4245 displayed (the actual correct answer).
The problem with this is that I need more than the 3 decimal places that it displays on the LCD. From the manual I get that the Single precision variables have 7 digit precision. Is there a way to get more decimal places out of these variables without resorting to brute force (ie strip off the integer part, multiply by big scaling factor and then display both parts)?
Thanks, that's got it. I didn't see that in the manual as it is nowhere near the single variable definition. It's either in the "String" section or all the way at the end in the "Define" section. Not the right place in my opinion. Anyway, I got it. Thank you.
In that case, if I am not sure what value the variable would hold (I know, I'm the programmer and input validation is a must), then I might have to resort to the brute force method I mentioned earlier of removing the integer part and scaling the fractional part to be large enough to meet the precision requirements.
Floating point numbers use representation rather than absolute... This is why fixed point math can actually be more accurate..
Because a single is based on 32 bits ( same as a long ) the mantissa is 23 bits long, we have to represent a number by percentages..... 0.5, 0.25, 0.125, 0.0625 etc.... Some numbers require more mantissa bits than others... 3.333 is a bad one whereas 1.5 or 3.25 are dead simple..