Click on image to see enlargment

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

            bug524.cpp

1     #include <stdio.h>
2
3     double abs( double x ) { return x >= 0 ? x : -x; }
4     double sqrt( double y )
5         {
6         double x = 1.0;             /* initial estimate */
7         int error;
8
9         if( y <= 0 ) return 0;      /* ensure positive value */
10        while( abs( error = y - x * x ) > .0005 )
11            x = x + error / (2*x);  /* adjust estimate */
12        return x;
13        }
14 
15    int main()
16        {
17        printf( "sqrt(%d) = %8.3f\n", 2, sqrt(2) );
18        return 0;
19        }

In computing the square root of 2 by a variant of Newton's method the programmer was shocked to find that the answer printed was 1.5 -- what's the mistake?


bug524.cpp lint Output

--- Module:   bug524.cpp
                                  _
    while( abs( error = y - x * x ) > .0005 )
bug524.cpp(10) : Warning 524: Loss of precision (assignment) (double to int)
bug524.cpp(10) : Info 747: Significant prototype coercion (arg. no. 1) int to double
                                           _
    printf( "sqrt(%d) = %8.3f\n", 2, sqrt(2) );
bug524.cpp(17) : Info 747: Significant prototype coercion (arg. no. 1) int to double

Reference Manual Explanation

 
524   Loss of precision (Context) (Type to Type)  -- There is a possible loss of a
      fraction in converting from a float to an integral quantity.  Use of a cast will 
      suppress this message.

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

Previous Bug - Bug #437 - December 2003

[ new ] 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 2006, Gimpel Software, All rights reserved.