T
thomas.mertes
For a hash function I want to reinterpret the bits of a float
expression as unsigned long. The normal cast
(unsigned long) float_expression
truncates the float to an (unsigned long) integer.
But this is not the effect I want for a hash function.
With unions my hash function can be implemented:
union {
long unsigned hashvalue;
float floatvalue;
} value;
value.floatvalue = float_expression;
Now
value.hashvalue
contains the reinterpreted bits as intended. But I think a shorter
solution (without assignment or memcpy to a variable) should
be possible.
When I cast first to (void *) and after that to (unsigned long)
the gcc gives me the error:
cannot convert to a pointer type
It seems casting a float to a pointer is prohibited by gcc.
Has anybody an idea to reinterpret the bits of a float as
unsigned long without assignment or memcpy (some sort
of tricky cast).
Greetings Thomas Mertes
Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7
expression as unsigned long. The normal cast
(unsigned long) float_expression
truncates the float to an (unsigned long) integer.
But this is not the effect I want for a hash function.
With unions my hash function can be implemented:
union {
long unsigned hashvalue;
float floatvalue;
} value;
value.floatvalue = float_expression;
Now
value.hashvalue
contains the reinterpreted bits as intended. But I think a shorter
solution (without assignment or memcpy to a variable) should
be possible.
When I cast first to (void *) and after that to (unsigned long)
the gcc gives me the error:
cannot convert to a pointer type
It seems casting a float to a pointer is prohibited by gcc.
Has anybody an idea to reinterpret the bits of a float as
unsigned long without assignment or memcpy (some sort
of tricky cast).
Greetings Thomas Mertes
Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7