www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/19/08:13:37

Date: Sat, 19 Nov 94 11:36:46 GMT
From: dolan AT fnoc DOT navy DOT mil (Kent Dolan)
To: davis AT amy DOT tch DOT harvard DOT edu
Subject: Re: printf or floating point error?????
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

John,

Well, the expected correction wasn't long in coming.

KPD>  [blathers on about casts, forgetting that only pointer casts preserve
       the pointed to bit patterns]

JD> I do not think that this statement is correct. 
JD> Consider:

JD>    float x, y;
JD>    long i = 10;

JD>    /* assume sizeof (float) == sizeof (long) */

JD>    x = (float) i;
JD>    y = *(float *) &i;

JD> Here `x' will have the value of 10.0 whereas `y' has the bit pattern
JD> that you are suggesting above.  `y' will will almost certainly not be
JD> 10.0 and `x' will not have the bit pattern of `i'.  That is, the float
JD> cast above will definitely modify the bit pattern of i.

I think I will give up even pretending to understand this stuff:

-------------------------------8<---cut here--->8-------------------------------
#include <stdio.h>
#include <stdlib.h>

main()
{
  long i = 10 ;
  float x , y , z ;
  double w ;
  
  x = i ;
  y = (float) i ;
  z = *(float *) &i ;

  printf( "long is: %d bytes, float is: %d bytes, double is: %d bytes.\n",
          sizeof( long ), sizeof( float ), sizeof( double ) );

  printf( "i=%8.8XH, x=%8.8XH, y=%8.8XH, z=%8.8XH\n", i, x, y, z );

  printf( "i=%g, " , w=i );
  printf( "x=%g, " , w=x );
  printf( "y=%g, " , w=y );
  printf( "z=%g\n" , w=z );

  exit(0);
}
-------------------------------8<---cut here--->8-------------------------------
long is: 4 bytes, float is: 4 bytes, double is: 8 bytes.
i=0000000AH, x=40240000H, y=00000000H, z=40240000H
i=10, x=10, y=10, z=1.4013e-44
-------------------------------8<---cut here--->8-------------------------------

Huh?

Variables x and z hold the same bit pattern, but x and y give the same results?

With the same print format and mechanism?

That from a SunOS 4.1.4 or so delivered cc(), pre-ANSI C.

Xanthian.
--
Kent, the man from xanth.
Kent Paul Dolan, CSC contractor at Fleet Numerical.  (408) 656-4363.
(Navy Unix email:   )  (Navy cc:Mail email: )  (real world email:     )
<dolan AT fnoc DOT navy DOT mil>  <dolank AT fnoc DOT navy DOT mil>  <xanthian AT well DOT sf DOT ca DOT us>

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019