3 <!-- Created by texi2html 1.56k from cln.texi on 28 August 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><cln/io.h></CODE> defines a type <CODE>cl_istream</CODE>, which is
122 the type of the first argument to all input functions. <CODE>cl_istream</CODE>
123 is the same as <CODE>std::istream&</CODE>.
132 <CODE>cl_istream stdin</CODE>
136 contains the standard input stream.
140 These are the simple input functions:
145 <DT><CODE>int freadchar (cl_istream stream)</CODE>
147 Reads a character from <CODE>stream</CODE>. Returns <CODE>cl_EOF</CODE> (not a <SAMP>`char'</SAMP>!)
148 if the end of stream was encountered or an error occurred.
150 <DT><CODE>int funreadchar (cl_istream stream, int c)</CODE>
152 Puts back <CODE>c</CODE> onto <CODE>stream</CODE>. <CODE>c</CODE> must be the result of the
153 last <CODE>freadchar</CODE> operation on <CODE>stream</CODE>.
157 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
158 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
159 defines, in <CODE><cln/<VAR>type</VAR>_io.h></CODE>, the following input function:
164 <DT><CODE>cl_istream operator>> (cl_istream stream, <VAR>type</VAR>& result)</CODE>
166 Reads a number from <CODE>stream</CODE> and stores it in the <CODE>result</CODE>.
170 The most flexible input functions, defined in <CODE><cln/<VAR>type</VAR>_io.h></CODE>,
176 <DT><CODE>cl_N read_complex (cl_istream stream, const cl_read_flags& flags)</CODE>
178 <DT><CODE>cl_R read_real (cl_istream stream, const cl_read_flags& flags)</CODE>
180 <DT><CODE>cl_F read_float (cl_istream stream, const cl_read_flags& flags)</CODE>
182 <DT><CODE>cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)</CODE>
184 <DT><CODE>cl_I read_integer (cl_istream stream, const cl_read_flags& flags)</CODE>
186 Reads a number from <CODE>stream</CODE>. The <CODE>flags</CODE> are parameters which
187 affect the input syntax. Whitespace before the number is silently skipped.
189 <DT><CODE>cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
191 <DT><CODE>cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
193 <DT><CODE>cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
195 <DT><CODE>cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
197 <DT><CODE>cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
199 Reads a number from a string in memory. The <CODE>flags</CODE> are parameters which
200 affect the input syntax. The string starts at <CODE>string</CODE> and ends at
201 <CODE>string_limit</CODE> (exclusive limit). <CODE>string_limit</CODE> may also be
202 <CODE>NULL</CODE>, denoting the entire string, i.e. equivalent to
203 <CODE>string_limit = string + strlen(string)</CODE>. If <CODE>end_of_parse</CODE> is
204 <CODE>NULL</CODE>, the string in memory must contain exactly one number and nothing
205 more, else a fatal error will be signalled. If <CODE>end_of_parse</CODE>
206 is not <CODE>NULL</CODE>, <CODE>*end_of_parse</CODE> will be assigned a pointer past
207 the last parsed character (i.e. <CODE>string_limit</CODE> if nothing came after
208 the number). Whitespace is not allowed.
212 The structure <CODE>cl_read_flags</CODE> contains the following fields:
217 <DT><CODE>cl_read_syntax_t syntax</CODE>
219 The possible results of the read operation. Possible values are
220 <CODE>syntax_number</CODE>, <CODE>syntax_real</CODE>, <CODE>syntax_rational</CODE>,
221 <CODE>syntax_integer</CODE>, <CODE>syntax_float</CODE>, <CODE>syntax_sfloat</CODE>,
222 <CODE>syntax_ffloat</CODE>, <CODE>syntax_dfloat</CODE>, <CODE>syntax_lfloat</CODE>.
224 <DT><CODE>cl_read_lsyntax_t lsyntax</CODE>
226 Specifies the language-dependent syntax variant for the read operation.
231 <DT><CODE>lsyntax_standard</CODE>
233 accept standard algebraic notation only, no complex numbers,
234 <DT><CODE>lsyntax_algebraic</CODE>
236 accept the algebraic notation <CODE><VAR>x</VAR>+<VAR>y</VAR>i</CODE> for complex numbers,
237 <DT><CODE>lsyntax_commonlisp</CODE>
239 accept the <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> syntaxes for binary, octal,
241 <CODE>#<VAR>base</VAR>R</CODE> for rational numbers in a given base,
242 <CODE>#c(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE> for complex numbers,
243 <DT><CODE>lsyntax_all</CODE>
245 accept all of these extensions.
248 <DT><CODE>unsigned int rational_base</CODE>
250 The base in which rational numbers are read.
252 <DT><CODE>cl_float_format_t float_flags.default_float_format</CODE>
254 The float format used when reading floats with exponent marker <SAMP>`e'</SAMP>.
256 <DT><CODE>cl_float_format_t float_flags.default_lfloat_format</CODE>
258 The float format used when reading floats with exponent marker <SAMP>`l'</SAMP>.
260 <DT><CODE>cl_boolean float_flags.mantissa_dependent_float_format</CODE>
262 When this flag is true, floats specified with more digits than corresponding
263 to the exponent marker they contain, but without <VAR>_nnn</VAR> suffix, will get a
264 precision corresponding to their number of significant digits.
269 <H2><A NAME="SEC47" HREF="cln_toc.html#TOC47">5.3 Output functions</A></H2>
272 Including <CODE><cln/io.h></CODE> defines a type <CODE>cl_ostream</CODE>, which is
273 the type of the first argument to all output functions. <CODE>cl_ostream</CODE>
274 is the same as <CODE>std::ostream&</CODE>.
283 <CODE>cl_ostream stdout</CODE>
287 contains the standard output stream.
296 <CODE>cl_ostream stderr</CODE>
300 contains the standard error output stream.
304 These are the simple output functions:
309 <DT><CODE>void fprintchar (cl_ostream stream, char c)</CODE>
311 Prints the character <CODE>x</CODE> literally on the <CODE>stream</CODE>.
313 <DT><CODE>void fprint (cl_ostream stream, const char * string)</CODE>
315 Prints the <CODE>string</CODE> literally on the <CODE>stream</CODE>.
317 <DT><CODE>void fprintdecimal (cl_ostream stream, int x)</CODE>
319 <DT><CODE>void fprintdecimal (cl_ostream stream, const cl_I& x)</CODE>
321 Prints the integer <CODE>x</CODE> in decimal on the <CODE>stream</CODE>.
323 <DT><CODE>void fprintbinary (cl_ostream stream, const cl_I& x)</CODE>
325 Prints the integer <CODE>x</CODE> in binary (base 2, without prefix)
326 on the <CODE>stream</CODE>.
328 <DT><CODE>void fprintoctal (cl_ostream stream, const cl_I& x)</CODE>
330 Prints the integer <CODE>x</CODE> in octal (base 8, without prefix)
331 on the <CODE>stream</CODE>.
333 <DT><CODE>void fprinthexadecimal (cl_ostream stream, const cl_I& x)</CODE>
335 Prints the integer <CODE>x</CODE> in hexadecimal (base 16, without prefix)
336 on the <CODE>stream</CODE>.
340 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
341 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
342 defines, in <CODE><cln/<VAR>type</VAR>_io.h></CODE>, the following output functions:
347 <DT><CODE>void fprint (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
349 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
351 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
352 on the global printer settings in the variable <CODE>default_print_flags</CODE>.
353 The <CODE>ostream</CODE> flags and settings (flags, width and locale) are
358 The most flexible output function, defined in <CODE><cln/<VAR>type</VAR>_io.h></CODE>,
362 void print_complex (cl_ostream stream, const cl_print_flags& flags,
364 void print_real (cl_ostream stream, const cl_print_flags& flags,
366 void print_float (cl_ostream stream, const cl_print_flags& flags,
368 void print_rational (cl_ostream stream, const cl_print_flags& flags,
370 void print_integer (cl_ostream stream, const cl_print_flags& flags,
375 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The <CODE>flags</CODE> are
376 parameters which affect the output.
380 The structure type <CODE>cl_print_flags</CODE> contains the following fields:
385 <DT><CODE>unsigned int rational_base</CODE>
387 The base in which rational numbers are printed. Default is <CODE>10</CODE>.
389 <DT><CODE>cl_boolean rational_readably</CODE>
391 If this flag is true, rational numbers are printed with radix specifiers in
392 Common Lisp syntax (<CODE>#<VAR>n</VAR>R</CODE> or <CODE>#b</CODE> or <CODE>#o</CODE> or <CODE>#x</CODE>
393 prefixes, trailing dot). Default is false.
395 <DT><CODE>cl_boolean float_readably</CODE>
397 If this flag is true, type specific exponent markers have precedence over 'E'.
400 <DT><CODE>cl_float_format_t default_float_format</CODE>
402 Floating point numbers of this format will be printed using the 'E' exponent
403 marker. Default is <CODE>cl_float_format_ffloat</CODE>.
405 <DT><CODE>cl_boolean complex_readably</CODE>
407 If this flag is true, complex numbers will be printed using the Common Lisp
408 syntax <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>. Default is false.
410 <DT><CODE>cl_string univpoly_varname</CODE>
412 Univariate polynomials with no explicit indeterminate name will be printed
413 using this variable name. Default is <CODE>"x"</CODE>.
417 The global variable <CODE>default_print_flags</CODE> contains the default values,
418 used by the function <CODE>fprint</CODE>.
422 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>.