1. 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.
    Dismiss Notice

Pic Simulator Basic Compiler: Printing Single Precision Variables

Discussion in 'Oshonsoft' started by Mity Eltu, Mar 28, 2015.

  1. Mity Eltu

    Mity Eltu Member

    Joined:
    Apr 1, 2014
    Messages:
    49
    Likes:
    0
    Location:
    Tennessee, USA
    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 (text):
    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)?
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Put this at the beginning of your code..

    Define SINGLE_DECIMAL_PLACES = 6 ; 1~6 possible
     
  3. Mity Eltu

    Mity Eltu Member

    Joined:
    Apr 1, 2014
    Messages:
    49
    Likes:
    0
    Location:
    Tennessee, USA
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    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
     
  6. Mity Eltu

    Mity Eltu Member

    Joined:
    Apr 1, 2014
    Messages:
    49
    Likes:
    0
    Location:
    Tennessee, USA
    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.
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Double checked.
    See this error message from the IDE.

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

    Attached Files:

  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    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.....
     

Share This Page