12
hido13
6y

I was looking up for a bug in my code that caused a fail in one of the test.
Hours later I found that negative integer division in python is just stupid and -1 / 10 = -1.
The sad part is that -1/10 != -(1/10) contradicting the associative property of multiplication over the real numbers.
FUCK YOU PYTHON.

Comments
  • 4
    To me it's working as intended. You are doing integer division -> answer will be floored to the integer value.

    -1 / 10 is -0,1 and gets floored to -1.

    -(1 / 10) is -0,1 but you are doing the division first and 1 / 10 = 0,1 which is floored to 0 and therefore answer is 0.

    Edit: And this is pretty much the standard behaviour in all the major programming languages. If you wish to get the right answer, just turn the dividend or divider to float / double.
  • 1
    @okkimus why do u use , instead of . in floats.. like 0,1 instead of 0.1??? Both these characters are close by!!!
  • 2
    @AnonymusPanther

    Just the convention used in my country :D You can imagine the same confusion when I needed to start using "." while programming...
  • 0
    @okkimus No, in most language it is 0.
    Check it.
    It still contradict a very basic property of the real numbers which is a serious issue.
  • 0
    @AnonymusPanther I have no reason of saving integers as floats, and should not have a reason to convert the types just to get a reasonable division.
  • 0
    @hido13

    Oh yeah, actually you are correct. Tried few repls and Java, C++ and bunch of others gave 0 for both. Ruby gave the same as Python.

    Though the Python way just makes more sense for me.

    And what contradicts basic property of real numbers?
  • 2
    @okkimus
    I wonder if there's other countries than Finland that use comma as decimal separator.

    I hate that we use it here although dot seems to be somewhat international standard these days.
  • 0
    @Julien00859

    Yeah, it seems to be python 2.

    I started to look in to it it's actually kinda interesting question. They changes it in python 3 to change the type to double if needed. Though you can use "//" operator to do the good ol' integer division.

    But I started to investigate why there's difference between python 2 and Java. I checked and they have same operator precedence...so there's something else happening under the hood.
  • 2
    All the scandinavian countries use comma, Germany does too. I wonder if most of Europe does, and it's only the english speaking countries that use dot for decimals. Our way looks weird to them because the comma is a thousands separator, but here we just throw in a space for easier readability of large numbers.
  • 0
    @Julien00859 I prefer python3, it wasn't my call.
  • 0
    @okkimus If you find the reason for that please comment it here
  • 1
    I Greece we also use comma for decimals and dot to separate very big numbers, ie 1.234.567

    Also, most scripted languages have fucked up math operations.
Add Comment