Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug777.cpp

1    #include <iostream.h>
2
3    int main()
4        {
5        const double three = 3.0;
6        double x, y, z;
7        x = 1 / three;
8        y = 4 / three;
9        z = 5 / three;
10        if( x + y == z )
11            cout << "1/3 + 4/3 == 5/3 \n";
12        else
13            cout << "1/3 + 4/3 != 5/3 \n";
14        return 0;
15        }

The programmer expected the test for equality to succeed but instead most compilers test this as not equal. How come?


bug777.cpp lint Output

--- Module:   bug777.cpp (C++)
                   _
    if( x + y == z )
bug777.cpp(15) : Info 777: Testing floats for equality

Reference Manual Explanation


777    Testing float's for equality  -- This message is issued when the
       operands of operators == and != are some form of floating type
       (float, double, or long double).  Testing for equality between
       two floating point quantities is suspect because of round-off
       error and the lack of perfect representation of fractions.  If
       your numerical algorithm calls for such testing turn the message
       off.  The message is suppressed when one of the operands can be
       represented exactly, such as 0 or 13.5.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #864 - July 2009

Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2009, Gimpel Software, All rights reserved.