473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting the integer representation of a float


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?
Oct 4 '06 #1
7 2952
Stein Gulbrandsen wrote:
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).
Oct 4 '06 #2
"Stein Gulbrandsen" <stein@homewrote in message
news:45******@news.broadpark.no...
: 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[i];
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Oct 4 '06 #3
Ivan Vecerina wrote:
"Stein Gulbrandsen" <stein@homewrote in message
news:45******@news.broadpark.no...
: 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.
Oct 4 '06 #4
Ivan Vecerina wrote:
[..]
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[i];
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 4 '06 #5
Ron Natalie wrote:
Ivan Vecerina wrote:
>"Stein Gulbrandsen" <stein@homewrote in message
news:45******@news.broadpark.no...
>>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.
Please quote the Standard. All I see is "unspecified", which is
basically the same as "undefined".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 4 '06 #6
Victor Bazarov wrote:
Ron Natalie wrote:
>Ivan Vecerina wrote:
>>"Stein Gulbrandsen" <stein@homewrote in message
news:45******@news.broadpark.no...
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.

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.

Oct 4 '06 #7
Stein Gulbrandsen wrote:
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
Oct 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4648
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
3
2591
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
6
3677
by: Aaron | last post by:
I'm trying to get an average of a long and a std::vector::size_type, but keep getting 0 for some reason. I've tried the following: float avg = some_long / some_vec.size(); float avg =...
17
2235
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
43
8375
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
17
2464
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
7
6229
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
26
2876
by: Pietro Cerutti | last post by:
Hi group, I always thought that applying a binary operator such as ==, !=, <= or well defined. Now, I'm passing a program through splint and it says: Dangerous equality comparison involving...
1
7936
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.