You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
14 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from cln.texi on 4 May 2000 -->
  4. <TITLE>CLN, a Class Library for Numbers - 5. Input/Output</TITLE>
  5. </HEAD>
  6. <BODY>
  7. 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>.
  8. <P><HR><P>
  9. <H1><A NAME="SEC43" HREF="cln_toc.html#TOC43">5. Input/Output</A></H1>
  10. <P>
  11. <A NAME="IDX227"></A>
  12. <H2><A NAME="SEC44" HREF="cln_toc.html#TOC44">5.1 Internal and printed representation</A></H2>
  13. <P>
  14. <A NAME="IDX228"></A>
  15. <P>
  16. All computations deal with the internal representations of the numbers.
  17. <P>
  18. Every number has an external representation as a sequence of ASCII characters.
  19. Several external representations may denote the same number, for example,
  20. "20.0" and "20.000".
  21. <P>
  22. Converting an internal to an external representation is called "printing",
  23. <A NAME="IDX229"></A>
  24. converting an external to an internal representation is called "reading".
  25. <A NAME="IDX230"></A>
  26. In CLN, it is always true that conversion of an internal to an external
  27. representation and then back to an internal representation will yield the
  28. same internal representation. Symbolically: <CODE>read(print(x)) == x</CODE>.
  29. This is called "print-read consistency".
  30. <P>
  31. Different types of numbers have different external representations (case
  32. is insignificant):
  33. <DL COMPACT>
  34. <DT>Integers
  35. <DD>
  36. External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+. The reader also accepts the
  37. Common Lisp syntaxes <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>.</CODE> with a trailing dot
  38. for decimal integers
  39. and the <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes.
  40. <DT>Rational numbers
  41. <DD>
  42. External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>/</CODE>{<VAR>digit</VAR>}+.
  43. The <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes are allowed
  44. here as well.
  45. <DT>Floating-point numbers
  46. <DD>
  47. External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
  48. <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
  49. <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}+. A precision specifier
  50. of the form _<VAR>prec</VAR> may be appended. There must be at least
  51. one digit in the non-exponent part. The exponent has the syntax
  52. <VAR>expmarker</VAR> <VAR>expsign</VAR> {<VAR>digit</VAR>}+.
  53. The exponent marker is
  54. <UL>
  55. <LI>
  56. <SAMP>`s'</SAMP> for short-floats,
  57. <LI>
  58. <SAMP>`f'</SAMP> for single-floats,
  59. <LI>
  60. <SAMP>`d'</SAMP> for double-floats,
  61. <LI>
  62. <SAMP>`L'</SAMP> for long-floats,
  63. </UL>
  64. or <SAMP>`e'</SAMP>, which denotes a default float format. The precision specifying
  65. suffix has the syntax _<VAR>prec</VAR> where <VAR>prec</VAR> denotes the number of
  66. valid mantissa digits (in decimal, excluding leading zeroes), cf. also
  67. function <SAMP>`cl_float_format'</SAMP>.
  68. <DT>Complex numbers
  69. <DD>
  70. External representation:
  71. <UL>
  72. <LI>
  73. In algebraic notation: <CODE><VAR>realpart</VAR>+<VAR>imagpart</VAR>i</CODE>. Of course,
  74. if <VAR>imagpart</VAR> is negative, its printed representation begins with
  75. a <SAMP>`-'</SAMP>, and the <SAMP>`+'</SAMP> between <VAR>realpart</VAR> and <VAR>imagpart</VAR>
  76. may be omitted. Note that this notation cannot be used when the <VAR>imagpart</VAR>
  77. is rational and the rational number's base is &#62;18, because the <SAMP>`i'</SAMP>
  78. is then read as a digit.
  79. <LI>
  80. In Common Lisp notation: <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>.
  81. </UL>
  82. </DL>
  83. <H2><A NAME="SEC45" HREF="cln_toc.html#TOC45">5.2 Input functions</A></H2>
  84. <P>
  85. Including <CODE>&#60;cl_io.h&#62;</CODE> defines a type <CODE>cl_istream</CODE>, which is
  86. the type of the first argument to all input functions. Unless you build
  87. and use CLN with the macro CL_IO_STDIO being defined, <CODE>cl_istream</CODE>
  88. is the same as <CODE>istream&#38;</CODE>.
  89. <P>
  90. The variable
  91. <UL>
  92. <LI>
  93. <CODE>cl_istream cl_stdin</CODE>
  94. </UL>
  95. <P>
  96. contains the standard input stream.
  97. <P>
  98. These are the simple input functions:
  99. <DL COMPACT>
  100. <DT><CODE>int freadchar (cl_istream stream)</CODE>
  101. <DD>
  102. Reads a character from <CODE>stream</CODE>. Returns <CODE>cl_EOF</CODE> (not a <SAMP>`char'</SAMP>!)
  103. if the end of stream was encountered or an error occurred.
  104. <DT><CODE>int funreadchar (cl_istream stream, int c)</CODE>
  105. <DD>
  106. Puts back <CODE>c</CODE> onto <CODE>stream</CODE>. <CODE>c</CODE> must be the result of the
  107. last <CODE>freadchar</CODE> operation on <CODE>stream</CODE>.
  108. </DL>
  109. <P>
  110. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  111. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  112. defines, in <CODE>&#60;cl_<VAR>type</VAR>_io.h&#62;</CODE>, the following input function:
  113. <DL COMPACT>
  114. <DT><CODE>cl_istream operator&#62;&#62; (cl_istream stream, <VAR>type</VAR>&#38; result)</CODE>
  115. <DD>
  116. Reads a number from <CODE>stream</CODE> and stores it in the <CODE>result</CODE>.
  117. </DL>
  118. <P>
  119. The most flexible input functions, defined in <CODE>&#60;cl_<VAR>type</VAR>_io.h&#62;</CODE>,
  120. are the following:
  121. <DL COMPACT>
  122. <DT><CODE>cl_N read_complex (cl_istream stream, const cl_read_flags&#38; flags)</CODE>
  123. <DD>
  124. <DT><CODE>cl_R read_real (cl_istream stream, const cl_read_flags&#38; flags)</CODE>
  125. <DD>
  126. <DT><CODE>cl_F read_float (cl_istream stream, const cl_read_flags&#38; flags)</CODE>
  127. <DD>
  128. <DT><CODE>cl_RA read_rational (cl_istream stream, const cl_read_flags&#38; flags)</CODE>
  129. <DD>
  130. <DT><CODE>cl_I read_integer (cl_istream stream, const cl_read_flags&#38; flags)</CODE>
  131. <DD>
  132. Reads a number from <CODE>stream</CODE>. The <CODE>flags</CODE> are parameters which
  133. affect the input syntax. Whitespace before the number is silently skipped.
  134. <DT><CODE>cl_N read_complex (const cl_read_flags&#38; flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
  135. <DD>
  136. <DT><CODE>cl_R read_real (const cl_read_flags&#38; flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
  137. <DD>
  138. <DT><CODE>cl_F read_float (const cl_read_flags&#38; flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
  139. <DD>
  140. <DT><CODE>cl_RA read_rational (const cl_read_flags&#38; flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
  141. <DD>
  142. <DT><CODE>cl_I read_integer (const cl_read_flags&#38; flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
  143. <DD>
  144. Reads a number from a string in memory. The <CODE>flags</CODE> are parameters which
  145. affect the input syntax. The string starts at <CODE>string</CODE> and ends at
  146. <CODE>string_limit</CODE> (exclusive limit). <CODE>string_limit</CODE> may also be
  147. <CODE>NULL</CODE>, denoting the entire string, i.e. equivalent to
  148. <CODE>string_limit = string + strlen(string)</CODE>. If <CODE>end_of_parse</CODE> is
  149. <CODE>NULL</CODE>, the string in memory must contain exactly one number and nothing
  150. more, else a fatal error will be signalled. If <CODE>end_of_parse</CODE>
  151. is not <CODE>NULL</CODE>, <CODE>*end_of_parse</CODE> will be assigned a pointer past
  152. the last parsed character (i.e. <CODE>string_limit</CODE> if nothing came after
  153. the number). Whitespace is not allowed.
  154. </DL>
  155. <P>
  156. The structure <CODE>cl_read_flags</CODE> contains the following fields:
  157. <DL COMPACT>
  158. <DT><CODE>cl_read_syntax_t syntax</CODE>
  159. <DD>
  160. The possible results of the read operation. Possible values are
  161. <CODE>syntax_number</CODE>, <CODE>syntax_real</CODE>, <CODE>syntax_rational</CODE>,
  162. <CODE>syntax_integer</CODE>, <CODE>syntax_float</CODE>, <CODE>syntax_sfloat</CODE>,
  163. <CODE>syntax_ffloat</CODE>, <CODE>syntax_dfloat</CODE>, <CODE>syntax_lfloat</CODE>.
  164. <DT><CODE>cl_read_lsyntax_t lsyntax</CODE>
  165. <DD>
  166. Specifies the language-dependent syntax variant for the read operation.
  167. Possible values are
  168. <DL COMPACT>
  169. <DT><CODE>lsyntax_standard</CODE>
  170. <DD>
  171. accept standard algebraic notation only, no complex numbers,
  172. <DT><CODE>lsyntax_algebraic</CODE>
  173. <DD>
  174. accept the algebraic notation <CODE><VAR>x</VAR>+<VAR>y</VAR>i</CODE> for complex numbers,
  175. <DT><CODE>lsyntax_commonlisp</CODE>
  176. <DD>
  177. accept the <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> syntaxes for binary, octal,
  178. hexadecimal numbers,
  179. <CODE>#<VAR>base</VAR>R</CODE> for rational numbers in a given base,
  180. <CODE>#c(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE> for complex numbers,
  181. <DT><CODE>lsyntax_all</CODE>
  182. <DD>
  183. accept all of these extensions.
  184. </DL>
  185. <DT><CODE>unsigned int rational_base</CODE>
  186. <DD>
  187. The base in which rational numbers are read.
  188. <DT><CODE>cl_float_format_t float_flags.default_float_format</CODE>
  189. <DD>
  190. The float format used when reading floats with exponent marker <SAMP>`e'</SAMP>.
  191. <DT><CODE>cl_float_format_t float_flags.default_lfloat_format</CODE>
  192. <DD>
  193. The float format used when reading floats with exponent marker <SAMP>`l'</SAMP>.
  194. <DT><CODE>cl_boolean float_flags.mantissa_dependent_float_format</CODE>
  195. <DD>
  196. When this flag is true, floats specified with more digits than corresponding
  197. to the exponent marker they contain, but without <VAR>_nnn</VAR> suffix, will get a
  198. precision corresponding to their number of significant digits.
  199. </DL>
  200. <H2><A NAME="SEC46" HREF="cln_toc.html#TOC46">5.3 Output functions</A></H2>
  201. <P>
  202. Including <CODE>&#60;cl_io.h&#62;</CODE> defines a type <CODE>cl_ostream</CODE>, which is
  203. the type of the first argument to all output functions. Unless you build
  204. and use CLN with the macro CL_IO_STDIO being defined, <CODE>cl_ostream</CODE>
  205. is the same as <CODE>ostream&#38;</CODE>.
  206. <P>
  207. The variable
  208. <UL>
  209. <LI>
  210. <CODE>cl_ostream cl_stdout</CODE>
  211. </UL>
  212. <P>
  213. contains the standard output stream.
  214. <P>
  215. The variable
  216. <UL>
  217. <LI>
  218. <CODE>cl_ostream cl_stderr</CODE>
  219. </UL>
  220. <P>
  221. contains the standard error output stream.
  222. <P>
  223. These are the simple output functions:
  224. <DL COMPACT>
  225. <DT><CODE>void fprintchar (cl_ostream stream, char c)</CODE>
  226. <DD>
  227. Prints the character <CODE>x</CODE> literally on the <CODE>stream</CODE>.
  228. <DT><CODE>void fprint (cl_ostream stream, const char * string)</CODE>
  229. <DD>
  230. Prints the <CODE>string</CODE> literally on the <CODE>stream</CODE>.
  231. <DT><CODE>void fprintdecimal (cl_ostream stream, int x)</CODE>
  232. <DD>
  233. <DT><CODE>void fprintdecimal (cl_ostream stream, const cl_I&#38; x)</CODE>
  234. <DD>
  235. Prints the integer <CODE>x</CODE> in decimal on the <CODE>stream</CODE>.
  236. <DT><CODE>void fprintbinary (cl_ostream stream, const cl_I&#38; x)</CODE>
  237. <DD>
  238. Prints the integer <CODE>x</CODE> in binary (base 2, without prefix)
  239. on the <CODE>stream</CODE>.
  240. <DT><CODE>void fprintoctal (cl_ostream stream, const cl_I&#38; x)</CODE>
  241. <DD>
  242. Prints the integer <CODE>x</CODE> in octal (base 8, without prefix)
  243. on the <CODE>stream</CODE>.
  244. <DT><CODE>void fprinthexadecimal (cl_ostream stream, const cl_I&#38; x)</CODE>
  245. <DD>
  246. Prints the integer <CODE>x</CODE> in hexadecimal (base 16, without prefix)
  247. on the <CODE>stream</CODE>.
  248. </DL>
  249. <P>
  250. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  251. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  252. defines, in <CODE>&#60;cl_<VAR>type</VAR>_io.h&#62;</CODE>, the following output functions:
  253. <DL COMPACT>
  254. <DT><CODE>void fprint (cl_ostream stream, const <VAR>type</VAR>&#38; x)</CODE>
  255. <DD>
  256. <DT><CODE>cl_ostream operator&#60;&#60; (cl_ostream stream, const <VAR>type</VAR>&#38; x)</CODE>
  257. <DD>
  258. Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
  259. on the global printer settings in the variable <CODE>cl_default_print_flags</CODE>.
  260. The <CODE>ostream</CODE> flags and settings (flags, width and locale) are
  261. ignored.
  262. </DL>
  263. <P>
  264. The most flexible output function, defined in <CODE>&#60;cl_<VAR>type</VAR>_io.h&#62;</CODE>,
  265. are the following:
  266. <PRE>
  267. void print_complex (cl_ostream stream, const cl_print_flags&#38; flags,
  268. const cl_N&#38; z);
  269. void print_real (cl_ostream stream, const cl_print_flags&#38; flags,
  270. const cl_R&#38; z);
  271. void print_float (cl_ostream stream, const cl_print_flags&#38; flags,
  272. const cl_F&#38; z);
  273. void print_rational (cl_ostream stream, const cl_print_flags&#38; flags,
  274. const cl_RA&#38; z);
  275. void print_integer (cl_ostream stream, const cl_print_flags&#38; flags,
  276. const cl_I&#38; z);
  277. </PRE>
  278. <P>
  279. Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The <CODE>flags</CODE> are
  280. parameters which affect the output.
  281. <P>
  282. The structure type <CODE>cl_print_flags</CODE> contains the following fields:
  283. <DL COMPACT>
  284. <DT><CODE>unsigned int rational_base</CODE>
  285. <DD>
  286. The base in which rational numbers are printed. Default is <CODE>10</CODE>.
  287. <DT><CODE>cl_boolean rational_readably</CODE>
  288. <DD>
  289. If this flag is true, rational numbers are printed with radix specifiers in
  290. Common Lisp syntax (<CODE>#<VAR>n</VAR>R</CODE> or <CODE>#b</CODE> or <CODE>#o</CODE> or <CODE>#x</CODE>
  291. prefixes, trailing dot). Default is false.
  292. <DT><CODE>cl_boolean float_readably</CODE>
  293. <DD>
  294. If this flag is true, type specific exponent markers have precedence over 'E'.
  295. Default is false.
  296. <DT><CODE>cl_float_format_t default_float_format</CODE>
  297. <DD>
  298. Floating point numbers of this format will be printed using the 'E' exponent
  299. marker. Default is <CODE>cl_float_format_ffloat</CODE>.
  300. <DT><CODE>cl_boolean complex_readably</CODE>
  301. <DD>
  302. If this flag is true, complex numbers will be printed using the Common Lisp
  303. syntax <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>. Default is false.
  304. <DT><CODE>cl_string univpoly_varname</CODE>
  305. <DD>
  306. Univariate polynomials with no explicit indeterminate name will be printed
  307. using this variable name. Default is <CODE>"x"</CODE>.
  308. </DL>
  309. <P>
  310. The global variable <CODE>cl_default_print_flags</CODE> contains the default values,
  311. used by the function <CODE>fprint</CODE>.
  312. <P><HR><P>
  313. 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>.
  314. </BODY>
  315. </HTML>