From: mohammed on
i have a problem calculating this !
1 - 2.3 + 1.72 - 0.42
it does'nt result to zero !! ..it's included in if condition and it must result into 0 :S so wht shall i do?
thanks in advance
From: John D'Errico on
"mohammed " <m.eng.mahrous(a)gmail.com> wrote in message <hqmrul$q8a$1(a)fred.mathworks.com>...
> i have a problem calculating this !
> 1 - 2.3 + 1.72 - 0.42
> it does'nt result to zero !! ..it's included in if condition and it must result into 0 :S so wht shall i do?
> thanks in advance

Surprise! You have won the award as the 100 millionth
person to have "discovered" floating point arithmetic.

Read this:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Learn to use tolerances, rounding, etc., when making
tests on floating point numbers.

John
From: Yi Cao on
"mohammed " <m.eng.mahrous(a)gmail.com> wrote in message <hqmrul$q8a$1(a)fred.mathworks.com>...
> i have a problem calculating this !
> 1 - 2.3 + 1.72 - 0.42
> it does'nt result to zero !! ..it's included in if condition and it must result into 0 :S so wht shall i do?
> thanks in advance

You cannot assume a real result equals to an exact value. For this example, if you use it in a if condition such as

if (1 - 2.3 + 1.72 - 0.42) == 0

you can convert it to

if abs(1 - 2.3 + 1.72 - 0.42) < eps

Read 'help eps' to learn more if you do not understand this.

HTH
Yi