The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: What am I missing here?
Replies: 5   Last Post: Jan 28, 2013 11:03 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: What am I missing here?
Posted: Jan 28, 2013 11:03 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Gautam Sethi" <> wrote in message
> On Saturday, January 26, 2013 8:53:06 PM UTC-5, Roger Stafford wrote:
>> Gautam Sethi <> wrote in message
>> <>...

>> > Folks, this is driving me nuts! Can someone explain what may be going
>> > on? ......

>> - - - - - - - - -
>> Welcome to the world of binary arithmetic, which is what your computer
>> is using. It cannot get an exact result for .1, .2, .3, and .4, just as
>> your decimal calculator cannot get an exact result for 1/3. All of your
>> findings stem from that and the different roundings that are a
>> consequence. Remember that the "==" and 'intersect' operations expect
>> exact equality right down to the smallest bit.
>> Roger Stafford

> Sure, but why pick on poor .3?

We don't "pick on" poor 0.3 any more than decimal calculations "pick on" 2/3
(which when rounded is 0.6666 ... *snip arbitrary number of 6's* ... 67.)

> Why do I not see the same issue for .2 or .4 or the other numbers?

Because the way we calculate the result of the colon operator happens to
work out that the roundoff error results in the exact double precision
representation of 0.2 and 0.4.

For more information, see question 1 in the Math/Algorithms section of the
newsgroup FAQ and the references it includes (particularly the Cleve's
Corner article.)

> And if this really the root of the issue, shouldn't the operator == be
> equivalent to <= eps or something similar?

No. Assuming neither a nor b are NaN, exactly one of the following relations
should hold:

a == b
a < b
b < a

This is the law of trichotomy.

If we made == have a "fudge factor" then trichotomy would break down because
a == b and a < b (or a == b and b < a) could both be true. This could break
user code that assumed trichotomy.

In addition, section 5.7 of the IEEE 754-1985 floating-point standard

"It shall be possible to compare floating-point numbers in all supported
formats, even if the operands' formats differ. Comparisons are exact and
never overflow nor underflow."

To reiterate Roger's statement: welcome to the world of floating-point

Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.