Reputation: 43
I'm doing an equation solver. The code goes like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Equation solver of type ax + by = 0, for y\n");
int a, b, x;
float y;
printf("Enter value of a as integer: ");
scanf("%d", &a);
printf("Enter value of b as integer: ");
scanf("%d", &b);
printf("Enter value of x as integer: ");
scanf("%d", &x);
y = a*x/b; //We have equation ax + by = 0, from that: ax = by, and that is: y = ax/b
printf("Solution for y of equation: %dx + %dy = 0, for x = %d, is: %.2f", a, b, x, y);
return 0;
}
When I enter a = 3, b = 5 and x = 3, the output is supposed to be 1.80, but I get 1.00. Why is this? Wouldn't expression a*x/b be converted into type of float? I'm just beginner and we only mentioned type conversion with our professor, maybe I got it wrong?
Upvotes: 3
Views: 82
Reputation: 117298
In a*x/b
all variables are int
s so it's a pure integer calculation.
If you cast one of the first operands used to float
, the other will also be implicitly converted to float
y = (float) a * x / b;
More about what implicit conversions there are can be found here: Implicit conversions
Broken down according to operator precedence:
Precedence | Operator | Description | Associativity |
---|---|---|---|
3 | * / % |
Multiplication, division, and remainder | Left-to-right |
14 | = |
Simple assignment | Right-to-left |
y = a*x/b; // with a = 3, b = 5 and x = 3
According to operator precedence, a*x
will be performed first and as can be read in the Usual arithmetic conversions chapter on Implicit conversions:
"The arguments of the following arithmetic operators [*, /, %, +, -, ...]
undergo implicit conversions for the purpose of obtaining the common real type, which is the type in which the calculation is performed"
If "both operands are integers. Both operands undergo integer promotions". Since both a
and x
are int
- no promotion is necessary and the common type is therefore int
and the calculation is done using int
s:
3 * 3 => 9
Next according to operator precedence comes the result of a*x
divided by b
. Again, b
is an int
and 9
is an int
so the same procedure as above gives us int
as the common type.
9 / 5 => 1
The last step according to operator precedence is the simple assignment y = 1
. In the chapter "Conversion as if by assignment" we can read:
"In the assignment operator, the value of the right-hand operand is converted to the unqualified type of the left-hand operand."
This means that it's only now that the result of the calculation, 1
, becomes 1.f
(float
)
1 => 1.f
and that's what's assigned to y
.
By converting a
to float
, like in y = (float) a * x / b;
, you can follow the rules for finding the common type and see that x
will be converted to float
and then b
, so the calculation becomes 3.f * 3.f / 5.f
and the result will be 1.8f
.
If you are sure that a * x
will not cause integer overflow (~produce a value that is too large or small to store in an int
), you can delay the conversion until the division:
y = a * x / (float) b;
It will now do an integer multiplication and only then will the result of that multiplication be converted to float
and divided by 5.f
, as if done like so:
y = (float) (3 * 3) / 5.f;
Upvotes: 3
Reputation: 223937
Wouldn't expression a*x/b be converted into type of float?
The result of that expression gets converted to float
before being assigned to y
. Since each operand has type int
, the math is done with that type.
Specifically, a*x
and b
both have type int
, so integer division is performed which truncates the fractional part.
If you cast one of the variables to type float
, then floating point division will be performed.
y = (float)a*x/b;
Upvotes: 3