logic question.

Sep 30, 2014 at 2:57pm
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 Sep 30, 2014 at 2:59pm
Sep 30, 2014 at 4:30pm
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.
Sep 30, 2014 at 4:44pm
ok thank you, I thought the declaring term determined the value not the number
Sep 30, 2014 at 4:57pm
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.