[MUD-Dev] Re: [CODE QUESTION] How to encode floats into bytes?
Travis S. Casey
efindel at io.com
Wed Sep 9 14:42:51 CEST 1998
On Wed, 9 Sep 1998, Ola Fosheim Gr=F8stad wrote:
> Adam Wiggins wrote:
> > On Mon, 7 Sep 1998, Ben Greear wrote:
>=20
> > > float f =3D 42.5;
> > > char* bytes =3D (char*)(&f);
>=20
> > Not only that, but if you decide to compile with -ffast-math one day, y=
ou
> > may get some funny results.
>=20
> Depending on the compiler the following should fix errors introduced by
> optimization?
>=20
> volatile float f =3D 42.5f;
If I understand the "volatile" keyword correctly, it tells the compiler
that other programs may be able to change the data object in question, and
therefore, the program should not try to optimize out references to the=20
variable.
(That is, if you were to do something like:
f =3D 0;
if (f =3D=3D 5) {
printf("It's five!\n");
} else {
printf("It's zero!\n");
}
The optimizer should not assume that just because your program didn't=20
change the value of f between lines 1 and 2, that it didn't change.)
However, it *doesn't* tell the compiler not to optimize uses of the
variable in other ways... hence, a "fast math" library might still
introduce rounding errors or other errors... it just would actually access
the variable every time its value was requested.
Again, this is if my understanding of "volatile" is correct.
--
|\ _,,,---,,_ Travis S. Casey <efindel at io.com>
ZZzz /,`.-'`' -. ;-;;,_ No one agrees with me. Not even me.
|,4- ) )-,_..;\ ( `'-'
'---''(_/--' `-'\_)=20
More information about the mud-dev-archive
mailing list