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

fix the default arguments of mathutils.clamp #128

Merged
merged 3 commits into from
Jun 18, 2017
Merged

fix the default arguments of mathutils.clamp #128

merged 3 commits into from
Jun 18, 2017

Conversation

tiwo
Copy link
Contributor

@tiwo tiwo commented Jun 18, 2017

mathutils.clamp is supposed to restrict an arbitrary number to the interval given by its last two (optional) arguments. But it is incorrect when not both lower and upper arguments are given

The default arguments are None, which isn't comparable in Python 3 and is less than x for any x in Python 2 (except NaN).

expression Python 2 Python 3 fixed
clamp(1) None raises TypeError 1
clamp(1, upper=2) 1 raises TypeError 1
clamp(1, lower=0) raises ValueError raises TypeError 1

This fixes mathutils.clamp by simply using positive infinity and negative infinity as default bounds, and adds two test cases as well as an example in the function's docstring.

tiwo added 3 commits June 18, 2017 14:53
these are expected to fail

* for Python 3, because the argument defaults (None) are incomparable
* for Python 2, because None<x for all ints and floats x, thus clamp(x)
  returns None
@mahmoud
Copy link
Owner

mahmoud commented Jun 18, 2017

Excellent points and excellent contribution! Very nice work, @tiwo, thank you!

@mahmoud mahmoud merged commit 620af22 into mahmoud:master Jun 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants