IEEE Floating Point to Integer Hack

This topic is 3818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Disclaimer: First of all, I apologize for posting this here, as it is related to a method of doing it in javascript, but I really do not think anyone in the Web Development forum would answer this, as this is really a hard-core programming issue. So kindly bear with me. :-) Most C & C++ programmers would be aware of the 'hack' that can be used to get the bit-level representation of IEEE standard floating point numbers by representing it as an integer. Done as:
float f = 103.403;
int32 a = (*int32)&f;

I wish to know if someone knows of a way to do this in Java, and as an added bonus, javascript. Actually a javascript answer is preferable, however outlandish it may seem. Thanks -Shashank

Share on other sites
Quote:
 as this is really a hard-core programming issue.
It's not that hard-core, it's part of standard classes, look at javadoc.

Double.doubleToLongbits(), Float.floatToIntBits()

javascript doesn't provide a standard way of doing this.

Share on other sites
Well, when I was writing this thread, I actually needed an answer with javascript as the language, but then at the end I thought of adding Java too to figure out an analogous solution as both use the 'reference' model for objects.

Thanks for the pointers, and I referred to this as 'hard-core' w.r.t the sort of things conventionally done with javascript.

Shashank

Share on other sites
Quote:
 Original post by Shashank Shekharint32 a = (*int32)&f;

A pointer to int is not an int, you have to dereference it:
int32 a = *((int32*)&f);

Share on other sites
Yeah, sorry. Dumb typo or temporary blanking of mind. Call it whatever you may. :-)

Share on other sites
< Insert assorted warnings about portability, ill-formed conversions, and undefined behavior here >

Share on other sites
In C++ I do it like this:

float f = 103.403;
int a = (int&)f;

Share on other sites
You can also do it using a union, but I'm not sure what the advantage of that is.

Share on other sites
Quote:
 Original post by Sc4FreakYou can also do it using a union, but I'm not sure what the advantage of that is.

Quote:
 Strictly speaking, reading a member of a union different from the one written to is undefined in ANSI/ISO C99 except in the special case of type-punning to a char*, similar to the example below: Casting to char*. However, it is an extremely common idiom and is well-supported by all major compilers. As a practical matter, reading and writing to any member of a union, in any order, is acceptable practice.

source

Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by Sc4FreakYou can also do it using a union, but I'm not sure what the advantage of that is.

Quote:
 Strictly speaking, reading a member of a union different from the one written to is undefined in ANSI/ISO C99 except in the special case of type-punning to a char*, similar to the example below: Casting to char*. However, it is an extremely common idiom and is well-supported by all major compilers. As a practical matter, reading and writing to any member of a union, in any order, is acceptable practice.

source

OT: So this is basically undefined?:

union{	struct	{		float r, g, b, a;	};	float rgba[4];};

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• Forum Statistics

• Total Topics
633430
• Total Posts
3011828
• Who's Online (See full list)

There are no registered users currently online

×