1% STK_SPRINTF_COLVECT_FIXEDPOINT ...
2
3% Copyright Notice
4%
5%    Copyright (C) 2013, 2014 SUPELEC
6%
7%    Author:  Julien Bect  <julien.bect@centralesupelec.fr>
8
9% Copying Permission Statement
10%
11%    This file is part of
12%
13%            STK: a Small (Matlab/Octave) Toolbox for Kriging
14%               (http://sourceforge.net/projects/kriging)
15%
16%    STK is free software: you can redistribute it and/or modify it under
17%    the terms of the GNU General Public License as published by the Free
18%    Software Foundation,  either version 3  of the License, or  (at your
19%    option) any later version.
20%
21%    STK is distributed  in the hope that it will  be useful, but WITHOUT
22%    ANY WARRANTY;  without even the implied  warranty of MERCHANTABILITY
23%    or FITNESS  FOR A  PARTICULAR PURPOSE.  See  the GNU  General Public
24%    License for more details.
25%
26%    You should  have received a copy  of the GNU  General Public License
27%    along with STK.  If not, see <http://www.gnu.org/licenses/>.
28
29function [str, err] = stk_sprintf_colvect_fixedpoint(x, max_width)
30
31if isempty(x),
32    str = '';
33    err = 0;
34    return;
35end
36
37if nargin < 2,
38    max_width = 8;
39end
40
41% turn x into a column vector
42x = double(x);
43x = x(:);
44
45% get rid of infinities
46is_inf = isinf (x);
47is_pos = (x > 0);
48is_pinf = is_inf & is_pos;
49is_minf = is_inf & (~ is_pos);
50x(is_inf) = 0.0;
51
52% get rid of negative zeros
53is_zero = (x == 0);
54x(is_zero) = 0.0;
55
56% Is there any negative element ?
57any_negative = any(x < 0);
58
59% Start without decimal part
60ax = abs(x);
61n1 = max(1, floor(log10(max(ax))) + 1);
62n2 = 0;
63n3 = any_negative;
64
65% Abort this is already too long
66if (n1 + n2 + n3) > max_width
67    str = repmat('#', length(x), max_width);
68    err = +inf;
69    return;
70end
71
72% Otherwise, this is our current best solution
73best_err = max(abs(fix(x) - x));
74best_n2  = 0;
75
76% Should we add a decimal part ?
77if (best_err > eps) && ((n1 + n2 + n3 + 2) <= max_width)
78    % We can add a decimal part, so let's do it...
79    while (best_err > eps) && ((n1 + n2 + n3) < max_width)
80        n2  = n2 + 1; % add one decimal
81        n3  = 1 + any_negative; % +1 for the comma
82        c   = 10 ^ (-n2);
83        xx  = (round (ax / c)) * c;
84        err = max (abs (ax - xx));
85        if err < best_err,
86            best_err = err;
87            best_n2  = n2;
88        end
89    end
90end
91err = best_err;
92n2  = best_n2;
93
94fmt = sprintf ('%%%d.%df', n1 + n2 + n3, n2);
95str = arrayfun (@(u) sprintf (fmt, u), x, 'UniformOutput', false);
96str = char (str{:});
97
98% fix infinities
99if any (is_pinf),
100    str(is_pinf, :) = [repmat(' ', 1, max_width - 3) 'Inf'];
101end
102if any (is_minf)
103    str(is_minf, :) = [repmat(' ', 1, max_width - 4) '-Inf'];
104end
105
106end % function
107
108%!shared x, s
109%! x = [1.2; 3.48];
110%!test s = stk_sprintf_colvect_fixedpoint(x, 1);
111%!assert (isequal(s, ['1'; '3']))
112%!test s = stk_sprintf_colvect_fixedpoint(x, 2);
113%!assert (isequal(s, ['1'; '3']))
114%!test s = stk_sprintf_colvect_fixedpoint(x, 3);
115%!assert (isequal(s, ['1.2'; '3.5']))
116%!test s = stk_sprintf_colvect_fixedpoint(x, 4);
117%!assert (isequal(s, ['1.20'; '3.48']))
118%!test s = stk_sprintf_colvect_fixedpoint(x, 5);
119%!assert (isequal(s, ['1.20'; '3.48']))
120
121%!shared x, s
122%! x = [1.2; -3.48];
123%!test s = stk_sprintf_colvect_fixedpoint(x, 1);
124%!assert (isequal(s, ['#'; '#']))
125%!test s = stk_sprintf_colvect_fixedpoint(x, 2);
126%!assert (isequal(s, [' 1'; '-3']))
127%!test s = stk_sprintf_colvect_fixedpoint(x, 3);
128%!assert (isequal(s, [' 1'; '-3']))
129%!test s = stk_sprintf_colvect_fixedpoint(x, 4);
130%!assert (isequal(s, [' 1.2'; '-3.5']))
131%!test s = stk_sprintf_colvect_fixedpoint(x, 5);
132%!assert (isequal(s, [' 1.20'; '-3.48']))
133%!test s = stk_sprintf_colvect_fixedpoint(x, 6);
134%!assert (isequal(s, [' 1.20'; '-3.48']))
135
136%!shared x, s
137%! x = [0.2; 0.48];
138%!test s = stk_sprintf_colvect_fixedpoint(x, 1);
139%!assert (isequal(s, ['0'; '0']))
140%!test s = stk_sprintf_colvect_fixedpoint(x, 2);
141%!assert (isequal(s, ['0'; '0']))
142%!test s = stk_sprintf_colvect_fixedpoint(x, 3);
143%!assert (isequal(s, ['0.2'; '0.5']))
144%!test s = stk_sprintf_colvect_fixedpoint(x, 4);
145%!assert (isequal(s, ['0.20'; '0.48']))
146%!test s = stk_sprintf_colvect_fixedpoint(x, 5);
147%!assert (isequal(s, ['0.20'; '0.48']))
148