|
Mathias Neuhaus <no.mathias@spam.dokom.net> wrote:
> > float p;
> >
> > p = 0.3;
> >
> > if(p == 0.4)
> > do something;
>
> 0th law of computer science:
>
> NEVER use == on floats; it simply doesn't work as expected.
>
> In most cases a value cannot be exactly represented as a float
> variable; so MOST comparisons for equality WILL fail.
Exceptions to the 0th law:
You are manipulating and comparing floating-point values you know are
integers with a magnitude less than a million or two.
You are manipulating and comparing values you know are integer multiples of
some power of two (especially negative powers of two, i.e fractions like
eights). (This is really a superset of the first exception.)
The exceptions would not apply to numbers like 0.3 and 0.4.
BTW, did the original poster ever find resolution to this? Was it some
silly non-float bug (like = instead of == as somebody suggested)? Or does
the floating point code still seem to be in error? Nobody got back to me
on this, so of course I didn't check it out. If this is still a problem,
e-mail kekoa@graphics.stanford.edu until I fix it.
I do not think the error in question was a violation of the 0th law, since
in this case I believe "do something" was executing indicating (supposedly)
that 0.3 == 0.4, which is definitely incorrect.
-Kekoa
|
|
Message is in Reply To:
| | Re: Floating ponts ??
|
| Hi there! (...) 0th law of computer science: NEVER use == on floats; it simply doesn't work as expected. In most cases a value cannot be exactly represented as a float variable; so MOST comparisons for equality WILL fail. (this is NOT an issue of (...) (24 years ago, 17-May-01, to lugnet.robotics.rcx.legos)
|
5 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|