Question why does this happen? Is this just a C language thing?
I'm following the cs50 course.
#include <stdio.h>
int main(void)
{
int testInt = 5;
printf("%f", testInt / 4.0);
}
Output is 1.250000 -- float value
Question why does this happen? Is this just a C language thing?
I'm following the cs50 course.
#include <stdio.h>
int main(void)
{
int testInt = 5;
printf("%f", testInt / 4.0);
}
Output is 1.250000 -- float value
When an expression is being evaluated the compiler needs to determine the common type of operands of the expression.
So for this expression
testInt / 4.0
(where 4.0
is a floating constant of the type double
) as the range of values of an object of the type double
is greater than the range of values of an object of the type int
then the compiler converts the object of the type int
to an object of the type double
(because it is safer to make this conversion instead of converting an object of the type double
to an object of the type int
at least due to truncation of the object of the type double
) and performs the operation.
Such conversions are called the usual arithmetic conversions and described in the C Standard.
From the C Standard (6.3.1.8 Usual arithmetic conversions)
Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
Is this just a C language thing?
The answer is "because that's how the C language defines the operation."
It is common in many languages to promote an integer to a floating point before doing an operation with another floating point value.
If it didn't work this way, there would be many accidental loss-of-precision (or loss-of-information) bugs.
Why does dividing a int with a float result in a float?
In C, with operators like *, / +, -, %
, when the 2 operands are of different types, a common one is found by converting the lower ranking one to the higher one.
int
ranks lower than float
, so the int
operand is converted.
The language could have specified int * float
differently - perhaps as some some_type mult_int_by_float(int, float)
operation. Yet that approach leads to many combinations and still leaves the type of the result unanswered. Promoting the lesser ranked type is simpler.
A language with N types could N*N different multiply operations. With C's approach of ranking and conversion, it is more like N different multiply operations.
Is this just a C language thing?
Yes. The TL/DR; version is that the operand with the narrower/less precise type is converted to same type as the operand with the wider/more precise type, and the type of the result is the same as that of the operand with the wider/more precise type. Here's the specific set of rules:
6.3.1.8 Usual arithmetic conversions
- Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:
- First, if the corresponding real type of either operand is
long double
, the other operand is converted, without change of type domain, to a type whose corresponding real type islong double
.- Otherwise, if the corresponding real type of either operand is
double
, the other operand is converted, without change of type domain, to a type whose corresponding real type isdouble
.- Otherwise, if the corresponding real type of either operand is
float
, the other operand is converted, without change of type domain, to a type whose corresponding real type isfloat
. 62)- Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
- Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
62) For example, addition of adouble _Complex
and afloat
entails just the conversion of thefloat
operand todouble
(and yields adouble _Complex
result).
If both operands are integers, then the result is an integer. If either operand is a floating-point type, then the result has a floating-point type.