annotate scripts/general/num2str.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 1993-2020 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
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
5 ##
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
9 ## 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
10 ## 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
11 ## 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
12 ## (at your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ## 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
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
18 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
19 ## 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
20 ## 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
21 ## <https://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
22
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20486
diff changeset
24 ## @deftypefn {} {} num2str (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20486
diff changeset
25 ## @deftypefnx {} {} num2str (@var{x}, @var{precision})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20486
diff changeset
26 ## @deftypefnx {} {} 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
27 ## 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
28 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## 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
30 ## 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
31 ## 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
32 ## @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
33 ##
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
34 ## Examples:
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
35 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
36 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
37 ## 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
38 ## @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
39 ##
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, 4)
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.5
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 ## 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
44 ## @result{} s =
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
45 ## 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
46 ## 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
47 ## whos s
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
48 ## @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
49 ## 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
50 ## ==== ==== ==== ===== =====
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
51 ## 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
52 ## 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
53 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
54 ## 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
55 ## @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
56 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
57 ##
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
58 ## 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
59 ## 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
60 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
61 ## 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
62 ##
16816
12005245b645 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 16724
diff changeset
63 ## 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
64 ## the string.
576daea679fe num2str.m: Clarify in docstring that leading spaces will be trimmed (bug #38463)
Rik <rik@octave.org>
parents: 15732
diff changeset
65 ##
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
66 ## 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
67 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
68 ## 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
69 ## 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
70 ## unpredictable.
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
71 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
72 ## 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
73 ## 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
74 ## @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
75 ## @seealso{sprintf, int2str, mat2str}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
76 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
77
4229
40153a2affd6 [project @ 2002-12-18 20:37:32 by jwe]
jwe
parents: 3408
diff changeset
78 function retval = num2str (x, arg)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
79
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
80 if (nargin != 1 && nargin != 2)
5947
009fa69b6182 [project @ 2006-08-21 16:15:20 by jwe]
jwe
parents: 5939
diff changeset
81 print_usage ();
19700
00e31f316a3a Fix Matlab incompatibility of "ismatrix" (bug #42422).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 19697
diff changeset
82 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
83 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
84 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
85
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5355
diff changeset
86 if (ischar (x))
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
87 retval = x;
5947
009fa69b6182 [project @ 2006-08-21 16:15:20 by jwe]
jwe
parents: 5939
diff changeset
88 elseif (isempty (x))
009fa69b6182 [project @ 2006-08-21 16:15:20 by jwe]
jwe
parents: 5939
diff changeset
89 retval = "";
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
90 elseif (isreal (x))
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
91 if (nargin == 2)
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
92 if (ischar (arg))
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
93 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
94 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
95 fmt = sprintf ("%%%d.%dg", arg+7, arg);
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
96 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
97 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
98 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
99 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
100 if (isnumeric (x))
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
101 ## 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
102 valid = isfinite (x(:));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
103 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
104 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
105 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
106 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
107
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
108 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
109 ## Floating point input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
110 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
111 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
112 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
113 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
114 ## Integer input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
115 ndgt += 3;
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
116 if (any (! valid))
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); # 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
118 endif
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
119 fmt = sprintf ("%%%d.0f", ndgt);
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
120 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
121 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
122 ## Logical input
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
123 fmt = "%3d";
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
124 endif
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
125 endif
20195
ea5fdb2ae637 num2str.m: Correctly parse newlines in single-quoted format strings (bug #44684).
Rik <rik@octave.org>
parents: 20158
diff changeset
126 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
127 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
128 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
129 x = permute (x, [2, 3:nd, 1]);
20266
f357e076776f num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20195
diff changeset
130 if (! (sum (fmt == "%") > 1 || any (strcmp (fmt, {"%s", "%c"}))))
f357e076776f num2str.m: Fixed two Matlab compatibility issues (bug #45174, bug #44864).
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 20195
diff changeset
131 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
132 endif
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
133 strtmp = sprintf (fmt, x);
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
134 retval = 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
135 else # Complex matrix input
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
136 if (nargin == 2)
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5355
diff changeset
137 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
138 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
139 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
140 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
141 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
142 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
143 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
144 else
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
145 ## 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
146 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
147 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
148 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
149 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
150 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
151 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
152 endif
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
153
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
154 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
155 ## Floating point input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
156 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
157 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
158 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
159 else
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
160 ## Integer input
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 21003
diff changeset
161 ndgt += 3;
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22470
diff changeset
162 ## 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
163 ## 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
164 fmt = sprintf ("%%%d.0f%%-+%d.0fi", ndgt, ndgt);
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
165 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
166 endif
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
167
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
168 ## 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
169 ## 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
170 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
171 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
172 idx = repmat ({':'}, nd, 1);
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
173 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
174 perm(2:2:2*nc) = nc + (1:nc);
4914
1c0442da75fd [project @ 2004-07-23 16:55:13 by jwe]
jwe
parents: 4911
diff changeset
175 idx{2} = perm;
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
176 x = horzcat (real (x), imag (x));
4914
1c0442da75fd [project @ 2004-07-23 16:55:13 by jwe]
jwe
parents: 4911
diff changeset
177 x = x(idx{:});
21076
b433f9990452 strip trailing whitespace from files
John W. Eaton <jwe@octave.org>
parents: 21057
diff changeset
178
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
179 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
180 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
181
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
182 ## 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
183 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
184 tmp = regexprep (tmp, "( +)i", "i$1");
4878
4eaf35cfdb11 [project @ 2004-04-23 15:42:10 by jwe]
jwe
parents: 4691
diff changeset
185
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
186 retval = strtrim (char (ostrsplit (tmp(1:end-1), "\n")));
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
187 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
188
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
189 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
190
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
191
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
192 ## Basic tests
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
193 %!assert (num2str (123), "123")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
194 %!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
195 %!assert (num2str (123.456, 4), "123.5")
21562
6c2fd62db1f7 maint: Eliminate accidental double spaces in code.
Rik <rik@octave.org>
parents: 21317
diff changeset
196 %!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
197 %!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
198 %!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
199
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
200 ## Exceptional values
14537
f8e041f11b18 num2str.m: Update function to address bug #36117 and bug #36121.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
201 %!assert (num2str (19440606), "19440606")
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
202 %!assert (num2str (2^33), "8589934592")
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
203 %!assert (num2str (-2^33), "-8589934592")
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
204 %!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
205 %!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
206 %!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
207 %!assert (num2str (inf), "Inf")
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
208 %!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
209 %!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
210 %!assert (num2str ([complex(Inf,0), complex(0,-Inf)]), "Inf+0i 0-Infi")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
211 %!assert (num2str (complex(Inf,1)), "Inf+1i")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
212 %!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
213 %!assert (num2str (nan), "NaN")
17292
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
214 %!assert (num2str (complex (NaN, 1)), "NaN+1i")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
215 %!assert (num2str (complex (1, NaN)), "1+NaNi")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
216 %!assert (num2str (NA), "NA")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
217 %!assert (num2str (complex (NA, 1)), "NA+1i")
c5073ed27cdc Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents: 16994
diff changeset
218 %!assert (num2str (complex (1, NA)), "1+NAi")
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
219
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
220 ## 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
221 %!shared m, x
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
222 %! m = magic (3);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
223 %! 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
224
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
225 ## real case
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
226 %!test <*46770>
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
227 %! y = num2str (x);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
228 %! 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
229 %! 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
230 %! "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
231 %! "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
232
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
233 ## complex case
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
234 %!test <*46770>
21003
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
235 %! 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
236 %! y = num2str (x);
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
237 %! 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
238 %! 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
239 %! "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
240 %! "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
241
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
242 ## Clear shared variables
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
243 %!shared
b51078f07886 num2str.m: Expand ND-arrays in columns for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
244
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
245 ## 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
246 %!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
247 %!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
248 ## Even exact integers in IEEE notation should use exponential notation
25603
e55fb9685803 num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133)
Rik <rik@octave.org>
parents: 25602
diff changeset
249 %!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
250 ## 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
251 %!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
252 %! "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
253
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
254 ## 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
255 %!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
256
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22470
diff changeset
257 ## 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
258 %!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
259
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
260 ## 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
261 ## 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
262 %!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
263
25654
6541a0de38e1 num2str.m: Add new BIST tests and comments explaining tests.
Rik <rik@octave.org>
parents: 25603
diff changeset
264 ## Test input validation
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
265 %!error num2str ()
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
266 %!error num2str (1, 2, 3)
14538
34c932e669c8 num2str.m: Redraft code for better Matlab compatibility.
Rik <octave@nomad.inbox5.com>
parents: 14537
diff changeset
267 %!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
268 %!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
269 %!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
270 %!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
271 %!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
272 %!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
273 %!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
274 %!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
275 %!error <PRECISION must be a scalar integer .= 0> num2str (1+1i, 1.5)