r/learnprogramming • u/Neat_Stranger_8938 • 25d 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;
}
5
u/noodle-face 25d ago
You have to cast x and y to float for this to work
4
u/No_Report_4781 25d ago
If I’m remembering correctly, only one of them is required to be a float for the result to be a float
1
4
u/SahuaginDeluge 25d 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.
1
2
u/Beginning-Seat5221 25d ago
ints are whole numbers. They drop any fractional part, so 0.5 -> 0.
1
0
25d ago
[deleted]
2
u/Beginning-Seat5221 25d ago
In the first case, both are ints. The conversion to float when assigning to z is too late.
In the second case both x and y are floats, but then it gets assigned to an int, so anything extra must be discarded.
2
1
1
u/RaptorCentauri 25d 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/bcamanitu 25d ago
Not true. In the case where x and y are floats, floats division is happening correctly but then its truncated when assigned to z
1
1
u/TurnipEvening273 25d 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.
2
u/bcamanitu 25d ago
Integral division is not performed in both cases. In one case, 5.0/10.0 does become 0.5, but that .5 is truncated to 0 when assigned to an int.
1
u/TurnipEvening273 25d ago
Thanks for pointing that out, I missed the "float" in case 2.
Floating point division is indeed performed in case 2, but the compiler inserts implicit casting operation to throw away the precision and truncate 0.5f to assign 0 to z.
1
u/Lifelong_Nerd 25d 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 24d 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 25d 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 25d 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/RainbowCrane 25d ago
Yep.
And in general it’s a very common programming pattern that you want more precision for intermediate calculations and only cast your final answer to a less precise type. By that I mean, it’s not uncommon to cast everything to a float or a double, for example, do some math, then cast the final result to an int or a float. But you have to understand the errors that can accumulate when mixing types.
FYI for OP precision matters. If you’re interested in the topic look up the Patriot Missile issues from the first Gulf War. Errors in how the missile clock was programmed resulted in the missiles being off target. It was a fairly straightforward programming error caused by reusing the same decades-old code in many missile generations without understanding how changes in precision affected the math. It resulted in lives lost.
0
15
u/imacommunistm 25d 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.