-1

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

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
sw30
  • 77
  • 5
  • 2
    Because that's how C works. Thought experiment: What would happen to the fractional part of your division if C didn't implicitly convert the calculation to a double? – Robert Harvey Feb 10 '21 at 17:29
  • Why _wouldn't_ it? – underscore_d Feb 10 '21 at 17:31
  • 1
    Detail: In C, `4.0` is type `double`. `4.0f` is type `float`. Both are _floating point_. – chux - Reinstate Monica Feb 10 '21 at 17:31
  • ...and in a world where it didn't, your `printf()` would simply have undefined behaviour, because you'd be telling it to print a `float` (really `double`) but actually passing it an `int`, which is UB. One possible manifestation of UB would be printing the right thing, granted. The point is that just seeing a floating-point value from `printf()` doesn't necessarily mean anything, although in this case your call and conclusion are correct. – underscore_d Feb 10 '21 at 17:32

4 Answers4

2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    +1 for quoting the specific relevant section of the ISO-C standard. It might be useful to point out that the literal constant `4.0` has type `double`, per the ISO-C standard. – njuffa Feb 10 '21 at 17:42
1

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Hmmm, with `int, float`, there _is_ loss-of-precision in the `int` to `float` conversion of large `int` values. Same applies to `long long, double`. The "If it didn't work this way ... many accidental loss-of-precision" does not hold well. – chux - Reinstate Monica Feb 10 '21 at 18:00
  • @chux: True, but you would probably choose `int` arithmetic in that case, wouldn't you? `float` isn't a magic bullet; it's never going to give you more than 6 to 9 decimal digits of precision, and lopping off the fractional part of a calculation is almost always going to produce greater errors. – Robert Harvey Feb 10 '21 at 18:10
  • @RobertHarvey If loss of precision was a concern, code could use `(double)some_int * (double) some_float` or `(long long)some_int * (long long)some_other_int`. The point is that "loss of precision" is not the strong reason for converting to a common type - it is more of a simplicity thing. – chux - Reinstate Monica Feb 10 '21 at 18:15
  • @chux: This is all part and parcel of the stated specification for implicit conversion in C. All designs, including programming language designs, are an exercise in tradeoffs. The great thing about C is that it still allows you to be explicit about what you want to do. – Robert Harvey Feb 10 '21 at 18:18
0

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

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

  1. 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 is long 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 is double.
    • 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 is float. 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 a double _Complex and a float entails just the conversion of the float operand to double (and yields a double _Complex result).

C 2011 Online Draft

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198