# HG changeset patch # User Kai T. Ohlhus # Date 1443015094 -7200 # Node ID 315b7d51d6c86e14e407913aaacb73bea1d81324 # Parent cd4a8b4631da8b065a61256af94f6d11f4abd5c6 randi.m: Display warnings in case of range exceedings. diff -r cd4a8b4631da -r 315b7d51d6c8 scripts/general/randi.m --- a/scripts/general/randi.m Wed Sep 23 02:45:54 2015 -0700 +++ b/scripts/general/randi.m Wed Sep 23 15:31:34 2015 +0200 @@ -61,7 +61,7 @@ nargoutchk (0, 1); if (! (isnumeric (bounds) && all (bounds == fix (bounds)))) - error ("randi: IMIN and IMAX must be integer bounds"); + error ("randi: IMIN and IMAX must be integer bounds."); endif bounds = real (double (bounds)); @@ -69,13 +69,13 @@ imin = 1; imax = bounds; if (imax < 1) - error ("randi: require IMAX >= 1"); + error ("randi: require IMAX >= 1."); endif else imin = bounds(1); imax = bounds(2); if (imax < imin) - error ("randi: require IMIN <= IMAX"); + error ("randi: require IMIN <= IMAX."); endif endif @@ -88,16 +88,31 @@ ## Limit set by use of class double in rand() if (imax >= flintmax ()) - error ("randi: maximum integer IMAX must be smaller than flintmax ()"); + error ("randi: maximum integer IMAX must be smaller than flintmax ()."); endif if ((imax - imin) >= flintmax ()) - error ("randi: maximum integer range must be smaller than flintmax ()"); + error ("randi: maximum integer range must be smaller than flintmax ()."); endif ri = imin + floor ( (imax-imin+1)*rand (varargin{:}) ); if (! strcmp (rclass, "double")) + if (strfind (rclass, "int")) + maxval = intmax (rclass); + minval = intmin (rclass); + elseif (strcmp (rclass, "single")) + maxval = flintmax (rclass); + minval = -maxval; + endif + if ((imax >= maxval) || ((imax - imin) >= maxval)) + warning (["randi: maximum integer IMAX or range exceeds requested ", ... + "type. Values might be truncated to requested type."]); + endif + if (imin < minval) + warning (["randi: minimum integer IMIN exceeds requested type. ", ... + "Values might be truncated to requested type."]); + endif ri = cast (ri, rclass); endif @@ -127,12 +142,6 @@ %! assert (max (ri), int8 (10)); %! assert (class (ri), "int8"); %!test -%! ri = randi ([-5, 10], 1000, 1, "uint8"); -%! assert (ri, fix (ri)); -%! assert (min (ri), uint8 (-5)); -%! assert (max (ri), uint8 (10)); -%! assert (class (ri), "uint8"); -%!test %! ri = randi ([-5; 10], 1000, 1, "single"); %! assert (ri, fix (ri)); %! assert (min (ri), single (-5)); @@ -141,6 +150,24 @@ %! %!assert (size (randi (10, 3,1,2)), [3, 1, 2]) +## Test range exceedings +%!warning +%! ri = randi ([-5, 10], 1000, 1, "uint8"); +%! assert (ri, fix (ri)); +%! assert (min (ri), uint8 (-5)); +%! assert (max (ri), uint8 (10)); +%! assert (class (ri), "uint8"); +%!warning randi (intmax("int8"), 10, 1, "int8"); +%!warning randi (flintmax("single"), 10, 1, "single"); +%!warning +%! randi ([-1, intmax("int8") - 1], 10, 1, "int8"); +%!warning +%! randi ([-1, flintmax("single") - 1], 10, 1, "single"); +%!warning +%! randi ([-flintmax("single"), 0], 10, 1, "single"); +%!warning +%! randi ([-flintmax("single") + 1, 1], 10, 1, "single"); + ## Test input validation %!error (randi ()) %!error (randi ("test"))