[Bf-committers] Verse 2005 Errors

Mathias Panzenböck grosser.meister.morti at gmx.net
Fri Sep 15 15:20:50 CEST 2006


It seems to work this way:

panzi at a2000p:~$ gcc -Wall -std=c99 -pedantic -x c -
#include <stdio.h>
const unsigned MAX_FLOAT_AS_UNSIGNED = 0x7f7fffff;
#define MAX_FLOAT *(const float*)(&MAX_FLOAT_AS_UNSIGNED)

int main() {
   float f = MAX_FLOAT;
   printf("%f\n",f);
   return 0;
}
panzi at a2000p:~$ ./a.out
340282346638528859811704183484516925440.000000


But when I use -O2 I get this warning:

panzi at a2000p:~$ gcc -O2 -Wall -std=c99 -pedantic -x c -
#include <stdio.h>
const unsigned MAX_FLOAT_AS_UNSIGNED = 0x7f7fffff;
#define MAX_FLOAT *(const float*)(&MAX_FLOAT_AS_UNSIGNED)

int main() {
   float f = MAX_FLOAT;
   printf("%f\n",f);
   return 0;
}
<stdin>: In function 'main':
<stdin>:6: warning: dereferencing type-punned pointer will break strict-aliasing rules
panzi at a2000p:~$ ./a.out
340282346638528859811704183484516925440.000000


	-panzi


Emil Brink wrote:
> Branan Riley wrote:
> 
>> # define SET_TO_MAX_FLOAT(x) *((unsigned*)(&x)) = 0x7f7fffff
> 
> 
> Yeah, that (give or take a few parenthesis), is what I've come up with as
> well, but currently the definitions in the public verse.h header are:
> 
> #define V_REAL64_MAX         1.7976931348623158e+308
> #define V_REAL32_MAX         3.40282347e+38F
> 
> So, unless we want to change the interface, we really need a constant that
> can be directly assigned to a floating point variable. Likewise, we need
> the constant to be available for direct comparisons against variables.
> 
> Right now, I guess I'll try and get hold of the trouble-making Microsoft
> compiler (it's on a laptop currently used by my boss, I think), and try
> the adjusted literal values on that. The literals I've come up with so far
> are 3.4028235e+38f and 1.7976931348623157e+308, respectively. It is my
> hope that using these results in the wanted bit patterns, and should do so
> on all IEEE 754 systems. But I need to test it. Feel free to tell me if it
> works out on your system, and which compiler(s) you tried it with, if you
> have the inclination.
> 
> Thanks for your input. Regards,
> 
> /Emil



More information about the Bf-committers mailing list