explicit output unexpected

1
2
3
4
5
6
7
8
9
int i1 = 10;
	int i2 = 4;
	float g = (float)i1 / i2;  //here you want to tell the division op to do floating division
	cout<<g<<endl;
         second block
	static_cast<float>(i1);      //change to float
	static_cast<float>(i2);      //change to float
	g = i1/i2;                   //get int instead of float
	cout<<g<<endl;

output:
2.5 (expected)
2.0 (unexpected)
Last edited on
@Line 3
float g = (float)i1 / i2
No. float / int

float g = (float)i1 / (float)i2
Yes. float / float


@Line 6,7
you cast i1 and i2 to float but discard the result.

@Line 8
i1 and i2 are ints so the answer is int.

1
2
3
float f1 = static_cast<float>(i1);
float f2 = static_cast<float>(i2);
g = f1 / f2;

@Line 3
 
float g = (float)i1 / i2

No. float / int


Hi Jaybob I don't understand this particular response. Can you tell me more abt (float) i1/i2?
Is the result of int i1 and int i2 converted to float once they are divided?
Last edited on
float g = (float)i1 / i2;
float / int results in float. No problem.

1
2
static_cast<float>(i1);      //change to float
static_cast<float>(i2);      //change to float 


This doesn't do anything. The float values created by the static_cast calls are not used. They are equivalent to doing
1
2
(float)i1;
(float)i2;


Since the float values are not used, when the end of the statement (;) is reached, the float values are thrown away, and you are back to int values.
I guess it really comes down to understanding why an integer can't be promoted to a float. fixed the above error

newbie error:
float g = 10/4. This equals 2 because there is no implicit promotion from int to float. Why is this so?


fixed:
1
2
float i3 = static_cast<float>(i1);
float i4 = static_cast<float>(i2);
Last edited on
I guess it really comes down to understanding why an integer can't be promoted to a float. fixed the above error

You can promote an int to a float, but you have to realize how it happens.

Promotion of one type to another involves creating temporary anonymous variables. In static_cast<float>(i1), you create a temporary float value and assign it the value from i1. The type of i1 never changes. i1 was, is and will always be an int. However, an unnamed float value initialized with i1's value is available and must be used or assigned before the end of the statement.

In the case of float g = 10/4, you have to look at operator precedence. The division operator happens before the assignment operator. So, the compiler divides 10 (int) by 4 (int). Because both operands are integers, the compiler performs integer division, and creates a temporary integer value with the result (2). Then the assignment occurs, and the float value g is assigned the temporary value, and ends up with the value 2.0.
Great - got most of it. Plz look at this other example of newbie exemble:

int foo
float goo
float g

g = foo / goo.

The division operator happens before the assignment operator.


so int/float (before operator assignment) = ?

In the case where you are using literal values (such as 10, or 4), replacing one or both of the integer literal value with a floating point literal value (10.0 or 4.0) will cause both operands to be converted to floating point values, and the division will be done using floating point math.


Got it but why?

Also when should I be using the f suffix for my floats?

Final question: can you permanently change a var type? For e.g. int to float, where the variable is now permanently float without using typecast

Thank you
Last edited on
can you permanently change a var type?
No. Variable type is set on declaration and cannot change during its lifetime
Topic archived. No new replies allowed.