LB Booster
« maths problem »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 05:07am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: maths problem  (Read 464 times)
hammerjit
New Member
Image


member is offline

Avatar




PM


Posts: 26
xx maths problem
« Thread started on: Apr 21st, 2015, 07:03am »

Hi, I am using LBB and when run this code I am not getting "same or more" as a result.

Can someone please guide me.

Code:
hkval = 50000*.60

valueD = hkval/1000

valueC = int(valueD)


if valueC < valueD then
   print "less"
else
    print "same or more"
end if
 
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: maths problem
« Reply #1 on: Apr 21st, 2015, 07:23am »

You are expecting exact equal of two numbers
Problem is that valueD is floating-point number
(example: you could not exactly represent 1/3 in decimals, you'll get 0.3333333333333. About same way, computer cannot present 1/10 exactly in binary.)
They are stored in computer memory approximately.
Even if you expect it to be 30 it might be something like 30.000000000000001 or 29.9999999999999

The only things that could be guaranteed be exactly equal is integers.
Floating point numbers might be meaningfully compared with given precision.
Code:
precision = 1e-5

a = 1
b = 1.0001

if abs(a-b)<precision then
    print "a and b equal up to ";precision
else
    print "a and b are different"
end if

b = 1.000001
if abs(a-b)<precision then
    print "a and b equal up to ";precision
else
    print "a and b are different"
end if
 
« Last Edit: Apr 21st, 2015, 07:25am by tsh73 » User IP Logged

hammerjit
New Member
Image


member is offline

Avatar




PM


Posts: 26
xx Re: maths problem
« Reply #2 on: Apr 21st, 2015, 07:34am »

so how do I apply it in my example code? is there a possible way?
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: maths problem
« Reply #3 on: Apr 21st, 2015, 08:46am »

Code:
precision = 1e-5

hkval = 50000*.60

valueD = hkval/1000

valueC = int(valueD)


if valueC < valueD-precision then
   print "definitely less"
else
    print "approximately same or more"
end if

 
User IP Logged

hammerjit
New Member
Image


member is offline

Avatar




PM


Posts: 26
xx Re: maths problem
« Reply #4 on: Apr 21st, 2015, 08:53am »

thank you so much....that really helped!!!
grin grin grin
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: maths problem
« Reply #5 on: Apr 21st, 2015, 09:09am »

on Apr 21st, 2015, 07:34am, hammerjit wrote:
so how do I apply it in my example code? is there a possible way?

What is the problem you are trying to solve? Your code, as it stands, appears to be trying to check whether or not valueD is an integer. It is giving the correct result, because in this case valueD is actually:

Code:
30.00000000000000000173472347597680709441192448139190673828125 

(for the reason Anatoly explained).

LBB performs its floating-point calculations to considerably greater accuracy than JB or LB (about 19 significant figures compared with about 16) but small differences of this kind are unavoidable unless you use only integer arithmetic.

Richard.
« Last Edit: Apr 21st, 2015, 5:49pm by Richard Russell » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: maths problem
« Reply #6 on: Apr 21st, 2015, 2:52pm »

Here's a good illustration of why you have to be careful. Make a small change to the program: change 0.60 to 0.56 as follows:

Code:
hkval = 50000*.56

valueD = hkval/1000

valueC = int(valueD)


if valueC < valueD then
   print "less"
else
    print "same or more"
end if 

Now the program reports "less" when run in JB and "same or more" when run in LBB - in other words the opposite of the earlier results!

Richard.
User IP Logged

hammerjit
New Member
Image


member is offline

Avatar




PM


Posts: 26
xx Re: maths problem
« Reply #7 on: Apr 22nd, 2015, 06:23am »

yes Richard, I understand it now. Thanks again
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »


This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls