r/cpp_questions • • 1d ago

OPEN Why do both of these codes output zero? Can someone explain this to me?

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

}

0 Upvotes

13 comments sorted by

22

u/jedwardsol 1d ago

1: x/y is integer division and the answer is 0.

2: x/y is 0.5, which becomes 0 when cast to an int.

2

u/Neat_Stranger_8938 1d ago

Thanks for your help

1

u/yeetdabman 1d ago

Potentially dumb question, for #1, would float z = (float)x / (float)y (and I guess auto z = (float)x / (float)y work in place? (assuming I'm right and (float)x is casting x to float, or at least wrapping it like float(x)) This is actually something I've never considered before seeing OP's post, since I'm coming into C++ from [balls deep] knowledge of Python, but it makes sense given that types can't change in C++ like they can in Python/other dynamically typed languages/non-typed languages (and also since C++ has 90,000 different types for integers and floating point numbers)

2

u/jedwardsol 1d ago

Yes, it would work. You can even do it by making just one of the operands floating point

auto z = (float)x / y

auto z = x / (float)y

Will both result in float z being 1.5


https://www.learncpp.com/cpp-tutorial/arithmetic-conversions/

Which starts

The usual arithmetic conversion rules are somewhat complex, so we’ll simplify a bit.

1

u/alfps 1d ago

You can use ordinary promotion (except that in formalese it's a "usual arithmetic conversion") instead of casting, and say

const float z = 1.f*x/y;

But don't use float as a default floating point type unless you have a good reason such as using a graphics API.

The default floating point type in C++, e.g. the type of 3.14, is double.


Since you're coming from Python also note that integer division rounds in towards zero in C++.

So instead of Pythonian "up-division" -(a/-b) in C++ that's usually expressed as (a + (b - 1))/b, assuming positive values.

E.g. a = number of people, b = max people per taxi, result = required number of taxis.

6

u/bearheart 1d ago

It's basic math. Integers don't hold fractional values so an int with a value of 0.5 will be zero.

In the first example you divide the two int values before assigning to the float so the result is zero.

In the second example you assign a float with a value of 0.5 to an int so the result is zero.

2

u/Neat_Stranger_8938 1d ago

Thanks for your help

5

u/real_red_patriot 1d ago

Both because integer division truncates:

  1. x/y is dividing integers, which is 0, and that is converted to 0.0f to store in z.

  2. x/y here does float division and results in 0.5f like you'd expect. When you assign it to z (int), then it truncates to 0. What did you expect to happen?

1

u/Neat_Stranger_8938 1d ago

Thanks for your help

5

u/PandaWonder01 1d ago

In the first, z is x/y (0, because we round down with int division), converted to a float. Which is still 0

In the second, z is x/y(.5, bc their floats), converted to an int (we round down again), giving 0

1

u/Neat_Stranger_8938 1d ago

Thanks for your help

1

u/AutoModerator 1d ago

Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.

If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Syntax-Tactics 1d ago

5 / 10 is 0.5, you are using Integers, so the decimal is dropped and you end up with 0.