r/learnprogramming • u/Neat_Stranger_8938 • 21d ago
Why do both of these codes output zero? Can someone explain this to me?
include <iostream>
using namespace std;
int main()
{
int x = 5, y = 10;
float z = x / y;
cout << z << endl;
}
2) #include <iostream>
using namespace std;
int main()
{
float x = 5, y = 10;
int z = x / y;
cout << z << endl;
}
1
Upvotes
15
u/imacommunistm 21d ago
That's operator precedence and integer division. Arithmetic operators (+ - * /) has higher precedence than the assignment operator (=), so they get evaluated first.
So on the assignment of
float z = x / y;the block x / y gets evaluated first, and due to integer division truncation (rounding towards zero), 0.5 becomes 0. And when it evaluates to the assignment operator, it'll be just
float z = 0. To prevent this, you cast either of x and y, the other will also be promoted to float for floating point division.For the second assignment:
int z = x / y;the x / y division gets evaluated as usual as integer division. The result is assigned as zero as intended.
if you get it to do floating point division, like
int z = (float)x / y, the resulting expression is then casted to its respective type of int when the assignment operator gets evaluated (it'll beint z = 0.5fat that step), it'll get the fractional part truncated and it'll be zero. Though the compiler will throw a warning for this specific action for potential loss of data due to implicit cast.5
u/noodle-face 21d ago
You have to cast x and y to float for this to work
4
u/SahuaginDeluge 21d ago
is this homework?
int 5 / 10 just computes to 0 with integer division which you then convert/assign to float z as 0f.
float 5 / 10 is 0.5f but then if you assign 0.5f to an int, it truncates to 0 during the conversion.
2
u/Beginning-Seat5221 21d ago
ints are whole numbers. They drop any fractional part, so 0.5 -> 0.
2
u/BrighterSomedays 21d ago
Change int to float or convert for division.
1
u/LeeRyman 21d ago edited 21d ago
Assignment from expression undergoes implicit conversion...
1
u/RaptorCentauri 21d ago
In both cases you are dividing the integer 5 by the integer 10. That gives you integer 0. THEN you are storing it to the variable z
1
u/TurnipEvening273 21d ago
Floating point division is a completely separate operation than the integaral division, (integaral division is performed here in both the cases.) Integaral division is far efficient than the former, unless one requires precision. Hence, the compiler defaults to integaral division operation, and onlt generates Floating point division if the user specifyies atleast one operand to be a Floating point value. In each each case the z's type has no effect on the division operation(which has to be performed before assigning division result to z). The result is then nonetheless implicitly casted to z's type which in both cases yields 0.
1
u/Lifelong_Nerd 20d ago
The two examples highlight how assignments work:
Step 1: evaluate the right side of the "="(and the left side if necessary). Step 2: If the left and right sides' types differ then convert to the right side's type to the left side's. Step 3: Assign the result.
In your first example, x and y are integers. The result of integer division is an integer. In this case, 5/6 evaluates to 0. Next it converts the integer 0 to a float in order to match the type of the left hand side, resulting in 0.0. Finally it makes the assignment.
In the second example x and y are floats and the result of x/y is 0.5. then it converts 0.5 to an integer to match the left side. Converting 0.5 to int gives 0. Finally it assigns the result (0) to z
1
u/jacksonanna373 20d ago
The type of `z` can't change how the division already happened. First, `5 / 10` is integer division, so it becomes 0 before going into `float`. Second, `5.0f / 10.0f` gives 0.5, then assigning it to `int` drops the fraction. Same output, two different ways to lose the .5.
0
u/pixel293 21d ago
It's been a while since I've done C but I believe for this:
int x = 5, y = 10;
float z = x / y;
Since x and y are integers the compiler does integer division THEN converts it to a float, and integer division truncates. So since the 5/10 is less than 1 you get 0. If you cast x and y to floats then you would get floating point division.
With:
float x = 5, y = 10;
int z = x / y;
Since x and y are floats that does a floating point division, then the result is converted to an integer, which again truncates, so you are left with 0.
0
u/spinwizard69 21d ago
You already have the right answers but this is a very real issue with programing languages. Perfectly acceptable equations will get evaluated in unexpected ways. Worse it can vary with languages.
This is where paying attention to types is such a big deal.
0
u/rsham004 21d ago
Use ai for these things, it helps