|
Post by sirbop on Apr 24, 2023 20:36:40 GMT
I was adding two values and checking if the sum was .19.
When I tested a=1.99 and b=-1.8 the sum was 0.19 (as expected)
but when I tested if a + b = .19 I found they were not equal.
Instead - (a+b) -.19 = -0.55511151e-16
Where have I gone wrong.
a= 1.99
b= -1.8
print (a+b) - .19
gives -0.55511151e-16
Thanks.
|
|
|
Post by Rod on Apr 24, 2023 21:21:45 GMT
You have done nothing wrong. This is just how all computers handle floats. You can never compare two floats for equality because they will always differ very slightly. Epsilon is the term to google. It’s the way the maths processor works and simply the fact that there is no endless precision, we have a finite precision and when numbers are reached by different routes there will be these small differences.
You can also limit the precision with int() or using() functions which will make the lower precision number you create equal.
I am sure others will add to this.
|
|
|
Post by Brandon Parker on Apr 25, 2023 1:06:23 GMT
To understand it fully, research the IEEE 754 standard.
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Apr 25, 2023 6:01:28 GMT
My favorite example is print (0.1+0.1+0.1)-0.3 gives 0.55511151e-16 (and "3*0.1-0.3" too)
See, there is a nice number 1/3 But whatever you try you cannot write it down as decimal exactly - it's endless periodical 0.33333...
And for binary (where it finally gets coded anyway), 0.1 decimal is endless periodical (by Google: 0.00011001100110011... )
So 0.1 in computer is always approximate number (and many many others too). (=> If you want it exact keep it integer. Really working advice!)
EDIT
Same caveat applies to loops Who might guess that first loop covers end point, but second one does not (having one iteration less)?
for x = 0 to 0.5 step 0.1 print x next
print
for x = 1 to 1.5 step 0.1 print x next 0 0.1 0.2 0.3 0.4 0.5
1 1.1 1.2 1.3 1.4
|
|
|
Post by plus on Apr 25, 2023 16:10:59 GMT
CloseEnough = .00001 a = 1.99 b = -1.8 ' if a+b = .19 then abs(a+b -.19) < CloseEnough if abs(a+b -.19) < CloseEnough then print "a + b is close enough to .19" else print "a + b is no where near .19."
|
|