PDA

View Full Version : questions about fractions



stapper
08-03-2017, 23:42
hi,

I have 2 questions
1)
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

2)
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

ErosOlmi
09-03-2017, 01:46
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)")
PrintL
F = Round(Frac(JD), 6)
PrintL(F & " = F" )

PrintL "Press a key to end program"

WaitKey


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:
https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-console-compiler/46143-standard-for-floating-point-arithmetic-ieee-754-in-powerbasic

stapper
11-03-2017, 16:00
thanks for the info Eros
I did not know that converting to extend happens when printing it.