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

BUG: Fix array printing with precision=0. #8107

Merged
merged 1 commit into from
Oct 7, 2016

Conversation

wrwrwr
Copy link
Contributor

@wrwrwr wrwrwr commented Oct 1, 2016

Not a particularly significant matter, but trying something as follows:

import numpy as np
np.set_printoptions(precision=0)
print(np.array([.1]))

results in:

ValueError: unsupported format character '-' (0x2d) at index 4

This is easily fixed by handling the case of rounding to integers when counting significant digits.

I've also included instances of ties-to-even rounding in the test case, after the docstring for rint(), but maybe that's too specific (IEEE 754 defines more rounding modes).

z = s.rstrip('0')
return precision - len(s) + len(z)
else:
return len(s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it just return zero? In the non-zero case there is a decimal point that stops the rstrip so the precision returned is the actual number of digits past the decimal point minus trailing zeros. In the zero case that would always be zero if the decimal point was still there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, definitely, thank you for the careful look.

@@ -129,6 +129,13 @@ def test_basic(self):
np.set_printoptions(precision=4)
assert_equal(repr(x), "array([ 1.5 , 0. , 1.2346])")

def test_precision_zero(self):
np.set_printoptions(precision=0)
x = np.array([0, .7, 1.5, -1.5])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need some examples like 100.0.

Values rounding to zero were formatted with negative precision.
@wrwrwr
Copy link
Contributor Author

wrwrwr commented Oct 7, 2016

Changed _digits() to properly calculate the count of decimal digits past the decimal point (the incorrect return was masked by a min() in fillFormat()) and extended the test (including a few non-float cases).

@charris charris merged commit fda7484 into numpy:master Oct 7, 2016
@charris
Copy link
Member

charris commented Oct 7, 2016

Thanks @wrwrwr .

@wrwrwr wrwrwr deleted the fix-precision-zero branch October 10, 2016 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants