RE: The joy of type aliasing and C

Brian Hook <[email protected]>
Newsgroups gmane.games.devel.general
Message-ID <20031230125351.635679@GATEWAY>
> 2) What is the most portable way (that avoids risk of alias
> optimisation) of getting a float into binary representation, in
> native form and/or a particular standard form (IEEE, etc.)?

float f = 1.0f;
const unsigned char *c = ( const unsigned char * ) &f;
int i;

for ( i = 0; i < sizeof( f ); i++ )
{
   store_some_byte( c[ i ] );
}

That is 100% legal, as long as it stays in char form.  That is how 
you're expected to transmit it over, say, a network connection if 
necessary, assuming the machine on the other side understands the same 
floating point format.

Floating point in the C standard is borderline disastrous.

Factoid #1: signed integer representation is not specified in the 
standard, so you could conceivably run on a system with, say, 
ones-complement arithmetic.

Factoid #2: C99 introduced support for IEEE-754 floating point 
operations and formats officially, but it's an optional extension that 
can be examined by checking __STDC_IEC_559__

Factoid #3: There's a standard #pragma to tell the compiler not to 
dork with the floating point control and status words while you do 
stuff that relies on that state remaining constant.

Brian




-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id78&alloc_id371&op=click
_______________________________________________
Gamedevlists-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-general
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_idU7
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.