Getting the integer representation of a float

  • Thread starter Stein Gulbrandsen
  • Start date
S

Stein Gulbrandsen

What is the best way to get to the integer representation of a float?

I would like to do
int& toIntByCast (float& a) {return *reinterpret_cast<int*> (&a);}
but is this legal?

Is this safer?
int toIntByUnion (const float& a) {
union {float f; int i;} u = {a};
return u.i;
}

Or are there better ways?
 
R

Ron Natalie

Stein said:
What is the best way to get to the integer representation of a float?

I would like to do
int& toIntByCast (float& a) {return *reinterpret_cast<int*> (&a);}
but is this legal?

Is this safer?
int toIntByUnion (const float& a) {
union {float f; int i;} u = {a};
return u.i;
}

Or are there better ways?
They both rely on the whim of the implementation but should
provide appoximately the same chance of working. The only
real guarantee is to copy the float to sizeof(float) chars and
then sizeof(int) chars back to int (hoping the two sizes are
indeed thee same).
 
I

Ivan Vecerina

: What is the best way to get to the integer representation of a float?
:
: I would like to do
: int& toIntByCast (float& a) {return *reinterpret_cast<int*> (&a);}
: but is this legal?
Formally, behavior will be undefined.

: Is this safer?
: int toIntByUnion (const float& a) {
: union {float f; int i;} u = {a};
: return u.i;
: }
This would probably even be less portable.

: Or are there better ways?

The first option displayed above is used often, and will
"work" as you expect on most platforms. The union trick
is just more complicated and not better in any way.

Formally, the correct way to access the representation
of an object (here a float) is to access it as an array
of 'unsigned char':
unsigned char* p = (unsigned char*)aFloat;
for( int i = 0 ; i<sizeof(aFloat) ; ++i )
... read p;


hth -Ivan
 
R

Ron Natalie

Ivan said:
: What is the best way to get to the integer representation of a float?
:
: I would like to do
: int& toIntByCast (float& a) {return *reinterpret_cast<int*> (&a);}
: but is this legal?
Formally, behavior will be undefined.
Implmentation defined.
 
V

Victor Bazarov

Ivan said:
[..]
Formally, the correct way to access the representation
of an object (here a float) is to access it as an array
of 'unsigned char':
unsigned char* p = (unsigned char*)aFloat;

unsigned char* p = (unsigned char*) & aFloat;
for( int i = 0 ; i<sizeof(aFloat) ; ++i )
... read p;


V
 
R

Ron Natalie

Victor said:
Please quote the Standard. All I see is "unspecified", which is
basically the same as "undefined".
I guess you are right. The implementation defined applies to types
other than pointers.

The real answer is aliasing to chars are the only safe thing you can do.
 
W

Walter Bright

Stein said:
What is the best way to get to the integer representation of a float?

Since you're stepping outside of what's strictly portable anyway, you
might as well go ahead and just do:

*(int*)&a

I'd probably throw in:

assert(sizeof(int) == sizeof(float));

so you'll be alerted if the first line isn't going to work.

Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top