Skip to content

Commit 16f9778

Browse files
committed
Don't convert vmin, vmax to floats.
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.)
1 parent 3590ce2 commit 16f9778

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/matplotlib/colors.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,6 @@ def __call__(self, value, clip=None):
918918
elif vmin > vmax:
919919
raise ValueError("minvalue must be less than or equal to maxvalue")
920920
else:
921-
vmin = float(vmin)
922-
vmax = float(vmax)
923921
if clip:
924922
mask = np.ma.getmask(result)
925923
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
@@ -938,8 +936,7 @@ def __call__(self, value, clip=None):
938936
def inverse(self, value):
939937
if not self.scaled():
940938
raise ValueError("Not invertible until scaled")
941-
vmin = float(self.vmin)
942-
vmax = float(self.vmax)
939+
vmin, vmax = self.vmin, self.vmax
943940

944941
if cbook.iterable(value):
945942
val = np.ma.asarray(value)

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ def test_Normalize():
194194
_scalar_tester(norm, vals)
195195
_mask_tester(norm, vals)
196196

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

198203
def test_SymLogNorm():
199204
"""

0 commit comments

Comments
 (0)