Mercurial > octave
comparison scripts/plot/draw/hist.m @ 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 | bd51beb6205e |
children | 376ecc6794e4 |
comparison
equal
deleted
inserted
replaced
28522:a5541f5a78dd | 28523:68000a4df5a8 |
---|---|
113 equal_bin_spacing = true; | 113 equal_bin_spacing = true; |
114 | 114 |
115 ## Process possible second argument | 115 ## Process possible second argument |
116 if (nargin == 1 || ischar (varargin{iarg})) | 116 if (nargin == 1 || ischar (varargin{iarg})) |
117 n = 10; | 117 n = 10; |
118 ## Use range type to preserve accuracy | 118 ## Use integer range values and perform division last to preserve |
119 ## accuracy. | |
119 if (min_val != max_val) | 120 if (min_val != max_val) |
120 x = (0.5:n) * (1/n); | 121 x = 1:2:2*n; |
121 x = (max_val - min_val) * x + min_val; | 122 x = ((max_val - min_val) * x + 2*n*min_val) / (2*n); |
122 else | 123 else |
123 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val; | 124 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val; |
124 endif | 125 endif |
125 x = x.'; # Convert to matrix; | 126 x = x.'; # Convert to matrix; |
126 else | 127 else |
138 if (isscalar (x)) | 139 if (isscalar (x)) |
139 n = fix (x); | 140 n = fix (x); |
140 if (n <= 0) | 141 if (n <= 0) |
141 error ("hist: number of bins NBINS must be positive"); | 142 error ("hist: number of bins NBINS must be positive"); |
142 endif | 143 endif |
143 ## Use range type to preserve accuracy | 144 ## Use integer range values and perform division last to preserve |
145 ## accuracy. | |
144 if (min_val != max_val) | 146 if (min_val != max_val) |
145 x = (0.5:n) * (1/n); | 147 x = 1:2:2*n; |
146 x = (max_val - min_val) * x + min_val; | 148 x = ((max_val - min_val) * x + 2*n*min_val) / (2*n); |
147 else | 149 else |
148 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val; | 150 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val; |
149 endif | 151 endif |
150 x = x.'; # Convert to matrix; | 152 x = x.'; # Convert to matrix; |
151 elseif (isvector (x)) | 153 elseif (isvector (x)) |