From: Bogdan Cristea on 10 Jan 2010 13:40 The following code gives the wrong result: x = 0:0.1:5; find(x==1.4) The output of the find function is an empty matrix. Should be this considered as a bug ?
From: Jan Simon on 10 Jan 2010 14:28 Dear Bogdan! > The following code gives the wrong result: > x = 0:0.1:5; > find(x==1.4) > The output of the find function is an empty matrix. Should be this considered as a bug ? Have you read the FAQ as suugested e.g. on the MathWorks web interface of this newsgroup: "Check if your question is answered in the comp.soft-sys.matlab FAQ" ? There you find: http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F So this is not a bug, but the typical, expected and intented behaviour of floating point numbers. Kind regards, welcome to this newsgroup, Jan
From: Bogdan Cristea on 11 Jan 2010 01:26 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hid9o3$e29$1(a)fred.mathworks.com>... > Dear Bogdan! > > > The following code gives the wrong result: > > x = 0:0.1:5; > > find(x==1.4) > > The output of the find function is an empty matrix. Should be this considered as a bug ? > > Have you read the FAQ as suugested e.g. on the MathWorks web interface of this newsgroup: "Check if your question is answered in the comp.soft-sys.matlab FAQ" ? > There you find: > http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F > > So this is not a bug, but the typical, expected and intented behaviour of floating point numbers. > > Kind regards, welcome to this newsgroup, Jan Yes, you have right, I have forgotten about how the floating point numbers are represented in a PC. Nevertheless, in a high level language, the tolerance when comparing numbers could be taken into account.
From: Matt J on 11 Jan 2010 03:02 "Bogdan Cristea" <cristeab(a)gmail.com> wrote in message <hieg9r$ra3$1(a)fred.mathworks.com>... > Yes, you have right, I have forgotten about how the floating point numbers are represented in a PC. > Nevertheless, in a high level language, the tolerance when comparing numbers could be taken into account. =============== It can be taken into account, but not by the comparison operator a==b, because the tolerance you would want depends on the context in which it is used. However, this FEX submission does something along the lines of what you are talking about: http://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf
From: Jan Simon on 11 Jan 2010 04:20
Dear Bogdan! > Nevertheless, in a high level language, the tolerance when comparing numbers could be taken into account. Correct! It *must* be taken into account. Nevertheless, an exact working operator for comparing floating point numbers is needed also. Besides the excellent ismemberf of Bruno, you can manage the tolerance manually also, e.g. for an absolute tolerance: find(abs(x - 0.4) < 10 * eps) or for a relative tolerance (needed e.g. if working with very small values): find(abs(x - 0.000004) ./ max(x, 0.000004) < 0.01) Even a high level language as Matlab needs a well defined set of low level functions. Good luck, Jan |