Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81b072a

Browse files
author
Nathan Goldbaum
committed
use nextafter to correct floating point round-off error
1 parent 8820859 commit 81b072a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/matplotlib/animation.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,19 @@
6262
# how-to-encode-series-of-images-into-h264-using-x264-api-c-c )
6363

6464

65+
def correct_roundoff(x, dpi, n):
66+
if int(x*dpi) % n != 0:
67+
if int(np.nextafter(x, np.inf)*dpi) % n == 0:
68+
x = np.nextafter(x, np.inf)
69+
if int(np.nextafter(x, -np.inf)*dpi) % n == 0:
70+
x = np.nextafter(x, -np.inf)
71+
return x
72+
73+
6574
def adjusted_figsize(w, h, dpi, n):
66-
wnew = np.round(int(w * dpi / n) * n / dpi)
67-
hnew = np.round(int(h * dpi / n) * n / dpi)
68-
return wnew, hnew
75+
wnew = int(w * dpi / n) * n / dpi
76+
hnew = int(h * dpi / n) * n / dpi
77+
return (correct_roundoff(wnew, dpi, n), correct_roundoff(hnew, dpi, n))
6978

7079

7180
# A registry for available MovieWriter classes

0 commit comments

Comments
 (0)