From: mohammed on 21 Apr 2010 08:44 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 21 Apr 2010 09:08 "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 21 Apr 2010 09:20 "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
|
Pages: 1 Prev: subpixel image classification using fuzzy logic tool Next: for loop with big array |