Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division in place acts differently according to Python version #120

Open
antnieszka opened this issue Dec 10, 2016 · 5 comments
Open

Division in place acts differently according to Python version #120

antnieszka opened this issue Dec 10, 2016 · 5 comments

Comments

@antnieszka
Copy link
Contributor

antnieszka commented Dec 10, 2016

This doctest works in py2.7, fails in 3.4.

        >>> c = Curve([[0, 0], [5, 5], [10, 10]], dtype=np.int)
        >>> c.rescale(-1, allow_cast=False)
        >>> print(c.y)
        [  0  -5 -10]

py3:

Failed example:
    c.rescale(-1, allow_cast=False)
Exception raised:
    Traceback (most recent call last):
      File "/home/ant6/bin/pycharm-2016.3/helpers/pycharm/docrunner.py", line 140, in __run
        compileflags, 1), test.globs)
      File "<doctest curve.Curve.rescale[7]>", line 1, in <module>
        c.rescale(-1, allow_cast=False)
      File "/home/ant6/PycharmProjects/beprof/beprof/curve.py", line 109, in rescale
        self.y /= factor
    TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''

With allow_cast=True both act the same and pass.

@grzanka
Copy link
Collaborator

grzanka commented Dec 10, 2016

Beware that in py3 you have two division operators: // and /

@antnieszka
Copy link
Contributor Author

https://www.python.org/dev/peps/pep-0238/

    In Python 3.0, the classic division semantics will be removed; the
    classic division APIs will become synonymous with true division.

Probably we could use from __future__ import division to have true division, not classic in py2.7?

@antnieszka
Copy link
Contributor Author

@grzanka this too probably should be resolved before release to pypy?

@grzanka
Copy link
Collaborator

grzanka commented Dec 11, 2016

Yes. I was thinking a bit more about it and the conclusion was that we should prefer floats over ints.
Can you show an example how from __future__ import division works ?

@antnieszka
Copy link
Contributor Author

antnieszka commented Dec 11, 2016

According to this PEP: https://www.python.org/dev/peps/pep-0238/

  • Classic division will remain the default in the Python 2.x
    series; true division will be standard in Python 3.0.

For Python 2:

  • The future division statement, spelled "from __future__ import
    division", will change the / operator to mean true division
    throughout the module.

Classic division (default in Python 2)

>>> 1 / 2          # integer truncation (floor division)
0
>>> 1.0 / 2.0      # returns real quotient (true division)
0.5

True division

>>> from __future__ import division  # 2.2+-only
>>>
>>> 1 / 2               # returns real quotient
0.5
>>> 1.0 / 2.0           # returns real quotient
0.5

As said earlier - true division is default for Python 3.x, so is / used in our code.
More on topic: http://www.informit.com/articles/article.aspx?p=1439189

Oh, and __future__ import is ignored in Python 3, so it will only affect beprof if running with Python 2.

@antnieszka antnieszka added this to the 0.1.1 milestone Dec 11, 2016
@antnieszka antnieszka modified the milestones: 0.1.0, Next release Dec 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants