changeset 10040:9bfde701dba7 octave-forge

miscellaneous: Adding truncate function for evaluation of the community.
author jpicarbajal
date Sun, 15 Apr 2012 11:00:12 +0000
parents f7009e743728
children ef15f1106049
files main/miscellaneous/devel/truncate.m
diffstat 1 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/main/miscellaneous/devel/truncate.m	Sun Apr 15 11:00:07 2012 +0000
+++ b/main/miscellaneous/devel/truncate.m	Sun Apr 15 11:00:12 2012 +0000
@@ -14,15 +14,19 @@
 %%    along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 %% -*- texinfo -*-
-%% @deftypefn {Function File} {@var{y} = } truncate (@var{x}, @var{order})
+%% @deftypefn {Function File} {@var{y} = } truncate (@var{x}, @var{order},@var{method})
+%% @deftypefnx {Function File} {@var{y} = } truncate (@dots,@var{method})
 %% Truncates @var{X} to @var{order} of magnitude.
 %%
-%% Example
+%% The optional argument @var{method} can be a hanlde to a function used to
+%% truncate the number. Default is @code{@round}.
+%%
+%% Examples:
 %% @example
 %%    format long
 %%    x = 987654321.123456789;
 %%    order = [3:-1:0 -(1:3)]';
-%%    y = truncate(x,order)
+%%    y = truncate (x,order)
 %% y =
 %%   987654000.000000
 %%   987654300.000000
@@ -31,13 +35,20 @@
 %%   987654321.100000
 %%   987654321.120000
 %%   987654321.123000
+%%
+%%    format
+%%    [truncate(0.127,-2), truncate(0.127,-2,@floor)]
+%% ans =
+%%    0.13000   0.12000
+%%
 %% @end example
+
 %%
 %% @seealso{round,fix,ceil,floor}
 %% @end deftypefn
 
-function y = truncate (x,order)
+function y = truncate (x,order,method=@round)
   ino = 0.1.^order;
   o = 10.^order;
-  y = round (x.*ino).*o;
+  y = method (x.*ino).*o;
 end