[NTLUG:Discuss] The wrong computation example from the newsgroup

Steve Baker sjbaker1 at airmail.net
Sun Mar 18 00:25:36 CST 2001


Fred James wrote:

> Then, just as a point of interest, changing the original printf statement to
>   printf("%2.0f = %2.0f\n", (((60/6)*0.3) + (10*0.7)), ((( a/b)*0.3) + ( c*0.7)))
> did yield "10 = 10"
> 
> What up with that?

(My earlier reply explains most of this)

When 'printf' has to convert an IEEE float into an ASCII decimal, it has
to do lots more math on it. More math means more roundoff error - and roundoff
can be either upwards or downwards.  In this case, it appears to be upwards. There
is still no inconsistancy or error - just a lot of people not thinking.

The *CORRECT* version of this print statement is:

  printf("%d = %d\n", 
       (int) rint(((60/6)*0.3) + (10*0.7)),
       (int) rint((( a/b)*0.3) + ( c*0.7)) ) ;

...which consistantly prints 10==10 on every computer on the planet under every
OS (providing it supports the rint function of course).

If you want a real puzzle, tell me the answers to these sums:

Given:

   #define A   10
   #define B  -10
   #define C    4
   #define D   -4
   int a =  A ;
   int b =  B ;
   int c =  C ;
   int d =  D ;

What is the value of:

   A % C
   A % D
   B % C
   B % D

   a % c
   a % d
   b % c
   b % d

...A % C is 2...what about the others?   The answer to this is different on some
computers compared to others.

-- 
Steve Baker   HomeEmail: <sjbaker1 at airmail.net>
              WorkEmail: <sjbaker at link.com>
              HomePage : http://web2.airmail.net/sjbaker1
              Projects : http://plib.sourceforge.net
                         http://tuxaqfh.sourceforge.net
                         http://tuxkart.sourceforge.net
                         http://prettypoly.sourceforge.net
                         http://freeglut.sourceforge.net



More information about the Discuss mailing list