Bug 1756 – comparing a constant to artihmetic expression with floating point types fails
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2007-12-31T06:21:00Z
Last change time
2015-06-09T01:14:23Z
Assigned to
bugzilla
Creator
sven.stoenner
Comments
Comment #0 by sven.stoenner — 2007-12-31T06:21:46Z
the direct comparison of a constant to an arithmetic expression does not work correctly when using variables:
// float
float a = 0.6f, 0.8f;
float c = a / b;
assert(c == 0.6f / 0.8f); // ok
assert(c == a / b); // fails
assert(0.75f == 0.6f / 0.8f); // ok
assert(0.75f == a / b); // fails
// double
double a = 0.6, b = 0.8;
double c = a / b;
assert(c == 0.6 / 0.8); // fails
assert(c == a / b); // fails
assert(0.75 == 0.6 / 0.8); // ok
assert(0.75 == a / b); // fails
Comment #1 by dhasenan — 2007-12-31T07:25:28Z
[email protected] wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=1756
>
> Summary: comparing a constant to artihmetic expression with
> floating point types fails
> Product: D
> Version: 2.008
> Platform: PC
> OS/Version: Windows
> Status: NEW
> Severity: normal
> Priority: P2
> Component: DMD
> AssignedTo: [email protected]
> ReportedBy: [email protected]
>
>
> the direct comparison of a constant to an arithmetic expression does not work
> correctly when using variables:
>
> // float
> float a = 0.6f, 0.8f;
Is this exactly what you tried?
It should of course be:
float a = 0.6f, b = 0.8f;
Comment #2 by sven.stoenner — 2007-12-31T07:55:40Z
> Is this exactly what you tried?
> It should of course be:
> float a = 0.6f, b = 0.8f;
>
no, i've tried
float a = 0.6f, b = 0.8f;
this failure is occured while editing this report, sorry.
Comment #3 by sven.stoenner — 2007-12-31T08:18:03Z
2007/12/31, [email protected] <[email protected]>:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=1756
>
>
>
>
>
> ------- Comment #1 from [email protected] 2007-12-31 07:25 -------
> [email protected] wrote:
> > http://d.puremagic.com/issues/show_bug.cgi?id=1756
> >
> > Summary: comparing a constant to artihmetic expression with
> > floating point types fails
> > Product: D
> > Version: 2.008
> > Platform: PC
> > OS/Version: Windows
> > Status: NEW
> > Severity: normal
> > Priority: P2
> > Component: DMD
> > AssignedTo: [email protected]
> > ReportedBy: [email protected]
> >
> >
> > the direct comparison of a constant to an arithmetic expression does not
> work
> > correctly when using variables:
> >
> > // float
> > float a = 0.6f, 0.8f;
>
> Is this exactly what you tried?
> It should of course be:
> float a = 0.6f, b = 0.8f;
>
>
> --
> Configure bugmail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
>
no, i've tried
float a = 0.6f, b = 0.8f;
this failure is occured while editing this report, sorry.
<br><div><span class="gmail_quote">2007/12/31, <a href="mailto:[email protected]">[email protected]</a> <<a href="mailto:[email protected]">[email protected]</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<a href="http://d.puremagic.com/issues/show_bug.cgi?id=1756">http://d.puremagic.com/issues/show_bug.cgi?id=1756</a><br><br><br><br><br><br>------- Comment #1 from <a href="mailto:[email protected]">[email protected]</a>
2007-12-31 07:25 -------<br><a href="mailto:[email protected]">[email protected]</a> wrote:<br>> <a href="http://d.puremagic.com/issues/show_bug.cgi?id=1756">http://d.puremagic.com/issues/show_bug.cgi?id=1756
</a><br>><br>> Summary: comparing a constant to artihmetic expression with<br>> floating point types fails<br>> Product: D<br>> Version: 2.008<br>> Platform: PC
<br>> OS/Version: Windows<br>> Status: NEW<br>> Severity: normal<br>> Priority: P2<br>> Component: DMD<br>> AssignedTo: <a href="mailto:[email protected]">
[email protected]</a><br>> ReportedBy: <a href="mailto:[email protected]">[email protected]</a><br>><br>><br>> the direct comparison of a constant to an arithmetic expression does not work
<br>> correctly when using variables:<br>><br>> // float<br>> float a = 0.6f, 0.8f;<br><br>Is this exactly what you tried?<br>It should of course be:<br>float a = 0.6f, b = 0.8f;<br><br><br>--<br>Configure bugmail:
<a href="http://d.puremagic.com/issues/userprefs.cgi?tab=email">http://d.puremagic.com/issues/userprefs.cgi?tab=email</a><br>------- You are receiving this mail because: -------<br>You reported the bug, or are watching the reporter.
<br></blockquote></div><br>no, i've tried<br>float a = 0.6f, b = 0.8f;<br><br>this failure is occured while editing this report, sorry.<br>
Comment #4 by wbaxter — 2007-12-31T13:11:48Z
You should almost never rely on == to compare floating point numbers.
The failure you're seeing is a perfect example of why not.
Here's the first thing I could google up about it:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
but if you'll look you should find dozens of references saying the same thing.
That said, I can see why you would expect those particular cases to work, but in general you should never be surprised by equality failing in floating point comparisons. There are just too many things that can go wrong.
Comment #5 by bugzilla — 2008-08-28T17:56:00Z
Due to roundoff error, expressions that are mathematically the same are not the same with computer floating point.