Results 1 to 3 of 3

Thread: questions about fractions

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Belgium Beveren
    Rep Power

    questions about fractions


    I have 2 questions
    I give JD a value off : 2436116.31
    If I print JD it gives the correct value
    but if i print the fraction it gives : .310000000055879354

    If i give F the value of the fraction of JD
    I expected it wil be .31
    but it is .310000000055879354
    if i do math with than i have a problem

    How can i avoid this ?

    kind regards
    Attached Files Attached Files

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Milan - Italy
    Blog Entries
    Rep Power
    The reason is that all internal thinBasic numeric calculations are performed using EXTENDED numeric type, that has 18 digits of precision.
    When you call FRAC, it rounds the result of fit the precision of the EXTENDED data type.
    Then the EXTENDED result is assigned to your DOUBLE variable.

    Also when you PRINT a number thinBasic convert you numeric variable into an EXTENDED number before printing it.
    So your DOUBLE variable is OK internally but output must be controlled.
    I do not think there are problems in calculations unless you need to save/printl data somewhere. At that point you need to control the output using some rounding functions

    You can use ROUND function to return the number of decimals you need:
    '---Load Console Module
    Uses "Console"
    Dim JD As double
    Dim F As double
    JD = 2436116.31
    PrintL(jd & " = JD")
    PrintL(Round(Frac(JD), 6)  & " = frac(JD)") 
    F = Round(Frac(JD), 6)
    PrintL(F  & " = F" )
    PrintL "Press a key to end program"
    Also STR$(...), TSTR$(...), FORMAT$(...) can be used to control number of decimals to output.

    Found something on Power Basic (the compiler I use to create thinBasic) forum regarding this:
    Last edited by ErosOlmi; 09-03-2017 at 07:52. | |
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Belgium Beveren
    Rep Power
    thanks for the info Eros
    I did not know that converting to extend happens when printing it.
    Last edited by stapper; 11-03-2017 at 16:01.

Similar Threads

  1. O2 - some basic questions
    By ReneMiner in forum O2 JIT Compiler / Assembler / Oxygen project
    Replies: 34
    Last Post: 13-08-2013, 11:11
  2. three questions
    By christianssen in forum thinBasic General
    Replies: 6
    Last Post: 10-03-2010, 11:12
  3. A few more questions..
    By oldpapa49 in forum thinBasic General
    Replies: 12
    Last Post: 21-03-2009, 21:45
  4. CGI Module questions
    By ShadowEO in forum CGI and FastCGI
    Replies: 3
    Last Post: 14-03-2009, 05:39
  5. DT questions
    By Dave in forum DT (Date module)
    Replies: 9
    Last Post: 16-04-2008, 09:48

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts