6.0==5 ? 
Author Message
 6.0==5 ?

Hi,

I have the following code:
--------------
#include <stdio.h>
#include <math.h>

int a;
double x,y,c;
void main (void) {
   x = log(2.0);
   y = log(64.0);
   a = (int)(y / x);
   printf ("%f %i\n",y/x,a);

Quote:
}

---------------
What I get is
6.0000 5
on my Intel-based machine. The same Program compiled and started on a sun or
sgi will give me
6.0000 6

My question is:
Does printf round the result of y/x ? Or why do I get such results ?

Thanks for your answers
Felix



Fri, 25 Aug 2000 03:00:00 GMT  
 6.0==5 ?


Quote:
>Hi,

>I have the following code:
>--------------
>#include <stdio.h>
>#include <math.h>

>int a;
>double x,y,c;
>void main (void) {
>   x = log(2.0);
>   y = log(64.0);
>   a = (int)(y / x);

suppose that y/x is 5.999999999999999999 on machine Q and 6.000000000000000001
on machine R.  In this case, you will get 5 on Q and 6 on R.  Both are equally
correct.  This (int) cast throws away the fractional part.

Quote:
>   printf ("%f %i\n",y/x,a);
>}
>---------------
>What I get is
>6.0000 5
>on my Intel-based machine. The same Program compiled and started on a sun or
>sgi will give me
>6.0000 6

>My question is:
>Does printf round the result of y/x ? Or why do I get such results ?

Please read the C FAQ section on floating point rounding.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Fri, 25 Aug 2000 03:00:00 GMT  
 6.0==5 ?


Quote:
>#include <stdio.h>
>#include <math.h>
>int a;
>double x,y,c;
>void main (void) {
>   x = log(2.0);
>   y = log(64.0);
>   a = (int)(y / x);
>   printf ("%f %i\n",y/x,a);
>}
>---------------
>What I get is
>6.0000 5
>on my Intel-based machine. The same Program compiled and started on a sun or
>sgi will give me
>6.0000 6

>My question is:
>Does printf round the result of y/x ? Or why do I get such results ?

The mathematical result of log(64)/log(2) is 6.0, of course. But
floating point data types are only approximations of real numbers.
Thus the calculated values for log(64) and log(2) will differ from the
mathematical values and the quotient too, will differ. Depending on
the implementation of log, the result may not be the same on different
machines. On one machine it may be 6.000000000000000001, on another
5.999999999999999999. Once it will be rounded to 6 and once to 5 by
the cast to (int).

Nothing to complain about.

Regards
Horst

   *** Las orillas del Nahuel Huapi ***



Fri, 25 Aug 2000 03:00:00 GMT  
 6.0==5 ?



Quote:
" writes:
>Hi,

>I have the following code:
>--------------
>#include <stdio.h>
>#include <math.h>

>int a;
>double x,y,c;
>void main (void) {

main returns int in C, always.

Quote:
>   x = log(2.0);
>   y = log(64.0);
>   a = (int)(y / x);
>   printf ("%f %i\n",y/x,a);
>}
>---------------
>What I get is
>6.0000 5
>on my Intel-based machine. The same Program compiled and started on a sun or
>sgi will give me
>6.0000 6

>My question is:
>Does printf round the result of y/x ? Or why do I get such results ?

This has nothing to do with printf. When you convert a floating point value
to an integer type the fractional part of the result is discarded.
Floating point arithmetic doesn't in general guarantee exact results so
y/x might evaluate to something like 5.9999999999.. which when converted
to int will end up as 5. Change The %f to something like %.20f to see
a much better approximation of what y/x evaluated to. Be aware however that
intermediate results can be held at a higher precision than values
stored in variables so you may get more consistent results if you write:

    c = y / x;

and use c instead of y / x in subsequent expressions.

--
-----------------------------------------


-----------------------------------------



Fri, 25 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. VJ++ 6.0 and VC++ 6.0.

2. VC++ 6.0 and VJ++ 6.0???

3. Problem with Visual Studio 6.0 (Visual C++ 6.0) !!!!

4. VC++ 6.0 and VJ++ 6.0???

5. VC++ 6.0 , MFC 6.0?

6. Using ADO 2.6 with Visual Baisc 6.0 is more efficient than Visual C++ 6.0?

7. Visual C++ 6.0 and Visual Basic 6.0 on Win NT 4.0 Arabic.

8. Visual C++ 6.0 and Visual Basic 6.0 on Win NT 4.0 Arabic.

9. What's up with DirectX SDK 6.0 and VC 6.0 ?

10. Vc++ 6.0 dll creation to use in Vb 6.0 step by step help needed?

11. Conversion utility for libraries from MS C 6.0 to MS VC++ 6.0?

12. Visual C++ 6.0 and Visual Basic 6.0 on Win NT 4.0 Arabic.

 

 
Powered by phpBB® Forum Software