I don't think your example with == would ever fail. According to the
IEEE floating point standard, integers within the dynamic range of the
number type must be represented exactly and must compare as equal.

Furthermore, the four basic operations on integers within the dynamic
range of the floating point number type that have integer results
within that dynamic range must be exact. Thus, 25.0 - 5.0, 13.0 + 7.0,
4.0 * 5.0, and 60.0 / 3.0 all must equal 20.0 exactly.

Where you run into trouble is when the numbers in question cannot be
represented exactly in the finite precision floating point format --
something like this:

double d1 = 6.1;
double d2 = 11.6;
double d3 = 17.7;

assert(d1 + d2 == d3); // might fail, and Wrong way to do it !!

And your "correct and recommended way" is missing an absolute value:

assert(abs(d1 + d2 - d3) < 1.0e-5); // given you assume precision of
1e-5 is the correct and recommended way.

Dave

On Jan 6, 4:19 pm, Avi Dullu <avi.du...@gmail.com> wrote:
> Just to mention, floating point numbers r always compared *for equality*
> like
>
> double d1 = 90.0;
> double d2 = 90.0;
>
> assert(d1 == d2); // might fail, and Wrong way to do !!
>
> assert(d1 - d2 < 1e-5); // given u assume precision of 1e-5, is the correct
> and recommended way.
>
> Programmers should realize their critical importance and responsibility in a
> world gone digital. They are in many ways similar to the priests and monks
> of Europe's Dark Ages; they are the only ones with the training and insight
> to read and interpret the "scripture" of this age.
>
>
>
> On Fri, Jan 7, 2011 at 1:47 AM, juver++ <avpostni...@gmail.com> wrote:
> > Numbers without fractional parts are represented exactly (in a range
> > supported by double).
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algoge...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups­.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to