annotate scripts/general/num2str.m @ 33623:4517f929c59d bytecode-interpreter tip

maint: Merge default to bytecode-interpreter
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 May 2024 16:22:41 -0400
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 1993-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
13 ## (at your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6997
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
25
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{str} =} num2str (@var{x})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {@var{str} =} num2str (@var{x}, @var{precision})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
29 ## @deftypefnx {} {@var{str} =} num2str (@var{x}, @var{format})
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## Convert a number (or array) to a string (or a character array).
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## The optional second argument may either give the number of significant
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## digits (@var{precision}) to be used in the output or a format template
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## string (@var{format}) as in @code{sprintf} (@pxref{Formatted Output}).
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## @code{num2str} can also process complex numbers.
16197
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
36 ##
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
37 ## Examples:
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
38 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
39 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
40 ## num2str (123.456)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
41 ## @result{} 123.456
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
42 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
43 ## num2str (123.456, 4)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
44 ## @result{} 123.5
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
45 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
46 ## s = num2str ([1, 1.34; 3, 3.56], "%5.1f")
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
47 ## @result{} s =
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
48 ## 1.0 1.3
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
49 ## 3.0 3.6
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
50 ## whos s
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
51 ## @result{} Variables in the current scope:
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
52 ## Attr Name Size Bytes Class
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
53 ## ==== ==== ==== ===== =====
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
54 ## s 2x8 16 char
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
55 ## Total is 16 elements using 16 bytes
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
56 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
57 ## num2str (1.234 + 27.3i)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
58 ## @result{} 1.234+27.3i
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
59 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
60 ##
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
61 ## The @code{num2str} function is not very flexible. For better control
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
62 ## over the results, use @code{sprintf} (@pxref{Formatted Output}).
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
63 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
64 ## Programming Notes:
16197
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
65 ##
16816
12005245b645 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 16724
diff changeset
66 ## For @sc{matlab} compatibility, leading spaces are stripped before returning
16197
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
67 ## the string.
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
68 ##
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
69 ## Integers larger than @code{flintmax} may not be displayed correctly.
16197
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
70 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
71 ## For complex @var{x}, the format string may only contain one output
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
72 ## conversion specification and nothing else. Otherwise, results will be
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
73 ## unpredictable.
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
74 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
75 ## Any optional @var{format} specified by the programmer is used without
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
76 ## modification. This is in contrast to @sc{matlab} which tampers with the
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
77 ## @var{format} based on internal heuristics.
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
78 ## @seealso{sprintf, int2str, mat2str}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
79 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
80
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
81 function str = num2str (x, arg)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
82
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
83 if (nargin < 1)
5947
009fa69b6182 [project @ 2006-08-21 16:15:20 by jwe]
jwe
parents: 5939
diff changeset
84 print_usage ();
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
85 elseif (! (isnumeric (x) || islogical (x) || ischar (x)))
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
86 error ("num2str: X must be a numeric, logical, or character array");
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
87 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
88
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5355
diff changeset
89 if (ischar (x))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
90 str = x;
5947
009fa69b6182 [project @ 2006-08-21 16:15:20 by jwe]
jwe
parents: 5939
diff changeset
91 elseif (isempty (x))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
92 str = "";
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
93 elseif (isreal (x))
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
94 if (nargin == 2)
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
95 if (ischar (arg))
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
96 fmt = arg;
19725
5f2c0ca0ef51 Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents: 19700
diff changeset
97 elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg))
29880
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
98 if (isfloat (x))
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
99 fmt = sprintf ("%%%d.%dg", arg+7, arg);
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
100 else
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
101 fmt = sprintf ("%%%dd", arg);
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
102 endif
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
103 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
104 error ("num2str: PRECISION must be a scalar integer >= 0");
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
105 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
106 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
107 if (isnumeric (x))
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
108 ## Set up a suitable format string while ignoring Inf/NaN entries
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
109 valid = isfinite (x(:));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
110 ndgt = floor (log10 (max (abs (x(valid)))));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
111 if (isempty (ndgt) || ndgt == -Inf)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
112 ndgt = 0; # All Inf or all zero array
15732
82b0ad43a939 num2str: Take into account inf inputs. Add tests for inf and NaN
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14538
diff changeset
113 endif
82b0ad43a939 num2str: Take into account inf inputs. Add tests for inf and NaN
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14538
diff changeset
114
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
115 if (ndgt > 15 || any (x(valid) != fix (x(valid))))
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
116 ## Floating point input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
117 ndgt = max (ndgt + 5, 5); # Keep at least 5 significant digits
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
118 ndgt = min (ndgt, 16); # Cap significant digits at 16
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
119 fmt = sprintf ("%%%d.%dg", ndgt+7, ndgt);
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
120 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
121 ## Integer input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
122 ndgt += 3;
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
123 if (any (! valid))
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
124 ndgt = max (ndgt, 5); # Allow space for Inf/NaN
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
125 endif
29879
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
126 if (isfloat (x))
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
127 fmt = sprintf ("%%%d.0f", ndgt);
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
128 else
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
129 fmt = sprintf ("%%%dd", ndgt);
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
130 endif
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
131 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
132 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
133 ## Logical input
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
134 fmt = "%3d";
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
135 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
136 endif
20195
ea5fdb2ae637 num2str.m: Correctly parse newlines in single-quoted format strings (bug #44684).
Rik <rik@octave.org>
parents: 20158
diff changeset
137 fmt = do_string_escapes (fmt); # required now that '\n' is interpreted.
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
138 nd = ndims (x);
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
139 nc = columns (x) * (nd - 1); # ND-arrays are expanded in columns
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
140 x = permute (x, [2, 3:nd, 1]);
29123
51bb0e599af4 num2str.m: Fix corner case with literal percent ("%%") at end of format (bug #59527).
John W. Eaton <jwe@octave.org>
parents: 28912
diff changeset
141 if (! (sum (strrep (fmt, "%%", "") == "%") > 1
51bb0e599af4 num2str.m: Fix corner case with literal percent ("%%") at end of format (bug #59527).
John W. Eaton <jwe@octave.org>
parents: 28912
diff changeset
142 || any (strcmp (fmt, {"%s", "%c"}))))
20266
f357e076776f num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20195
diff changeset
143 fmt = [deblank(repmat (fmt, 1, nc)), "\n"];
f357e076776f num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20195
diff changeset
144 endif
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
145 strtmp = sprintf (fmt, x);
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
146 str = strtrim (char (ostrsplit (strtmp, "\n", true)));
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
147 else # Complex matrix input
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
148 if (nargin == 2)
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5355
diff changeset
149 if (ischar (arg))
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
150 fmt = [deblank(arg) "%-+" arg(2:end) "i"];
19725
5f2c0ca0ef51 Ensure that numbers passed to integer *printf format codes are integers (bug #44245).
Rik <rik@octave.org>
parents: 19700
diff changeset
151 elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg))
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
152 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
153 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
154 error ("num2str: PRECISION must be a scalar integer >= 0");
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
155 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
156 else
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
157 ## Set up a suitable format string while ignoring Inf/NaN entries
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
158 valid_real = isfinite (real (x(:)));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
159 valid_imag = isfinite (imag (x(:)));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
160 ndgt = floor (log10 (max (max (abs (real (x(valid_real)))),
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
161 max (abs (imag (x(valid_imag)))))));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
162 if (isempty (ndgt) || ndgt == -Inf)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
163 ndgt = 0; # All Inf or all zero array
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
164 endif
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
165
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
166 if (any (x(valid_real & valid_imag) != fix (x(valid_real & valid_imag))))
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
167 ## Floating point input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
168 ndgt = max (ndgt + 5, 5); # Keep at least 5 significant digits
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
169 ndgt = min (ndgt, 16); # Cap significant digits at 16
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
170 fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", ndgt+7, ndgt, ndgt+7, ndgt);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
171 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
172 ## Integer input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
173 ndgt += 3;
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22470
diff changeset
174 ## FIXME: Integers must be masked to show only 16 significant digits
22470
9d4cb0cf9cd2 maint: tag xtests and tests that fail on some systems with bug numbers
Mike Miller <mtmiller@octave.org>
parents: 22323
diff changeset
175 ## See test case for bug #36133 below
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
176 fmt = sprintf ("%%%d.0f%%-+%d.0fi", ndgt, ndgt);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
177 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
178 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
179
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
180 ## Manipulate the complex value to have real values in the odd
4914
1c0442da75fd [project @ 2004-07-23 16:55:13 by jwe]
jwe
parents: 4911
diff changeset
181 ## columns and imaginary values in the even columns.
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
182 nd = ndims (x);
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
183 nc = columns (x);
12676
2783fa95cab7 Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
184 idx = repmat ({':'}, nd, 1);
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
185 perm(1:2:2*nc) = 1:nc;
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
186 perm(2:2:2*nc) = nc + (1:nc);
4914
1c0442da75fd [project @ 2004-07-23 16:55:13 by jwe]
jwe
parents: 4911
diff changeset
187 idx{2} = perm;
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
188 x = horzcat (real (x), imag (x));
4914
1c0442da75fd [project @ 2004-07-23 16:55:13 by jwe]
jwe
parents: 4911
diff changeset
189 x = x(idx{:});
21076
b433f9990452 strip trailing whitespace from files
John W. Eaton <jwe@octave.org>
parents: 21057
diff changeset
190
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
191 fmt = [deblank(repmat(fmt, 1, nc * (nd - 1))), "\n"];
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
192 tmp = sprintf (fmt, permute (x, [2, 3:nd, 1]));
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
193
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
194 ## Put the "i"'s where they are supposed to be.
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
195 tmp = regexprep (tmp, " +i\n", "i\n");
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
196 tmp = regexprep (tmp, "( +)i", "i$1");
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
197
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
198 str = strtrim (char (ostrsplit (tmp(1:end-1), "\n")));
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
199 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
200
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
201 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
202
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
203
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
204 ## Basic tests
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
205 %!assert (num2str (123), "123")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
206 %!assert (num2str (1.23), "1.23")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
207 %!assert (num2str (123.456, 4), "123.5")
21562
6c2fd62db1f7 maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents: 21317
diff changeset
208 %!assert (num2str ([1, 1.34; 3, 3.56], "%5.1f"), ["1.0 1.3"; "3.0 3.6"])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
209 %!assert (num2str (1.234 + 27.3i), "1.234+27.3i")
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21076
diff changeset
210 %!assert (num2str ([true false true]), "1 0 1")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
211
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
212 ## Exceptional values
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
213 %!assert (num2str (19440606), "19440606")
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
214 %!assert (num2str (2^33), "8589934592")
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
215 %!assert (num2str (-2^33), "-8589934592")
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
216 %!assert (num2str (2^33+1i), "8589934592+1i")
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
217 %!assert (num2str (-2^33+1i), "-8589934592+1i")
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
218 %!assert (num2str ([0 0 0]), "0 0 0")
15732
82b0ad43a939 num2str: Take into account inf inputs. Add tests for inf and NaN
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14538
diff changeset
219 %!assert (num2str (inf), "Inf")
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
220 %!assert (num2str ([inf -inf]), "Inf -Inf")
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
221 %!assert (num2str ([inf NaN -inf]), "Inf NaN -Inf")
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
222 %!assert (num2str ([complex(Inf,0), complex(0,-Inf)]), "Inf+0i 0-Infi")
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28891
diff changeset
223 %!assert (num2str (complex (Inf,1)), "Inf+1i")
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28891
diff changeset
224 %!assert (num2str (complex (1,Inf)), "1+Infi")
15732
82b0ad43a939 num2str: Take into account inf inputs. Add tests for inf and NaN
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14538
diff changeset
225 %!assert (num2str (nan), "NaN")
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
226 %!assert (num2str (complex (NaN, 1)), "NaN+1i")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
227 %!assert (num2str (complex (1, NaN)), "1+NaNi")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
228 %!assert (num2str (NA), "NA")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
229 %!assert (num2str (complex (NA, 1)), "NA+1i")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
230 %!assert (num2str (complex (1, NA)), "1+NAi")
29879
ca30a6c1acc3 num2str.m: Don't convert integer input to floating point.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
231 %!assert (num2str (int64 (-flintmax ()) - 1), "-9007199254740993")
29880
dbc191149841 num2str.m: Also fix integer input with second input argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29879
diff changeset
232 %!assert (num2str (int64 (-flintmax ()) - 1, 18), "-9007199254740993")
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
233
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
234 ## ND-arrays are concatenated in columns
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
235 %!shared m, x
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
236 %! m = magic (3);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
237 %! x = cat (3, m, -m);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
238
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
239 ## real case
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
240 %!test <*46770>
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
241 %! y = num2str (x);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
242 %! assert (rows (y) == 3);
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
243 %! assert (y, ["8 1 6 -8 -1 -6"
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
244 %! "3 5 7 -3 -5 -7"
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
245 %! "4 9 2 -4 -9 -2"]);
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
246
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
247 ## complex case
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
248 %!test <*46770>
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
249 %! x(1,1,2) = -8+2i;
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
250 %! y = num2str (x);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
251 %! assert (rows (y) == 3);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
252 %! assert (y, ["8+0i 1+0i 6+0i -8+2i -1+0i -6+0i"
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
253 %! "3+0i 5+0i 7+0i -3+0i -5+0i -7+0i"
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
254 %! "4+0i 9+0i 2+0i -4+0i -9+0i -2+0i"]);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
255
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
256 ## Clear shared variables
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
257 %!shared
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
258
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
259 ## Integers greater than 1e15 should switch to exponential notation
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
260 %!assert <*36133> (num2str (1e15), "1000000000000000")
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
261 %!assert <*36133> (num2str (1e16), "1e+16")
25602
8f8d3fef29a8 num2str.m: Update Matlab Compatibility BIST tests (bug #36133).
Rik <rik@octave.org>
parents: 25054
diff changeset
262 ## Even exact integers in IEEE notation should use exponential notation
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28891
diff changeset
263 %!assert <*36133> (num2str (2^512), "1.34078079299426e+154")
25602
8f8d3fef29a8 num2str.m: Update Matlab Compatibility BIST tests (bug #36133).
Rik <rik@octave.org>
parents: 25054
diff changeset
264 ## Mixed integer/floating point arrays
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
265 %!assert <*36133> (num2str ([2.1, 1e23, pi]),
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
266 %! "2.1 9.999999999999999e+22 3.141592653589793")
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
267
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
268 ## Large integers should not switch sign when printed due to overflow
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
269 %!assert <*36121> (num2str (2.4e9, 15), "2400000000")
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
270
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22470
diff changeset
271 ## Test for extra rows generated from newlines in format
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
272 %!assert <*44864> (rows (num2str (magic (3), "%3d %3d %3d\n")), 3)
20195
ea5fdb2ae637 num2str.m: Correctly parse newlines in single-quoted format strings (bug #44684).
Rik <rik@octave.org>
parents: 20158
diff changeset
273
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
274 ## Test that string conversion of numeric objects results in characters
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
275 ## if the numbers are within range for ASCII.
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
276 %!assert <*45174> (num2str ([65 66 67], "%s"), "ABC")
20266
f357e076776f num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20195
diff changeset
277
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
278 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27923
diff changeset
279 %!error <Invalid call> num2str ()
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
280 %!error <X must be a numeric> num2str ({1})
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
281 %!error <PRECISION must be a scalar integer .= 0> num2str (1, {1})
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
282 %!error <PRECISION must be a scalar integer .= 0> num2str (1, ones (2))
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
283 %!error <PRECISION must be a scalar integer .= 0> num2str (1, -1)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
284 %!error <PRECISION must be a scalar integer .= 0> num2str (1, 1.5)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
285 %!error <PRECISION must be a scalar integer .= 0> num2str (1+1i, {1})
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
286 %!error <PRECISION must be a scalar integer .= 0> num2str (1+1i, ones (2))
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
287 %!error <PRECISION must be a scalar integer .= 0> num2str (1+1i, -1)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
288 %!error <PRECISION must be a scalar integer .= 0> num2str (1+1i, 1.5)