Bug 80906 - Math error in comparison
Summary: Math error in comparison
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 8.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2003-01-02 04:46 UTC by Kyle Bateman
Modified: 2016-11-24 15:28 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2003-01-07 16:36:36 UTC
Embargoed:


Attachments (Terms of Use)

Description Kyle Bateman 2003-01-02 04:46:24 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130

Description of problem:
When doing a simple comparison of certain particular numbers, I can
generate compare errors. I can add two numbers, compare the result to
the expected sum and they don't compare equal, even though they are
equal. 

I've demonstrated the same problem in a tcl script and in a compiled C program
so I'm assuming its a libc thing.



Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
Compile and run this program:

#include <stdio.h>

double A =      1537.44;
double B =       161.40;
double C =      1698.84;

main () {
  if (A + B == C)
    printf("%f = %f\n",A + B, C);
  else if (A + B > C)
    printf("%f > %f\n",A + B, C);
  else
    printf("%f < %f\n",A + B, C);
}


Actual Results:  The answer shows:

1698.840000 > 1698.840000


Expected Results:  I would expect:

1698.840000 = 1698.840000


Additional info:

Is this a bug?  Or is one expected to deal with minor bit errors like this even
when doing simple math like this?

Comment 1 Miloslav Trmac 2003-01-02 14:45:09 UTC
NOTABUG.
Floating-point arithmetic is seldom "exact", and what is exact in
decimal, doesn't have to be exact in binary:

double A = 1537.44;
double B = 161.40;
double C = 1698.84;
main ()
{
  printf ("%.20f %.20f %.20f %.20f\n", A, B, C, A + B);
  return 0;
}

produces:
1537.44000000000005456968 161.40000000000000568434 1698.83999999999991814548 
1698.84000000000014551915



Note You need to log in before you can comment on or make changes to this bug.