Type punning in C

Jerry Berkman jerry at violet.berkeley.edu
Wed Oct 11 04:58:51 AEST 1989


In article <475 at idacrd.UUCP> desj at idacrd.UUCP (David desJardins) writes:
>   Does anyone have any ideas on how one should go about converting an
>integer to the floating-point number with the same bit pattern?
>The best way I have found to do this in FORTRAN is to write

>      INTEGER I
>      REAL X
>      X = OR (I, 0).

>(The Cray FORTRAN compiler optimizes out the trivial OR instruction.)
>   -- David desJardins

Why not use equivalence?
	INTEGER I
	REAL X, IX
	EQUIVALENCE (X,IX)

	IX = I
The Fortran standard specifies that a REAL and INTEGER occupy the same space.
The only problem is this might fool some optimizers.

	- Jerry Berkman
	  jerry at violet.berkeley.edu



More information about the Comp.lang.c mailing list