3 <!-- Created by texi2html 1.56k from cln.texi on 5 May 2000 -->
5 <TITLE>CLN, a Class Library for Numbers - 5. Input/Output</TITLE>
8 Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_4.html">previous</A>, <A HREF="cln_6.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
12 <H1><A NAME="SEC44" HREF="cln_toc.html#TOC44">5. Input/Output</A></H1>
19 <H2><A NAME="SEC45" HREF="cln_toc.html#TOC45">5.1 Internal and printed representation</A></H2>
25 All computations deal with the internal representations of the numbers.
29 Every number has an external representation as a sequence of ASCII characters.
30 Several external representations may denote the same number, for example,
35 Converting an internal to an external representation is called "printing",
37 converting an external to an internal representation is called "reading".
39 In CLN, it is always true that conversion of an internal to an external
40 representation and then back to an internal representation will yield the
41 same internal representation. Symbolically: <CODE>read(print(x)) == x</CODE>.
42 This is called "print-read consistency".
46 Different types of numbers have different external representations (case
54 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+. The reader also accepts the
55 Common Lisp syntaxes <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>.</CODE> with a trailing dot
57 and the <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes.
61 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>/</CODE>{<VAR>digit</VAR>}+.
62 The <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes are allowed
65 <DT>Floating-point numbers
67 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
68 <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
69 <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}+. A precision specifier
70 of the form _<VAR>prec</VAR> may be appended. There must be at least
71 one digit in the non-exponent part. The exponent has the syntax
72 <VAR>expmarker</VAR> <VAR>expsign</VAR> {<VAR>digit</VAR>}+.
73 The exponent marker is
79 <SAMP>`s'</SAMP> for short-floats,
82 <SAMP>`f'</SAMP> for single-floats,
85 <SAMP>`d'</SAMP> for double-floats,
88 <SAMP>`L'</SAMP> for long-floats,
91 or <SAMP>`e'</SAMP>, which denotes a default float format. The precision specifying
92 suffix has the syntax _<VAR>prec</VAR> where <VAR>prec</VAR> denotes the number of
93 valid mantissa digits (in decimal, excluding leading zeroes), cf. also
94 function <SAMP>`cl_float_format'</SAMP>.
98 External representation:
103 In algebraic notation: <CODE><VAR>realpart</VAR>+<VAR>imagpart</VAR>i</CODE>. Of course,
104 if <VAR>imagpart</VAR> is negative, its printed representation begins with
105 a <SAMP>`-'</SAMP>, and the <SAMP>`+'</SAMP> between <VAR>realpart</VAR> and <VAR>imagpart</VAR>
106 may be omitted. Note that this notation cannot be used when the <VAR>imagpart</VAR>
107 is rational and the rational number's base is >18, because the <SAMP>`i'</SAMP>
108 is then read as a digit.
111 In Common Lisp notation: <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>.
118 <H2><A NAME="SEC46" HREF="cln_toc.html#TOC46">5.2 Input functions</A></H2>
121 Including <CODE><cl_io.h></CODE> defines a type <CODE>cl_istream</CODE>, which is
122 the type of the first argument to all input functions. Unless you build
123 and use CLN with the macro CL_IO_STDIO being defined, <CODE>cl_istream</CODE>
124 is the same as <CODE>istream&</CODE>.
133 <CODE>cl_istream cl_stdin</CODE>
137 contains the standard input stream.
141 These are the simple input functions:
146 <DT><CODE>int freadchar (cl_istream stream)</CODE>
148 Reads a character from <CODE>stream</CODE>. Returns <CODE>cl_EOF</CODE> (not a <SAMP>`char'</SAMP>!)
149 if the end of stream was encountered or an error occurred.
151 <DT><CODE>int funreadchar (cl_istream stream, int c)</CODE>
153 Puts back <CODE>c</CODE> onto <CODE>stream</CODE>. <CODE>c</CODE> must be the result of the
154 last <CODE>freadchar</CODE> operation on <CODE>stream</CODE>.
158 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
159 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
160 defines, in <CODE><cl_<VAR>type</VAR>_io.h></CODE>, the following input function:
165 <DT><CODE>cl_istream operator>> (cl_istream stream, <VAR>type</VAR>& result)</CODE>
167 Reads a number from <CODE>stream</CODE> and stores it in the <CODE>result</CODE>.
171 The most flexible input functions, defined in <CODE><cl_<VAR>type</VAR>_io.h></CODE>,
177 <DT><CODE>cl_N read_complex (cl_istream stream, const cl_read_flags& flags)</CODE>
179 <DT><CODE>cl_R read_real (cl_istream stream, const cl_read_flags& flags)</CODE>
181 <DT><CODE>cl_F read_float (cl_istream stream, const cl_read_flags& flags)</CODE>
183 <DT><CODE>cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)</CODE>
185 <DT><CODE>cl_I read_integer (cl_istream stream, const cl_read_flags& flags)</CODE>
187 Reads a number from <CODE>stream</CODE>. The <CODE>flags</CODE> are parameters which
188 affect the input syntax. Whitespace before the number is silently skipped.
190 <DT><CODE>cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
192 <DT><CODE>cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
194 <DT><CODE>cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
196 <DT><CODE>cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
198 <DT><CODE>cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
200 Reads a number from a string in memory. The <CODE>flags</CODE> are parameters which
201 affect the input syntax. The string starts at <CODE>string</CODE> and ends at
202 <CODE>string_limit</CODE> (exclusive limit). <CODE>string_limit</CODE> may also be
203 <CODE>NULL</CODE>, denoting the entire string, i.e. equivalent to
204 <CODE>string_limit = string + strlen(string)</CODE>. If <CODE>end_of_parse</CODE> is
205 <CODE>NULL</CODE>, the string in memory must contain exactly one number and nothing
206 more, else a fatal error will be signalled. If <CODE>end_of_parse</CODE>
207 is not <CODE>NULL</CODE>, <CODE>*end_of_parse</CODE> will be assigned a pointer past
208 the last parsed character (i.e. <CODE>string_limit</CODE> if nothing came after
209 the number). Whitespace is not allowed.
213 The structure <CODE>cl_read_flags</CODE> contains the following fields:
218 <DT><CODE>cl_read_syntax_t syntax</CODE>
220 The possible results of the read operation. Possible values are
221 <CODE>syntax_number</CODE>, <CODE>syntax_real</CODE>, <CODE>syntax_rational</CODE>,
222 <CODE>syntax_integer</CODE>, <CODE>syntax_float</CODE>, <CODE>syntax_sfloat</CODE>,
223 <CODE>syntax_ffloat</CODE>, <CODE>syntax_dfloat</CODE>, <CODE>syntax_lfloat</CODE>.
225 <DT><CODE>cl_read_lsyntax_t lsyntax</CODE>
227 Specifies the language-dependent syntax variant for the read operation.
232 <DT><CODE>lsyntax_standard</CODE>
234 accept standard algebraic notation only, no complex numbers,
235 <DT><CODE>lsyntax_algebraic</CODE>
237 accept the algebraic notation <CODE><VAR>x</VAR>+<VAR>y</VAR>i</CODE> for complex numbers,
238 <DT><CODE>lsyntax_commonlisp</CODE>
240 accept the <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> syntaxes for binary, octal,
242 <CODE>#<VAR>base</VAR>R</CODE> for rational numbers in a given base,
243 <CODE>#c(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE> for complex numbers,
244 <DT><CODE>lsyntax_all</CODE>
246 accept all of these extensions.
249 <DT><CODE>unsigned int rational_base</CODE>
251 The base in which rational numbers are read.
253 <DT><CODE>cl_float_format_t float_flags.default_float_format</CODE>
255 The float format used when reading floats with exponent marker <SAMP>`e'</SAMP>.
257 <DT><CODE>cl_float_format_t float_flags.default_lfloat_format</CODE>
259 The float format used when reading floats with exponent marker <SAMP>`l'</SAMP>.
261 <DT><CODE>cl_boolean float_flags.mantissa_dependent_float_format</CODE>
263 When this flag is true, floats specified with more digits than corresponding
264 to the exponent marker they contain, but without <VAR>_nnn</VAR> suffix, will get a
265 precision corresponding to their number of significant digits.
270 <H2><A NAME="SEC47" HREF="cln_toc.html#TOC47">5.3 Output functions</A></H2>
273 Including <CODE><cl_io.h></CODE> defines a type <CODE>cl_ostream</CODE>, which is
274 the type of the first argument to all output functions. Unless you build
275 and use CLN with the macro CL_IO_STDIO being defined, <CODE>cl_ostream</CODE>
276 is the same as <CODE>ostream&</CODE>.
285 <CODE>cl_ostream cl_stdout</CODE>
289 contains the standard output stream.
298 <CODE>cl_ostream cl_stderr</CODE>
302 contains the standard error output stream.
306 These are the simple output functions:
311 <DT><CODE>void fprintchar (cl_ostream stream, char c)</CODE>
313 Prints the character <CODE>x</CODE> literally on the <CODE>stream</CODE>.
315 <DT><CODE>void fprint (cl_ostream stream, const char * string)</CODE>
317 Prints the <CODE>string</CODE> literally on the <CODE>stream</CODE>.
319 <DT><CODE>void fprintdecimal (cl_ostream stream, int x)</CODE>
321 <DT><CODE>void fprintdecimal (cl_ostream stream, const cl_I& x)</CODE>
323 Prints the integer <CODE>x</CODE> in decimal on the <CODE>stream</CODE>.
325 <DT><CODE>void fprintbinary (cl_ostream stream, const cl_I& x)</CODE>
327 Prints the integer <CODE>x</CODE> in binary (base 2, without prefix)
328 on the <CODE>stream</CODE>.
330 <DT><CODE>void fprintoctal (cl_ostream stream, const cl_I& x)</CODE>
332 Prints the integer <CODE>x</CODE> in octal (base 8, without prefix)
333 on the <CODE>stream</CODE>.
335 <DT><CODE>void fprinthexadecimal (cl_ostream stream, const cl_I& x)</CODE>
337 Prints the integer <CODE>x</CODE> in hexadecimal (base 16, without prefix)
338 on the <CODE>stream</CODE>.
342 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
343 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
344 defines, in <CODE><cl_<VAR>type</VAR>_io.h></CODE>, the following output functions:
349 <DT><CODE>void fprint (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
351 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
353 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
354 on the global printer settings in the variable <CODE>cl_default_print_flags</CODE>.
355 The <CODE>ostream</CODE> flags and settings (flags, width and locale) are
360 The most flexible output function, defined in <CODE><cl_<VAR>type</VAR>_io.h></CODE>,
364 void print_complex (cl_ostream stream, const cl_print_flags& flags,
366 void print_real (cl_ostream stream, const cl_print_flags& flags,
368 void print_float (cl_ostream stream, const cl_print_flags& flags,
370 void print_rational (cl_ostream stream, const cl_print_flags& flags,
372 void print_integer (cl_ostream stream, const cl_print_flags& flags,
377 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The <CODE>flags</CODE> are
378 parameters which affect the output.
382 The structure type <CODE>cl_print_flags</CODE> contains the following fields:
387 <DT><CODE>unsigned int rational_base</CODE>
389 The base in which rational numbers are printed. Default is <CODE>10</CODE>.
391 <DT><CODE>cl_boolean rational_readably</CODE>
393 If this flag is true, rational numbers are printed with radix specifiers in
394 Common Lisp syntax (<CODE>#<VAR>n</VAR>R</CODE> or <CODE>#b</CODE> or <CODE>#o</CODE> or <CODE>#x</CODE>
395 prefixes, trailing dot). Default is false.
397 <DT><CODE>cl_boolean float_readably</CODE>
399 If this flag is true, type specific exponent markers have precedence over 'E'.
402 <DT><CODE>cl_float_format_t default_float_format</CODE>
404 Floating point numbers of this format will be printed using the 'E' exponent
405 marker. Default is <CODE>cl_float_format_ffloat</CODE>.
407 <DT><CODE>cl_boolean complex_readably</CODE>
409 If this flag is true, complex numbers will be printed using the Common Lisp
410 syntax <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>. Default is false.
412 <DT><CODE>cl_string univpoly_varname</CODE>
414 Univariate polynomials with no explicit indeterminate name will be printed
415 using this variable name. Default is <CODE>"x"</CODE>.
419 The global variable <CODE>cl_default_print_flags</CODE> contains the default values,
420 used by the function <CODE>fprint</CODE>.
424 Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_4.html">previous</A>, <A HREF="cln_6.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.