-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum_to_str.m
64 lines (62 loc) · 1.91 KB
/
num_to_str.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
%% Copyright (c) 2019-2019, Yichao Yu <[email protected]>
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 3.0 of the License, or (at your option) any later version.
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
% You should have received a copy of the GNU Lesser General Public
% License along with this library.
function str = num_to_str(num)
if isinteger(num) || islogical(num)
str = sprintf('%d', num);
return;
end
if ~isfinite(num)
if isnan(num)
str = 'nan';
elseif num > 0
str = 'inf';
else
str = '-inf';
end
return;
elseif num == 0
% Ignore signed zero for now...
str = '0';
return;
end
assert(isnumeric(num));
% This is very inefficient but I don't really care...
% Don't really want to implement any fancy algorithms in matlab...
function [ok, str] = try_format(fmt, ndig)
str = sprintf(sprintf('%%.%d%s', ndig, fmt), num);
ok = sscanf(str, '%f') == num;
end
anum = abs(num);
if anum >= 1e6 || anum < 1e-4
for i = 0:20
[ok, str] = try_format('e', i);
if ok
break;
end
end
% This is stupid but whatever........
if anum > 1
str = strrep(str, 'e+', 'e');
str = strrep(str, 'e0', 'e');
else
str = strrep(str, 'e-0', 'e-');
end
else
for i = 0:20
[ok, str] = try_format('f', i);
if ok
break;
end
end
end
end