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.

3985 lines
135 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename cln.info
  4. @settitle CLN, a Class Library for Numbers
  5. @c @setchapternewpage off
  6. @c For `info' only.
  7. @paragraphindent 0
  8. @c For TeX only.
  9. @iftex
  10. @c I hate putting "@noindent" in front of every paragraph.
  11. @parindent=0pt
  12. @end iftex
  13. @c %**end of header
  14. @c My own index.
  15. @defindex my
  16. @c Don't need the other types of indices.
  17. @synindex cp my
  18. @synindex fn my
  19. @synindex vr my
  20. @synindex ky my
  21. @synindex pg my
  22. @synindex tp my
  23. @c For `info' only.
  24. @ifinfo
  25. This file documents @sc{cln}, a Class Library for Numbers.
  26. Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
  27. Richard Kreckel, @code{<kreckel@@ginac.de>}.
  28. Copyright (C) Bruno Haible 1995, 1996, 1997, 1998, 1999, 2000.
  29. Permission is granted to make and distribute verbatim copies of
  30. this manual provided the copyright notice and this permission notice
  31. are preserved on all copies.
  32. @ignore
  33. Permission is granted to process this file through TeX and print the
  34. results, provided the printed document carries copying permission
  35. notice identical to this one except for the removal of this paragraph
  36. (this paragraph not being relevant to the printed manual).
  37. @end ignore
  38. Permission is granted to copy and distribute modified versions of this
  39. manual under the conditions for verbatim copying, provided that the entire
  40. resulting derived work is distributed under the terms of a permission
  41. notice identical to this one.
  42. Permission is granted to copy and distribute translations of this manual
  43. into another language, under the above conditions for modified versions,
  44. except that this permission notice may be stated in a translation approved
  45. by the author.
  46. @end ifinfo
  47. @c For TeX only.
  48. @c prevent ugly black rectangles on overfull hbox lines:
  49. @finalout
  50. @titlepage
  51. @title CLN, a Class Library for Numbers
  52. @author by Bruno Haible
  53. @page
  54. @vskip 0pt plus 1filll
  55. Copyright @copyright{} Bruno Haible 1995, 1996, 1997, 1998, 1999, 2000.
  56. @sp 2
  57. Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
  58. Richard Kreckel, @code{<kreckel@@ginac.de>}.
  59. Permission is granted to make and distribute verbatim copies of
  60. this manual provided the copyright notice and this permission notice
  61. are preserved on all copies.
  62. Permission is granted to copy and distribute modified versions of this
  63. manual under the conditions for verbatim copying, provided that the entire
  64. resulting derived work is distributed under the terms of a permission
  65. notice identical to this one.
  66. Permission is granted to copy and distribute translations of this manual
  67. into another language, under the above conditions for modified versions,
  68. except that this permission notice may be stated in a translation approved
  69. by the author.
  70. @end titlepage
  71. @page
  72. @node Top, Introduction, (dir), (dir)
  73. @c @menu
  74. @c * Introduction:: Introduction
  75. @c @end menu
  76. @menu
  77. * Introduction::
  78. * Installation::
  79. * Ordinary number types::
  80. * Functions on numbers::
  81. * Input/Output::
  82. * Rings::
  83. * Modular integers::
  84. * Symbolic data types::
  85. * Univariate polynomials::
  86. * Internals::
  87. * Using the library::
  88. * Customizing::
  89. * Index::
  90. --- The Detailed Node Listing ---
  91. Installation
  92. * Prerequisites::
  93. * Building the library::
  94. * Installing the library::
  95. * Cleaning up::
  96. Prerequisites
  97. * C++ compiler::
  98. * Make utility::
  99. * Sed utility::
  100. Ordinary number types
  101. * Exact numbers::
  102. * Floating-point numbers::
  103. * Complex numbers::
  104. * Conversions::
  105. Functions on numbers
  106. * Constructing numbers::
  107. * Elementary functions::
  108. * Elementary rational functions::
  109. * Elementary complex functions::
  110. * Comparisons::
  111. * Rounding functions::
  112. * Roots::
  113. * Transcendental functions::
  114. * Functions on integers::
  115. * Functions on floating-point numbers::
  116. * Conversion functions::
  117. * Random number generators::
  118. * Obfuscating operators::
  119. Constructing numbers
  120. * Constructing integers::
  121. * Constructing rational numbers::
  122. * Constructing floating-point numbers::
  123. * Constructing complex numbers::
  124. Transcendental functions
  125. * Exponential and logarithmic functions::
  126. * Trigonometric functions::
  127. * Hyperbolic functions::
  128. * Euler gamma::
  129. * Riemann zeta::
  130. Functions on integers
  131. * Logical functions::
  132. * Number theoretic functions::
  133. * Combinatorial functions::
  134. Conversion functions
  135. * Conversion to floating-point numbers::
  136. * Conversion to rational numbers::
  137. Input/Output
  138. * Internal and printed representation::
  139. * Input functions::
  140. * Output functions::
  141. Modular integers
  142. * Modular integer rings::
  143. * Functions on modular integers::
  144. Symbolic data types
  145. * Strings::
  146. * Symbols::
  147. Univariate polynomials
  148. * Univariate polynomial rings::
  149. * Functions on univariate polynomials::
  150. * Special polynomials::
  151. Internals
  152. * Why C++ ?::
  153. * Memory efficiency::
  154. * Speed efficiency::
  155. * Garbage collection::
  156. Using the library
  157. * Compiler options::
  158. * Include files::
  159. * An Example::
  160. * Debugging support::
  161. Customizing
  162. * Error handling::
  163. * Floating-point underflow::
  164. * Customizing I/O::
  165. * Customizing the memory allocator::
  166. @end menu
  167. @node Introduction, Installation, Top, Top
  168. @comment node-name, next, previous, up
  169. @chapter Introduction
  170. @noindent
  171. CLN is a library for computations with all kinds of numbers.
  172. It has a rich set of number classes:
  173. @itemize @bullet
  174. @item
  175. Integers (with unlimited precision),
  176. @item
  177. Rational numbers,
  178. @item
  179. Floating-point numbers:
  180. @itemize @minus
  181. @item
  182. Short float,
  183. @item
  184. Single float,
  185. @item
  186. Double float,
  187. @item
  188. Long float (with unlimited precision),
  189. @end itemize
  190. @item
  191. Complex numbers,
  192. @item
  193. Modular integers (integers modulo a fixed integer),
  194. @item
  195. Univariate polynomials.
  196. @end itemize
  197. @noindent
  198. The subtypes of the complex numbers among these are exactly the
  199. types of numbers known to the Common Lisp language. Therefore
  200. @code{CLN} can be used for Common Lisp implementations, giving
  201. @samp{CLN} another meaning: it becomes an abbreviation of
  202. ``Common Lisp Numbers''.
  203. @noindent
  204. The CLN package implements
  205. @itemize @bullet
  206. @item
  207. Elementary functions (@code{+}, @code{-}, @code{*}, @code{/}, @code{sqrt},
  208. comparisons, @dots{}),
  209. @item
  210. Logical functions (logical @code{and}, @code{or}, @code{not}, @dots{}),
  211. @item
  212. Transcendental functions (exponential, logarithmic, trigonometric, hyperbolic
  213. functions and their inverse functions).
  214. @end itemize
  215. @noindent
  216. CLN is a C++ library. Using C++ as an implementation language provides
  217. @itemize @bullet
  218. @item
  219. efficiency: it compiles to machine code,
  220. @item
  221. type safety: the C++ compiler knows about the number types and complains
  222. if, for example, you try to assign a float to an integer variable.
  223. @item
  224. algebraic syntax: You can use the @code{+}, @code{-}, @code{*}, @code{=},
  225. @code{==}, @dots{} operators as in C or C++.
  226. @end itemize
  227. @noindent
  228. CLN is memory efficient:
  229. @itemize @bullet
  230. @item
  231. Small integers and short floats are immediate, not heap allocated.
  232. @item
  233. Heap-allocated memory is reclaimed through an automatic, non-interruptive
  234. garbage collection.
  235. @end itemize
  236. @noindent
  237. CLN is speed efficient:
  238. @itemize @bullet
  239. @item
  240. The kernel of CLN has been written in assembly language for some CPUs
  241. (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
  242. @item
  243. @cindex GMP
  244. On all CPUs, CLN may be configured to use the superefficient low-level
  245. routines from GNU GMP version 3.
  246. @item
  247. It uses Karatsuba multiplication, which is significantly faster
  248. for large numbers than the standard multiplication algorithm.
  249. @item
  250. For very large numbers (more than 12000 decimal digits), it uses
  251. @iftex
  252. Sch{@"o}nhage-Strassen
  253. @cindex Sch{@"o}nhage-Strassen multiplication
  254. @end iftex
  255. @ifinfo
  256. Sch�nhage-Strassen
  257. @cindex Sch�nhage-Strassen multiplication
  258. @end ifinfo
  259. multiplication, which is an asymptotically optimal multiplication
  260. algorithm, for multiplication, division and radix conversion.
  261. @end itemize
  262. @noindent
  263. CLN aims at being easily integrated into larger software packages:
  264. @itemize @bullet
  265. @item
  266. The garbage collection imposes no burden on the main application.
  267. @item
  268. The library provides hooks for memory allocation and exceptions.
  269. @end itemize
  270. @node Installation, Ordinary number types, Introduction, Top
  271. @chapter Installation
  272. This section describes how to install the CLN package on your system.
  273. @menu
  274. * Prerequisites::
  275. * Building the library::
  276. * Installing the library::
  277. * Cleaning up::
  278. @end menu
  279. @node Prerequisites, Building the library, Installation, Installation
  280. @section Prerequisites
  281. @menu
  282. * C++ compiler::
  283. * Make utility::
  284. * Sed utility::
  285. @end menu
  286. @node C++ compiler, Make utility, Prerequisites, Prerequisites
  287. @subsection C++ compiler
  288. To build CLN, you need a C++ compiler.
  289. Actually, you need GNU @code{g++ 2.7.0} or newer.
  290. On HPPA, you need GNU @code{g++ 2.8.0} or newer.
  291. I recommend GNU @code{g++ 2.95} or newer.
  292. The following C++ features are used:
  293. classes, member functions,
  294. overloading of functions and operators,
  295. constructors and destructors, inline, const,
  296. multiple inheritance, templates.
  297. The following C++ features are not used:
  298. @code{new}, @code{delete}, virtual inheritance,
  299. exceptions.
  300. CLN relies on semi-automatic ordering of initializations
  301. of static and global variables, a feature which I could
  302. implement for GNU g++ only.
  303. @ignore
  304. @comment cl_modules.h requires g++
  305. Therefore nearly any C++ compiler will do.
  306. The following C++ compilers are known to compile CLN:
  307. @itemize @minus
  308. @item
  309. GNU @code{g++ 2.7.0}, @code{g++ 2.7.2}
  310. @item
  311. SGI @code{CC 4}
  312. @end itemize
  313. The following C++ compilers are known to be unusable for CLN:
  314. @itemize @minus
  315. @item
  316. On SunOS 4, @code{CC 2.1}, because it doesn't grok @code{//} comments
  317. in lines containing @code{#if} or @code{#elif} preprocessor commands.
  318. @item
  319. On AIX 3.2.5, @code{xlC}, because it doesn't grok the template syntax
  320. in @code{cl_SV.h} and @code{cl_GV.h}, because it forces most class types
  321. to have default constructors, and because it probably miscompiles the
  322. integer multiplication routines.
  323. @item
  324. On AIX 4.1.4.0, @code{xlC}, because when optimizing, it sometimes converts
  325. @code{short}s to @code{int}s by zero-extend.
  326. @item
  327. GNU @code{g++ 2.5.8}
  328. @item
  329. On HPPA, GNU @code{g++ 2.7.x}, because the semi-automatic ordering of
  330. initializations will not work.
  331. @end itemize
  332. @end ignore
  333. @cindex @code{make}
  334. @node Make utility, Sed utility, C++ compiler, Prerequisites
  335. @subsection Make utility
  336. To build CLN, you also need to have GNU @code{make} installed.
  337. @cindex @code{sed}
  338. @node Sed utility, , Make utility, Prerequisites
  339. @subsection Sed utility
  340. To build CLN on HP-UX, you also need to have GNU @code{sed} installed.
  341. This is because the libtool script, which creates the CLN library, relies
  342. on @code{sed}, and the vendor's @code{sed} utility on these systems is too
  343. limited.
  344. @node Building the library, Installing the library, Prerequisites, Installation
  345. @section Building the library
  346. As with any autoconfiguring GNU software, installation is as easy as this:
  347. @example
  348. $ ./configure
  349. $ make
  350. $ make check
  351. @end example
  352. If on your system, @samp{make} is not GNU @code{make}, you have to use
  353. @samp{gmake} instead of @samp{make} above.
  354. The @code{configure} command checks out some features of your system and
  355. C++ compiler and builds the @code{Makefile}s. The @code{make} command
  356. builds the library. This step may take 4 hours on an average workstation.
  357. The @code{make check} runs some test to check that no important subroutine
  358. has been miscompiled.
  359. The @code{configure} command accepts options. To get a summary of them, try
  360. @example
  361. $ ./configure --help
  362. @end example
  363. Some of the options are explained in detail in the @samp{INSTALL.generic} file.
  364. You can specify the C compiler, the C++ compiler and their options through
  365. the following environment variables when running @code{configure}:
  366. @table @code
  367. @item CC
  368. Specifies the C compiler.
  369. @item CFLAGS
  370. Flags to be given to the C compiler when compiling programs (not when linking).
  371. @item CXX
  372. Specifies the C++ compiler.
  373. @item CXXFLAGS
  374. Flags to be given to the C++ compiler when compiling programs (not when linking).
  375. @end table
  376. Examples:
  377. @example
  378. $ CC="gcc" CFLAGS="-O" CXX="g++" CXXFLAGS="-O" ./configure
  379. $ CC="gcc -V 2.7.2" CFLAGS="-O -g" \
  380. CXX="g++ -V 2.7.2" CXXFLAGS="-O -g" ./configure
  381. $ CC="gcc -V 2.8.1" CFLAGS="-O -fno-exceptions" \
  382. CXX="g++ -V 2.8.1" CXXFLAGS="-O -fno-exceptions" ./configure
  383. $ CC="gcc -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" \
  384. CXX="g++ -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" ./configure
  385. @end example
  386. @ignore
  387. @comment cl_modules.h requires g++
  388. You should not mix GNU and non-GNU compilers. So, if @code{CXX} is a non-GNU
  389. compiler, @code{CC} should be set to a non-GNU compiler as well. Examples:
  390. @example
  391. $ CC="cc" CFLAGS="-O" CXX="CC" CXXFLAGS="-O" ./configure
  392. $ CC="gcc -V 2.7.0" CFLAGS="-g" CXX="g++ -V 2.7.0" CXXFLAGS="-g" ./configure
  393. @end example
  394. On SGI Irix 5, if you wish not to use @code{g++}:
  395. @example
  396. $ CC="cc" CFLAGS="-O" CXX="CC" CXXFLAGS="-O -Olimit 16000" ./configure
  397. @end example
  398. On SGI Irix 6, if you wish not to use @code{g++}:
  399. @example
  400. $ CC="cc -32" CFLAGS="-O" CXX="CC -32" CXXFLAGS="-O -Olimit 34000" \
  401. ./configure --without-gmp
  402. $ CC="cc -n32" CFLAGS="-O" CXX="CC -n32" CXXFLAGS="-O \
  403. -OPT:const_copy_limit=32400 -OPT:global_limit=32400 -OPT:fprop_limit=4000" \
  404. ./configure --without-gmp
  405. @end example
  406. @end ignore
  407. Note that for these environment variables to take effect, you have to set
  408. them (assuming a Bourne-compatible shell) on the same line as the
  409. @code{configure} command. If you made the settings in earlier shell
  410. commands, you have to @code{export} the environment variables before
  411. calling @code{configure}. In a @code{csh} shell, you have to use the
  412. @samp{setenv} command for setting each of the environment variables.
  413. On Linux, @code{g++} needs 15 MB to compile the tests. So you should better
  414. have 17 MB swap space and 1 MB room in $TMPDIR.
  415. If you use @code{g++} version 2.7.x, don't add @samp{-O2} to the CXXFLAGS,
  416. because @samp{g++ -O} generates better code for CLN than @samp{g++ -O2}.
  417. If you use @code{g++} version 2.8.x or egcs-2.91.x (a.k.a. egcs-1.1) or
  418. gcc-2.95.x, I recommend adding @samp{-fno-exceptions} to the CXXFLAGS.
  419. This will likely generate better code.
  420. If you use @code{g++} version egcs-2.91.x (egcs-1.1) or gcc-2.95.x on Sparc,
  421. add either @samp{-O} or @samp{-O2 -fno-schedule-insns} to the CXXFLAGS.
  422. With full @samp{-O2}, @code{g++} miscompiles the division routines. Also, for
  423. --enable-shared to work, you need egcs-1.1.2 or newer.
  424. By default, only a static library is built. You can build CLN as a shared
  425. library too, by calling @code{configure} with the option @samp{--enable-shared}.
  426. To get it built as a shared library only, call @code{configure} with the options
  427. @samp{--enable-shared --disable-static}.
  428. If you use @code{g++} version egcs-2.91.x (egcs-1.1) on Sparc, you cannot
  429. use @samp{--enable-shared} because @code{g++} would miscompile parts of the
  430. library.
  431. @node Installing the library, Cleaning up, Building the library, Installation
  432. @section Installing the library
  433. @cindex installation
  434. As with any autoconfiguring GNU software, installation is as easy as this:
  435. @example
  436. $ make install
  437. @end example
  438. The @samp{make install} command installs the library and the include files
  439. into public places (@file{/usr/local/lib/} and @file{/usr/local/include/},
  440. if you haven't specified a @code{--prefix} option to @code{configure}).
  441. This step may require superuser privileges.
  442. If you have already built the library and wish to install it, but didn't
  443. specify @code{--prefix=@dots{}} at configure time, just re-run
  444. @code{configure}, giving it the same options as the first time, plus
  445. the @code{--prefix=@dots{}} option.
  446. @node Cleaning up, , Installing the library, Installation
  447. @section Cleaning up
  448. You can remove system-dependent files generated by @code{make} through
  449. @example
  450. $ make clean
  451. @end example
  452. You can remove all files generated by @code{make}, thus reverting to a
  453. virgin distribution of CLN, through
  454. @example
  455. $ make distclean
  456. @end example
  457. @node Ordinary number types, Functions on numbers, Installation, Top
  458. @chapter Ordinary number types
  459. CLN implements the following class hierarchy:
  460. @example
  461. Number
  462. cl_number
  463. <cl_number.h>
  464. |
  465. |
  466. Real or complex number
  467. cl_N
  468. <cl_complex.h>
  469. |
  470. |
  471. Real number
  472. cl_R
  473. <cl_real.h>
  474. |
  475. +-------------------+-------------------+
  476. | |
  477. Rational number Floating-point number
  478. cl_RA cl_F
  479. <cl_rational.h> <cl_float.h>
  480. | |
  481. | +-------------+-------------+-------------+
  482. Integer | | | |
  483. cl_I Short-Float Single-Float Double-Float Long-Float
  484. <cl_integer.h> cl_SF cl_FF cl_DF cl_LF
  485. <cl_sfloat.h> <cl_ffloat.h> <cl_dfloat.h> <cl_lfloat.h>
  486. @end example
  487. @cindex @code{cl_number}
  488. @cindex abstract class
  489. The base class @code{cl_number} is an abstract base class.
  490. It is not useful to declare a variable of this type except if you want
  491. to completely disable compile-time type checking and use run-time type
  492. checking instead.
  493. @cindex @code{cl_N}
  494. @cindex real number
  495. @cindex complex number
  496. The class @code{cl_N} comprises real and complex numbers. There is
  497. no special class for complex numbers since complex numbers with imaginary
  498. part @code{0} are automatically converted to real numbers.
  499. @cindex @code{cl_R}
  500. The class @code{cl_R} comprises real numbers of different kinds. It is an
  501. abstract class.
  502. @cindex @code{cl_RA}
  503. @cindex rational number
  504. @cindex integer
  505. The class @code{cl_RA} comprises exact real numbers: rational numbers, including
  506. integers. There is no special class for non-integral rational numbers
  507. since rational numbers with denominator @code{1} are automatically converted
  508. to integers.
  509. @cindex @code{cl_F}
  510. The class @code{cl_F} implements floating-point approximations to real numbers.
  511. It is an abstract class.
  512. @menu
  513. * Exact numbers::
  514. * Floating-point numbers::
  515. * Complex numbers::
  516. * Conversions::
  517. @end menu
  518. @node Exact numbers, Floating-point numbers, Ordinary number types, Ordinary number types
  519. @section Exact numbers
  520. @cindex exact number
  521. Some numbers are represented as exact numbers: there is no loss of information
  522. when such a number is converted from its mathematical value to its internal
  523. representation. On exact numbers, the elementary operations (@code{+},
  524. @code{-}, @code{*}, @code{/}, comparisons, @dots{}) compute the completely
  525. correct result.
  526. In CLN, the exact numbers are:
  527. @itemize @bullet
  528. @item
  529. rational numbers (including integers),
  530. @item
  531. complex numbers whose real and imaginary parts are both rational numbers.
  532. @end itemize
  533. Rational numbers are always normalized to the form
  534. @code{@var{numerator}/@var{denominator}} where the numerator and denominator
  535. are coprime integers and the denominator is positive. If the resulting
  536. denominator is @code{1}, the rational number is converted to an integer.
  537. Small integers (typically in the range @code{-2^30}@dots{}@code{2^30-1},
  538. for 32-bit machines) are especially efficient, because they consume no heap
  539. allocation. Otherwise the distinction between these immediate integers
  540. (called ``fixnums'') and heap allocated integers (called ``bignums'')
  541. is completely transparent.
  542. @node Floating-point numbers, Complex numbers, Exact numbers, Ordinary number types
  543. @section Floating-point numbers
  544. @cindex floating-point number
  545. Not all real numbers can be represented exactly. (There is an easy mathematical
  546. proof for this: Only a countable set of numbers can be stored exactly in
  547. a computer, even if one assumes that it has unlimited storage. But there
  548. are uncountably many real numbers.) So some approximation is needed.
  549. CLN implements ordinary floating-point numbers, with mantissa and exponent.
  550. @cindex rounding error
  551. The elementary operations (@code{+}, @code{-}, @code{*}, @code{/}, @dots{})
  552. only return approximate results. For example, the value of the expression
  553. @code{(cl_F) 0.3 + (cl_F) 0.4} prints as @samp{0.70000005}, not as
  554. @samp{0.7}. Rounding errors like this one are inevitable when computing
  555. with floating-point numbers.
  556. Nevertheless, CLN rounds the floating-point results of the operations @code{+},
  557. @code{-}, @code{*}, @code{/}, @code{sqrt} according to the ``round-to-even''
  558. rule: It first computes the exact mathematical result and then returns the
  559. floating-point number which is nearest to this. If two floating-point numbers
  560. are equally distant from the ideal result, the one with a @code{0} in its least
  561. significant mantissa bit is chosen.
  562. Similarly, testing floating point numbers for equality @samp{x == y}
  563. is gambling with random errors. Better check for @samp{abs(x - y) < epsilon}
  564. for some well-chosen @code{epsilon}.
  565. Floating point numbers come in four flavors:
  566. @itemize @bullet
  567. @item
  568. @cindex @code{cl_SF}
  569. Short floats, type @code{cl_SF}.
  570. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  571. and 17 mantissa bits (including the ``hidden'' bit).
  572. They don't consume heap allocation.
  573. @item
  574. @cindex @code{cl_FF}
  575. Single floats, type @code{cl_FF}.
  576. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  577. and 24 mantissa bits (including the ``hidden'' bit).
  578. In CLN, they are represented as IEEE single-precision floating point numbers.
  579. This corresponds closely to the C/C++ type @samp{float}.
  580. @item
  581. @cindex @code{cl_DF}
  582. Double floats, type @code{cl_DF}.
  583. They have 1 sign bit, 11 exponent bits (including the exponent's sign),
  584. and 53 mantissa bits (including the ``hidden'' bit).
  585. In CLN, they are represented as IEEE double-precision floating point numbers.
  586. This corresponds closely to the C/C++ type @samp{double}.
  587. @item
  588. @cindex @code{cl_LF}
  589. Long floats, type @code{cl_LF}.
  590. They have 1 sign bit, 32 exponent bits (including the exponent's sign),
  591. and n mantissa bits (including the ``hidden'' bit), where n >= 64.
  592. The precision of a long float is unlimited, but once created, a long float
  593. has a fixed precision. (No ``lazy recomputation''.)
  594. @end itemize
  595. Of course, computations with long floats are more expensive than those
  596. with smaller floating-point formats.
  597. CLN does not implement features like NaNs, denormalized numbers and
  598. gradual underflow. If the exponent range of some floating-point type
  599. is too limited for your application, choose another floating-point type
  600. with larger exponent range.
  601. @cindex @code{cl_F}
  602. As a user of CLN, you can forget about the differences between the
  603. four floating-point types and just declare all your floating-point
  604. variables as being of type @code{cl_F}. This has the advantage that
  605. when you change the precision of some computation (say, from @code{cl_DF}
  606. to @code{cl_LF}), you don't have to change the code, only the precision
  607. of the initial values. Also, many transcendental functions have been
  608. declared as returning a @code{cl_F} when the argument is a @code{cl_F},
  609. but such declarations are missing for the types @code{cl_SF}, @code{cl_FF},
  610. @code{cl_DF}, @code{cl_LF}. (Such declarations would be wrong if
  611. the floating point contagion rule happened to change in the future.)
  612. @node Complex numbers, Conversions, Floating-point numbers, Ordinary number types
  613. @section Complex numbers
  614. @cindex complex number
  615. Complex numbers, as implemented by the class @code{cl_N}, have a real
  616. part and an imaginary part, both real numbers. A complex number whose
  617. imaginary part is the exact number @code{0} is automatically converted
  618. to a real number.
  619. Complex numbers can arise from real numbers alone, for example
  620. through application of @code{sqrt} or transcendental functions.
  621. @node Conversions, , Complex numbers, Ordinary number types
  622. @section Conversions
  623. @cindex conversion
  624. Conversions from any class to any its superclasses (``base classes'' in
  625. C++ terminology) is done automatically.
  626. Conversions from the C built-in types @samp{long} and @samp{unsigned long}
  627. are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
  628. @code{cl_N} and @code{cl_number}.
  629. Conversions from the C built-in types @samp{int} and @samp{unsigned int}
  630. are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
  631. @code{cl_N} and @code{cl_number}. However, these conversions emphasize
  632. efficiency. Their range is therefore limited:
  633. @itemize @minus
  634. @item
  635. The conversion from @samp{int} works only if the argument is < 2^29 and > -2^29.
  636. @item
  637. The conversion from @samp{unsigned int} works only if the argument is < 2^29.
  638. @end itemize
  639. In a declaration like @samp{cl_I x = 10;} the C++ compiler is able to
  640. do the conversion of @code{10} from @samp{int} to @samp{cl_I} at compile time
  641. already. On the other hand, code like @samp{cl_I x = 1000000000;} is
  642. in error.
  643. So, if you want to be sure that an @samp{int} whose magnitude is not guaranteed
  644. to be < 2^29 is correctly converted to a @samp{cl_I}, first convert it to a
  645. @samp{long}. Similarly, if a large @samp{unsigned int} is to be converted to a
  646. @samp{cl_I}, first convert it to an @samp{unsigned long}.
  647. Conversions from the C built-in type @samp{float} are provided for the classes
  648. @code{cl_FF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
  649. Conversions from the C built-in type @samp{double} are provided for the classes
  650. @code{cl_DF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
  651. Conversions from @samp{const char *} are provided for the classes
  652. @code{cl_I}, @code{cl_RA},
  653. @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F},
  654. @code{cl_R}, @code{cl_N}.
  655. The easiest way to specify a value which is outside of the range of the
  656. C++ built-in types is therefore to specify it as a string, like this:
  657. @cindex Rubik's cube
  658. @example
  659. cl_I order_of_rubiks_cube_group = "43252003274489856000";
  660. @end example
  661. Note that this conversion is done at runtime, not at compile-time.
  662. Conversions from @code{cl_I} to the C built-in types @samp{int},
  663. @samp{unsigned int}, @samp{long}, @samp{unsigned long} are provided through
  664. the functions
  665. @table @code
  666. @item int cl_I_to_int (const cl_I& x)
  667. @cindex @code{cl_I_to_int ()}
  668. @itemx unsigned int cl_I_to_uint (const cl_I& x)
  669. @cindex @code{cl_I_to_uint ()}
  670. @itemx long cl_I_to_long (const cl_I& x)
  671. @cindex @code{cl_I_to_long ()}
  672. @itemx unsigned long cl_I_to_ulong (const cl_I& x)
  673. @cindex @code{cl_I_to_ulong ()}
  674. Returns @code{x} as element of the C type @var{ctype}. If @code{x} is not
  675. representable in the range of @var{ctype}, a runtime error occurs.
  676. @end table
  677. Conversions from the classes @code{cl_I}, @code{cl_RA},
  678. @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F} and
  679. @code{cl_R}
  680. to the C built-in types @samp{float} and @samp{double} are provided through
  681. the functions
  682. @table @code
  683. @item float cl_float_approx (const @var{type}& x)
  684. @cindex @code{cl_float_approx ()}
  685. @itemx double cl_double_approx (const @var{type}& x)
  686. @cindex @code{cl_double_approx ()}
  687. Returns an approximation of @code{x} of C type @var{ctype}.
  688. If @code{abs(x)} is too close to 0 (underflow), 0 is returned.
  689. If @code{abs(x)} is too large (overflow), an IEEE infinity is returned.
  690. @end table
  691. Conversions from any class to any of its subclasses (``derived classes'' in
  692. C++ terminology) are not provided. Instead, you can assert and check
  693. that a value belongs to a certain subclass, and return it as element of that
  694. class, using the @samp{As} and @samp{The} macros.
  695. @cindex @code{As() ()}
  696. @code{As(@var{type})(@var{value})} checks that @var{value} belongs to
  697. @var{type} and returns it as such.
  698. @cindex @code{The() ()}
  699. @code{The(@var{type})(@var{value})} assumes that @var{value} belongs to
  700. @var{type} and returns it as such. It is your responsibility to ensure
  701. that this assumption is valid.
  702. Example:
  703. @example
  704. @group
  705. cl_I x = @dots{};
  706. if (!(x >= 0)) abort();
  707. cl_I ten_x = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer.
  708. // In general, it would be a rational number.
  709. @end group
  710. @end example
  711. @node Functions on numbers, Input/Output, Ordinary number types, Top
  712. @chapter Functions on numbers
  713. Each of the number classes declares its mathematical operations in the
  714. corresponding include file. For example, if your code operates with
  715. objects of type @code{cl_I}, it should @code{#include <cl_integer.h>}.
  716. @menu
  717. * Constructing numbers::
  718. * Elementary functions::
  719. * Elementary rational functions::
  720. * Elementary complex functions::
  721. * Comparisons::
  722. * Rounding functions::
  723. * Roots::
  724. * Transcendental functions::
  725. * Functions on integers::
  726. * Functions on floating-point numbers::
  727. * Conversion functions::
  728. * Random number generators::
  729. * Obfuscating operators::
  730. @end menu
  731. @node Constructing numbers, Elementary functions, Functions on numbers, Functions on numbers
  732. @section Constructing numbers
  733. Here is how to create number objects ``from nothing''.
  734. @menu
  735. * Constructing integers::
  736. * Constructing rational numbers::
  737. * Constructing floating-point numbers::
  738. * Constructing complex numbers::
  739. @end menu
  740. @node Constructing integers, Constructing rational numbers, Constructing numbers, Constructing numbers
  741. @subsection Constructing integers
  742. @code{cl_I} objects are most easily constructed from C integers and from
  743. strings. See @ref{Conversions}.
  744. @node Constructing rational numbers, Constructing floating-point numbers, Constructing integers, Constructing numbers
  745. @subsection Constructing rational numbers
  746. @code{cl_RA} objects can be constructed from strings. The syntax
  747. for rational numbers is described in @ref{Internal and printed representation}.
  748. Another standard way to produce a rational number is through application
  749. of @samp{operator /} or @samp{recip} on integers.
  750. @node Constructing floating-point numbers, Constructing complex numbers, Constructing rational numbers, Constructing numbers
  751. @subsection Constructing floating-point numbers
  752. @code{cl_F} objects with low precision are most easily constructed from
  753. C @samp{float} and @samp{double}. See @ref{Conversions}.
  754. To construct a @code{cl_F} with high precision, you can use the conversion
  755. from @samp{const char *}, but you have to specify the desired precision
  756. within the string. (See @ref{Internal and printed representation}.)
  757. Example:
  758. @example
  759. cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
  760. @end example
  761. will set @samp{e} to the given value, with a precision of 40 decimal digits.
  762. The programmatic way to construct a @code{cl_F} with high precision is
  763. through the @code{cl_float} conversion function, see
  764. @ref{Conversion to floating-point numbers}. For example, to compute
  765. @code{e} to 40 decimal places, first construct 1.0 to 40 decimal places
  766. and then apply the exponential function:
  767. @example
  768. cl_float_format_t precision = cl_float_format(40);
  769. cl_F e = exp(cl_float(1,precision));
  770. @end example
  771. @node Constructing complex numbers, , Constructing floating-point numbers, Constructing numbers
  772. @subsection Constructing complex numbers
  773. Non-real @code{cl_N} objects are normally constructed through the function
  774. @example
  775. cl_N complex (const cl_R& realpart, const cl_R& imagpart)
  776. @end example
  777. See @ref{Elementary complex functions}.
  778. @node Elementary functions, Elementary rational functions, Constructing numbers, Functions on numbers
  779. @section Elementary functions
  780. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  781. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  782. defines the following operations:
  783. @table @code
  784. @item @var{type} operator + (const @var{type}&, const @var{type}&)
  785. @cindex @code{operator + ()}
  786. Addition.
  787. @item @var{type} operator - (const @var{type}&, const @var{type}&)
  788. @cindex @code{operator - ()}
  789. Subtraction.
  790. @item @var{type} operator - (const @var{type}&)
  791. Returns the negative of the argument.
  792. @item @var{type} plus1 (const @var{type}& x)
  793. @cindex @code{plus1 ()}
  794. Returns @code{x + 1}.
  795. @item @var{type} minus1 (const @var{type}& x)
  796. @cindex @code{minus1 ()}
  797. Returns @code{x - 1}.
  798. @item @var{type} operator * (const @var{type}&, const @var{type}&)
  799. @cindex @code{operator * ()}
  800. Multiplication.
  801. @item @var{type} square (const @var{type}& x)
  802. @cindex @code{square ()}
  803. Returns @code{x * x}.
  804. @end table
  805. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
  806. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  807. defines the following operations:
  808. @table @code
  809. @item @var{type} operator / (const @var{type}&, const @var{type}&)
  810. @cindex @code{operator / ()}
  811. Division.
  812. @item @var{type} recip (const @var{type}&)
  813. @cindex @code{recip ()}
  814. Returns the reciprocal of the argument.
  815. @end table
  816. The class @code{cl_I} doesn't define a @samp{/} operation because
  817. in the C/C++ language this operator, applied to integral types,
  818. denotes the @samp{floor} or @samp{truncate} operation (which one of these,
  819. is implementation dependent). (@xref{Rounding functions})
  820. Instead, @code{cl_I} defines an ``exact quotient'' function:
  821. @table @code
  822. @item cl_I exquo (const cl_I& x, const cl_I& y)
  823. @cindex @code{exquo ()}
  824. Checks that @code{y} divides @code{x}, and returns the quotient @code{x}/@code{y}.
  825. @end table
  826. The following exponentiation functions are defined:
  827. @table @code
  828. @item cl_I expt_pos (const cl_I& x, const cl_I& y)
  829. @cindex @code{expt_pos ()}
  830. @itemx cl_RA expt_pos (const cl_RA& x, const cl_I& y)
  831. @code{y} must be > 0. Returns @code{x^y}.
  832. @item cl_RA expt (const cl_RA& x, const cl_I& y)
  833. @cindex @code{expt ()}
  834. @itemx cl_R expt (const cl_R& x, const cl_I& y)
  835. @itemx cl_N expt (const cl_N& x, const cl_I& y)
  836. Returns @code{x^y}.
  837. @end table
  838. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  839. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  840. defines the following operation:
  841. @table @code
  842. @item @var{type} abs (const @var{type}& x)
  843. @cindex @code{abs ()}
  844. Returns the absolute value of @code{x}.
  845. This is @code{x} if @code{x >= 0}, and @code{-x} if @code{x <= 0}.
  846. @end table
  847. The class @code{cl_N} implements this as follows:
  848. @table @code
  849. @item cl_R abs (const cl_N x)
  850. Returns the absolute value of @code{x}.
  851. @end table
  852. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  853. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  854. defines the following operation:
  855. @table @code
  856. @item @var{type} signum (const @var{type}& x)
  857. @cindex @code{signum ()}
  858. Returns the sign of @code{x}, in the same number format as @code{x}.
  859. This is defined as @code{x / abs(x)} if @code{x} is non-zero, and
  860. @code{x} if @code{x} is zero. If @code{x} is real, the value is either
  861. 0 or 1 or -1.
  862. @end table
  863. @node Elementary rational functions, Elementary complex functions, Elementary functions, Functions on numbers
  864. @section Elementary rational functions
  865. Each of the classes @code{cl_RA}, @code{cl_I} defines the following operations:
  866. @table @code
  867. @item cl_I numerator (const @var{type}& x)
  868. @cindex @code{numerator ()}
  869. Returns the numerator of @code{x}.
  870. @item cl_I denominator (const @var{type}& x)
  871. @cindex @code{denominator ()}
  872. Returns the denominator of @code{x}.
  873. @end table
  874. The numerator and denominator of a rational number are normalized in such
  875. a way that they have no factor in common and the denominator is positive.
  876. @node Elementary complex functions, Comparisons, Elementary rational functions, Functions on numbers
  877. @section Elementary complex functions
  878. The class @code{cl_N} defines the following operation:
  879. @table @code
  880. @item cl_N complex (const cl_R& a, const cl_R& b)
  881. @cindex @code{complex ()}
  882. Returns the complex number @code{a+bi}, that is, the complex number with
  883. real part @code{a} and imaginary part @code{b}.
  884. @end table
  885. Each of the classes @code{cl_N}, @code{cl_R} defines the following operations:
  886. @table @code
  887. @item cl_R realpart (const @var{type}& x)
  888. @cindex @code{realpart ()}
  889. Returns the real part of @code{x}.
  890. @item cl_R imagpart (const @var{type}& x)
  891. @cindex @code{imagpart ()}
  892. Returns the imaginary part of @code{x}.
  893. @item @var{type} conjugate (const @var{type}& x)
  894. @cindex @code{conjugate ()}
  895. Returns the complex conjugate of @code{x}.
  896. @end table
  897. We have the relations
  898. @itemize @asis
  899. @item
  900. @code{x = complex(realpart(x), imagpart(x))}
  901. @item
  902. @code{conjugate(x) = complex(realpart(x), -imagpart(x))}
  903. @end itemize
  904. @node Comparisons, Rounding functions, Elementary complex functions, Functions on numbers
  905. @section Comparisons
  906. @cindex comparison
  907. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  908. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  909. defines the following operations:
  910. @table @code
  911. @item bool operator == (const @var{type}&, const @var{type}&)
  912. @cindex @code{operator == ()}
  913. @itemx bool operator != (const @var{type}&, const @var{type}&)
  914. @cindex @code{operator != ()}
  915. Comparison, as in C and C++.
  916. @item uint32 cl_equal_hashcode (const @var{type}&)
  917. @cindex @code{cl_equal_hashcode ()}
  918. Returns a 32-bit hash code that is the same for any two numbers which are
  919. the same according to @code{==}. This hash code depends on the number's value,
  920. not its type or precision.
  921. @item cl_boolean zerop (const @var{type}& x)
  922. @cindex @code{zerop ()}
  923. Compare against zero: @code{x == 0}
  924. @end table
  925. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  926. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  927. defines the following operations:
  928. @table @code
  929. @item cl_signean cl_compare (const @var{type}& x, const @var{type}& y)
  930. @cindex @code{cl_compare ()}
  931. Compares @code{x} and @code{y}. Returns +1 if @code{x}>@code{y},
  932. -1 if @code{x}<@code{y}, 0 if @code{x}=@code{y}.
  933. @item bool operator <= (const @var{type}&, const @var{type}&)
  934. @cindex @code{operator <= ()}
  935. @itemx bool operator < (const @var{type}&, const @var{type}&)
  936. @cindex @code{operator < ()}
  937. @itemx bool operator >= (const @var{type}&, const @var{type}&)
  938. @cindex @code{operator >= ()}
  939. @itemx bool operator > (const @var{type}&, const @var{type}&)
  940. @cindex @code{operator > ()}
  941. Comparison, as in C and C++.
  942. @item cl_boolean minusp (const @var{type}& x)
  943. @cindex @code{minusp ()}
  944. Compare against zero: @code{x < 0}
  945. @item cl_boolean plusp (const @var{type}& x)
  946. @cindex @code{plusp ()}
  947. Compare against zero: @code{x > 0}
  948. @item @var{type} max (const @var{type}& x, const @var{type}& y)
  949. @cindex @code{max ()}
  950. Return the maximum of @code{x} and @code{y}.
  951. @item @var{type} min (const @var{type}& x, const @var{type}& y)
  952. @cindex @code{min ()}
  953. Return the minimum of @code{x} and @code{y}.
  954. @end table
  955. When a floating point number and a rational number are compared, the float
  956. is first converted to a rational number using the function @code{rational}.
  957. Since a floating point number actually represents an interval of real numbers,
  958. the result might be surprising.
  959. For example, @code{(cl_F)(cl_R)"1/3" == (cl_R)"1/3"} returns false because
  960. there is no floating point number whose value is exactly @code{1/3}.
  961. @node Rounding functions, Roots, Comparisons, Functions on numbers
  962. @section Rounding functions
  963. @cindex rounding
  964. When a real number is to be converted to an integer, there is no ``best''
  965. rounding. The desired rounding function depends on the application.
  966. The Common Lisp and ISO Lisp standards offer four rounding functions:
  967. @table @code
  968. @item floor(x)
  969. This is the largest integer <=@code{x}.
  970. @item ceiling(x)
  971. This is the smallest integer >=@code{x}.
  972. @item truncate(x)
  973. Among the integers between 0 and @code{x} (inclusive) the one nearest to @code{x}.
  974. @item round(x)
  975. The integer nearest to @code{x}. If @code{x} is exactly halfway between two
  976. integers, choose the even one.
  977. @end table
  978. These functions have different advantages:
  979. @code{floor} and @code{ceiling} are translation invariant:
  980. @code{floor(x+n) = floor(x) + n} and @code{ceiling(x+n) = ceiling(x) + n}
  981. for every @code{x} and every integer @code{n}.
  982. On the other hand, @code{truncate} and @code{round} are symmetric:
  983. @code{truncate(-x) = -truncate(x)} and @code{round(-x) = -round(x)},
  984. and furthermore @code{round} is unbiased: on the ``average'', it rounds
  985. down exactly as often as it rounds up.
  986. The functions are related like this:
  987. @itemize @asis
  988. @item
  989. @code{ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1}
  990. for rational numbers @code{m/n} (@code{m}, @code{n} integers, @code{n}>0), and
  991. @item
  992. @code{truncate(x) = sign(x) * floor(abs(x))}
  993. @end itemize
  994. Each of the classes @code{cl_R}, @code{cl_RA},
  995. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  996. defines the following operations:
  997. @table @code
  998. @item cl_I floor1 (const @var{type}& x)
  999. @cindex @code{floor1 ()}
  1000. Returns @code{floor(x)}.
  1001. @item cl_I ceiling1 (const @var{type}& x)
  1002. @cindex @code{ceiling1 ()}
  1003. Returns @code{ceiling(x)}.
  1004. @item cl_I truncate1 (const @var{type}& x)
  1005. @cindex @code{truncate1 ()}
  1006. Returns @code{truncate(x)}.
  1007. @item cl_I round1 (const @var{type}& x)
  1008. @cindex @code{round1 ()}
  1009. Returns @code{round(x)}.
  1010. @end table
  1011. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  1012. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1013. defines the following operations:
  1014. @table @code
  1015. @item cl_I floor1 (const @var{type}& x, const @var{type}& y)
  1016. Returns @code{floor(x/y)}.
  1017. @item cl_I ceiling1 (const @var{type}& x, const @var{type}& y)
  1018. Returns @code{ceiling(x/y)}.
  1019. @item cl_I truncate1 (const @var{type}& x, const @var{type}& y)
  1020. Returns @code{truncate(x/y)}.
  1021. @item cl_I round1 (const @var{type}& x, const @var{type}& y)
  1022. Returns @code{round(x/y)}.
  1023. @end table
  1024. These functions are called @samp{floor1}, @dots{} here instead of
  1025. @samp{floor}, @dots{}, because on some systems, system dependent include
  1026. files define @samp{floor} and @samp{ceiling} as macros.
  1027. In many cases, one needs both the quotient and the remainder of a division.
  1028. It is more efficient to compute both at the same time than to perform
  1029. two divisions, one for quotient and the next one for the remainder.
  1030. The following functions therefore return a structure containing both
  1031. the quotient and the remainder. The suffix @samp{2} indicates the number
  1032. of ``return values''. The remainder is defined as follows:
  1033. @itemize @bullet
  1034. @item
  1035. for the computation of @code{quotient = floor(x)},
  1036. @code{remainder = x - quotient},
  1037. @item
  1038. for the computation of @code{quotient = floor(x,y)},
  1039. @code{remainder = x - quotient*y},
  1040. @end itemize
  1041. and similarly for the other three operations.
  1042. Each of the classes @code{cl_R}, @code{cl_RA},
  1043. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1044. defines the following operations:
  1045. @table @code
  1046. @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
  1047. @itemx @var{type}_div_t floor2 (const @var{type}& x)
  1048. @itemx @var{type}_div_t ceiling2 (const @var{type}& x)
  1049. @itemx @var{type}_div_t truncate2 (const @var{type}& x)
  1050. @itemx @var{type}_div_t round2 (const @var{type}& x)
  1051. @end table
  1052. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  1053. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1054. defines the following operations:
  1055. @table @code
  1056. @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
  1057. @itemx @var{type}_div_t floor2 (const @var{type}& x, const @var{type}& y)
  1058. @cindex @code{floor2 ()}
  1059. @itemx @var{type}_div_t ceiling2 (const @var{type}& x, const @var{type}& y)
  1060. @cindex @code{ceiling2 ()}
  1061. @itemx @var{type}_div_t truncate2 (const @var{type}& x, const @var{type}& y)
  1062. @cindex @code{truncate2 ()}
  1063. @itemx @var{type}_div_t round2 (const @var{type}& x, const @var{type}& y)
  1064. @cindex @code{round2 ()}
  1065. @end table
  1066. Sometimes, one wants the quotient as a floating-point number (of the
  1067. same format as the argument, if the argument is a float) instead of as
  1068. an integer. The prefix @samp{f} indicates this.
  1069. Each of the classes
  1070. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1071. defines the following operations:
  1072. @table @code
  1073. @item @var{type} ffloor (const @var{type}& x)
  1074. @cindex @code{ffloor ()}
  1075. @itemx @var{type} fceiling (const @var{type}& x)
  1076. @cindex @code{fceiling ()}
  1077. @itemx @var{type} ftruncate (const @var{type}& x)
  1078. @cindex @code{ftruncate ()}
  1079. @itemx @var{type} fround (const @var{type}& x)
  1080. @cindex @code{fround ()}
  1081. @end table
  1082. and similarly for class @code{cl_R}, but with return type @code{cl_F}.
  1083. The class @code{cl_R} defines the following operations:
  1084. @table @code
  1085. @item cl_F ffloor (const @var{type}& x, const @var{type}& y)
  1086. @itemx cl_F fceiling (const @var{type}& x, const @var{type}& y)
  1087. @itemx cl_F ftruncate (const @var{type}& x, const @var{type}& y)
  1088. @itemx cl_F fround (const @var{type}& x, const @var{type}& y)
  1089. @end table
  1090. These functions also exist in versions which return both the quotient
  1091. and the remainder. The suffix @samp{2} indicates this.
  1092. Each of the classes
  1093. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1094. defines the following operations:
  1095. @table @code
  1096. @item struct @var{type}_fdiv_t @{ @var{type} quotient; @var{type} remainder; @};
  1097. @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x)
  1098. @cindex @code{ffloor2 ()}
  1099. @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x)
  1100. @cindex @code{fceiling2 ()}
  1101. @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x)
  1102. @cindex @code{ftruncate2 ()}
  1103. @itemx @var{type}_fdiv_t fround2 (const @var{type}& x)
  1104. @cindex @code{fround2 ()}
  1105. @end table
  1106. and similarly for class @code{cl_R}, but with quotient type @code{cl_F}.
  1107. The class @code{cl_R} defines the following operations:
  1108. @table @code
  1109. @item struct @var{type}_fdiv_t @{ cl_F quotient; cl_R remainder; @};
  1110. @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x, const @var{type}& y)
  1111. @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x, const @var{type}& y)
  1112. @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x, const @var{type}& y)
  1113. @itemx @var{type}_fdiv_t fround2 (const @var{type}& x, const @var{type}& y)
  1114. @end table
  1115. Other applications need only the remainder of a division.
  1116. The remainder of @samp{floor} and @samp{ffloor} is called @samp{mod}
  1117. (abbreviation of ``modulo''). The remainder @samp{truncate} and
  1118. @samp{ftruncate} is called @samp{rem} (abbreviation of ``remainder'').
  1119. @itemize @bullet
  1120. @item
  1121. @code{mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y}
  1122. @item
  1123. @code{rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y}
  1124. @end itemize
  1125. If @code{x} and @code{y} are both >= 0, @code{mod(x,y) = rem(x,y) >= 0}.
  1126. In general, @code{mod(x,y)} has the sign of @code{y} or is zero,
  1127. and @code{rem(x,y)} has the sign of @code{x} or is zero.
  1128. The classes @code{cl_R}, @code{cl_I} define the following operations:
  1129. @table @code
  1130. @item @var{type} mod (const @var{type}& x, const @var{type}& y)
  1131. @cindex @code{mod ()}
  1132. @itemx @var{type} rem (const @var{type}& x, const @var{type}& y)
  1133. @cindex @code{rem ()}
  1134. @end table
  1135. @node Roots, Transcendental functions, Rounding functions, Functions on numbers
  1136. @section Roots
  1137. Each of the classes @code{cl_R},
  1138. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1139. defines the following operation:
  1140. @table @code
  1141. @item @var{type} sqrt (const @var{type}& x)
  1142. @cindex @code{sqrt ()}
  1143. @code{x} must be >= 0. This function returns the square root of @code{x},
  1144. normalized to be >= 0. If @code{x} is the square of a rational number,
  1145. @code{sqrt(x)} will be a rational number, else it will return a
  1146. floating-point approximation.
  1147. @end table
  1148. The classes @code{cl_RA}, @code{cl_I} define the following operation:
  1149. @table @code
  1150. @item cl_boolean sqrtp (const @var{type}& x, @var{type}* root)
  1151. @cindex @code{sqrtp ()}
  1152. This tests whether @code{x} is a perfect square. If so, it returns true
  1153. and the exact square root in @code{*root}, else it returns false.
  1154. @end table
  1155. Furthermore, for integers, similarly:
  1156. @table @code
  1157. @item cl_boolean isqrt (const @var{type}& x, @var{type}* root)
  1158. @cindex @code{isqrt ()}
  1159. @code{x} should be >= 0. This function sets @code{*root} to
  1160. @code{floor(sqrt(x))} and returns the same value as @code{sqrtp}:
  1161. the boolean value @code{(expt(*root,2) == x)}.
  1162. @end table
  1163. For @code{n}th roots, the classes @code{cl_RA}, @code{cl_I}
  1164. define the following operation:
  1165. @table @code
  1166. @item cl_boolean rootp (const @var{type}& x, const cl_I& n, @var{type}* root)
  1167. @cindex @code{rootp ()}
  1168. @code{x} must be >= 0. @code{n} must be > 0.
  1169. This tests whether @code{x} is an @code{n}th power of a rational number.
  1170. If so, it returns true and the exact root in @code{*root}, else it returns
  1171. false.
  1172. @end table
  1173. The only square root function which accepts negative numbers is the one
  1174. for class @code{cl_N}:
  1175. @table @code
  1176. @item cl_N sqrt (const cl_N& z)
  1177. @cindex @code{sqrt ()}
  1178. Returns the square root of @code{z}, as defined by the formula
  1179. @code{sqrt(z) = exp(log(z)/2)}. Conversion to a floating-point type
  1180. or to a complex number are done if necessary. The range of the result is the
  1181. right half plane @code{realpart(sqrt(z)) >= 0}
  1182. including the positive imaginary axis and 0, but excluding
  1183. the negative imaginary axis.
  1184. The result is an exact number only if @code{z} is an exact number.
  1185. @end table
  1186. @node Transcendental functions, Functions on integers, Roots, Functions on numbers
  1187. @section Transcendental functions
  1188. @cindex transcendental functions
  1189. The transcendental functions return an exact result if the argument
  1190. is exact and the result is exact as well. Otherwise they must return
  1191. inexact numbers even if the argument is exact.
  1192. For example, @code{cos(0) = 1} returns the rational number @code{1}.
  1193. @menu
  1194. * Exponential and logarithmic functions::
  1195. * Trigonometric functions::
  1196. * Hyperbolic functions::
  1197. * Euler gamma::
  1198. * Riemann zeta::
  1199. @end menu
  1200. @node Exponential and logarithmic functions, Trigonometric functions, Transcendental functions, Transcendental functions
  1201. @subsection Exponential and logarithmic functions
  1202. @table @code
  1203. @item cl_R exp (const cl_R& x)
  1204. @cindex @code{exp ()}
  1205. @itemx cl_N exp (const cl_N& x)
  1206. Returns the exponential function of @code{x}. This is @code{e^x} where
  1207. @code{e} is the base of the natural logarithms. The range of the result
  1208. is the entire complex plane excluding 0.
  1209. @item cl_R ln (const cl_R& x)
  1210. @cindex @code{ln ()}
  1211. @code{x} must be > 0. Returns the (natural) logarithm of x.
  1212. @item cl_N log (const cl_N& x)
  1213. @cindex @code{log ()}
  1214. Returns the (natural) logarithm of x. If @code{x} is real and positive,
  1215. this is @code{ln(x)}. In general, @code{log(x) = log(abs(x)) + i*phase(x)}.
  1216. The range of the result is the strip in the complex plane
  1217. @code{-pi < imagpart(log(x)) <= pi}.
  1218. @item cl_R phase (const cl_N& x)
  1219. @cindex @code{phase ()}
  1220. Returns the angle part of @code{x} in its polar representation as a
  1221. complex number. That is, @code{phase(x) = atan(realpart(x),imagpart(x))}.
  1222. This is also the imaginary part of @code{log(x)}.
  1223. The range of the result is the interval @code{-pi < phase(x) <= pi}.
  1224. The result will be an exact number only if @code{zerop(x)} or
  1225. if @code{x} is real and positive.
  1226. @item cl_R log (const cl_R& a, const cl_R& b)
  1227. @code{a} and @code{b} must be > 0. Returns the logarithm of @code{a} with
  1228. respect to base @code{b}. @code{log(a,b) = ln(a)/ln(b)}.
  1229. The result can be exact only if @code{a = 1} or if @code{a} and @code{b}
  1230. are both rational.
  1231. @item cl_N log (const cl_N& a, const cl_N& b)
  1232. Returns the logarithm of @code{a} with respect to base @code{b}.
  1233. @code{log(a,b) = log(a)/log(b)}.
  1234. @item cl_N expt (const cl_N& x, const cl_N& y)
  1235. @cindex @code{expt ()}
  1236. Exponentiation: Returns @code{x^y = exp(y*log(x))}.
  1237. @end table
  1238. The constant e = exp(1) = 2.71828@dots{} is returned by the following functions:
  1239. @table @code
  1240. @item cl_F cl_exp1 (cl_float_format_t f)
  1241. @cindex @code{exp1 ()}
  1242. Returns e as a float of format @code{f}.
  1243. @item cl_F cl_exp1 (const cl_F& y)
  1244. Returns e in the float format of @code{y}.
  1245. @item cl_F cl_exp1 (void)
  1246. Returns e as a float of format @code{cl_default_float_format}.
  1247. @end table
  1248. @node Trigonometric functions, Hyperbolic functions, Exponential and logarithmic functions, Transcendental functions
  1249. @subsection Trigonometric functions
  1250. @table @code
  1251. @item cl_R sin (const cl_R& x)
  1252. @cindex @code{sin ()}
  1253. Returns @code{sin(x)}. The range of the result is the interval
  1254. @code{-1 <= sin(x) <= 1}.
  1255. @item cl_N sin (const cl_N& z)
  1256. Returns @code{sin(z)}. The range of the result is the entire complex plane.
  1257. @item cl_R cos (const cl_R& x)
  1258. @cindex @code{cos ()}
  1259. Returns @code{cos(x)}. The range of the result is the interval
  1260. @code{-1 <= cos(x) <= 1}.
  1261. @item cl_N cos (const cl_N& x)
  1262. Returns @code{cos(z)}. The range of the result is the entire complex plane.
  1263. @item struct cl_cos_sin_t @{ cl_R cos; cl_R sin; @};
  1264. @cindex @code{cl_cos_sin_t}
  1265. @itemx cl_cos_sin_t cl_cos_sin (const cl_R& x)
  1266. Returns both @code{sin(x)} and @code{cos(x)}. This is more efficient than
  1267. @cindex @code{cl_cos_sin ()}
  1268. computing them separately. The relation @code{cos^2 + sin^2 = 1} will
  1269. hold only approximately.
  1270. @item cl_R tan (const cl_R& x)
  1271. @cindex @code{tan ()}
  1272. @itemx cl_N tan (const cl_N& x)
  1273. Returns @code{tan(x) = sin(x)/cos(x)}.
  1274. @item cl_N cis (const cl_R& x)
  1275. @cindex @code{cis ()}
  1276. @itemx cl_N cis (const cl_N& x)
  1277. Returns @code{exp(i*x)}. The name @samp{cis} means ``cos + i sin'', because
  1278. @code{e^(i*x) = cos(x) + i*sin(x)}.
  1279. @cindex @code{asin}
  1280. @cindex @code{asin ()}
  1281. @item cl_N asin (const cl_N& z)
  1282. Returns @code{arcsin(z)}. This is defined as
  1283. @code{arcsin(z) = log(iz+sqrt(1-z^2))/i} and satisfies
  1284. @code{arcsin(-z) = -arcsin(z)}.
  1285. The range of the result is the strip in the complex domain
  1286. @code{-pi/2 <= realpart(arcsin(z)) <= pi/2}, excluding the numbers
  1287. with @code{realpart = -pi/2} and @code{imagpart < 0} and the numbers
  1288. with @code{realpart = pi/2} and @code{imagpart > 0}.
  1289. @ignore
  1290. Proof: This follows from arcsin(z) = arsinh(iz)/i and the corresponding
  1291. results for arsinh.
  1292. @end ignore
  1293. @item cl_N acos (const cl_N& z)
  1294. @cindex @code{acos ()}
  1295. Returns @code{arccos(z)}. This is defined as
  1296. @code{arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i}
  1297. @ignore
  1298. Kahan's formula:
  1299. @code{arccos(z) = 2*log(sqrt((1+z)/2)+i*sqrt((1-z)/2))/i}
  1300. @end ignore
  1301. and satisfies @code{arccos(-z) = pi - arccos(z)}.
  1302. The range of the result is the strip in the complex domain
  1303. @code{0 <= realpart(arcsin(z)) <= pi}, excluding the numbers
  1304. with @code{realpart = 0} and @code{imagpart < 0} and the numbers
  1305. with @code{realpart = pi} and @code{imagpart > 0}.
  1306. @ignore
  1307. Proof: This follows from the results about arcsin.
  1308. @end ignore
  1309. @cindex @code{atan}
  1310. @cindex @code{atan ()}
  1311. @item cl_R atan (const cl_R& x, const cl_R& y)
  1312. Returns the angle of the polar representation of the complex number
  1313. @code{x+iy}. This is @code{atan(y/x)} if @code{x>0}. The range of
  1314. the result is the interval @code{-pi < atan(x,y) <= pi}. The result will
  1315. be an exact number only if @code{x > 0} and @code{y} is the exact @code{0}.
  1316. WARNING: In Common Lisp, this function is called as @code{(atan y x)},
  1317. with reversed order of arguments.
  1318. @item cl_R atan (const cl_R& x)
  1319. Returns @code{arctan(x)}. This is the same as @code{atan(1,x)}. The range
  1320. of the result is the interval @code{-pi/2 < atan(x) < pi/2}. The result
  1321. will be an exact number only if @code{x} is the exact @code{0}.
  1322. @item cl_N atan (const cl_N& z)
  1323. Returns @code{arctan(z)}. This is defined as
  1324. @code{arctan(z) = (log(1+iz)-log(1-iz)) / 2i} and satisfies
  1325. @code{arctan(-z) = -arctan(z)}. The range of the result is
  1326. the strip in the complex domain
  1327. @code{-pi/2 <= realpart(arctan(z)) <= pi/2}, excluding the numbers
  1328. with @code{realpart = -pi/2} and @code{imagpart >= 0} and the numbers
  1329. with @code{realpart = pi/2} and @code{imagpart <= 0}.
  1330. @ignore
  1331. Proof: arctan(z) = artanh(iz)/i, we know the range of the artanh function.
  1332. @end ignore
  1333. @end table
  1334. @cindex pi
  1335. @cindex Archimedes' constant
  1336. Archimedes' constant pi = 3.14@dots{} is returned by the following functions:
  1337. @table @code
  1338. @item cl_F cl_pi (cl_float_format_t f)
  1339. @cindex @code{cl_pi}
  1340. Returns pi as a float of format @code{f}.
  1341. @item cl_F cl_pi (const cl_F& y)
  1342. Returns pi in the float format of @code{y}.
  1343. @item cl_F cl_pi (void)
  1344. Returns pi as a float of format @code{cl_default_float_format}.
  1345. @end table
  1346. @node Hyperbolic functions, Euler gamma, Trigonometric functions, Transcendental functions
  1347. @subsection Hyperbolic functions
  1348. @table @code
  1349. @item cl_R sinh (const cl_R& x)
  1350. @cindex @code{sinh ()}
  1351. Returns @code{sinh(x)}.
  1352. @item cl_N sinh (const cl_N& z)
  1353. Returns @code{sinh(z)}. The range of the result is the entire complex plane.
  1354. @item cl_R cosh (const cl_R& x)
  1355. @cindex @code{cosh ()}
  1356. Returns @code{cosh(x)}. The range of the result is the interval
  1357. @code{cosh(x) >= 1}.
  1358. @item cl_N cosh (const cl_N& z)
  1359. Returns @code{cosh(z)}. The range of the result is the entire complex plane.
  1360. @item struct cl_cosh_sinh_t @{ cl_R cosh; cl_R sinh; @};
  1361. @cindex @code{cl_cosh_sinh_t}
  1362. @itemx cl_cosh_sinh_t cl_cosh_sinh (const cl_R& x)
  1363. @cindex @code{cl_cosh_sinh ()}
  1364. Returns both @code{sinh(x)} and @code{cosh(x)}. This is more efficient than
  1365. computing them separately. The relation @code{cosh^2 - sinh^2 = 1} will
  1366. hold only approximately.
  1367. @item cl_R tanh (const cl_R& x)
  1368. @cindex @code{tanh ()}
  1369. @itemx cl_N tanh (const cl_N& x)
  1370. Returns @code{tanh(x) = sinh(x)/cosh(x)}.
  1371. @item cl_N asinh (const cl_N& z)
  1372. @cindex @code{asinh ()}
  1373. Returns @code{arsinh(z)}. This is defined as
  1374. @code{arsinh(z) = log(z+sqrt(1+z^2))} and satisfies
  1375. @code{arsinh(-z) = -arsinh(z)}.
  1376. @ignore
  1377. Proof: Knowing the range of log, we know -pi < imagpart(arsinh(z)) <= pi.
  1378. Actually, z+sqrt(1+z^2) can never be real and <0, so
  1379. -pi < imagpart(arsinh(z)) < pi.
  1380. We have (z+sqrt(1+z^2))*(-z+sqrt(1+(-z)^2)) = (1+z^2)-z^2 = 1, hence the
  1381. logs of both factors sum up to 0 mod 2*pi*i, hence to 0.
  1382. @end ignore
  1383. The range of the result is the strip in the complex domain
  1384. @code{-pi/2 <= imagpart(arsinh(z)) <= pi/2}, excluding the numbers
  1385. with @code{imagpart = -pi/2} and @code{realpart > 0} and the numbers
  1386. with @code{imagpart = pi/2} and @code{realpart < 0}.
  1387. @ignore
  1388. Proof: Write z = x+iy. Because of arsinh(-z) = -arsinh(z), we may assume
  1389. that z is in Range(sqrt), that is, x>=0 and, if x=0, then y>=0.
  1390. If x > 0, then Re(z+sqrt(1+z^2)) = x + Re(sqrt(1+z^2)) >= x > 0,
  1391. so -pi/2 < imagpart(log(z+sqrt(1+z^2))) < pi/2.
  1392. If x = 0 and y >= 0, arsinh(z) = log(i*y+sqrt(1-y^2)).
  1393. If y <= 1, the realpart is 0 and the imagpart is >= 0 and <= pi/2.
  1394. If y >= 1, the imagpart is pi/2 and the realpart is
  1395. log(y+sqrt(y^2-1)) >= log(y) >= 0.
  1396. @end ignore
  1397. @ignore
  1398. Moreover, if z is in Range(sqrt),
  1399. log(sqrt(1+z^2)+z) = 2 artanh(z/(1+sqrt(1+z^2)))
  1400. (for a proof, see file src/cl_C_asinh.cc).
  1401. @end ignore
  1402. @item cl_N acosh (const cl_N& z)
  1403. @cindex @code{acosh ()}
  1404. Returns @code{arcosh(z)}. This is defined as
  1405. @code{arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))}.
  1406. The range of the result is the half-strip in the complex domain
  1407. @code{-pi < imagpart(arcosh(z)) <= pi, realpart(arcosh(z)) >= 0},
  1408. excluding the numbers with @code{realpart = 0} and @code{-pi < imagpart < 0}.
  1409. @ignore
  1410. Proof: sqrt((z+1)/2) and sqrt((z-1)/2)) lie in Range(sqrt), hence does
  1411. their sum, hence its log has an imagpart <= pi/2 and > -pi/2.
  1412. If z is in Range(sqrt), we have
  1413. sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1)
  1414. ==> (sqrt((z+1)/2)+sqrt((z-1)/2))^2 = (z+1)/2 + sqrt(z^2-1) + (z-1)/2
  1415. = z + sqrt(z^2-1)
  1416. ==> arcosh(z) = log(z+sqrt(z^2-1)) mod 2*pi*i
  1417. and since the imagpart of both expressions is > -pi, <= pi
  1418. ==> arcosh(z) = log(z+sqrt(z^2-1))
  1419. To prove that the realpart of this is >= 0, write z = x+iy with x>=0,
  1420. z^2-1 = u+iv with u = x^2-y^2-1, v = 2xy,
  1421. sqrt(z^2-1) = p+iq with p = sqrt((sqrt(u^2+v^2)+u)/2) >= 0,
  1422. q = sqrt((sqrt(u^2+v^2)-u)/2) * sign(v),
  1423. then |z+sqrt(z^2-1)|^2 = |x+iy + p+iq|^2
  1424. = (x+p)^2 + (y+q)^2
  1425. = x^2 + 2xp + p^2 + y^2 + 2yq + q^2
  1426. >= x^2 + p^2 + y^2 + q^2 (since x>=0, p>=0, yq>=0)
  1427. = x^2 + y^2 + sqrt(u^2+v^2)
  1428. >= x^2 + y^2 + |u|
  1429. >= x^2 + y^2 - u
  1430. = 1 + 2*y^2
  1431. >= 1
  1432. hence realpart(log(z+sqrt(z^2-1))) = log(|z+sqrt(z^2-1)|) >= 0.
  1433. Equality holds only if y = 0 and u <= 0, i.e. 0 <= x < 1.
  1434. In this case arcosh(z) = log(x+i*sqrt(1-x^2)) has imagpart >=0.
  1435. Otherwise, -z is in Range(sqrt).
  1436. If y != 0, sqrt((z+1)/2) = i^sign(y) * sqrt((-z-1)/2),
  1437. sqrt((z-1)/2) = i^sign(y) * sqrt((-z+1)/2),
  1438. hence arcosh(z) = sign(y)*pi/2*i + arcosh(-z),
  1439. and this has realpart > 0.
  1440. If y = 0 and -1<=x<=0, we still have sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1),
  1441. ==> arcosh(z) = log(z+sqrt(z^2-1)) = log(x+i*sqrt(1-x^2))
  1442. has realpart = 0 and imagpart > 0.
  1443. If y = 0 and x<=-1, however, sqrt(z+1)*sqrt(z-1) = - sqrt(z^2-1),
  1444. ==> arcosh(z) = log(z-sqrt(z^2-1)) = pi*i + arcosh(-z).
  1445. This has realpart >= 0 and imagpart = pi.
  1446. @end ignore
  1447. @item cl_N atanh (const cl_N& z)
  1448. @cindex @code{atanh ()}
  1449. Returns @code{artanh(z)}. This is defined as
  1450. @code{artanh(z) = (log(1+z)-log(1-z)) / 2} and satisfies
  1451. @code{artanh(-z) = -artanh(z)}. The range of the result is
  1452. the strip in the complex domain
  1453. @code{-pi/2 <= imagpart(artanh(z)) <= pi/2}, excluding the numbers
  1454. with @code{imagpart = -pi/2} and @code{realpart <= 0} and the numbers
  1455. with @code{imagpart = pi/2} and @code{realpart >= 0}.
  1456. @ignore
  1457. Proof: Write z = x+iy. Examine
  1458. imagpart(artanh(z)) = (atan(1+x,y) - atan(1-x,-y))/2.
  1459. Case 1: y = 0.
  1460. x > 1 ==> imagpart = -pi/2, realpart = 1/2 log((x+1)/(x-1)) > 0,
  1461. x < -1 ==> imagpart = pi/2, realpart = 1/2 log((-x-1)/(-x+1)) < 0,
  1462. |x| < 1 ==> imagpart = 0
  1463. Case 2: y > 0.
  1464. imagpart(artanh(z))
  1465. = (atan(1+x,y) - atan(1-x,-y))/2
  1466. = ((pi/2 - atan((1+x)/y)) - (-pi/2 - atan((1-x)/-y)))/2
  1467. = (pi - atan((1+x)/y) - atan((1-x)/y))/2
  1468. > (pi - pi/2 - pi/2 )/2 = 0
  1469. and (1+x)/y > (1-x)/y
  1470. ==> atan((1+x)/y) > atan((-1+x)/y) = - atan((1-x)/y)
  1471. ==> imagpart < pi/2.
  1472. Hence 0 < imagpart < pi/2.
  1473. Case 3: y < 0.
  1474. By artanh(z) = -artanh(-z) and case 2, -pi/2 < imagpart < 0.
  1475. @end ignore
  1476. @end table
  1477. @node Euler gamma, Riemann zeta, Hyperbolic functions, Transcendental functions
  1478. @subsection Euler gamma
  1479. @cindex Euler's constant
  1480. Euler's constant C = 0.577@dots{} is returned by the following functions:
  1481. @table @code
  1482. @item cl_F cl_eulerconst (cl_float_format_t f)
  1483. @cindex @code{cl_eulerconst ()}
  1484. Returns Euler's constant as a float of format @code{f}.
  1485. @item cl_F cl_eulerconst (const cl_F& y)
  1486. Returns Euler's constant in the float format of @code{y}.
  1487. @item cl_F cl_eulerconst (void)
  1488. Returns Euler's constant as a float of format @code{cl_default_float_format}.
  1489. @end table
  1490. Catalan's constant G = 0.915@dots{} is returned by the following functions:
  1491. @cindex Catalan's constant
  1492. @table @code
  1493. @item cl_F cl_catalanconst (cl_float_format_t f)
  1494. @cindex @code{cl_catalanconst ()}
  1495. Returns Catalan's constant as a float of format @code{f}.
  1496. @item cl_F cl_catalanconst (const cl_F& y)
  1497. Returns Catalan's constant in the float format of @code{y}.
  1498. @item cl_F cl_catalanconst (void)
  1499. Returns Catalan's constant as a float of format @code{cl_default_float_format}.
  1500. @end table
  1501. @node Riemann zeta, , Euler gamma, Transcendental functions
  1502. @subsection Riemann zeta
  1503. @cindex Riemann's zeta
  1504. Riemann's zeta function at an integral point @code{s>1} is returned by the
  1505. following functions:
  1506. @table @code
  1507. @item cl_F cl_zeta (int s, cl_float_format_t f)
  1508. @cindex @code{cl_zeta ()}
  1509. Returns Riemann's zeta function at @code{s} as a float of format @code{f}.
  1510. @item cl_F cl_zeta (int s, const cl_F& y)
  1511. Returns Riemann's zeta function at @code{s} in the float format of @code{y}.
  1512. @item cl_F cl_zeta (int s)
  1513. Returns Riemann's zeta function at @code{s} as a float of format
  1514. @code{cl_default_float_format}.
  1515. @end table
  1516. @node Functions on integers, Functions on floating-point numbers, Transcendental functions, Functions on numbers
  1517. @section Functions on integers
  1518. @menu
  1519. * Logical functions::
  1520. * Number theoretic functions::
  1521. * Combinatorial functions::
  1522. @end menu
  1523. @node Logical functions, Number theoretic functions, Functions on integers, Functions on integers
  1524. @subsection Logical functions
  1525. Integers, when viewed as in two's complement notation, can be thought as
  1526. infinite bit strings where the bits' values eventually are constant.
  1527. For example,
  1528. @example
  1529. 17 = ......00010001
  1530. -6 = ......11111010
  1531. @end example
  1532. The logical operations view integers as such bit strings and operate
  1533. on each of the bit positions in parallel.
  1534. @table @code
  1535. @item cl_I lognot (const cl_I& x)
  1536. @cindex @code{lognot ()}
  1537. @itemx cl_I operator ~ (const cl_I& x)
  1538. @cindex @code{operator ~ ()}
  1539. Logical not, like @code{~x} in C. This is the same as @code{-1-x}.
  1540. @item cl_I logand (const cl_I& x, const cl_I& y)
  1541. @cindex @code{logand ()}
  1542. @itemx cl_I operator & (const cl_I& x, const cl_I& y)
  1543. @cindex @code{operator & ()}
  1544. Logical and, like @code{x & y} in C.
  1545. @item cl_I logior (const cl_I& x, const cl_I& y)
  1546. @cindex @code{logior ()}
  1547. @itemx cl_I operator | (const cl_I& x, const cl_I& y)
  1548. @cindex @code{operator | ()}
  1549. Logical (inclusive) or, like @code{x | y} in C.
  1550. @item cl_I logxor (const cl_I& x, const cl_I& y)
  1551. @cindex @code{logxor ()}
  1552. @itemx cl_I operator ^ (const cl_I& x, const cl_I& y)
  1553. @cindex @code{operator ^ ()}
  1554. Exclusive or, like @code{x ^ y} in C.
  1555. @item cl_I logeqv (const cl_I& x, const cl_I& y)
  1556. @cindex @code{logeqv ()}
  1557. Bitwise equivalence, like @code{~(x ^ y)} in C.
  1558. @item cl_I lognand (const cl_I& x, const cl_I& y)
  1559. @cindex @code{lognand ()}
  1560. Bitwise not and, like @code{~(x & y)} in C.
  1561. @item cl_I lognor (const cl_I& x, const cl_I& y)
  1562. @cindex @code{lognor ()}
  1563. Bitwise not or, like @code{~(x | y)} in C.
  1564. @item cl_I logandc1 (const cl_I& x, const cl_I& y)
  1565. @cindex @code{logandc1 ()}
  1566. Logical and, complementing the first argument, like @code{~x & y} in C.
  1567. @item cl_I logandc2 (const cl_I& x, const cl_I& y)
  1568. @cindex @code{logandc2 ()}
  1569. Logical and, complementing the second argument, like @code{x & ~y} in C.
  1570. @item cl_I logorc1 (const cl_I& x, const cl_I& y)
  1571. @cindex @code{logorc1 ()}
  1572. Logical or, complementing the first argument, like @code{~x | y} in C.
  1573. @item cl_I logorc2 (const cl_I& x, const cl_I& y)
  1574. @cindex @code{logorc2 ()}
  1575. Logical or, complementing the second argument, like @code{x | ~y} in C.
  1576. @end table
  1577. These operations are all available though the function
  1578. @table @code
  1579. @item cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)
  1580. @cindex @code{boole ()}
  1581. @end table
  1582. where @code{op} must have one of the 16 values (each one stands for a function
  1583. which combines two bits into one bit): @code{boole_clr}, @code{boole_set},
  1584. @code{boole_1}, @code{boole_2}, @code{boole_c1}, @code{boole_c2},
  1585. @code{boole_and}, @code{boole_ior}, @code{boole_xor}, @code{boole_eqv},
  1586. @code{boole_nand}, @code{boole_nor}, @code{boole_andc1}, @code{boole_andc2},
  1587. @code{boole_orc1}, @code{boole_orc2}.
  1588. @cindex @code{boole_clr}
  1589. @cindex @code{boole_set}
  1590. @cindex @code{boole_1}
  1591. @cindex @code{boole_2}
  1592. @cindex @code{boole_c1}
  1593. @cindex @code{boole_c2}
  1594. @cindex @code{boole_and}
  1595. @cindex @code{boole_xor}
  1596. @cindex @code{boole_eqv}
  1597. @cindex @code{boole_nand}
  1598. @cindex @code{boole_nor}
  1599. @cindex @code{boole_andc1}
  1600. @cindex @code{boole_andc2}
  1601. @cindex @code{boole_orc1}
  1602. @cindex @code{boole_orc2}
  1603. Other functions that view integers as bit strings:
  1604. @table @code
  1605. @item cl_boolean logtest (const cl_I& x, const cl_I& y)
  1606. @cindex @code{logtest ()}
  1607. Returns true if some bit is set in both @code{x} and @code{y}, i.e. if
  1608. @code{logand(x,y) != 0}.
  1609. @item cl_boolean logbitp (const cl_I& n, const cl_I& x)
  1610. @cindex @code{logbitp ()}
  1611. Returns true if the @code{n}th bit (from the right) of @code{x} is set.
  1612. Bit 0 is the least significant bit.
  1613. @item uintL logcount (const cl_I& x)
  1614. @cindex @code{logcount ()}
  1615. Returns the number of one bits in @code{x}, if @code{x} >= 0, or
  1616. the number of zero bits in @code{x}, if @code{x} < 0.
  1617. @end table
  1618. The following functions operate on intervals of bits in integers.
  1619. The type
  1620. @example
  1621. struct cl_byte @{ uintL size; uintL position; @};
  1622. @end example
  1623. @cindex @code{cl_byte}
  1624. represents the bit interval containing the bits
  1625. @code{position}@dots{}@code{position+size-1} of an integer.
  1626. The constructor @code{cl_byte(size,position)} constructs a @code{cl_byte}.
  1627. @table @code
  1628. @item cl_I ldb (const cl_I& n, const cl_byte& b)
  1629. @cindex @code{ldb ()}
  1630. extracts the bits of @code{n} described by the bit interval @code{b}
  1631. and returns them as a nonnegative integer with @code{b.size} bits.
  1632. @item cl_boolean ldb_test (const cl_I& n, const cl_byte& b)
  1633. @cindex @code{ldb_test ()}
  1634. Returns true if some bit described by the bit interval @code{b} is set in
  1635. @code{n}.
  1636. @item cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
  1637. @cindex @code{dpb ()}
  1638. Returns @code{n}, with the bits described by the bit interval @code{b}
  1639. replaced by @code{newbyte}. Only the lowest @code{b.size} bits of
  1640. @code{newbyte} are relevant.
  1641. @end table
  1642. The functions @code{ldb} and @code{dpb} implicitly shift. The following
  1643. functions are their counterparts without shifting:
  1644. @table @code
  1645. @item cl_I mask_field (const cl_I& n, const cl_byte& b)
  1646. @cindex @code{mask_field ()}
  1647. returns an integer with the bits described by the bit interval @code{b}
  1648. copied from the corresponding bits in @code{n}, the other bits zero.
  1649. @item cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
  1650. @cindex @code{deposit_field ()}
  1651. returns an integer where the bits described by the bit interval @code{b}
  1652. come from @code{newbyte} and the other bits come from @code{n}.
  1653. @end table
  1654. The following relations hold:
  1655. @itemize @asis
  1656. @item
  1657. @code{ldb (n, b) = mask_field(n, b) >> b.position},
  1658. @item
  1659. @code{dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)},
  1660. @item
  1661. @code{deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)}.
  1662. @end itemize
  1663. The following operations on integers as bit strings are efficient shortcuts
  1664. for common arithmetic operations:
  1665. @table @code
  1666. @item cl_boolean oddp (const cl_I& x)
  1667. @cindex @code{oddp ()}
  1668. Returns true if the least significant bit of @code{x} is 1. Equivalent to
  1669. @code{mod(x,2) != 0}.
  1670. @item cl_boolean evenp (const cl_I& x)
  1671. @cindex @code{evenp ()}
  1672. Returns true if the least significant bit of @code{x} is 0. Equivalent to
  1673. @code{mod(x,2) == 0}.
  1674. @item cl_I operator << (const cl_I& x, const cl_I& n)
  1675. @cindex @code{operator << ()}
  1676. Shifts @code{x} by @code{n} bits to the left. @code{n} should be >=0.
  1677. Equivalent to @code{x * expt(2,n)}.
  1678. @item cl_I operator >> (const cl_I& x, const cl_I& n)
  1679. @cindex @code{operator >> ()}
  1680. Shifts @code{x} by @code{n} bits to the right. @code{n} should be >=0.
  1681. Bits shifted out to the right are thrown away.
  1682. Equivalent to @code{floor(x / expt(2,n))}.
  1683. @item cl_I ash (const cl_I& x, const cl_I& y)
  1684. @cindex @code{ash ()}
  1685. Shifts @code{x} by @code{y} bits to the left (if @code{y}>=0) or
  1686. by @code{-y} bits to the right (if @code{y}<=0). In other words, this
  1687. returns @code{floor(x * expt(2,y))}.
  1688. @item uintL integer_length (const cl_I& x)
  1689. @cindex @code{integer_length ()}
  1690. Returns the number of bits (excluding the sign bit) needed to represent @code{x}
  1691. in two's complement notation. This is the smallest n >= 0 such that
  1692. -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
  1693. 2^(n-1) <= x < 2^n.
  1694. @item uintL ord2 (const cl_I& x)
  1695. @cindex @code{ord2 ()}
  1696. @code{x} must be non-zero. This function returns the number of 0 bits at the
  1697. right of @code{x} in two's complement notation. This is the largest n >= 0
  1698. such that 2^n divides @code{x}.
  1699. @item uintL power2p (const cl_I& x)
  1700. @cindex @code{power2p ()}
  1701. @code{x} must be > 0. This function checks whether @code{x} is a power of 2.
  1702. If @code{x} = 2^(n-1), it returns n. Else it returns 0.
  1703. (See also the function @code{logp}.)
  1704. @end table
  1705. @node Number theoretic functions, Combinatorial functions, Logical functions, Functions on integers
  1706. @subsection Number theoretic functions
  1707. @table @code
  1708. @item uint32 gcd (uint32 a, uint32 b)
  1709. @cindex @code{gcd ()}
  1710. @itemx cl_I gcd (const cl_I& a, const cl_I& b)
  1711. This function returns the greatest common divisor of @code{a} and @code{b},
  1712. normalized to be >= 0.
  1713. @item cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)
  1714. @cindex @code{xgcd ()}
  1715. This function (``extended gcd'') returns the greatest common divisor @code{g} of
  1716. @code{a} and @code{b} and at the same time the representation of @code{g}
  1717. as an integral linear combination of @code{a} and @code{b}:
  1718. @code{u} and @code{v} with @code{u*a+v*b = g}, @code{g} >= 0.
  1719. @code{u} and @code{v} will be normalized to be of smallest possible absolute
  1720. value, in the following sense: If @code{a} and @code{b} are non-zero, and
  1721. @code{abs(a) != abs(b)}, @code{u} and @code{v} will satisfy the inequalities
  1722. @code{abs(u) <= abs(b)/(2*g)}, @code{abs(v) <= abs(a)/(2*g)}.
  1723. @item cl_I lcm (const cl_I& a, const cl_I& b)
  1724. @cindex @code{lcm ()}
  1725. This function returns the least common multiple of @code{a} and @code{b},
  1726. normalized to be >= 0.
  1727. @item cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l)
  1728. @cindex @code{logp ()}
  1729. @itemx cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l)
  1730. @code{a} must be > 0. @code{b} must be >0 and != 1. If log(a,b) is
  1731. rational number, this function returns true and sets *l = log(a,b), else
  1732. it returns false.
  1733. @end table
  1734. @node Combinatorial functions, , Number theoretic functions, Functions on integers
  1735. @subsection Combinatorial functions
  1736. @table @code
  1737. @item cl_I factorial (uintL n)
  1738. @cindex @code{factorial ()}
  1739. @code{n} must be a small integer >= 0. This function returns the factorial
  1740. @code{n}! = @code{1*2*@dots{}*n}.
  1741. @item cl_I doublefactorial (uintL n)
  1742. @cindex @code{doublefactorial ()}
  1743. @code{n} must be a small integer >= 0. This function returns the
  1744. doublefactorial @code{n}!! = @code{1*3*@dots{}*n} or
  1745. @code{n}!! = @code{2*4*@dots{}*n}, respectively.
  1746. @item cl_I binomial (uintL n, uintL k)
  1747. @cindex @code{binomial ()}
  1748. @code{n} and @code{k} must be small integers >= 0. This function returns the
  1749. binomial coefficient
  1750. @tex
  1751. ${n \choose k} = {n! \over n! (n-k)!}$
  1752. @end tex
  1753. @ifinfo
  1754. (@code{n} choose @code{k}) = @code{n}! / @code{k}! @code{(n-k)}!
  1755. @end ifinfo
  1756. for 0 <= k <= n, 0 else.
  1757. @end table
  1758. @node Functions on floating-point numbers, Conversion functions, Functions on integers, Functions on numbers
  1759. @section Functions on floating-point numbers
  1760. Recall that a floating-point number consists of a sign @code{s}, an
  1761. exponent @code{e} and a mantissa @code{m}. The value of the number is
  1762. @code{(-1)^s * 2^e * m}.
  1763. Each of the classes
  1764. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1765. defines the following operations.
  1766. @table @code
  1767. @item @var{type} scale_float (const @var{type}& x, sintL delta)
  1768. @cindex @code{scale_float ()}
  1769. @itemx @var{type} scale_float (const @var{type}& x, const cl_I& delta)
  1770. Returns @code{x*2^delta}. This is more efficient than an explicit multiplication
  1771. because it copies @code{x} and modifies the exponent.
  1772. @end table
  1773. The following functions provide an abstract interface to the underlying
  1774. representation of floating-point numbers.
  1775. @table @code
  1776. @item sintL float_exponent (const @var{type}& x)
  1777. @cindex @code{float_exponent ()}
  1778. Returns the exponent @code{e} of @code{x}.
  1779. For @code{x = 0.0}, this is 0. For @code{x} non-zero, this is the unique
  1780. integer with @code{2^(e-1) <= abs(x) < 2^e}.
  1781. @item sintL float_radix (const @var{type}& x)
  1782. @cindex @code{float_radix ()}
  1783. Returns the base of the floating-point representation. This is always @code{2}.
  1784. @item @var{type} float_sign (const @var{type}& x)
  1785. @cindex @code{float_sign ()}
  1786. Returns the sign @code{s} of @code{x} as a float. The value is 1 for
  1787. @code{x} >= 0, -1 for @code{x} < 0.
  1788. @item uintL float_digits (const @var{type}& x)
  1789. @cindex @code{float_digits ()}
  1790. Returns the number of mantissa bits in the floating-point representation
  1791. of @code{x}, including the hidden bit. The value only depends on the type
  1792. of @code{x}, not on its value.
  1793. @item uintL float_precision (const @var{type}& x)
  1794. @cindex @code{float_precision ()}
  1795. Returns the number of significant mantissa bits in the floating-point
  1796. representation of @code{x}. Since denormalized numbers are not supported,
  1797. this is the same as @code{float_digits(x)} if @code{x} is non-zero, and
  1798. 0 if @code{x} = 0.
  1799. @end table
  1800. The complete internal representation of a float is encoded in the type
  1801. @cindex @code{cl_decoded_float}
  1802. @cindex @code{cl_decoded_sfloat}
  1803. @cindex @code{cl_decoded_ffloat}
  1804. @cindex @code{cl_decoded_dfloat}
  1805. @cindex @code{cl_decoded_lfloat}
  1806. @code{cl_decoded_float} (or @code{cl_decoded_sfloat}, @code{cl_decoded_ffloat},
  1807. @code{cl_decoded_dfloat}, @code{cl_decoded_lfloat}, respectively), defined by
  1808. @example
  1809. struct cl_decoded_@var{type}float @{
  1810. @var{type} mantissa; cl_I exponent; @var{type} sign;
  1811. @};
  1812. @end example
  1813. and returned by the function
  1814. @table @code
  1815. @item cl_decoded_@var{type}float decode_float (const @var{type}& x)
  1816. @cindex @code{decode_float ()}
  1817. For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
  1818. @code{x = (-1)^s * 2^e * m} and @code{0.5 <= m < 1.0}. For @code{x} = 0,
  1819. it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
  1820. @code{e} is the same as returned by the function @code{float_exponent}.
  1821. @end table
  1822. A complete decoding in terms of integers is provided as type
  1823. @example
  1824. @cindex @code{cl_idecoded_float}
  1825. struct cl_idecoded_float @{
  1826. cl_I mantissa; cl_I exponent; cl_I sign;
  1827. @};
  1828. @end example
  1829. by the following function:
  1830. @table @code
  1831. @item cl_idecoded_float integer_decode_float (const @var{type}& x)
  1832. @cindex @code{integer_decode_float ()}
  1833. For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
  1834. @code{x = (-1)^s * 2^e * m} and @code{m} an integer with @code{float_digits(x)}
  1835. bits. For @code{x} = 0, it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
  1836. WARNING: The exponent @code{e} is not the same as the one returned by
  1837. the functions @code{decode_float} and @code{float_exponent}.
  1838. @end table
  1839. Some other function, implemented only for class @code{cl_F}:
  1840. @table @code
  1841. @item cl_F float_sign (const cl_F& x, const cl_F& y)
  1842. @cindex @code{float_sign ()}
  1843. This returns a floating point number whose precision and absolute value
  1844. is that of @code{y} and whose sign is that of @code{x}. If @code{x} is
  1845. zero, it is treated as positive. Same for @code{y}.
  1846. @end table
  1847. @node Conversion functions, Random number generators, Functions on floating-point numbers, Functions on numbers
  1848. @section Conversion functions
  1849. @cindex conversion
  1850. @menu
  1851. * Conversion to floating-point numbers::
  1852. * Conversion to rational numbers::
  1853. @end menu
  1854. @node Conversion to floating-point numbers, Conversion to rational numbers, Conversion functions, Conversion functions
  1855. @subsection Conversion to floating-point numbers
  1856. The type @code{cl_float_format_t} describes a floating-point format.
  1857. @table @code
  1858. @item cl_float_format_t cl_float_format (uintL n)
  1859. @cindex @code{cl_float_format ()}
  1860. Returns the smallest float format which guarantees at least @code{n}
  1861. decimal digits in the mantissa (after the decimal point).
  1862. @item cl_float_format_t cl_float_format (const cl_F& x)
  1863. Returns the floating point format of @code{x}.
  1864. @item cl_float_format_t cl_default_float_format
  1865. @cindex @code{cl_default_float_format}
  1866. Global variable: the default float format used when converting rational numbers
  1867. to floats.
  1868. @end table
  1869. To convert a real number to a float, each of the types
  1870. @code{cl_R}, @code{cl_F}, @code{cl_I}, @code{cl_RA},
  1871. @code{int}, @code{unsigned int}, @code{float}, @code{double}
  1872. defines the following operations:
  1873. @table @code
  1874. @item cl_F cl_float (const @var{type}&x, cl_float_format_t f)
  1875. @cindex @code{cl_float}
  1876. Returns @code{x} as a float of format @code{f}.
  1877. @item cl_F cl_float (const @var{type}&x, const cl_F& y)
  1878. Returns @code{x} in the float format of @code{y}.
  1879. @item cl_F cl_float (const @var{type}&x)
  1880. Returns @code{x} as a float of format @code{cl_default_float_format} if
  1881. it is an exact number, or @code{x} itself if it is already a float.
  1882. @end table
  1883. Of course, converting a number to a float can lose precision.
  1884. Every floating-point format has some characteristic numbers:
  1885. @table @code
  1886. @item cl_F most_positive_float (cl_float_format_t f)
  1887. @cindex @code{most_positive_float ()}
  1888. Returns the largest (most positive) floating point number in float format @code{f}.
  1889. @item cl_F most_negative_float (cl_float_format_t f)
  1890. @cindex @code{most_negative_float ()}
  1891. Returns the smallest (most negative) floating point number in float format @code{f}.
  1892. @item cl_F least_positive_float (cl_float_format_t f)
  1893. @cindex @code{least_positive_float ()}
  1894. Returns the least positive floating point number (i.e. > 0 but closest to 0)
  1895. in float format @code{f}.
  1896. @item cl_F least_negative_float (cl_float_format_t f)
  1897. @cindex @code{least_negative_float ()}
  1898. Returns the least negative floating point number (i.e. < 0 but closest to 0)
  1899. in float format @code{f}.
  1900. @item cl_F float_epsilon (cl_float_format_t f)
  1901. @cindex @code{float_epsilon ()}
  1902. Returns the smallest floating point number e > 0 such that @code{1+e != 1}.
  1903. @item cl_F float_negative_epsilon (cl_float_format_t f)
  1904. @cindex @code{float_negative_epsilon ()}
  1905. Returns the smallest floating point number e > 0 such that @code{1-e != 1}.
  1906. @end table
  1907. @node Conversion to rational numbers, , Conversion to floating-point numbers, Conversion functions
  1908. @subsection Conversion to rational numbers
  1909. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_F}
  1910. defines the following operation:
  1911. @table @code
  1912. @item cl_RA rational (const @var{type}& x)
  1913. @cindex @code{rational ()}
  1914. Returns the value of @code{x} as an exact number. If @code{x} is already
  1915. an exact number, this is @code{x}. If @code{x} is a floating-point number,
  1916. the value is a rational number whose denominator is a power of 2.
  1917. @end table
  1918. In order to convert back, say, @code{(cl_F)(cl_R)"1/3"} to @code{1/3}, there is
  1919. the function
  1920. @table @code
  1921. @item cl_RA rationalize (const cl_R& x)
  1922. @cindex @code{rationalize ()}
  1923. If @code{x} is a floating-point number, it actually represents an interval
  1924. of real numbers, and this function returns the rational number with
  1925. smallest denominator (and smallest numerator, in magnitude)
  1926. which lies in this interval.
  1927. If @code{x} is already an exact number, this function returns @code{x}.
  1928. @end table
  1929. If @code{x} is any float, one has
  1930. @itemize @asis
  1931. @item
  1932. @code{cl_float(rational(x),x) = x}
  1933. @item
  1934. @code{cl_float(rationalize(x),x) = x}
  1935. @end itemize
  1936. @node Random number generators, Obfuscating operators, Conversion functions, Functions on numbers
  1937. @section Random number generators
  1938. A random generator is a machine which produces (pseudo-)random numbers.
  1939. The include file @code{<cl_random.h>} defines a class @code{cl_random_state}
  1940. which contains the state of a random generator. If you make a copy
  1941. of the random number generator, the original one and the copy will produce
  1942. the same sequence of random numbers.
  1943. The following functions return (pseudo-)random numbers in different formats.
  1944. Calling one of these modifies the state of the random number generator in
  1945. a complicated but deterministic way.
  1946. The global variable
  1947. @cindex @code{cl_default_random_state}
  1948. @example
  1949. cl_random_state cl_default_random_state
  1950. @end example
  1951. contains a default random number generator. It is used when the functions
  1952. below are called without @code{cl_random_state} argument.
  1953. @table @code
  1954. @item uint32 random32 (cl_random_state& randomstate)
  1955. @itemx uint32 random32 ()
  1956. @cindex @code{random32 ()}
  1957. Returns a random unsigned 32-bit number. All bits are equally random.
  1958. @item cl_I random_I (cl_random_state& randomstate, const cl_I& n)
  1959. @itemx cl_I random_I (const cl_I& n)
  1960. @cindex @code{random_I ()}
  1961. @code{n} must be an integer > 0. This function returns a random integer @code{x}
  1962. in the range @code{0 <= x < n}.
  1963. @item cl_F random_F (cl_random_state& randomstate, const cl_F& n)
  1964. @itemx cl_F random_F (const cl_F& n)
  1965. @cindex @code{random_F ()}
  1966. @code{n} must be a float > 0. This function returns a random floating-point
  1967. number of the same format as @code{n} in the range @code{0 <= x < n}.
  1968. @item cl_R random_R (cl_random_state& randomstate, const cl_R& n)
  1969. @itemx cl_R random_R (const cl_R& n)
  1970. @cindex @code{random_R ()}
  1971. Behaves like @code{random_I} if @code{n} is an integer and like @code{random_F}
  1972. if @code{n} is a float.
  1973. @end table
  1974. @node Obfuscating operators, , Random number generators, Functions on numbers
  1975. @section Obfuscating operators
  1976. @cindex modifying operators
  1977. The modifying C/C++ operators @code{+=}, @code{-=}, @code{*=}, @code{/=},
  1978. @code{&=}, @code{|=}, @code{^=}, @code{<<=}, @code{>>=}
  1979. are not available by default because their
  1980. use tends to make programs unreadable. It is trivial to get away without
  1981. them. However, if you feel that you absolutely need these operators
  1982. to get happy, then add
  1983. @example
  1984. #define WANT_OBFUSCATING_OPERATORS
  1985. @end example
  1986. @cindex @code{WANT_OBFUSCATING_OPERATORS}
  1987. to the beginning of your source files, before the inclusion of any CLN
  1988. include files. This flag will enable the following operators:
  1989. For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
  1990. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
  1991. @table @code
  1992. @item @var{type}& operator += (@var{type}&, const @var{type}&)
  1993. @cindex @code{operator += ()}
  1994. @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
  1995. @cindex @code{operator -= ()}
  1996. @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
  1997. @cindex @code{operator *= ()}
  1998. @itemx @var{type}& operator /= (@var{type}&, const @var{type}&)
  1999. @cindex @code{operator /= ()}
  2000. @end table
  2001. For the class @code{cl_I}:
  2002. @table @code
  2003. @item @var{type}& operator += (@var{type}&, const @var{type}&)
  2004. @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
  2005. @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
  2006. @itemx @var{type}& operator &= (@var{type}&, const @var{type}&)
  2007. @cindex @code{operator &= ()}
  2008. @itemx @var{type}& operator |= (@var{type}&, const @var{type}&)
  2009. @cindex @code{operator |= ()}
  2010. @itemx @var{type}& operator ^= (@var{type}&, const @var{type}&)
  2011. @cindex @code{operator ^= ()}
  2012. @itemx @var{type}& operator <<= (@var{type}&, const @var{type}&)
  2013. @cindex @code{operator <<= ()}
  2014. @itemx @var{type}& operator >>= (@var{type}&, const @var{type}&)
  2015. @cindex @code{operator >>= ()}
  2016. @end table
  2017. For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  2018. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
  2019. @table @code
  2020. @item @var{type}& operator ++ (@var{type}& x)
  2021. @cindex @code{operator ++ ()}
  2022. The prefix operator @code{++x}.
  2023. @item void operator ++ (@var{type}& x, int)
  2024. The postfix operator @code{x++}.
  2025. @item @var{type}& operator -- (@var{type}& x)
  2026. @cindex @code{operator -- ()}
  2027. The prefix operator @code{--x}.
  2028. @item void operator -- (@var{type}& x, int)
  2029. The postfix operator @code{x--}.
  2030. @end table
  2031. Note that by using these obfuscating operators, you wouldn't gain efficiency:
  2032. In CLN @samp{x += y;} is exactly the same as @samp{x = x+y;}, not more
  2033. efficient.
  2034. @node Input/Output, Rings, Functions on numbers, Top
  2035. @chapter Input/Output
  2036. @cindex Input/Output
  2037. @menu
  2038. * Internal and printed representation::
  2039. * Input functions::
  2040. * Output functions::
  2041. @end menu
  2042. @node Internal and printed representation, Input functions, Input/Output, Input/Output
  2043. @section Internal and printed representation
  2044. @cindex representation
  2045. All computations deal with the internal representations of the numbers.
  2046. Every number has an external representation as a sequence of ASCII characters.
  2047. Several external representations may denote the same number, for example,
  2048. "20.0" and "20.000".
  2049. Converting an internal to an external representation is called ``printing'',
  2050. @cindex printing
  2051. converting an external to an internal representation is called ``reading''.
  2052. @cindex reading
  2053. In CLN, it is always true that conversion of an internal to an external
  2054. representation and then back to an internal representation will yield the
  2055. same internal representation. Symbolically: @code{read(print(x)) == x}.
  2056. This is called ``print-read consistency''.
  2057. Different types of numbers have different external representations (case
  2058. is insignificant):
  2059. @table @asis
  2060. @item Integers
  2061. External representation: @var{sign}@{@var{digit}@}+. The reader also accepts the
  2062. Common Lisp syntaxes @var{sign}@{@var{digit}@}+@code{.} with a trailing dot
  2063. for decimal integers
  2064. and the @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes.
  2065. @item Rational numbers
  2066. External representation: @var{sign}@{@var{digit}@}+@code{/}@{@var{digit}@}+.
  2067. The @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes are allowed
  2068. here as well.
  2069. @item Floating-point numbers
  2070. External representation: @var{sign}@{@var{digit}@}*@var{exponent} or
  2071. @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}*@var{exponent} or
  2072. @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}+. A precision specifier
  2073. of the form _@var{prec} may be appended. There must be at least
  2074. one digit in the non-exponent part. The exponent has the syntax
  2075. @var{expmarker} @var{expsign} @{@var{digit}@}+.
  2076. The exponent marker is
  2077. @itemize @asis
  2078. @item
  2079. @samp{s} for short-floats,
  2080. @item
  2081. @samp{f} for single-floats,
  2082. @item
  2083. @samp{d} for double-floats,
  2084. @item
  2085. @samp{L} for long-floats,
  2086. @end itemize
  2087. or @samp{e}, which denotes a default float format. The precision specifying
  2088. suffix has the syntax _@var{prec} where @var{prec} denotes the number of
  2089. valid mantissa digits (in decimal, excluding leading zeroes), cf. also
  2090. function @samp{cl_float_format}.
  2091. @item Complex numbers
  2092. External representation:
  2093. @itemize @asis
  2094. @item
  2095. In algebraic notation: @code{@var{realpart}+@var{imagpart}i}. Of course,
  2096. if @var{imagpart} is negative, its printed representation begins with
  2097. a @samp{-}, and the @samp{+} between @var{realpart} and @var{imagpart}
  2098. may be omitted. Note that this notation cannot be used when the @var{imagpart}
  2099. is rational and the rational number's base is >18, because the @samp{i}
  2100. is then read as a digit.
  2101. @item
  2102. In Common Lisp notation: @code{#C(@var{realpart} @var{imagpart})}.
  2103. @end itemize
  2104. @end table
  2105. @node Input functions, Output functions, Internal and printed representation, Input/Output
  2106. @section Input functions
  2107. Including @code{<cl_io.h>} defines a type @code{cl_istream}, which is
  2108. the type of the first argument to all input functions. Unless you build
  2109. and use CLN with the macro CL_IO_STDIO being defined, @code{cl_istream}
  2110. is the same as @code{istream&}.
  2111. The variable
  2112. @itemize @asis
  2113. @item
  2114. @code{cl_istream cl_stdin}
  2115. @end itemize
  2116. contains the standard input stream.
  2117. These are the simple input functions:
  2118. @table @code
  2119. @item int freadchar (cl_istream stream)
  2120. Reads a character from @code{stream}. Returns @code{cl_EOF} (not a @samp{char}!)
  2121. if the end of stream was encountered or an error occurred.
  2122. @item int funreadchar (cl_istream stream, int c)
  2123. Puts back @code{c} onto @code{stream}. @code{c} must be the result of the
  2124. last @code{freadchar} operation on @code{stream}.
  2125. @end table
  2126. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  2127. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  2128. defines, in @code{<cl_@var{type}_io.h>}, the following input function:
  2129. @table @code
  2130. @item cl_istream operator>> (cl_istream stream, @var{type}& result)
  2131. Reads a number from @code{stream} and stores it in the @code{result}.
  2132. @end table
  2133. The most flexible input functions, defined in @code{<cl_@var{type}_io.h>},
  2134. are the following:
  2135. @table @code
  2136. @item cl_N read_complex (cl_istream stream, const cl_read_flags& flags)
  2137. @itemx cl_R read_real (cl_istream stream, const cl_read_flags& flags)
  2138. @itemx cl_F read_float (cl_istream stream, const cl_read_flags& flags)
  2139. @itemx cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)
  2140. @itemx cl_I read_integer (cl_istream stream, const cl_read_flags& flags)
  2141. Reads a number from @code{stream}. The @code{flags} are parameters which
  2142. affect the input syntax. Whitespace before the number is silently skipped.
  2143. @item cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  2144. @itemx cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  2145. @itemx cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  2146. @itemx cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  2147. @itemx cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  2148. Reads a number from a string in memory. The @code{flags} are parameters which
  2149. affect the input syntax. The string starts at @code{string} and ends at
  2150. @code{string_limit} (exclusive limit). @code{string_limit} may also be
  2151. @code{NULL}, denoting the entire string, i.e. equivalent to
  2152. @code{string_limit = string + strlen(string)}. If @code{end_of_parse} is
  2153. @code{NULL}, the string in memory must contain exactly one number and nothing
  2154. more, else a fatal error will be signalled. If @code{end_of_parse}
  2155. is not @code{NULL}, @code{*end_of_parse} will be assigned a pointer past
  2156. the last parsed character (i.e. @code{string_limit} if nothing came after
  2157. the number). Whitespace is not allowed.
  2158. @end table
  2159. The structure @code{cl_read_flags} contains the following fields:
  2160. @table @code
  2161. @item cl_read_syntax_t syntax
  2162. The possible results of the read operation. Possible values are
  2163. @code{syntax_number}, @code{syntax_real}, @code{syntax_rational},
  2164. @code{syntax_integer}, @code{syntax_float}, @code{syntax_sfloat},
  2165. @code{syntax_ffloat}, @code{syntax_dfloat}, @code{syntax_lfloat}.
  2166. @item cl_read_lsyntax_t lsyntax
  2167. Specifies the language-dependent syntax variant for the read operation.
  2168. Possible values are
  2169. @table @code
  2170. @item lsyntax_standard
  2171. accept standard algebraic notation only, no complex numbers,
  2172. @item lsyntax_algebraic
  2173. accept the algebraic notation @code{@var{x}+@var{y}i} for complex numbers,
  2174. @item lsyntax_commonlisp
  2175. accept the @code{#b}, @code{#o}, @code{#x} syntaxes for binary, octal,
  2176. hexadecimal numbers,
  2177. @code{#@var{base}R} for rational numbers in a given base,
  2178. @code{#c(@var{realpart} @var{imagpart})} for complex numbers,
  2179. @item lsyntax_all
  2180. accept all of these extensions.
  2181. @end table
  2182. @item unsigned int rational_base
  2183. The base in which rational numbers are read.
  2184. @item cl_float_format_t float_flags.default_float_format
  2185. The float format used when reading floats with exponent marker @samp{e}.
  2186. @item cl_float_format_t float_flags.default_lfloat_format
  2187. The float format used when reading floats with exponent marker @samp{l}.
  2188. @item cl_boolean float_flags.mantissa_dependent_float_format
  2189. When this flag is true, floats specified with more digits than corresponding
  2190. to the exponent marker they contain, but without @var{_nnn} suffix, will get a
  2191. precision corresponding to their number of significant digits.
  2192. @end table
  2193. @node Output functions, , Input functions, Input/Output
  2194. @section Output functions
  2195. Including @code{<cl_io.h>} defines a type @code{cl_ostream}, which is
  2196. the type of the first argument to all output functions. Unless you build
  2197. and use CLN with the macro CL_IO_STDIO being defined, @code{cl_ostream}
  2198. is the same as @code{ostream&}.
  2199. The variable
  2200. @itemize @asis
  2201. @item
  2202. @code{cl_ostream cl_stdout}
  2203. @end itemize
  2204. contains the standard output stream.
  2205. The variable
  2206. @itemize @asis
  2207. @item
  2208. @code{cl_ostream cl_stderr}
  2209. @end itemize
  2210. contains the standard error output stream.
  2211. These are the simple output functions:
  2212. @table @code
  2213. @item void fprintchar (cl_ostream stream, char c)
  2214. Prints the character @code{x} literally on the @code{stream}.
  2215. @item void fprint (cl_ostream stream, const char * string)
  2216. Prints the @code{string} literally on the @code{stream}.
  2217. @item void fprintdecimal (cl_ostream stream, int x)
  2218. @itemx void fprintdecimal (cl_ostream stream, const cl_I& x)
  2219. Prints the integer @code{x} in decimal on the @code{stream}.
  2220. @item void fprintbinary (cl_ostream stream, const cl_I& x)
  2221. Prints the integer @code{x} in binary (base 2, without prefix)
  2222. on the @code{stream}.
  2223. @item void fprintoctal (cl_ostream stream, const cl_I& x)
  2224. Prints the integer @code{x} in octal (base 8, without prefix)
  2225. on the @code{stream}.
  2226. @item void fprinthexadecimal (cl_ostream stream, const cl_I& x)
  2227. Prints the integer @code{x} in hexadecimal (base 16, without prefix)
  2228. on the @code{stream}.
  2229. @end table
  2230. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  2231. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  2232. defines, in @code{<cl_@var{type}_io.h>}, the following output functions:
  2233. @table @code
  2234. @item void fprint (cl_ostream stream, const @var{type}& x)
  2235. @itemx cl_ostream operator<< (cl_ostream stream, const @var{type}& x)
  2236. Prints the number @code{x} on the @code{stream}. The output may depend
  2237. on the global printer settings in the variable @code{cl_default_print_flags}.
  2238. The @code{ostream} flags and settings (flags, width and locale) are
  2239. ignored.
  2240. @end table
  2241. The most flexible output function, defined in @code{<cl_@var{type}_io.h>},
  2242. are the following:
  2243. @example
  2244. void print_complex (cl_ostream stream, const cl_print_flags& flags,
  2245. const cl_N& z);
  2246. void print_real (cl_ostream stream, const cl_print_flags& flags,
  2247. const cl_R& z);
  2248. void print_float (cl_ostream stream, const cl_print_flags& flags,
  2249. const cl_F& z);
  2250. void print_rational (cl_ostream stream, const cl_print_flags& flags,
  2251. const cl_RA& z);
  2252. void print_integer (cl_ostream stream, const cl_print_flags& flags,
  2253. const cl_I& z);
  2254. @end example
  2255. Prints the number @code{x} on the @code{stream}. The @code{flags} are
  2256. parameters which affect the output.
  2257. The structure type @code{cl_print_flags} contains the following fields:
  2258. @table @code
  2259. @item unsigned int rational_base
  2260. The base in which rational numbers are printed. Default is @code{10}.
  2261. @item cl_boolean rational_readably
  2262. If this flag is true, rational numbers are printed with radix specifiers in
  2263. Common Lisp syntax (@code{#@var{n}R} or @code{#b} or @code{#o} or @code{#x}
  2264. prefixes, trailing dot). Default is false.
  2265. @item cl_boolean float_readably
  2266. If this flag is true, type specific exponent markers have precedence over 'E'.
  2267. Default is false.
  2268. @item cl_float_format_t default_float_format
  2269. Floating point numbers of this format will be printed using the 'E' exponent
  2270. marker. Default is @code{cl_float_format_ffloat}.
  2271. @item cl_boolean complex_readably
  2272. If this flag is true, complex numbers will be printed using the Common Lisp
  2273. syntax @code{#C(@var{realpart} @var{imagpart})}. Default is false.
  2274. @item cl_string univpoly_varname
  2275. Univariate polynomials with no explicit indeterminate name will be printed
  2276. using this variable name. Default is @code{"x"}.
  2277. @end table
  2278. The global variable @code{cl_default_print_flags} contains the default values,
  2279. used by the function @code{fprint}.
  2280. @node Rings, Modular integers, Input/Output, Top
  2281. @chapter Rings
  2282. CLN has a class of abstract rings.
  2283. @example
  2284. Ring
  2285. cl_ring
  2286. <cl_ring.h>
  2287. @end example
  2288. Rings can be compared for equality:
  2289. @table @code
  2290. @item bool operator== (const cl_ring&, const cl_ring&)
  2291. @itemx bool operator!= (const cl_ring&, const cl_ring&)
  2292. These compare two rings for equality.
  2293. @end table
  2294. Given a ring @code{R}, the following members can be used.
  2295. @table @code
  2296. @item void R->fprint (cl_ostream stream, const cl_ring_element& x)
  2297. @itemx cl_boolean R->equal (const cl_ring_element& x, const cl_ring_element& y)
  2298. @itemx cl_ring_element R->zero ()
  2299. @itemx cl_boolean R->zerop (const cl_ring_element& x)
  2300. @itemx cl_ring_element R->plus (const cl_ring_element& x, const cl_ring_element& y)
  2301. @itemx cl_ring_element R->minus (const cl_ring_element& x, const cl_ring_element& y)
  2302. @itemx cl_ring_element R->uminus (const cl_ring_element& x)
  2303. @itemx cl_ring_element R->one ()
  2304. @itemx cl_ring_element R->canonhom (const cl_I& x)
  2305. @itemx cl_ring_element R->mul (const cl_ring_element& x, const cl_ring_element& y)
  2306. @itemx cl_ring_element R->square (const cl_ring_element& x)
  2307. @itemx cl_ring_element R->expt_pos (const cl_ring_element& x, const cl_I& y)
  2308. @end table
  2309. The following rings are built-in.
  2310. @table @code
  2311. @item cl_null_ring cl_0_ring
  2312. The null ring, containing only zero.
  2313. @item cl_complex_ring cl_C_ring
  2314. The ring of complex numbers. This corresponds to the type @code{cl_N}.
  2315. @item cl_real_ring cl_R_ring
  2316. The ring of real numbers. This corresponds to the type @code{cl_R}.
  2317. @item cl_rational_ring cl_RA_ring
  2318. The ring of rational numbers. This corresponds to the type @code{cl_RA}.
  2319. @item cl_integer_ring cl_I_ring
  2320. The ring of integers. This corresponds to the type @code{cl_I}.
  2321. @end table
  2322. Type tests can be performed for any of @code{cl_C_ring}, @code{cl_R_ring},
  2323. @code{cl_RA_ring}, @code{cl_I_ring}:
  2324. @table @code
  2325. @item cl_boolean instanceof (const cl_number& x, const cl_number_ring& R)
  2326. Tests whether the given number is an element of the number ring R.
  2327. @end table
  2328. @node Modular integers, Symbolic data types, Rings, Top
  2329. @chapter Modular integers
  2330. @cindex modular integer
  2331. @menu
  2332. * Modular integer rings::
  2333. * Functions on modular integers::
  2334. @end menu
  2335. @node Modular integer rings, Functions on modular integers, Modular integers, Modular integers
  2336. @section Modular integer rings
  2337. @cindex ring
  2338. CLN implements modular integers, i.e. integers modulo a fixed integer N.
  2339. The modulus is explicitly part of every modular integer. CLN doesn't
  2340. allow you to (accidentally) mix elements of different modular rings,
  2341. e.g. @code{(3 mod 4) + (2 mod 5)} will result in a runtime error.
  2342. (Ideally one would imagine a generic data type @code{cl_MI(N)}, but C++
  2343. doesn't have generic types. So one has to live with runtime checks.)
  2344. The class of modular integer rings is
  2345. @example
  2346. Ring
  2347. cl_ring
  2348. <cl_ring.h>
  2349. |
  2350. |
  2351. Modular integer ring
  2352. cl_modint_ring
  2353. <cl_modinteger.h>
  2354. @end example
  2355. and the class of all modular integers (elements of modular integer rings) is
  2356. @example
  2357. Modular integer
  2358. cl_MI
  2359. <cl_modinteger.h>
  2360. @end example
  2361. Modular integer rings are constructed using the function
  2362. @table @code
  2363. @item cl_modint_ring cl_find_modint_ring (const cl_I& N)
  2364. @cindex @code{cl_find_modint_ring ()}
  2365. This function returns the modular ring @samp{Z/NZ}. It takes care
  2366. of finding out about special cases of @code{N}, like powers of two
  2367. and odd numbers for which Montgomery multiplication will be a win,
  2368. @cindex Montgomery multiplication
  2369. and precomputes any necessary auxiliary data for computing modulo @code{N}.
  2370. There is a cache table of rings, indexed by @code{N} (or, more precisely,
  2371. by @code{abs(N)}). This ensures that the precomputation costs are reduced
  2372. to a minimum.
  2373. @end table
  2374. Modular integer rings can be compared for equality:
  2375. @table @code
  2376. @item bool operator== (const cl_modint_ring&, const cl_modint_ring&)
  2377. @cindex @code{operator == ()}
  2378. @itemx bool operator!= (const cl_modint_ring&, const cl_modint_ring&)
  2379. @cindex @code{operator != ()}
  2380. These compare two modular integer rings for equality. Two different calls
  2381. to @code{cl_find_modint_ring} with the same argument necessarily return the
  2382. same ring because it is memoized in the cache table.
  2383. @end table
  2384. @node Functions on modular integers, , Modular integer rings, Modular integers
  2385. @section Functions on modular integers
  2386. Given a modular integer ring @code{R}, the following members can be used.
  2387. @table @code
  2388. @item cl_I R->modulus
  2389. @cindex @code{modulus}
  2390. This is the ring's modulus, normalized to be nonnegative: @code{abs(N)}.
  2391. @item cl_MI R->zero()
  2392. @cindex @code{zero ()}
  2393. This returns @code{0 mod N}.
  2394. @item cl_MI R->one()
  2395. @cindex @code{one ()}
  2396. This returns @code{1 mod N}.
  2397. @item cl_MI R->canonhom (const cl_I& x)
  2398. @cindex @code{canonhom ()}
  2399. This returns @code{x mod N}.
  2400. @item cl_I R->retract (const cl_MI& x)
  2401. @cindex @code{etract ()}
  2402. This is a partial inverse function to @code{R->canonhom}. It returns the
  2403. standard representative (@code{>=0}, @code{<N}) of @code{x}.
  2404. @item cl_MI R->random(cl_random_state& randomstate)
  2405. @itemx cl_MI R->random()
  2406. @cindex @code{random ()}
  2407. This returns a random integer modulo @code{N}.
  2408. @end table
  2409. The following operations are defined on modular integers.
  2410. @table @code
  2411. @item cl_modint_ring x.ring ()
  2412. @cindex @code{ring()}
  2413. Returns the ring to which the modular integer @code{x} belongs.
  2414. @item cl_MI operator+ (const cl_MI&, const cl_MI&)
  2415. @cindex @code{operator + ()}
  2416. Returns the sum of two modular integers. One of the arguments may also be
  2417. a plain integer.
  2418. @item cl_MI operator- (const cl_MI&, const cl_MI&)
  2419. @cindex @code{operator - ()}
  2420. Returns the difference of two modular integers. One of the arguments may also be
  2421. a plain integer.
  2422. @item cl_MI operator- (const cl_MI&)
  2423. Returns the negative of a modular integer.
  2424. @item cl_MI operator* (const cl_MI&, const cl_MI&)
  2425. @cindex @code{operator * ()}
  2426. Returns the product of two modular integers. One of the arguments may also be
  2427. a plain integer.
  2428. @item cl_MI square (const cl_MI&)
  2429. @cindex @code{square ()}
  2430. Returns the square of a modular integer.
  2431. @item cl_MI recip (const cl_MI& x)
  2432. @cindex @code{recip ()}
  2433. Returns the reciprocal @code{x^-1} of a modular integer @code{x}. @code{x}
  2434. must be coprime to the modulus, otherwise an error message is issued.
  2435. @item cl_MI div (const cl_MI& x, const cl_MI& y)
  2436. @cindex @code{div ()}
  2437. Returns the quotient @code{x*y^-1} of two modular integers @code{x}, @code{y}.
  2438. @code{y} must be coprime to the modulus, otherwise an error message is issued.
  2439. @item cl_MI expt_pos (const cl_MI& x, const cl_I& y)
  2440. @cindex @code{expt_pos ()}
  2441. @code{y} must be > 0. Returns @code{x^y}.
  2442. @item cl_MI expt (const cl_MI& x, const cl_I& y)
  2443. @cindex @code{expt ()}
  2444. Returns @code{x^y}. If @code{y} is negative, @code{x} must be coprime to the
  2445. modulus, else an error message is issued.
  2446. @item cl_MI operator<< (const cl_MI& x, const cl_I& y)
  2447. @cindex @code{operator << ()}
  2448. Returns @code{x*2^y}.
  2449. @item cl_MI operator>> (const cl_MI& x, const cl_I& y)
  2450. @cindex @code{operator >> ()}
  2451. Returns @code{x*2^-y}. When @code{y} is positive, the modulus must be odd,
  2452. or an error message is issued.
  2453. @item bool operator== (const cl_MI&, const cl_MI&)
  2454. @cindex @code{operator == ()}
  2455. @itemx bool operator!= (const cl_MI&, const cl_MI&)
  2456. @cindex @code{operator != ()}
  2457. Compares two modular integers, belonging to the same modular integer ring,
  2458. for equality.
  2459. @item cl_boolean zerop (const cl_MI& x)
  2460. @cindex @code{zerop ()}
  2461. Returns true if @code{x} is @code{0 mod N}.
  2462. @end table
  2463. The following output functions are defined (see also the chapter on
  2464. input/output).
  2465. @table @code
  2466. @item void fprint (cl_ostream stream, const cl_MI& x)
  2467. @cindex @code{fprint ()}
  2468. @itemx cl_ostream operator<< (cl_ostream stream, const cl_MI& x)
  2469. @cindex @code{operator << ()}
  2470. Prints the modular integer @code{x} on the @code{stream}. The output may depend
  2471. on the global printer settings in the variable @code{cl_default_print_flags}.
  2472. @end table
  2473. @node Symbolic data types, Univariate polynomials, Modular integers, Top
  2474. @chapter Symbolic data types
  2475. @cindex symbolic type
  2476. CLN implements two symbolic (non-numeric) data types: strings and symbols.
  2477. @menu
  2478. * Strings::
  2479. * Symbols::
  2480. @end menu
  2481. @node Strings, Symbols, Symbolic data types, Symbolic data types
  2482. @section Strings
  2483. @cindex string
  2484. The class
  2485. @example
  2486. String
  2487. cl_string
  2488. <cl_string.h>
  2489. @end example
  2490. implements immutable strings.
  2491. Strings are constructed through the following constructors:
  2492. @table @code
  2493. @item cl_string (const char * s)
  2494. @cindex @code{cl_string ()}
  2495. Returns an immutable copy of the (zero-terminated) C string @code{s}.
  2496. @item cl_string (const char * ptr, unsigned long len)
  2497. Returns an immutable copy of the @code{len} characters at
  2498. @code{ptr[0]}, @dots{}, @code{ptr[len-1]}. NUL characters are allowed.
  2499. @end table
  2500. The following functions are available on strings:
  2501. @table @code
  2502. @item operator =
  2503. Assignment from @code{cl_string} and @code{const char *}.
  2504. @item s.length()
  2505. @cindex @code{length ()}
  2506. @itemx strlen(s)
  2507. @cindex @code{strlen ()}
  2508. Returns the length of the string @code{s}.
  2509. @item s[i]
  2510. @cindex @code{operator [] ()}
  2511. Returns the @code{i}th character of the string @code{s}.
  2512. @code{i} must be in the range @code{0 <= i < s.length()}.
  2513. @item bool equal (const cl_string& s1, const cl_string& s2)
  2514. @cindex @code{equal ()}
  2515. Compares two strings for equality. One of the arguments may also be a
  2516. plain @code{const char *}.
  2517. @end table
  2518. @node Symbols, , Strings, Symbolic data types
  2519. @section Symbols
  2520. @cindex symbol
  2521. Symbols are uniquified strings: all symbols with the same name are shared.
  2522. This means that comparison of two symbols is fast (effectively just a pointer
  2523. comparison), whereas comparison of two strings must in the worst case walk
  2524. both strings until their end.
  2525. Symbols are used, for example, as tags for properties, as names of variables
  2526. in polynomial rings, etc.
  2527. Symbols are constructed through the following constructor:
  2528. @table @code
  2529. @item cl_symbol (const cl_string& s)
  2530. @cindex @code{cl_symbol ()}
  2531. Looks up or creates a new symbol with a given name.
  2532. @end table
  2533. The following operations are available on symbols:
  2534. @table @code
  2535. @item cl_string (const cl_symbol& sym)
  2536. Conversion to @code{cl_string}: Returns the string which names the symbol
  2537. @code{sym}.
  2538. @item bool equal (const cl_symbol& sym1, const cl_symbol& sym2)
  2539. @cindex @code{equal ()}
  2540. Compares two symbols for equality. This is very fast.
  2541. @end table
  2542. @node Univariate polynomials, Internals, Symbolic data types, Top
  2543. @chapter Univariate polynomials
  2544. @cindex polynomial
  2545. @cindex univariate polynomial
  2546. @menu
  2547. * Univariate polynomial rings::
  2548. * Functions on univariate polynomials::
  2549. * Special polynomials::
  2550. @end menu
  2551. @node Univariate polynomial rings, Functions on univariate polynomials, Univariate polynomials, Univariate polynomials
  2552. @section Univariate polynomial rings
  2553. CLN implements univariate polynomials (polynomials in one variable) over an
  2554. arbitrary ring. The indeterminate variable may be either unnamed (and will be
  2555. printed according to @code{cl_default_print_flags.univpoly_varname}, which
  2556. defaults to @samp{x}) or carry a given name. The base ring and the
  2557. indeterminate are explicitly part of every polynomial. CLN doesn't allow you to
  2558. (accidentally) mix elements of different polynomial rings, e.g.
  2559. @code{(a^2+1) * (b^3-1)} will result in a runtime error. (Ideally this should
  2560. return a multivariate polynomial, but they are not yet implemented in CLN.)
  2561. The classes of univariate polynomial rings are
  2562. @example
  2563. Ring
  2564. cl_ring
  2565. <cl_ring.h>
  2566. |
  2567. |
  2568. Univariate polynomial ring
  2569. cl_univpoly_ring
  2570. <cl_univpoly.h>
  2571. |
  2572. +----------------+-------------------+
  2573. | | |
  2574. Complex polynomial ring | Modular integer polynomial ring
  2575. cl_univpoly_complex_ring | cl_univpoly_modint_ring
  2576. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  2577. |
  2578. +----------------+
  2579. | |
  2580. Real polynomial ring |
  2581. cl_univpoly_real_ring |
  2582. <cl_univpoly_real.h> |
  2583. |
  2584. +----------------+
  2585. | |
  2586. Rational polynomial ring |
  2587. cl_univpoly_rational_ring |
  2588. <cl_univpoly_rational.h> |
  2589. |
  2590. +----------------+
  2591. |
  2592. Integer polynomial ring
  2593. cl_univpoly_integer_ring
  2594. <cl_univpoly_integer.h>
  2595. @end example
  2596. and the corresponding classes of univariate polynomials are
  2597. @example
  2598. Univariate polynomial
  2599. cl_UP
  2600. <cl_univpoly.h>
  2601. |
  2602. +----------------+-------------------+
  2603. | | |
  2604. Complex polynomial | Modular integer polynomial
  2605. cl_UP_N | cl_UP_MI
  2606. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  2607. |
  2608. +----------------+
  2609. | |
  2610. Real polynomial |
  2611. cl_UP_R |
  2612. <cl_univpoly_real.h> |
  2613. |
  2614. +----------------+
  2615. | |
  2616. Rational polynomial |
  2617. cl_UP_RA |
  2618. <cl_univpoly_rational.h> |
  2619. |
  2620. +----------------+
  2621. |
  2622. Integer polynomial
  2623. cl_UP_I
  2624. <cl_univpoly_integer.h>
  2625. @end example
  2626. Univariate polynomial rings are constructed using the functions
  2627. @table @code
  2628. @item cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R)
  2629. @itemx cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R, const cl_symbol& varname)
  2630. This function returns the polynomial ring @samp{R[X]}, unnamed or named.
  2631. @code{R} may be an arbitrary ring. This function takes care of finding out
  2632. about special cases of @code{R}, such as the rings of complex numbers,
  2633. real numbers, rational numbers, integers, or modular integer rings.
  2634. There is a cache table of rings, indexed by @code{R} and @code{varname}.
  2635. This ensures that two calls of this function with the same arguments will
  2636. return the same polynomial ring.
  2637. @item cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R)
  2638. @cindex @code{cl_find_univpoly_ring ()}
  2639. @itemx cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R, const cl_symbol& varname)
  2640. @itemx cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R)
  2641. @itemx cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R, const cl_symbol& varname)
  2642. @itemx cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R)
  2643. @itemx cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R, const cl_symbol& varname)
  2644. @itemx cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R)
  2645. @itemx cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R, const cl_symbol& varname)
  2646. @itemx cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R)
  2647. @itemx cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R, const cl_symbol& varname)
  2648. These functions are equivalent to the general @code{cl_find_univpoly_ring},
  2649. only the return type is more specific, according to the base ring's type.
  2650. @end table
  2651. @node Functions on univariate polynomials, Special polynomials, Univariate polynomial rings, Univariate polynomials
  2652. @section Functions on univariate polynomials
  2653. Given a univariate polynomial ring @code{R}, the following members can be used.
  2654. @table @code
  2655. @item cl_ring R->basering()
  2656. @cindex @code{basering ()}
  2657. This returns the base ring, as passed to @samp{cl_find_univpoly_ring}.
  2658. @item cl_UP R->zero()
  2659. @cindex @code{zero ()}
  2660. This returns @code{0 in R}, a polynomial of degree -1.
  2661. @item cl_UP R->one()
  2662. @cindex @code{one ()}
  2663. This returns @code{1 in R}, a polynomial of degree <= 0.
  2664. @item cl_UP R->canonhom (const cl_I& x)
  2665. @cindex @code{canonhom ()}
  2666. This returns @code{x in R}, a polynomial of degree <= 0.
  2667. @item cl_UP R->monomial (const cl_ring_element& x, uintL e)
  2668. @cindex @code{monomial ()}
  2669. This returns a sparse polynomial: @code{x * X^e}, where @code{X} is the
  2670. indeterminate.
  2671. @item cl_UP R->create (sintL degree)
  2672. @cindex @code{create ()}
  2673. Creates a new polynomial with a given degree. The zero polynomial has degree
  2674. @code{-1}. After creating the polynomial, you should put in the coefficients,
  2675. using the @code{set_coeff} member function, and then call the @code{finalize}
  2676. member function.
  2677. @end table
  2678. The following are the only destructive operations on univariate polynomials.
  2679. @table @code
  2680. @item void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)
  2681. @cindex @code{set_coeff ()}
  2682. This changes the coefficient of @code{X^index} in @code{x} to be @code{y}.
  2683. After changing a polynomial and before applying any "normal" operation on it,
  2684. you should call its @code{finalize} member function.
  2685. @item void finalize (cl_UP& x)
  2686. @cindex @code{finalize ()}
  2687. This function marks the endpoint of destructive modifications of a polynomial.
  2688. It normalizes the internal representation so that subsequent computations have
  2689. less overhead. Doing normal computations on unnormalized polynomials may
  2690. produce wrong results or crash the program.
  2691. @end table
  2692. The following operations are defined on univariate polynomials.
  2693. @table @code
  2694. @item cl_univpoly_ring x.ring ()
  2695. @cindex @code{ring ()}
  2696. Returns the ring to which the univariate polynomial @code{x} belongs.
  2697. @item cl_UP operator+ (const cl_UP&, const cl_UP&)
  2698. @cindex @code{operator + ()}
  2699. Returns the sum of two univariate polynomials.
  2700. @item cl_UP operator- (const cl_UP&, const cl_UP&)
  2701. @cindex @code{operator - ()}
  2702. Returns the difference of two univariate polynomials.
  2703. @item cl_UP operator- (const cl_UP&)
  2704. Returns the negative of a univariate polynomial.
  2705. @item cl_UP operator* (const cl_UP&, const cl_UP&)
  2706. @cindex @code{operator * ()}
  2707. Returns the product of two univariate polynomials. One of the arguments may
  2708. also be a plain integer or an element of the base ring.
  2709. @item cl_UP square (const cl_UP&)
  2710. @cindex @code{square ()}
  2711. Returns the square of a univariate polynomial.
  2712. @item cl_UP expt_pos (const cl_UP& x, const cl_I& y)
  2713. @cindex @code{expt_pos ()}
  2714. @code{y} must be > 0. Returns @code{x^y}.
  2715. @item bool operator== (const cl_UP&, const cl_UP&)
  2716. @cindex @code{operator == ()}
  2717. @itemx bool operator!= (const cl_UP&, const cl_UP&)
  2718. @cindex @code{operator != ()}
  2719. Compares two univariate polynomials, belonging to the same univariate
  2720. polynomial ring, for equality.
  2721. @item cl_boolean zerop (const cl_UP& x)
  2722. @cindex @code{zerop ()}
  2723. Returns true if @code{x} is @code{0 in R}.
  2724. @item sintL degree (const cl_UP& x)
  2725. @cindex @code{degree ()}
  2726. Returns the degree of the polynomial. The zero polynomial has degree @code{-1}.
  2727. @item cl_ring_element coeff (const cl_UP& x, uintL index)
  2728. @cindex @code{coeff ()}
  2729. Returns the coefficient of @code{X^index} in the polynomial @code{x}.
  2730. @item cl_ring_element x (const cl_ring_element& y)
  2731. @cindex @code{operator () ()}
  2732. Evaluation: If @code{x} is a polynomial and @code{y} belongs to the base ring,
  2733. then @samp{x(y)} returns the value of the substitution of @code{y} into
  2734. @code{x}.
  2735. @item cl_UP deriv (const cl_UP& x)
  2736. @cindex @code{deriv ()}
  2737. Returns the derivative of the polynomial @code{x} with respect to the
  2738. indeterminate @code{X}.
  2739. @end table
  2740. The following output functions are defined (see also the chapter on
  2741. input/output).
  2742. @table @code
  2743. @item void fprint (cl_ostream stream, const cl_UP& x)
  2744. @cindex @code{fprint ()}
  2745. @itemx cl_ostream operator<< (cl_ostream stream, const cl_UP& x)
  2746. @cindex @code{operator << ()}
  2747. Prints the univariate polynomial @code{x} on the @code{stream}. The output may
  2748. depend on the global printer settings in the variable
  2749. @code{cl_default_print_flags}.
  2750. @end table
  2751. @node Special polynomials, , Functions on univariate polynomials, Univariate polynomials
  2752. @section Special polynomials
  2753. The following functions return special polynomials.
  2754. @table @code
  2755. @item cl_UP_I cl_tschebychev (sintL n)
  2756. @cindex @code{cl_tschebychev ()}
  2757. @cindex Tschebychev polynomial
  2758. Returns the n-th Tchebychev polynomial (n >= 0).
  2759. @item cl_UP_I cl_hermite (sintL n)
  2760. @cindex @code{cl_hermite ()}
  2761. @cindex Hermite polynomial
  2762. Returns the n-th Hermite polynomial (n >= 0).
  2763. @item cl_UP_RA cl_legendre (sintL n)
  2764. @cindex @code{cl_legendre ()}
  2765. @cindex Legende polynomial
  2766. Returns the n-th Legendre polynomial (n >= 0).
  2767. @item cl_UP_I cl_laguerre (sintL n)
  2768. @cindex @code{cl_laguerre ()}
  2769. @cindex Laguerre polynomial
  2770. Returns the n-th Laguerre polynomial (n >= 0).
  2771. @end table
  2772. Information how to derive the differential equation satisfied by each
  2773. of these polynomials from their definition can be found in the
  2774. @code{doc/polynomial/} directory.
  2775. @node Internals, Using the library, Univariate polynomials, Top
  2776. @chapter Internals
  2777. @menu
  2778. * Why C++ ?::
  2779. * Memory efficiency::
  2780. * Speed efficiency::
  2781. * Garbage collection::
  2782. @end menu
  2783. @node Why C++ ?, Memory efficiency, Internals, Internals
  2784. @section Why C++ ?
  2785. @cindex advocacy
  2786. Using C++ as an implementation language provides
  2787. @itemize @bullet
  2788. @item
  2789. Efficiency: It compiles to machine code.
  2790. @item
  2791. @cindex portability
  2792. Portability: It runs on all platforms supporting a C++ compiler. Because
  2793. of the availability of GNU C++, this includes all currently used 32-bit and
  2794. 64-bit platforms, independently of the quality of the vendor's C++ compiler.
  2795. @item
  2796. Type safety: The C++ compilers knows about the number types and complains if,
  2797. for example, you try to assign a float to an integer variable. However,
  2798. a drawback is that C++ doesn't know about generic types, hence a restriction
  2799. like that @code{operator+ (const cl_MI&, const cl_MI&)} requires that both
  2800. arguments belong to the same modular ring cannot be expressed as a compile-time
  2801. information.
  2802. @item
  2803. Algebraic syntax: The elementary operations @code{+}, @code{-}, @code{*},
  2804. @code{=}, @code{==}, ... can be used in infix notation, which is more
  2805. convenient than Lisp notation @samp{(+ x y)} or C notation @samp{add(x,y,&z)}.
  2806. @end itemize
  2807. With these language features, there is no need for two separate languages,
  2808. one for the implementation of the library and one in which the library's users
  2809. can program. This means that a prototype implementation of an algorithm
  2810. can be integrated into the library immediately after it has been tested and
  2811. debugged. No need to rewrite it in a low-level language after having prototyped
  2812. in a high-level language.
  2813. @node Memory efficiency, Speed efficiency, Why C++ ?, Internals
  2814. @section Memory efficiency
  2815. In order to save memory allocations, CLN implements:
  2816. @itemize @bullet
  2817. @item
  2818. Object sharing: An operation like @code{x+0} returns @code{x} without copying
  2819. it.
  2820. @item
  2821. @cindex garbage collection
  2822. @cindex reference counting
  2823. Garbage collection: A reference counting mechanism makes sure that any
  2824. number object's storage is freed immediately when the last reference to the
  2825. object is gone.
  2826. @item
  2827. Small integers are represented as immediate values instead of pointers
  2828. to heap allocated storage. This means that integers @code{> -2^29},
  2829. @code{< 2^29} don't consume heap memory, unless they were explicitly allocated
  2830. on the heap.
  2831. @end itemize
  2832. @node Speed efficiency, Garbage collection, Memory efficiency, Internals
  2833. @section Speed efficiency
  2834. Speed efficiency is obtained by the combination of the following tricks
  2835. and algorithms:
  2836. @itemize @bullet
  2837. @item
  2838. Small integers, being represented as immediate values, don't require
  2839. memory access, just a couple of instructions for each elementary operation.
  2840. @item
  2841. The kernel of CLN has been written in assembly language for some CPUs
  2842. (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
  2843. @item
  2844. On all CPUs, CLN may be configured to use the superefficient low-level
  2845. routines from GNU GMP version 3.
  2846. @item
  2847. For large numbers, CLN uses, instead of the standard @code{O(N^2)}
  2848. algorithm, the Karatsuba multiplication, which is an
  2849. @iftex
  2850. @tex
  2851. $O(N^{1.6})$
  2852. @end tex
  2853. @end iftex
  2854. @ifinfo
  2855. @code{O(N^1.6)}
  2856. @end ifinfo
  2857. algorithm.
  2858. @item
  2859. For very large numbers (more than 12000 decimal digits), CLN uses
  2860. @iftex
  2861. Sch{@"o}nhage-Strassen
  2862. @cindex Sch{@"o}nhage-Strassen
  2863. @end iftex
  2864. @ifinfo
  2865. Sch�nhage-Strassen
  2866. @cindex Sch�nhage-Strassen
  2867. @end ifinfo
  2868. multiplication, which is an asymptotically
  2869. optimal multiplication algorithm.
  2870. @item
  2871. These fast multiplication algorithms also give improvements in the speed
  2872. of division and radix conversion.
  2873. @end itemize
  2874. @node Garbage collection, , Speed efficiency, Internals
  2875. @section Garbage collection
  2876. @cindex garbage collection
  2877. All the number classes are reference count classes: They only contain a pointer
  2878. to an object in the heap. Upon construction, assignment and destruction of
  2879. number objects, only the objects' reference count are manipulated.
  2880. Memory occupied by number objects are automatically reclaimed as soon as
  2881. their reference count drops to zero.
  2882. For number rings, another strategy is implemented: There is a cache of,
  2883. for example, the modular integer rings. A modular integer ring is destroyed
  2884. only if its reference count dropped to zero and the cache is about to be
  2885. resized. The effect of this strategy is that recently used rings remain
  2886. cached, whereas undue memory consumption through cached rings is avoided.
  2887. @node Using the library, Customizing, Internals, Top
  2888. @chapter Using the library
  2889. For the following discussion, we will assume that you have installed
  2890. the CLN source in @code{$CLN_DIR} and built it in @code{$CLN_TARGETDIR}.
  2891. For example, for me it's @code{CLN_DIR="$HOME/cln"} and
  2892. @code{CLN_TARGETDIR="$HOME/cln/linuxelf"}. You might define these as
  2893. environment variables, or directly substitute the appropriate values.
  2894. @menu
  2895. * Compiler options::
  2896. * Include files::
  2897. * An Example::
  2898. * Debugging support::
  2899. @end menu
  2900. @node Compiler options, Include files, Using the library, Using the library
  2901. @section Compiler options
  2902. @cindex compiler options
  2903. Until you have installed CLN in a public place, the following options are
  2904. needed:
  2905. When you compile CLN application code, add the flags
  2906. @example
  2907. -I$CLN_DIR/include -I$CLN_TARGETDIR/include
  2908. @end example
  2909. to the C++ compiler's command line (@code{make} variable CFLAGS or CXXFLAGS).
  2910. When you link CLN application code to form an executable, add the flags
  2911. @example
  2912. $CLN_TARGETDIR/src/libcln.a
  2913. @end example
  2914. to the C/C++ compiler's command line (@code{make} variable LIBS).
  2915. If you did a @code{make install}, the include files are installed in a
  2916. public directory (normally @code{/usr/local/include}), hence you don't
  2917. need special flags for compiling. The library has been installed to a
  2918. public directory as well (normally @code{/usr/local/lib}), hence when
  2919. linking a CLN application it is sufficient to give the flag @code{-lcln}.
  2920. @node Include files, An Example, Compiler options, Using the library
  2921. @section Include files
  2922. @cindex include files
  2923. @cindex header files
  2924. Here is a summary of the include files and their contents.
  2925. @table @code
  2926. @item <cl_object.h>
  2927. General definitions, reference counting, garbage collection.
  2928. @item <cl_number.h>
  2929. The class cl_number.
  2930. @item <cl_complex.h>
  2931. Functions for class cl_N, the complex numbers.
  2932. @item <cl_real.h>
  2933. Functions for class cl_R, the real numbers.
  2934. @item <cl_float.h>
  2935. Functions for class cl_F, the floats.
  2936. @item <cl_sfloat.h>
  2937. Functions for class cl_SF, the short-floats.
  2938. @item <cl_ffloat.h>
  2939. Functions for class cl_FF, the single-floats.
  2940. @item <cl_dfloat.h>
  2941. Functions for class cl_DF, the double-floats.
  2942. @item <cl_lfloat.h>
  2943. Functions for class cl_LF, the long-floats.
  2944. @item <cl_rational.h>
  2945. Functions for class cl_RA, the rational numbers.
  2946. @item <cl_integer.h>
  2947. Functions for class cl_I, the integers.
  2948. @item <cl_io.h>
  2949. Input/Output.
  2950. @item <cl_complex_io.h>
  2951. Input/Output for class cl_N, the complex numbers.
  2952. @item <cl_real_io.h>
  2953. Input/Output for class cl_R, the real numbers.
  2954. @item <cl_float_io.h>
  2955. Input/Output for class cl_F, the floats.
  2956. @item <cl_sfloat_io.h>
  2957. Input/Output for class cl_SF, the short-floats.
  2958. @item <cl_ffloat_io.h>
  2959. Input/Output for class cl_FF, the single-floats.
  2960. @item <cl_dfloat_io.h>
  2961. Input/Output for class cl_DF, the double-floats.
  2962. @item <cl_lfloat_io.h>
  2963. Input/Output for class cl_LF, the long-floats.
  2964. @item <cl_rational_io.h>
  2965. Input/Output for class cl_RA, the rational numbers.
  2966. @item <cl_integer_io.h>
  2967. Input/Output for class cl_I, the integers.
  2968. @item <cl_input.h>
  2969. Flags for customizing input operations.
  2970. @item <cl_output.h>
  2971. Flags for customizing output operations.
  2972. @item <cl_malloc.h>
  2973. @code{cl_malloc_hook}, @code{cl_free_hook}.
  2974. @item <cl_abort.h>
  2975. @code{cl_abort}.
  2976. @item <cl_condition.h>
  2977. Conditions/exceptions.
  2978. @item <cl_string.h>
  2979. Strings.
  2980. @item <cl_symbol.h>
  2981. Symbols.
  2982. @item <cl_proplist.h>
  2983. Property lists.
  2984. @item <cl_ring.h>
  2985. General rings.
  2986. @item <cl_null_ring.h>
  2987. The null ring.
  2988. @item <cl_complex_ring.h>
  2989. The ring of complex numbers.
  2990. @item <cl_real_ring.h>
  2991. The ring of real numbers.
  2992. @item <cl_rational_ring.h>
  2993. The ring of rational numbers.
  2994. @item <cl_integer_ring.h>
  2995. The ring of integers.
  2996. @item <cl_numtheory.h>
  2997. Number threory functions.
  2998. @item <cl_modinteger.h>
  2999. Modular integers.
  3000. @item <cl_V.h>
  3001. Vectors.
  3002. @item <cl_GV.h>
  3003. General vectors.
  3004. @item <cl_GV_number.h>
  3005. General vectors over cl_number.
  3006. @item <cl_GV_complex.h>
  3007. General vectors over cl_N.
  3008. @item <cl_GV_real.h>
  3009. General vectors over cl_R.
  3010. @item <cl_GV_rational.h>
  3011. General vectors over cl_RA.
  3012. @item <cl_GV_integer.h>
  3013. General vectors over cl_I.
  3014. @item <cl_GV_modinteger.h>
  3015. General vectors of modular integers.
  3016. @item <cl_SV.h>
  3017. Simple vectors.
  3018. @item <cl_SV_number.h>
  3019. Simple vectors over cl_number.
  3020. @item <cl_SV_complex.h>
  3021. Simple vectors over cl_N.
  3022. @item <cl_SV_real.h>
  3023. Simple vectors over cl_R.
  3024. @item <cl_SV_rational.h>
  3025. Simple vectors over cl_RA.
  3026. @item <cl_SV_integer.h>
  3027. Simple vectors over cl_I.
  3028. @item <cl_SV_ringelt.h>
  3029. Simple vectors of general ring elements.
  3030. @item <cl_univpoly.h>
  3031. Univariate polynomials.
  3032. @item <cl_univpoly_integer.h>
  3033. Univariate polynomials over the integers.
  3034. @item <cl_univpoly_rational.h>
  3035. Univariate polynomials over the rational numbers.
  3036. @item <cl_univpoly_real.h>
  3037. Univariate polynomials over the real numbers.
  3038. @item <cl_univpoly_complex.h>
  3039. Univariate polynomials over the complex numbers.
  3040. @item <cl_univpoly_modint.h>
  3041. Univariate polynomials over modular integer rings.
  3042. @item <cl_timing.h>
  3043. Timing facilities.
  3044. @item <cln.h>
  3045. Includes all of the above.
  3046. @end table
  3047. @node An Example, Debugging support, Include files, Using the library
  3048. @section An Example
  3049. A function which computes the nth Fibonacci number can be written as follows.
  3050. @cindex Fibonacci number
  3051. @example
  3052. #include <cl_integer.h>
  3053. #include <cl_real.h>
  3054. // Returns F_n, computed as the nearest integer to
  3055. // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
  3056. const cl_I fibonacci (int n)
  3057. @{
  3058. // Need a precision of ((1+sqrt(5))/2)^-n.
  3059. cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
  3060. cl_R sqrt5 = sqrt(cl_float(5,prec));
  3061. cl_R phi = (1+sqrt5)/2;
  3062. return round1( expt(phi,n)/sqrt5 );
  3063. @}
  3064. @end example
  3065. Let's explain what is going on in detail.
  3066. The include file @code{<cl_integer.h>} is necessary because the type
  3067. @code{cl_I} is used in the function, and the include file @code{<cl_real.h>}
  3068. is needed for the type @code{cl_R} and the floating point number functions.
  3069. The order of the include files does not matter.
  3070. Then comes the function declaration. The argument is an @code{int}, the
  3071. result an integer. The return type is defined as @samp{const cl_I}, not
  3072. simply @samp{cl_I}, because that allows the compiler to detect typos like
  3073. @samp{fibonacci(n) = 100}. It would be possible to declare the return
  3074. type as @code{const cl_R} (real number) or even @code{const cl_N} (complex
  3075. number). We use the most specialized possible return type because functions
  3076. which call @samp{fibonacci} will be able to profit from the compiler's type
  3077. analysis: Adding two integers is slightly more efficient than adding the
  3078. same objects declared as complex numbers, because it needs less type
  3079. dispatch. Also, when linking to CLN as a non-shared library, this minimizes
  3080. the size of the resulting executable program.
  3081. The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
  3082. integer. In order to get a correct result, the absolute error should be less
  3083. than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
  3084. To this end, the first line computes a floating point precision for sqrt(5)
  3085. and phi.
  3086. Then sqrt(5) is computed by first converting the integer 5 to a floating point
  3087. number and than taking the square root. The converse, first taking the square
  3088. root of 5, and then converting to the desired precision, would not work in
  3089. CLN: The square root would be computed to a default precision (normally
  3090. single-float precision), and the following conversion could not help about
  3091. the lacking accuracy. This is because CLN is not a symbolic computer algebra
  3092. system and does not represent sqrt(5) in a non-numeric way.
  3093. The type @code{cl_R} for sqrt5 and, in the following line, phi is the only
  3094. possible choice. You cannot write @code{cl_F} because the C++ compiler can
  3095. only infer that @code{cl_float(5,prec)} is a real number. You cannot write
  3096. @code{cl_N} because a @samp{round1} does not exist for general complex
  3097. numbers.
  3098. When the function returns, all the local variables in the function are
  3099. automatically reclaimed (garbage collected). Only the result survives and
  3100. gets passed to the caller.
  3101. The file @code{fibonacci.cc} in the subdirectory @code{examples}
  3102. contains this implementation together with an even faster algorithm.
  3103. @node Debugging support, , An Example, Using the library
  3104. @section Debugging support
  3105. @cindex debugging
  3106. When debugging a CLN application with GNU @code{gdb}, two facilities are
  3107. available from the library:
  3108. @itemize @bullet
  3109. @item The library does type checks, range checks, consistency checks at
  3110. many places. When one of these fails, the function @code{cl_abort()} is
  3111. called. Its default implementation is to perform an @code{exit(1)}, so
  3112. you won't have a core dump. But for debugging, it is best to set a
  3113. breakpoint at this function:
  3114. @example
  3115. (gdb) break cl_abort
  3116. @end example
  3117. When this breakpoint is hit, look at the stack's backtrace:
  3118. @example
  3119. (gdb) where
  3120. @end example
  3121. @item The debugger's normal @code{print} command doesn't know about
  3122. CLN's types and therefore prints mostly useless hexadecimal addresses.
  3123. CLN offers a function @code{cl_print}, callable from the debugger,
  3124. for printing number objects. In order to get this function, you have
  3125. to define the macro @samp{CL_DEBUG} and then include all the header files
  3126. for which you want @code{cl_print} debugging support. For example:
  3127. @cindex @code{CL_DEBUG}
  3128. @example
  3129. #define CL_DEBUG
  3130. #include <cl_string.h>
  3131. @end example
  3132. Now, if you have in your program a variable @code{cl_string s}, and
  3133. inspect it under @code{gdb}, the output may look like this:
  3134. @example
  3135. (gdb) print s
  3136. $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
  3137. word = 134568800@}@}, @}
  3138. (gdb) call cl_print(s)
  3139. (cl_string) ""
  3140. $8 = 134568800
  3141. @end example
  3142. Note that the output of @code{cl_print} goes to the program's error output,
  3143. not to gdb's standard output.
  3144. Note, however, that the above facility does not work with all CLN types,
  3145. only with number objects and similar. Therefore CLN offers a member function
  3146. @code{debug_print()} on all CLN types. The same macro @samp{CL_DEBUG}
  3147. is needed for this member function to be implemented. Under @code{gdb},
  3148. you call it like this:
  3149. @cindex @code{debug_print ()}
  3150. @example
  3151. (gdb) print s
  3152. $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
  3153. word = 134568800@}@}, @}
  3154. (gdb) call s.debug_print()
  3155. (cl_string) ""
  3156. (gdb) define cprint
  3157. >call ($1).debug_print()
  3158. >end
  3159. (gdb) cprint s
  3160. (cl_string) ""
  3161. @end example
  3162. Unfortunately, this feature does not seem to work under all circumstances.
  3163. @end itemize
  3164. @node Customizing, Index, Using the library, Top
  3165. @chapter Customizing
  3166. @cindex customizing
  3167. @menu
  3168. * Error handling::
  3169. * Floating-point underflow::
  3170. * Customizing I/O::
  3171. * Customizing the memory allocator::
  3172. @end menu
  3173. @node Error handling, Floating-point underflow, Customizing, Customizing
  3174. @section Error handling
  3175. When a fatal error occurs, an error message is output to the standard error
  3176. output stream, and the function @code{cl_abort} is called. The default
  3177. version of this function (provided in the library) terminates the application.
  3178. To catch such a fatal error, you need to define the function @code{cl_abort}
  3179. yourself, with the prototype
  3180. @example
  3181. #include <cl_abort.h>
  3182. void cl_abort (void);
  3183. @end example
  3184. @cindex @code{cl_abort ()}
  3185. This function must not return control to its caller.
  3186. @node Floating-point underflow, Customizing I/O, Error handling, Customizing
  3187. @section Floating-point underflow
  3188. @cindex underflow
  3189. Floating point underflow denotes the situation when a floating-point number
  3190. is to be created which is so close to @code{0} that its exponent is too
  3191. low to be represented internally. By default, this causes a fatal error.
  3192. If you set the global variable
  3193. @example
  3194. cl_boolean cl_inhibit_floating_point_underflow
  3195. @end example
  3196. to @code{cl_true}, the error will be inhibited, and a floating-point zero
  3197. will be generated instead. The default value of
  3198. @code{cl_inhibit_floating_point_underflow} is @code{cl_false}.
  3199. @node Customizing I/O, Customizing the memory allocator, Floating-point underflow, Customizing
  3200. @section Customizing I/O
  3201. The output of the function @code{fprint} may be customized by changing the
  3202. value of the global variable @code{cl_default_print_flags}.
  3203. @cindex @code{cl_default_print_flags}
  3204. @node Customizing the memory allocator, , Customizing I/O, Customizing
  3205. @section Customizing the memory allocator
  3206. Every memory allocation of CLN is done through the function pointer
  3207. @code{cl_malloc_hook}. Freeing of this memory is done through the function
  3208. pointer @code{cl_free_hook}. The default versions of these functions,
  3209. provided in the library, call @code{malloc} and @code{free} and check
  3210. the @code{malloc} result against @code{NULL}.
  3211. If you want to provide another memory allocator, you need to define
  3212. the variables @code{cl_malloc_hook} and @code{cl_free_hook} yourself,
  3213. like this:
  3214. @example
  3215. #include <cl_malloc.h>
  3216. void* (*cl_malloc_hook) (size_t size) = @dots{};
  3217. void (*cl_free_hook) (void* ptr) = @dots{};
  3218. @end example
  3219. @cindex @code{cl_malloc_hook ()}
  3220. @cindex @code{cl_free_hook ()}
  3221. The @code{cl_malloc_hook} function must not return a @code{NULL} pointer.
  3222. It is not possible to change the memory allocator at runtime, because
  3223. it is already called at program startup by the constructors of some
  3224. global variables.
  3225. @c Indices
  3226. @node Index, , Customizing, Top
  3227. @unnumbered Index
  3228. @printindex my
  3229. @c Table of contents
  3230. @contents
  3231. @bye