logic question.

Simple question that i am not understanding fully
I understand that int stores integers as whole numbers and that double can hold a decimal place.

however if
int=i
i/3 and i/3.0 are not equivalent

while
double=j
j/6 and j/6.0 are always equivalent

The double I understand but the int I do not because I thought the function would treat 3.0 as 3
Last edited on
This is due to what is called 'Floating Integral Conversion'. In the case of 'i' the number 3 is a prvalue that can be represented as an integer, so it is treated as one in that operation. But the number 3.0 cannot be stored as an int, so in the second case 'i' is converted to a floating point for the purpose of the operation.

In the case of 'j', both 6 and 6.0 are values that can be stored in a float. So both prvalues are promoted to doubles and the operations are treated the same.
ok thank you, I thought the declaring term determined the value not the number
There are a few more considerations to take into account: http://en.cppreference.com/w/cpp/language/implicit_cast#Numeric_conversions
Topic archived. No new replies allowed.