Mercurial > octave
changeset 24521:30bcf1723ca8
chop.m: Deprecate function.
* scripts/deprecated/chop.m: Moved from scripts/general.
Add code to issue deprecated warning.
* scripts/deprecated/module.mk, scripts/general/module.mk: Update build system.
* NEWS: Announce deprecation.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 04 Jan 2018 12:05:47 -0800 |
parents | c5c11b07598a |
children | ec5591efafe4 |
files | NEWS scripts/deprecated/chop.m scripts/deprecated/module.mk scripts/general/chop.m scripts/general/module.mk |
diffstat | 5 files changed, 96 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS Wed Jan 03 07:52:11 2018 -0500 +++ b/NEWS Thu Jan 04 12:05:47 2018 -0800 @@ -122,6 +122,7 @@ Function | Replacement ---------------------|------------------ + chop | sprintf for visual results ** The following functions were deprecated in Octave 4.0 and have been removed from Octave 4.4.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/deprecated/chop.m Thu Jan 04 12:05:47 2018 -0800 @@ -0,0 +1,94 @@ +## Copyright (C) 2010-2017 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {} {} chop (@var{x}, @var{ndigits}, @var{base}) +## +## @code{chop} is deprecated and will be removed in Octave version 4.8. +## +## Truncate elements of @var{x} to a length of @var{ndigits} such that the +## resulting numbers are exactly divisible by @var{base}. +## +## If @var{base} is not specified it defaults to 10. +## +## @example +## @group +## format long +## chop (-pi, 5, 10) +## @result{} -3.14200000000000 +## chop (-pi, 5, 5) +## @result{} -3.14150000000000 +## @end group +## @end example +## @end deftypefn + +## Deprecated in version 4.4 + +function retval = chop (x, ndigits, base = 10) + + persistent warned = false; + if (! warned) + warned = true; + warning ("Octave:deprecated-function", + "chop is obsolete and will be removed from a future version of Octave"); + endif + + if (nargin == 2 || nargin == 3) + tmp = abs (x); + + ## Avoid computing log (0). + tmp(x == 0) = 1; + + ## Digits to the left of the decimal. + tmp = floor (log10 (tmp) + 1); + + ## The expression + ## + ## round (x .* inflate) + ## + ## produces an integer that contains the digits we want to keep. + ## Multiplying by deflate puts the decimal back where it belngs. + ## + ## Further scaling and rounding with the base factor produces a + ## value with ndigits exactly divisible by base. We skip that step + ## unless base was explicitly provided. + + inflate = 10 .^ (ndigits - tmp); + deflate = 1 ./ inflate; + if (nargin == 2) + retval = deflate .* round (x .* inflate); + else + retval = base .* deflate .* round (round (x .* inflate) ./ base); + endif + else + print_usage (); + endif + +endfunction + + +%!assert (chop (e, 3), 2.72) +%!assert (chop (e, 4), 2.718) +%!assert (chop (e, 4, 5), 2.72) +%!assert (chop (e, 4, 7), 2.716) +%!assert (chop (-e, 3), -2.72) +%!assert (chop (-e, 4), -2.718) +%!assert (chop (-e, 4, 5), -2.72) +%!assert (chop (-e, 4, 7), -2.716) +%!assert (chop (hilb (3), 3), [1,.5,.333;.5,.333,.25;.333,.25,.2]) +%!assert (chop (hilb (3), 2, 7), [.7,.49,.35;.49,.35,.28;.35,.28,.21], 2*eps)
--- a/scripts/deprecated/module.mk Wed Jan 03 07:52:11 2018 -0500 +++ b/scripts/deprecated/module.mk Thu Jan 04 12:05:47 2018 -0800 @@ -2,6 +2,7 @@ %canon_reldir%_FCN_FILES = \ %reldir%/bitmax.m \ + %reldir%/chop.m \ %reldir%/comma.m \ %reldir%/isstr.m \ %reldir%/mahalanobis.m \
--- a/scripts/general/chop.m Wed Jan 03 07:52:11 2018 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -## Copyright (C) 2010-2017 John W. Eaton -## -## This file is part of Octave. -## -## Octave is free software; you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 3 of the License, or -## (at your option) any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, see -## <http://www.gnu.org/licenses/>. - -## -*- texinfo -*- -## @deftypefn {} {} chop (@var{x}, @var{ndigits}, @var{base}) -## Truncate elements of @var{x} to a length of @var{ndigits} such that the -## resulting numbers are exactly divisible by @var{base}. -## -## If @var{base} is not specified it defaults to 10. -## -## @example -## @group -## format long -## chop (-pi, 5, 10) -## @result{} -3.14200000000000 -## chop (-pi, 5, 5) -## @result{} -3.14150000000000 -## @end group -## @end example -## @end deftypefn - -function retval = chop (x, ndigits, base = 10) - - if (nargin == 2 || nargin == 3) - tmp = abs (x); - - ## Avoid computing log (0). - tmp(x == 0) = 1; - - ## Digits to the left of the decimal. - tmp = floor (log10 (tmp) + 1); - - ## The expression - ## - ## round (x .* inflate) - ## - ## produces an integer that contains the digits we want to keep. - ## Multiplying by deflate puts the decimal back where it belngs. - ## - ## Further scaling and rounding with the base factor produces a - ## value with ndigits exactly divisible by base. We skip that step - ## unless base was explicitly provided. - - inflate = 10 .^ (ndigits - tmp); - deflate = 1 ./ inflate; - if (nargin == 2) - retval = deflate .* round (x .* inflate); - else - retval = base .* deflate .* round (round (x .* inflate) ./ base); - endif - else - print_usage (); - endif - -endfunction - - -%!assert (chop (e, 3), 2.72) -%!assert (chop (e, 4), 2.718) -%!assert (chop (e, 4, 5), 2.72) -%!assert (chop (e, 4, 7), 2.716) -%!assert (chop (-e, 3), -2.72) -%!assert (chop (-e, 4), -2.718) -%!assert (chop (-e, 4, 5), -2.72) -%!assert (chop (-e, 4, 7), -2.716) -%!assert (chop (hilb (3), 3), [1,.5,.333;.5,.333,.25;.333,.25,.2]) -%!assert (chop (hilb (3), 2, 7), [.7,.49,.35;.49,.35,.28;.35,.28,.21], 2*eps)