annotate scripts/general/int2str.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +0200
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: 23220
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: 23220
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: 22323
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: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
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: 7005
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: 23220
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} =} int2str (@var{n})
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
28 ## Convert an integer (or array of integers) to a string (or a character
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
29 ## array).
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
30 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
31 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
32 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
33 ## int2str (123)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
34 ## @result{} 123
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 ## s = int2str ([1, 2, 3; 4, 5, 6])
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
37 ## @result{} s =
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
38 ## 1 2 3
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
39 ## 4 5 6
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
40 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
41 ## whos s
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
42 ## @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
43 ## 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
44 ## ==== ==== ==== ===== =====
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
45 ## s 2x7 14 char
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
46 ## Total is 14 elements using 14 bytes
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
47 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
48 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
49 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
50 ## This function is not very flexible. For better control over the
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
51 ## results, use @code{sprintf} (@pxref{Formatted Output}).
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
52 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
53 ## Programming Notes:
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
54 ##
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
55 ## Non-integers are rounded to integers before display. Only the real part
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
56 ## of complex numbers is displayed.
21076
b433f9990452 strip trailing whitespace from files
John W. Eaton <jwe@octave.org>
parents: 21057
diff changeset
57 ##
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7540
diff changeset
58 ## @seealso{sprintf, num2str, mat2str}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 2847
diff changeset
59 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
60
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
61 function str = int2str (n)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
62
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
63 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
64 print_usage ();
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
65 elseif (! (isnumeric (n) || islogical (n) || ischar (n)))
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
66 error ("int2str: N must be a numeric, logical, or character array");
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
67 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
68
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
69 if (ischar (n))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
70 str = n;
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
71 return;
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
72 elseif (isempty (n))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
73 str = "";
12780
c99714aeb008 int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
74 return;
c99714aeb008 int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
75 endif
c99714aeb008 int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
76
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14402
diff changeset
77 n = round (real (n));
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
78
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
79 ## 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: 20852
diff changeset
80 nan_inf = ! isfinite (n(:));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
81 ndgt = floor (log10 (max (abs (n(! nan_inf)))));
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
82 if (isempty (ndgt) || ndgt == -Inf)
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
83 ndgt = 0; # All Inf or all zero array
12780
c99714aeb008 int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
84 endif
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
85
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
86 ndgt += 3;
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
87 if (any (nan_inf))
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
88 ndgt = max (ndgt, 5);
4305
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
89 endif
98e65d1728a1 [project @ 2003-01-23 16:48:11 by jwe]
jwe
parents: 4303
diff changeset
90
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
91 ## FIXME: Integers should be masked to show only 16 significant digits
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
92 fmt = sprintf ("%%%d.0f", ndgt);
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
93
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
94 nd = ndims (n);
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
95 nc = columns (n) * (nd - 1); # ND-arrays are expanded in columns
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
96 n = permute (n, [2, 3:nd, 1]);
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
97 fmt = [repmat(fmt, 1, nc), "\n"];
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
98 strtmp = sprintf (fmt, n);
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
99 str = strtrim (char (ostrsplit (strtmp, "\n", true)));
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
100
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
101 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
102
12780
c99714aeb008 int2str.m: Return "" for null input []. (Bug #33524)
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
103
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
104 %!assert (int2str (123), "123")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
105 %!assert (int2str (-123), "-123")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
106 %!assert (int2str (1.2), "1")
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
107 %!assert (int2str (1.6), "2")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
108 %!assert (int2str ([1, 2, 3; 4, 5, 6]), ["1 2 3";"4 5 6"])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
109 %!assert (int2str ([]), "")
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
110
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27923
diff changeset
111 %!error <Invalid call> int2str ()
21057
b0afe1993268 Overhaul num2str.m and int2str.m for Matlab compatibility (bug #46770).
Rik <rik@octave.org>
parents: 20852
diff changeset
112 %!error <N must be a numeric> int2str ({1})