Why does dividing a int with a float result in a float? - c

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 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.

Related

How is the standarized way to calculate float with integers?

Do any of you know how this will be calculated in C?
uint8_t samplerate = 200;
uint8_t Result;
Result = 0.5 * samplerate;
Now, the problem is that 0.5 is a float and samplerate an integer.
Result could then be either 0, because 0.5 is converted in an integer and therefore rounded to 0 (Result = 0 * 200 = 0). Or Result could be 100, because the compiler sees 0.5 first and converts samplerate into float (Result = 0.5 * 200 = 100).
Is there a standarized way how the compiler will handle these calculations?
I mean will the compiler look at the variable on the very left (in this case 0.5) first and convert the other to this, or will it look at the variable on the very right (samplerate) and convert the other variables to this?
I know how I could solve this problem but I look for an general answer, if this is C standarized and how will it calculate such equations?
When numeric values of various types are combined in a expression, they are subject to the usual arithmetic conversions, which is a set of rules which dictate which operand should be converted and to what type.
These conversions are spelled out in section 6.3.1.8 of the C standard:
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 .
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.
Note in particular the paragraph in bold, which is what applies in your case.
The floating point constant 0.5 has type double, so the value of other operand is converted to type double, and the result of the multiplication operator * has type double. This result is then assigned back to a variable of type uint8_t, so the double value is converted to this type for assignment.
So in this case Result will have the value 100.
Yes, of course this is controlled by the standard, there is no uncertainty here.
Basically the integer will be promoted to double (since the type of 0.5 is double, it's not float) and the computation will happen there, then the result will be truncated back down to uint8_t. The compiler will shout at you for the loss of precision, typically. If it does not, add more warning options as required.
Yes, there is a standard. In this case, the numbers in the expression are automatically converted to the wider type (one that occupies more bytes), so your expression will be evaluated as follows:
(0.5: double) * (0: uint8_t) => (0.5: double) * (0.0: double) == (0.0: double)
uint8_t Result = (0.0: double) => (0: uint8_t) // this is a forced cast, because Result is of type uint8_t
double is wider than uint8_t, so (0: uint8_t) is widened to (0.0: double). This cast doesn't lose information since double occupies enough space to fit all the data stored in uint8_t.

Result of an operator if different data types as arguments? (C)

I haven't been able to find an answer to the following question.
My question is:
What is the result of an operator when different data types (like int, or float) are being operated on?
For example,
float * int = ?
float / int = ?
We know that operations on same data types give results of the same data type. For instance,
float * float = float
But I wanted to know what happens in this other case?
This question has probably already been discussed here, but it has been hard for me to find something similar.
Thanks.
When operating on values if differing types, the operands undergo the usual arithmetic conversions.. These are specified in section 6.3.1.8 of the C standard.
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 .
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.
In the case of a float and an int as operands to * or /, the int operand will be converted to float.
Before performing the arithmetic operation, the compiler arranges for the "usual arithmetic conversions" to be performed.
The precise rules are a slightly complicated, and well documented in the provided link, but the basic idea is:
If either argument is a floating point type, both arguments are converted to the more precise floating point between the two arguments.
Otherwise, if both arguments are integer types, they are first promoted to at least int, and then if the two arguments are not the same width, the narrower one is converted to the type of the other one.
The real rules are more complicated for a couple of reasons:
A modern C compiler may implement complex (and imaginary) types, which play into the conversions.
Conversion between signed and unsigned types can be counter-intuitive. If you haven't read the precise rules, it is best to avoid this case.

Typecasting integers as doubles to get a double result from divison of the typecasted integers

I do not understand the underlying reason the output is a double between the following examples. In terms of:
Why does a double divided by an int result in a double?
Why does a int divided by a double result in a double?
#include <stdio.h>
int main(int agrc, char **argv)
{
double d;
int a=5,b=2;
d = (double)a/b;
printf("d= %G\n",d); // outputs 2.5
d = a/(double)b;
printf("d= %G\n",d); // outputs 2.5
}
From the C standard, section 6.3.1.8: 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.
Otherwise, the integer promotions are performed on both operands.
So if one operand to an arithmetic operator is int and the other is double, the standard states that the resulting expression has type double.
The cast has precedence over the division, and an operation between a double and an int will produce a double
Due to necessities in the evaluation, if one of the operands of a division is a double data type, the other is automatically promoted to that for means of calculation. In your example this happens because the operator precedence for an explicit cast is higher then for a divison.
If you want to only cast the result of the division, you can do:
d = (double)(a/b);
To ensure the integer division is performed first, and the explicit cast to double is performed second.
For additional context of #dbush 's excellent answer, it is important to note that the standard specifies that for all arithmetic type conversions containing differing types, that conversion is from the smaller of two types to the largest:
Summarized from C11 - 6.3.1.8 Usual arithmetic conversions:
1st: if real type of either operand is long double, the other is
converted to long double
2nd: Otherwise if real type of either
operand is double, the other is converted to double
3rd: Otherwise
if real type of either operand is float, the other is converted to
float
And it goes on to specify how integer promotions are made in similar fashion...

Integer conversion rank of signed and unsigned int

For example, If I have,
int a = 42;
unsigned b = 10;
int c = a + b;
For this statement, int c = a + b; Would a be first converted to an unsigned int or would it be b that will be converted to a signed int? Both unsigned int and signed have the same conversion rank so how do we know which one will be converted? Is there a Standard rule?
Short answer: Per C99 6.3.1.8-p1, as value will be converted to an unsigned int by, per C99 6.3.1.3-p2, having UINT_MAX+1 added to it until it falls in the range allowed by unsigned int. Since it is already in that range, no addition will be performed. By C99 6.3.1.3-p3, the results assigned back to int c would be implementation defined if (p1) and (p2) didn't apply. But in this case note the "value" clause of 6.3.1.3-p1. The value (52) in this case can be represented by int, so it is not changed, and is defined.
C99 6.3.1.3 Signed and unsigned integers
When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.60)
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
C99 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 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.

What is the difference between Integral Promotion and Balancing in C?

What is the difference between integral promotion and balancing. Can we sum up both the rules by saying that any type is converted to atleast int or unsigned int type before performing any operation(except logical operators &&, ||, !) and to a greater type if any of the operand is of type greater than int ?
"Integral promotions" is the old C90 term, the formal standard term is integer promotions.
Integer promotions is a rule that applies whenever a small integer type (bool, char, short and their signed equivalents) is used as an operand in an expression.
C11 6.3.1.1/4
If an int can represent all values of the original type (as restricted
by the width, for a bit-field), the value is converted to an int;
otherwise, it is converted to an unsigned int. These are called the
integer promotions. All other types are unchanged by the integer
promotions.
"Balancing" is the informal term referring to a set of rules known as the usual arithmetic conversions. They state how all implicit type promotions of each operand in an operation are done. Please note that the integer promotions are part of the usual arithmetic conversions:
C11 6.3.1.8
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.
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.
There are two different things in the standard but none is called balancing:
If an int can represent all values of the original type (as restricted
by the width, for a bit-field), the value is converted to an int;
otherwise, it is converted to an unsigned int. These are called the
integer promotions.58) All other types are unchanged by the integer
promotions.
....
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.
The general idea for operators that are applied to expressions of different type, is that the the operands are converted to the type where there is the less loss in precision. E.g if a is float and b is double, for the sum a + b a is converted to double before the addition. Similar if c is unsigned long it is first converted to double for c + b.

Resources