Implicit conversion from int to float?

Hi guys, I was wondering…

In this mini challenge, I was supposed to call my method with two floats. What I did instead is use the two variables we already had so I used a float and an int. The method still works perfectly so I guess my int was converted into a float by the method “Average”?

void Start () {
	int anInteger = 2;
	float aFloat = 2.5f;
	print("anInteger = " + anInteger);
	print("aFloat = " + aFloat);
	print("average = " + Average(anInteger, aFloat));
	print("square aFloat = " + Square(Square(aFloat)));
}

float Square (float x) {
	return x * x;
}

float Average (float x, float y) {
	return (x + y) / 2;
}

“For built-in numeric types, an implicit conversion can be made when the value to be stored can fit into the variable without being truncated or rounded off. For example, a variable of type long (8 byte integer) can store any value that an int (4 bytes on a 32-bit computer) can store.”

From this, I gather that an implicit conversion from integer to float would be okay, but not from float to integer. A float takes up more resources. So if you picture variables like boxes that hold numbers, I guess the float is a larger box. It won’t fit in a box made for an integer.

More info here: https://msdn.microsoft.com/en-us/library/ms173105.aspx

Privacy & Terms