Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic Simulator Basic Compiler: Printing Single Precision Variables

Status
Not open for further replies.

Mity Eltu

Member
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.
 
hi Mity,
I have found with Oshonsoft that if use SINGLE_DECIMAL_PLACES = 6 it 'shrinks' the possible length of the integer part.

If I recall correctly for the FP its a total of 8 or 9 digits for the variable , I will recheck, let you know.. [perhaps Ian could double check this]

eg: 99.123456
I required for GPS work 359.123456 , had settle for 5 decimal digits
 
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.

A little cumbersome but manageable if necessary.

Thanks.
 
hi,
Double checked.
See this error message from the IDE.

I recall now I bumped into this when calculating arctan's
E
 

Attachments

  • AAesp02.gif
    AAesp02.gif
    8.1 KB · Views: 352
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..

I would personally only use 3 decimal places.....
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top