conversion from float to int&



 DEVELOP > c-Plus-Plus > conversion from float to int&

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "liujiaping"
Date: 13 Aug 2007 04:27:59 AM
Object: conversion from float to int&
The code is as follows:
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 float a = 1.0f;
8 cout << (int)a << endl;
9 cout << (int&)a << endl;
10 cout << boolalpha << ( (int)a == (int&)a ) << endl;
11 float b = 0.0f;
12 cout << (int)b << endl;
13 cout << (int&)b << endl;
14 cout << boolalpha << ( (int)b == (int&)b ) << endl;
15 return 0;
16 }
The program output is:
1
1065353216
false
0
0
true
I am wondering why line 9 prints 1065353216, while line 13 prints 0.
Can anybody explains this?
And what will happen if I convert a variable of type float to a
variable of type int&?
Any help is appreciated.
.

User: "Alf P. Steinbach"

Title: Re: conversion from float to int& 13 Aug 2007 04:59:13 AM
* liujiaping:

The code is as follows:
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 float a = 1.0f;
8 cout << (int)a << endl;
9 cout << (int&)a << endl;
10 cout << boolalpha << ( (int)a == (int&)a ) << endl;
11 float b = 0.0f;
12 cout << (int)b << endl;
13 cout << (int&)b << endl;
14 cout << boolalpha << ( (int)b == (int&)b ) << endl;
15 return 0;
16 }

The program output is:
1
1065353216
false
0
0
true

I am wondering why line 9 prints 1065353216, while line 13 prints 0.

Don't wonder about that, just stay away from it.

Can anybody explains this?

"(int&)a" is equivalent to "*reinterpret_cast<int*>(&a)". In other
words, you're accessing the bits of a float is if they represented an
int. The result is highly system-dependent.
However, in general, most likely a C++ implementation will use IEEE
format for floats, where a 0 is represented as all zero bits.

And what will happen if I convert a variable of type float to a
variable of type int&?

There is no such thing as a variable of type int&. References are not
objects. The variable is of type int.
For your own good, just DON'T USE CASTS.
If you absolutely need to use a cast, absolutely don't use C-style casts
(like you do above), but instead one or more of dynamic_cast,
const_cast, static_cast, and reinterpret_cast. That way you can search
for casts in your code, see the what the indended effect is, and perhaps
even understand more when your compiler complains.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "liujiaping"

Title: Re: conversion from float to int& 13 Aug 2007 06:50:04 AM
On Aug 13, 5:59 pm, "Alf P. Steinbach" <al...@start.no> wrote:

* liujiaping:



The code is as follows:
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 float a = 1.0f;
8 cout << (int)a << endl;
9 cout << (int&)a << endl;
10 cout << boolalpha << ( (int)a == (int&)a ) << endl;
11 float b = 0.0f;
12 cout << (int)b << endl;
13 cout << (int&)b << endl;
14 cout << boolalpha << ( (int)b == (int&)b ) << endl;
15 return 0;
16 }


The program output is:
1
1065353216
false
0
0
true


I am wondering why line 9 prints 1065353216, while line 13 prints 0.


Don't wonder about that, just stay away from it.

Can anybody explains this?


"(int&)a" is equivalent to "*reinterpret_cast<int*>(&a)". In other
words, you're accessing the bits of a float is if they represented an
int. The result is highly system-dependent.

However, in general, most likely a C++ implementation will use IEEE
format for floats, where a 0 is represented as all zero bits.

And what will happen if I convert a variable of type float to a
variable of type int&?


There is no such thing as a variable of type int&. References are not
objects. The variable is of type int.

For your own good, just DON'T USE CASTS.

If you absolutely need to use a cast, absolutely don't use C-style casts
(like you do above), but instead one or more of dynamic_cast,
const_cast, static_cast, and reinterpret_cast. That way you can search
for casts in your code, see the what the indended effect is, and perhaps
even understand more when your compiler complains.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Many thanks for your explanation. It helps me a lot.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER