[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Comparing of values
From: |
Mike Miller |
Subject: |
RE: Comparing of values |
Date: |
Fri, 22 Oct 2004 12:26:06 -0500 (CDT) |
On Fri, 22 Oct 2004, Hall, Benjamin wrote:
Searching for exact matches to floating point numbers can be troublesome
just due to the nature of floating point numbers in computers
(unfortunately, I can't really give a better answer than that!). For
instance
3*0.05 - 0.15
ans =
2.7756e-017
One work-around is the following:
beta = 0.05:0.05:0.95;
beta( beta == 3*0.05 )
or
beta( abs( beta - 0.15 ) < 0.00000001 )
It seems to me that this problem has come up fairly often. Here's a
suggestion that might help:
Develop an additional boolean operator called "approximately equal" that
would return 1 if terms were within, say, 10*eps, and 0 otherwise. It
could use "~~", and work like this:
beta( beta ~~ 0.15 )
It would have the benefit of simplifying code and drawing attention to the
problem of rounding errors. It would not be MATLAB compatible. It would
require work that I am not able to do.
Even if that can't be done, maybe the 'help' entry for "==" could be
changed to add a bit about the way that comparisons are made -- exact
binary matches are required. For example, the computer's binary
representation of .15 does not exactly match its binary representation of
3*.05, and therefore, .15 == 3*.05 returns 0.
Mike
--
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology and Community Health
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------