1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
The num library: arbitrary-precision rational arithmetic
</TITLE>
</HEAD>
<BODY >
<A HREF="manual060.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual062.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H1>Chapter 19: The num library: arbitrary-precision rational arithmetic</H1>
The <TT>num</TT> library implements
exact-precision rational arithmetic. It is built upon the
state-of-the-art BigNum arbitrary-precision integer arithmetic
package, and therefore achieves very high performance.<BR>
<BR>
The functions provided in this library are fully documented in
<EM>The CAML Numbers Reference Manual</EM> by
Valrie Mnissier-Morain, technical report 141, INRIA, july 1992
(available electronically,
<TT>ftp://ftp.inria.fr/INRIA/publication/RT/RT-0141.ps.gz</TT>).
A summary of the functions is given below.<BR>
<BR>
Programs that use the <TT>num</TT> library must be linked in ``custom
runtime'' mode, as follows:
<PRE>
ocamlc -custom <I>other options</I> nums.cma <I>other files</I> -cclib -lnums
ocamlopt <I>other options</I> nums.cmxa <I>other files</I> -cclib -lnums
</PRE>
For interactive use of the <TT>nums</TT> library, do:
<PRE>
ocamlmktop -custom -o mytop nums.cma -cclib -lnums
./mytop
</PRE>
<H2>19.1 Module <TT>Num</TT>: operation on arbitrary-precision numbers</H2><A NAME="s:Num"></A>
<A NAME="@manual669"></A><PRE>
open Nat
open Big_int
open Ratio
</PRE>
<BLOCKQUOTE>
Numbers (type <CODE>num</CODE>) are arbitrary-precision rational numbers,
plus the special elements <CODE>1/0</CODE> (infinity) and <CODE>0/0</CODE> (undefined).
</BLOCKQUOTE>
<PRE>
type num = Int of int | Big_int of big_int | Ratio of ratio
</PRE>
<BLOCKQUOTE>
The type of numbers.
</BLOCKQUOTE>
<BLOCKQUOTE>
Arithmetic operations
</BLOCKQUOTE>
<PRE>
val (+/) : num -> num -> num
val add_num : num -> num -> num
</PRE>
<A NAME="@manual670"></A><A NAME="@manual671"></A><BLOCKQUOTE>
Addition
</BLOCKQUOTE>
<PRE>
val minus_num : num -> num
</PRE>
<A NAME="@manual672"></A><BLOCKQUOTE>
Unary negation.
</BLOCKQUOTE>
<PRE>
val (-/) : num -> num -> num
val sub_num : num -> num -> num
</PRE>
<A NAME="@manual673"></A><A NAME="@manual674"></A><BLOCKQUOTE>
Subtraction
</BLOCKQUOTE>
<PRE>
val (*/) : num -> num -> num
val mult_num : num -> num -> num
</PRE>
<A NAME="@manual675"></A><A NAME="@manual676"></A><BLOCKQUOTE>
Multiplication
</BLOCKQUOTE>
<PRE>
val square_num : num -> num
</PRE>
<A NAME="@manual677"></A><BLOCKQUOTE>
Squaring
</BLOCKQUOTE>
<PRE>
val (//) : num -> num -> num
val div_num : num -> num -> num
</PRE>
<A NAME="@manual678"></A><A NAME="@manual679"></A><BLOCKQUOTE>
Division
</BLOCKQUOTE>
<PRE>
val quo_num : num -> num -> num
val mod_num : num -> num -> num
</PRE>
<A NAME="@manual680"></A><A NAME="@manual681"></A><BLOCKQUOTE>
Euclidean division: quotient and remainder
</BLOCKQUOTE>
<PRE>
val (**/) : num -> num -> num
val power_num : num -> num -> num
</PRE>
<A NAME="@manual682"></A><A NAME="@manual683"></A><BLOCKQUOTE>
Exponentiation
</BLOCKQUOTE>
<PRE>
val is_integer_num : num -> bool
</PRE>
<A NAME="@manual684"></A><BLOCKQUOTE>
Test if a number is an integer
</BLOCKQUOTE>
<PRE>
val integer_num : num -> num
val floor_num : num -> num
val round_num : num -> num
val ceiling_num : num -> num
</PRE>
<A NAME="@manual685"></A><A NAME="@manual686"></A><A NAME="@manual687"></A><A NAME="@manual688"></A><BLOCKQUOTE>
Approximate a number by an integer.
<CODE>floor_num n</CODE> returns the largest integer smaller or equal to <CODE>n</CODE>.
<CODE>ceiling_num n</CODE> returns the smallest integer bigger or equal to <CODE>n</CODE>.
<CODE>integer_num n</CODE> returns the integer closest to <CODE>n</CODE>. In case of ties,
rounds towards zero.
<CODE>round_num n</CODE> returns the integer closest to <CODE>n</CODE>. In case of ties,
rounds off zero.
</BLOCKQUOTE>
<PRE>
val sign_num : num -> int
</PRE>
<A NAME="@manual689"></A><BLOCKQUOTE>
Return <CODE>-1</CODE>, <CODE>0</CODE> or <CODE>1</CODE> according to the sign of the argument.
</BLOCKQUOTE>
<PRE>
val (=/) : num -> num -> bool
val (</) : num -> num -> bool
val (>/) : num -> num -> bool
val (<=/) : num -> num -> bool
val (>=/) : num -> num -> bool
val (<>/) : num -> num -> bool
val eq_num : num -> num -> bool
val lt_num : num -> num -> bool
val le_num : num -> num -> bool
val gt_num : num -> num -> bool
val ge_num : num -> num -> bool
</PRE>
<A NAME="@manual690"></A><A NAME="@manual691"></A><A NAME="@manual692"></A><A NAME="@manual693"></A><A NAME="@manual694"></A><A NAME="@manual695"></A><A NAME="@manual696"></A><A NAME="@manual697"></A><A NAME="@manual698"></A><A NAME="@manual699"></A><A NAME="@manual700"></A><BLOCKQUOTE>
Usual comparisons between numbers
</BLOCKQUOTE>
<PRE>
val compare_num : num -> num -> int
</PRE>
<A NAME="@manual701"></A><BLOCKQUOTE>
Return <CODE>-1</CODE>, <CODE>0</CODE> or <CODE>1</CODE> if the first argument is less than,
equal to, or greater than the second argument.
</BLOCKQUOTE>
<PRE>
val max_num : num -> num -> num
val min_num : num -> num -> num
</PRE>
<A NAME="@manual702"></A><A NAME="@manual703"></A><BLOCKQUOTE>
Return the greater (resp. the smaller) of the two arguments.
</BLOCKQUOTE>
<PRE>
val abs_num : num -> num
</PRE>
<A NAME="@manual704"></A><BLOCKQUOTE>
Absolute value.
</BLOCKQUOTE>
<PRE>
val succ_num: num -> num
</PRE>
<A NAME="@manual705"></A><BLOCKQUOTE>
<CODE>succ n</CODE> is <CODE>n+1</CODE>
</BLOCKQUOTE>
<PRE>
val pred_num: num -> num
</PRE>
<A NAME="@manual706"></A><BLOCKQUOTE>
<CODE>pred n</CODE> is <CODE>n-1</CODE>
</BLOCKQUOTE>
<PRE>
val incr_num: num ref -> unit
</PRE>
<A NAME="@manual707"></A><BLOCKQUOTE>
<CODE>incr r</CODE> is <CODE>r:=!r+1</CODE>, where <CODE>r</CODE> is a reference to a number.
</BLOCKQUOTE>
<PRE>
val decr_num: num ref -> unit
</PRE>
<A NAME="@manual708"></A><BLOCKQUOTE>
<CODE>decr r</CODE> is <CODE>r:=!r-1</CODE>, where <CODE>r</CODE> is a reference to a number.
</BLOCKQUOTE>
<BLOCKQUOTE>
Coercions with strings
</BLOCKQUOTE>
<PRE>
val string_of_num : num -> string
</PRE>
<A NAME="@manual709"></A><BLOCKQUOTE>
Convert a number to a string, using fractional notation.
</BLOCKQUOTE>
<PRE>
val approx_num_fix : int -> num -> string
val approx_num_exp : int -> num -> string
</PRE>
<A NAME="@manual710"></A><A NAME="@manual711"></A><BLOCKQUOTE>
Approximate a number by a decimal. The first argument is the
required precision. The second argument is the number to
approximate. <CODE>approx_fix</CODE> uses decimal notation; the first
argument is the number of digits after the decimal point.
<CODE>approx_exp</CODE> uses scientific (exponential) notation; the
first argument is the number of digits in the mantissa.
</BLOCKQUOTE>
<PRE>
val num_of_string : string -> num
</PRE>
<A NAME="@manual712"></A><BLOCKQUOTE>
Convert a string to a number.
</BLOCKQUOTE>
<BLOCKQUOTE>
Coercions between numerical types
</BLOCKQUOTE>
<PRE>
val int_of_num : num -> int
val num_of_int : int -> num
val nat_of_num : num -> nat
val num_of_nat : nat -> num
val num_of_big_int : big_int -> num
val big_int_of_num : num -> big_int
val ratio_of_num : num -> ratio
val num_of_ratio : ratio -> num
val float_of_num : num -> float
</PRE>
<A NAME="@manual713"></A><A NAME="@manual714"></A><A NAME="@manual715"></A><A NAME="@manual716"></A><A NAME="@manual717"></A><A NAME="@manual718"></A><A NAME="@manual719"></A><A NAME="@manual720"></A><A NAME="@manual721"></A>
<H2>19.2 Module <TT>Arith_status</TT>: flags that control rational arithmetic</H2><A NAME="s:Arithstatus"></A>
<A NAME="@manual722"></A><PRE>
val arith_status: unit -> unit
</PRE>
<A NAME="@manual723"></A><BLOCKQUOTE>
Print the current status of the arithmetic flags.
</BLOCKQUOTE>
<PRE>
val get_error_when_null_denominator : unit -> bool
val set_error_when_null_denominator : bool -> unit
</PRE>
<A NAME="@manual724"></A><A NAME="@manual725"></A><BLOCKQUOTE>
Get or set the flag <CODE>null_denominator</CODE>. When on, attempting to
create a rational with a null denominator raises an exception.
When off, rationals with null denominators are accepted.
Initially: on.
</BLOCKQUOTE>
<PRE>
val get_normalize_ratio : unit -> bool
val set_normalize_ratio : bool -> unit
</PRE>
<A NAME="@manual726"></A><A NAME="@manual727"></A><BLOCKQUOTE>
Get or set the flag <CODE>normalize_ratio</CODE>. When on, rational
numbers are normalized after each operation. When off,
rational numbers are not normalized until printed.
Initially: off.
</BLOCKQUOTE>
<PRE>
val get_normalize_ratio_when_printing : unit -> bool
val set_normalize_ratio_when_printing : bool -> unit
</PRE>
<A NAME="@manual728"></A><A NAME="@manual729"></A><BLOCKQUOTE>
Get or set the flag <CODE>normalize_ratio_when_printing</CODE>.
When on, rational numbers are normalized before being printed.
When off, rational numbers are printed as is, without normalization.
Initially: on.
</BLOCKQUOTE>
<PRE>
val get_approx_printing : unit -> bool
val set_approx_printing : bool -> unit
</PRE>
<A NAME="@manual730"></A><A NAME="@manual731"></A><BLOCKQUOTE>
Get or set the flag <CODE>approx_printing</CODE>.
When on, rational numbers are printed as a decimal approximation.
When off, rational numbers are printed as a fraction.
Initially: off.
</BLOCKQUOTE>
<PRE>
val get_floating_precision : unit -> int
val set_floating_precision : int -> unit
</PRE>
<A NAME="@manual732"></A><A NAME="@manual733"></A><BLOCKQUOTE>
Get or set the parameter <CODE>floating_precision</CODE>.
This parameter is the number of digits displayed when
<CODE>approx_printing</CODE> is on.
Initially: 12.
</BLOCKQUOTE>
<HR>
<A HREF="manual060.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual062.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|