Jump to content
  • Advertisement
Sign in to follow this  
Shashank Shekhar

IEEE Floating Point to Integer Hack

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
You 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 this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Sc4Freak
You 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];
};


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!