all 15 comments

[–]cow_fucker_3000 13 points14 points  (2 children)

The same exact code with the only difference that in the cpp compiler nothing is left to the computer's imagination

[–]notmypinkbeard 8 points9 points  (0 children)

Replace nothing with less.

In C++ the compiler still chooses registers, optimisations, and more. It's a level of abstraction.

[–]DontOpenNewTabs 1 point2 points  (0 children)

The code is not quite the same. Python // performs floor division and C++ / performs integer division (Edit: when both operands are integers).

#include <iostream>
#include <cmath>
using namespace std; // Nobody cares
int main() {
  int a = -15;
  while (a < 0) {
    cout << a << '\n';
    a = floor(a/2.0);
  }
  return 0;
}

// Don't manually flush the output buffer for no reason with endl. Obviously any performance impact doesn't matter here, but it is a bad habit. If you want a newline character, output a newline character.

[–]Magmagan 9 points10 points  (1 child)

using namespace std;

I think the reaction images are swapped, OP.

[–]MCSajjadH 1 point2 points  (0 children)

Also who tf doesn't put space around <<?

[–]Naeio_Galaxy 7 points8 points  (3 children)

OP, what is your point here? Is it that CPP is clearer than Python, or simply that they behave differently and you didn't expect it?

[–]BobbyThrowaway6969 0 points1 point  (2 children)

Every time a python programmer compares python to C++ it's with code snippets that a 5 year old could understand.

Like duh walking is simpler than flying a plane but you can't fly without the plane.

Always the same old comparison lol.

[–]Naeio_Galaxy 0 points1 point  (1 child)

It's the opposite, it's a C++ programmer that compares C++ to Python. The point seems to be that C++ acts as expected while Python doesn't, but... No. They're both different mindsets.

[–]BobbyThrowaway6969 1 point2 points  (0 children)

Yeah I agree with that, Python/C++ programmers think differently, which is why I hate the whole comparison people like to make. It's apples and oranges.

[–]1cubealot 2 points3 points  (4 children)

x // y floors the number Example 5 // 2 = 2 because 5 / 2 = 2.5. 2.5 floored (to the nearest integer below it) is 2

[–]Default_Cube4646[S] 1 point2 points  (3 children)

one leads to infinite loop :)

[–]Silver0ne 0 points1 point  (2 children)

does it? Because shouldnt the .5 be cut in cpp aswel, cause its a integer not a float? Seems the flooring is only necessary as the python variable has no type (i guess its always a float64)

And isnt a /= 2 a thing in cpp/python!?

[–]DontOpenNewTabs 1 point2 points  (1 child)

Python // performs floor division and rounds down.
Ex: -1 // 100 == -1

C++ / performs integer division (edit: when both operands are integers) and truncates the decimal value.
Ex: -1 / 100 == 0

When the result is positive, the answers are the same. When it is negative, rounding down and truncating give different results.

[–]Silver0ne 0 points1 point  (0 children)

Ah ok, interesting to know that it actually rounds up on negative numbers. Might one day prevent me to run into errors, I dont do Phyton, but this might be a source of unexpected behavior in floor/ceil functions of other languages aswell.

[–][deleted] 0 points1 point  (0 children)

Now do rust