Author |
Topic: maths problem (Read 463 times) |
|
hammerjit
New Member
member is offline
Posts: 26
|
|
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
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
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 » |
Logged
|
|
|
|
hammerjit
New Member
member is offline
Posts: 26
|
|
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?
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
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
|
|
Logged
|
|
|
|
hammerjit
New Member
member is offline
Posts: 26
|
|
Re: maths problem
« Reply #4 on: Apr 21st, 2015, 08:53am » |
|
thank you so much....that really helped!!!
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
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.
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
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.
|
|
Logged
|
|
|
|
hammerjit
New Member
member is offline
Posts: 26
|
|
Re: maths problem
« Reply #7 on: Apr 22nd, 2015, 06:23am » |
|
yes Richard, I understand it now. Thanks again
|
|
Logged
|
|
|
|
|