changeset 28523:68000a4df5a8

hist.m: use different method for preserving accuracy of arithmetic on ranges * hist.m: When doing arithmetic on ranges, start with integer range values and perform division last.
author John W. Eaton <jwe@octave.org>
date Wed, 01 Jul 2020 15:27:22 -0400
parents a5541f5a78dd
children 455fe4a6f22c
files scripts/plot/draw/hist.m
diffstat 1 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/hist.m	Wed Jul 01 13:18:19 2020 -0400
+++ b/scripts/plot/draw/hist.m	Wed Jul 01 15:27:22 2020 -0400
@@ -115,10 +115,11 @@
   ## Process possible second argument
   if (nargin == 1 || ischar (varargin{iarg}))
     n = 10;
-    ## Use range type to preserve accuracy
+    ## Use integer range values and perform division last to preserve
+    ## accuracy.
     if (min_val != max_val)
-      x = (0.5:n) * (1/n);
-      x = (max_val - min_val) * x + min_val;
+      x = 1:2:2*n;
+      x = ((max_val - min_val) * x + 2*n*min_val) / (2*n);
     else
       x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val;
     endif
@@ -140,10 +141,11 @@
       if (n <= 0)
         error ("hist: number of bins NBINS must be positive");
       endif
-      ## Use range type to preserve accuracy
+      ## Use integer range values and perform division last to preserve
+      ## accuracy.
       if (min_val != max_val)
-        x = (0.5:n) * (1/n);
-        x = (max_val - min_val) * x + min_val;
+        x = 1:2:2*n;
+        x = ((max_val - min_val) * x + 2*n*min_val) / (2*n);
       else
         x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val;
       endif