Type punning in C

Richard O'Keefe ok at cs.mu.oz.au
Sun Oct 8 17:10:54 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?

	union { long i; float x; } pun;
	pun.i = the_integer_to_be_converted;
	the_single_precision_result = pun.f;

> One frequently (well, occasionally) needs to do this to implement certain
> bit manipulations efficiently on Cray computers and other vector machines ...

Just which operations are these?  If the operations are meaningful functions
of floating-point numbers (e.g. things like ilogb() copysign() nextafter())
it might make more sense to ask Cray to put _those_ in their library.

I really would like to know what the operations are.



More information about the Comp.lang.c mailing list