1## Copyright 2018 Oliver Heimlich
2##
3## This program is free software; you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation; either version 3 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; if not, see <http://www.gnu.org/licenses/>.
15
16## -*- texinfo -*-
17## @documentencoding UTF-8
18## @defmethod {@@infsup} fprintf (@var{fid}, @var{template}, @var{X})
19## @defmethodx {@@infsup} fprintf (@var{template}, @var{X})
20##
21## Write interval @var{X} under the control of a template string
22## @var{template} to the file descriptor @code{fid} and return the number of
23## characters printed.
24##
25## If @var{fid} is omitted, the output is written to @code{stdout} making the
26## function exactly equivalent to @command{printf}.
27##
28## See @command{help intervaltotext} for the syntax of the template string.
29##
30## @example
31## @group
32## fprintf ("The result lies within the box %[4g].\n", infsup (2, 3))
33##   @result{} The result lies within the box [   2,    3].
34## @end group
35## @end example
36##
37## @seealso{intervaltotext, @@infsup/printf, @@infsup/sprintf}
38## @end defmethod
39
40## Author: Oliver Heimlich
41## Keywords: interval
42## Created: 2018-06-30
43
44function chars_printed = fprintf (fid, template, x)
45
46  if (nargin < 2 || nargin > 3)
47    print_usage ();
48    return
49  endif
50
51  if (nargin < 3)
52    [fid, template, x] = deal (stdout, fid, template);
53  endif
54
55  if (isa (fid, "infsup"))
56    error ("handle FID must be an output stream");
57  endif
58
59  [template, literals] = printf_prepare (template, x);
60
61  if (nargout >= 1)
62    chars_printed = fprintf (fid, template, literals{:});
63  else
64    fprintf (fid, template, literals{:});
65  endif
66
67endfunction
68
69%!test
70%! if (compare_versions (OCTAVE_VERSION, "4.2", ">="))
71%!   assert (evalc ("n = fprintf ('%g', infsup ('pi'));"), "3.14159 3.1416");
72%!   assert (n, 14);
73%! endif
74