Strange result of a substraction
-
@macgile said:
i have tested with a = a + b the result is same
That is because the Ruby interpreter converts
a += b
to
a = a + b
before the expression is evaluated. -
@unknownuser said:
i dont compare anything !!!
i have tested with a = a + b the result is sameI just meant that is always good to avoid considering floats as exact numbers.
-
-
@voljanko said:
@unknownuser said:
i dont compare anything !!!
i have tested with a = a + b the result is sameI just meant that is always good to avoid considering floats as exact numbers.
oh yes

I thought the result would be near to 0 and not of 4.
-
Try to add some number,you will see that is zero and not 4.
-
A singleton method to increment a float a, by an argument arg, to dec number of decimal places. (It defaults to 1 decimal place.)
a = 9.0 def a.incr( arg = 1.0, dec = 1 ) dec = dec.to_i arg = round( arg.to_f * 10**dec ) # self is object a temp =( round( self * 10**dec ) + arg ).to_f self =( temp / 10**dec ) enduse it like:
a = 9.0 b = -1.8 a.incr(b) -
@macgile said:
I thought the result would be near to 0 and not of 4.
macguile,
4.44089209850063e-016
The e-016 at the end of the number means to move the decimal place 16 places to the left - making the actual number:
0.0000000000000000444089209850063
or very nearly zero. The reason the result is not exactly zero is due to floating point errors as linked above.
-
@dan rathbun said:
A singleton method to increment a float a, by an argument arg, to dec number of decimal places. (It defaults to 1 decimal place.)
a = 9.0 > def a.incr( arg = 1.0, dec = 1 ) > dec = dec.to_i > arg = round( arg.to_f * 10**dec ) > # self is object a > temp =( round( self * 10**dec ) + arg ).to_f > self =( temp / 10**dec ) > enduse it like:
a = 9.0 b = -1.8 a.incr(b)THANK for this solution Dan
Best Regard

-
@jim said:
@macgile said:
I thought the result would be near to 0 and not of 4.
macguile,
4.44089209850063e-016
The e-016 at the end of the number means to move the decimal place 16 places to the left - making the actual number:
0.0000000000000000444089209850063
or very nearly zero. The reason the result is not exactly zero is due to floating point errors as linked above.
thank i nderstand now

regard
-
The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic
Aims to provide both short and simple answers to the common recurring questions of novice programmers about floating-point numbers not 'adding up' correctly, and more in-depth information about how IEEE 754 floats work, when and how to use them correctly, and what to use instead when they are not appropriate.
(floating-point-gui.de)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement