Skip to content

Commit

Permalink
Don't convert vmin, vmax to floats.
Browse files Browse the repository at this point in the history
They may be float128's in which case precision would be lost; this can
result in `Normalize` returning values (barely) outside of `[0, 1]`.

(The cast to `float` was introduced in 28e1d2, referring to bug 2997687
on SF; it may be worth checking what it was about.)
  • Loading branch information
anntzer committed Jul 7, 2016
1 parent 3590ce2 commit 16f9778
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 1 addition & 4 deletions lib/matplotlib/colors.py
Expand Up @@ -918,8 +918,6 @@ def __call__(self, value, clip=None):
elif vmin > vmax:
raise ValueError("minvalue must be less than or equal to maxvalue")
else:
vmin = float(vmin)
vmax = float(vmax)
if clip:
mask = np.ma.getmask(result)
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
Expand All @@ -938,8 +936,7 @@ def __call__(self, value, clip=None):
def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until scaled")
vmin = float(self.vmin)
vmax = float(self.vmax)
vmin, vmax = self.vmin, self.vmax

if cbook.iterable(value):
val = np.ma.asarray(value)
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Expand Up @@ -194,6 +194,11 @@ def test_Normalize():
_scalar_tester(norm, vals)
_mask_tester(norm, vals)

# Don't lose precision on longdoubles (float128 on Linux).
vals = np.array([1.2345678901, 9.8765432109], dtype=np.longdouble)
norm = mcolors.Normalize(vals.min(), vals.max())
assert_array_equal(np.asarray(norm(vals)), [0, 1])


def test_SymLogNorm():
"""
Expand Down

0 comments on commit 16f9778

Please sign in to comment.