<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" href="style.css" type="text/css"> <meta content="text/html; charset=utf8" http-equiv="Content-Type"> <link rel="Start" href="index.html"> <link rel="previous" href="Z.html"> <link rel="next" href="Big_int_Z.html"> <link rel="Up" href="index.html"> <link title="Index of types" rel=Appendix href="index_types.html"> <link title="Index of exceptions" rel=Appendix href="index_exceptions.html"> <link title="Index of values" rel=Appendix href="index_values.html"> <link title="Index of modules" rel=Appendix href="index_modules.html"> <link title="Z" rel="Chapter" href="Z.html"> <link title="Q" rel="Chapter" href="Q.html"> <link title="Big_int_Z" rel="Chapter" href="Big_int_Z.html"><link title="Types" rel="Section" href="#1_Types"> <link title="Construction" rel="Section" href="#1_Construction"> <link title="Inspection" rel="Section" href="#1_Inspection"> <link title="Testing" rel="Section" href="#1_Testing"> <link title="Conversions" rel="Section" href="#1_Conversions"> <link title="Arithmetic operations" rel="Section" href="#1_Arithmeticoperations"> <link title="Printing" rel="Section" href="#1_Printing"> <link title="Prefix and infix operators" rel="Section" href="#1_Prefixandinfixoperators"> <title>Q</title> </head> <body> <div class="navbar"><a class="pre" href="Z.html" title="Z">Previous</a> <a class="up" href="index.html" title="Index">Up</a> <a class="post" href="Big_int_Z.html" title="Big_int_Z">Next</a> </div> <h1>Module <a href="type_Q.html">Q</a></h1> <pre><span class="keyword">module</span> Q: <code class="code">sig</code> <a href="Q.html">..</a> <code class="code">end</code></pre><div class="info module top"> Rationals. <p> This modules builds arbitrary precision rationals on top of arbitrary integers from module Z. <p> This file is part of the Zarith library http://forge.ocamlcore.org/projects/zarith . It is distributed under LGPL 2 licensing, with static linking exception. See the LICENSE file included in the distribution. <p> Copyright (c) 2010-2011 Antoine Miné, Abstraction project. Abstraction is part of the LIENS (Laboratoire d'Informatique de l'ENS), a joint laboratory by: CNRS (Centre national de la recherche scientifique, France), ENS (École normale supérieure, Paris, France), INRIA Rocquencourt (Institut national de recherche en informatique, France).<br> </div> <hr width="100%"> <br> <h1 id="1_Types">Types</h1><br> <pre><code><span id="TYPEt"><span class="keyword">type</span> <code class="type"></code>t</span> = {</code></pre><table class="typetable"> <tr> <td align="left" valign="top" > <code> </code></td> <td align="left" valign="top" > <code><span id="TYPEELTt.num">num</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a></code>;</code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Numerator.</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr> <tr> <td align="left" valign="top" > <code> </code></td> <td align="left" valign="top" > <code><span id="TYPEELTt.den">den</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a></code>;</code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Denominator, >= 0</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr></table> } <div class="info "> A rational is represented as a pair numerator/denominator, reduced to have a non-negative denominator and no common factor. This form is canonical (enabling polymorphic equality and hashing). The representation allows three special numbers: <code class="code">inf</code> (1/0), <code class="code">-inf</code> (-1/0) and <code class="code">undef</code> (0/0).<br> </div> <br> <h1 id="1_Construction">Construction</h1><br> <pre><span id="VALmake"><span class="keyword">val</span> make</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a> -> <a href="Z.html#TYPEt">Z.t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">make num den</code> constructs a new rational equal to <code class="code">num</code>/<code class="code">den</code>. It takes care of putting the rational in canonical form.<br> </div> <pre><span id="VALzero"><span class="keyword">val</span> zero</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALone"><span class="keyword">val</span> one</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALminus_one"><span class="keyword">val</span> minus_one</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre><div class="info "> 0, 1, -1.<br> </div> <pre><span id="VALinf"><span class="keyword">val</span> inf</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre><div class="info "> 1/0.<br> </div> <pre><span id="VALminus_inf"><span class="keyword">val</span> minus_inf</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre><div class="info "> -1/0.<br> </div> <pre><span id="VALundef"><span class="keyword">val</span> undef</span> : <code class="type"><a href="Q.html#TYPEt">t</a></code></pre><div class="info "> 0/0.<br> </div> <pre><span id="VALof_bigint"><span class="keyword">val</span> of_bigint</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a> -> <a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALof_int"><span class="keyword">val</span> of_int</span> : <code class="type">int -> <a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALof_int32"><span class="keyword">val</span> of_int32</span> : <code class="type">int32 -> <a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALof_int64"><span class="keyword">val</span> of_int64</span> : <code class="type">int64 -> <a href="Q.html#TYPEt">t</a></code></pre> <pre><span id="VALof_nativeint"><span class="keyword">val</span> of_nativeint</span> : <code class="type">nativeint -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Conversions from various integer types.<br> </div> <pre><span id="VALof_ints"><span class="keyword">val</span> of_ints</span> : <code class="type">int -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Conversion from an <code class="code">int</code> numerator and an <code class="code">int</code> denominator.<br> </div> <pre><span id="VALof_float"><span class="keyword">val</span> of_float</span> : <code class="type">float -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Conversion from a <code class="code">float</code>. The conversion is exact, and maps NaN to <code class="code">undef</code>.<br> </div> <pre><span id="VALof_string"><span class="keyword">val</span> of_string</span> : <code class="type">string -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Converts a string to a rational. Plain decimals, and <code class="code">/</code> separated decimal ratios (with optional sign) are understood. Additionally, the special <code class="code">inf</code>, <code class="code">-inf</code>, and <code class="code">undef</code> are recognized (they can also be typeset respectively as <code class="code">1/0</code>, <code class="code">-1/0</code>, <code class="code">0/0</code>).<br> </div> <br> <h1 id="1_Inspection">Inspection</h1><br> <pre><span id="VALnum"><span class="keyword">val</span> num</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Z.html#TYPEt">Z.t</a></code></pre><div class="info "> Get the numerator.<br> </div> <pre><span id="VALden"><span class="keyword">val</span> den</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Z.html#TYPEt">Z.t</a></code></pre><div class="info "> Get the denominator.<br> </div> <br> <h1 id="1_Testing">Testing</h1><br> <pre><code><span id="TYPEkind"><span class="keyword">type</span> <code class="type"></code>kind</span> = </code></pre><table class="typetable"> <tr> <td align="left" valign="top" > <code><span class="keyword">|</span></code></td> <td align="left" valign="top" > <code><span id="TYPEELTkind.ZERO"><span class="constructor">ZERO</span></span></code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >0</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr> <tr> <td align="left" valign="top" > <code><span class="keyword">|</span></code></td> <td align="left" valign="top" > <code><span id="TYPEELTkind.INF"><span class="constructor">INF</span></span></code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >infinity, i.e. 1/0</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr> <tr> <td align="left" valign="top" > <code><span class="keyword">|</span></code></td> <td align="left" valign="top" > <code><span id="TYPEELTkind.MINF"><span class="constructor">MINF</span></span></code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >minus infinity, i.e. -1/0</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr> <tr> <td align="left" valign="top" > <code><span class="keyword">|</span></code></td> <td align="left" valign="top" > <code><span id="TYPEELTkind.UNDEF"><span class="constructor">UNDEF</span></span></code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >undefined, i.e., 0/0</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr> <tr> <td align="left" valign="top" > <code><span class="keyword">|</span></code></td> <td align="left" valign="top" > <code><span id="TYPEELTkind.NZERO"><span class="constructor">NZERO</span></span></code></td> <td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >well-defined, non-infinity, non-zero number</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td> </tr></table> <div class="info "> Rationals can be categorized into different kinds, depending mainly on whether the numerator and/or denominator is null.<br> </div> <pre><span id="VALclassify"><span class="keyword">val</span> classify</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEkind">kind</a></code></pre><div class="info "> Determines the kind of a rational.<br> </div> <pre><span id="VALis_real"><span class="keyword">val</span> is_real</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Whether the argument is non-infinity and non-undefined.<br> </div> <pre><span id="VALsign"><span class="keyword">val</span> sign</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int</code></pre><div class="info "> Returns 1 if the argument is positive (including inf), -1 if it is negative (including -inf), and 0 if it is null or undefined.<br> </div> <pre><span id="VALcompare"><span class="keyword">val</span> compare</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> int</code></pre><div class="info "> <code class="code">compare x y</code> compares <code class="code">x</code> to <code class="code">y</code> and returns 1 if <code class="code">x</code> is strictly greater that <code class="code">y</code>, -1 if it is strictly smaller, and 0 if they are equal. This is a total ordering. Infinities are ordered in the natural way, while undefined is considered the smallest of all: undef = undef < -inf <= -inf < x < inf <= inf. This is consistent with OCaml's handling of floating-point infinities and NaN. <p> OCaml's polymorphic comparison will NOT return a result consistent with the ordering of rationals.<br> </div> <pre><span id="VALequal"><span class="keyword">val</span> equal</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Equality testing. This is consistent with <code class="code">compare</code>; in particular, <code class="code">undef</code>=<code class="code">undef</code>.<br> </div> <pre><span id="VALmin"><span class="keyword">val</span> min</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Returns the smallest of its arguments.<br> </div> <pre><span id="VALmax"><span class="keyword">val</span> max</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Returns the largest of its arguments.<br> </div> <pre><span id="VALleq"><span class="keyword">val</span> leq</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Less than or equal.<br> </div> <pre><span id="VALgeq"><span class="keyword">val</span> geq</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Greater than or equal.<br> </div> <pre><span id="VALlt"><span class="keyword">val</span> lt</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Less than (not equal).<br> </div> <pre><span id="VALgt"><span class="keyword">val</span> gt</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> bool</code></pre><div class="info "> Greater than (not equal).<br> </div> <br> <h1 id="1_Conversions">Conversions</h1><br> <pre><span id="VALto_bigint"><span class="keyword">val</span> to_bigint</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Z.html#TYPEt">Z.t</a></code></pre> <pre><span id="VALto_int"><span class="keyword">val</span> to_int</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int</code></pre> <pre><span id="VALto_int32"><span class="keyword">val</span> to_int32</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int32</code></pre> <pre><span id="VALto_int64"><span class="keyword">val</span> to_int64</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int64</code></pre> <pre><span id="VALto_nativeint"><span class="keyword">val</span> to_nativeint</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> nativeint</code></pre><div class="info "> Convert to integer by truncation. Raises a <code class="code">Divide_by_zero</code> if the argument is an infinity or undefined. Raises a <code class="code">Z.Overflow</code> if the result does not fit in the destination type.<br> </div> <pre><span id="VALto_string"><span class="keyword">val</span> to_string</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> string</code></pre><div class="info "> Converts to human-readable, decimal, <code class="code">/</code>-separated rational.<br> </div> <br> <h1 id="1_Arithmeticoperations">Arithmetic operations</h1><br> <br> In all operations, the result is <code class="code">undef</code> if one argument is <code class="code">undef</code>. Other operations can return <code class="code">undef</code>: such as <code class="code">inf</code>-<code class="code">inf</code>, <code class="code">inf</code>*0, 0/0.<br> <pre><span id="VALneg"><span class="keyword">val</span> neg</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Negation.<br> </div> <pre><span id="VALabs"><span class="keyword">val</span> abs</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Absolute value.<br> </div> <pre><span id="VALadd"><span class="keyword">val</span> add</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Addition.<br> </div> <pre><span id="VALsub"><span class="keyword">val</span> sub</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Subtraction. We have <code class="code">sub x y</code> = <code class="code">add x (neg y)</code>.<br> </div> <pre><span id="VALmul"><span class="keyword">val</span> mul</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Multiplication.<br> </div> <pre><span id="VALinv"><span class="keyword">val</span> inv</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Inverse. Note that <code class="code">inv 0</code> is defined, and equals <code class="code">inf</code>.<br> </div> <pre><span id="VALdiv"><span class="keyword">val</span> div</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Division. We have <code class="code">div x y</code> = <code class="code">mul x (inv y)</code>, and <code class="code">inv x</code> = <code class="code">div one x</code>.<br> </div> <pre><span id="VALmul_2exp"><span class="keyword">val</span> mul_2exp</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">mul_2exp x n</code> multiplies <code class="code">x</code> by 2 to the power of <code class="code">n</code>.<br> </div> <pre><span id="VALdiv_2exp"><span class="keyword">val</span> div_2exp</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> <code class="code">div_2exp x n</code> divides <code class="code">x</code> by 2 to the power of <code class="code">n</code>.<br> </div> <br> <h1 id="1_Printing">Printing</h1><br> <pre><span id="VALprint"><span class="keyword">val</span> print</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> unit</code></pre><div class="info "> Prints the argument on the standard output.<br> </div> <pre><span id="VALoutput"><span class="keyword">val</span> output</span> : <code class="type">Pervasives.out_channel -> <a href="Q.html#TYPEt">t</a> -> unit</code></pre><div class="info "> Prints the argument on the specified channel. Also intended to be used as <code class="code">%a</code> format printer in <code class="code">Printf.printf</code>.<br> </div> <pre><span id="VALsprint"><span class="keyword">val</span> sprint</span> : <code class="type">unit -> <a href="Q.html#TYPEt">t</a> -> string</code></pre><div class="info "> To be used as <code class="code">%a</code> format printer in <code class="code">Printf.sprintf</code>.<br> </div> <pre><span id="VALbprint"><span class="keyword">val</span> bprint</span> : <code class="type">Buffer.t -> <a href="Q.html#TYPEt">t</a> -> unit</code></pre><div class="info "> To be used as <code class="code">%a</code> format printer in <code class="code">Printf.bprintf</code>.<br> </div> <pre><span id="VALpp_print"><span class="keyword">val</span> pp_print</span> : <code class="type">Format.formatter -> <a href="Q.html#TYPEt">t</a> -> unit</code></pre><div class="info "> Prints the argument on the specified formatter. Also intended to be used as <code class="code">%a</code> format printer in <code class="code">Format.printf</code>.<br> </div> <br> <h1 id="1_Prefixandinfixoperators">Prefix and infix operators</h1><br> <br> Classic prefix and infix <code class="code">int</code> operators are redefined on <code class="code">t</code>.<br> <pre><span id="VAL(~-)"><span class="keyword">val</span> (~-)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Negation <code class="code">neg</code>.<br> </div> <pre><span id="VAL(~+)"><span class="keyword">val</span> (~+)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Identity.<br> </div> <pre><span id="VAL(+)"><span class="keyword">val</span> (+)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Addition <code class="code">add</code>.<br> </div> <pre><span id="VAL(-)"><span class="keyword">val</span> (-)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Subtraction <code class="code">sub</code>.<br> </div> <pre><span id="VAL( * )"><span class="keyword">val</span> ( * )</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Multiplication <code class="code">mul</code>.<br> </div> <pre><span id="VAL(/)"><span class="keyword">val</span> (/)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Division <code class="code">div</code>.<br> </div> <pre><span id="VAL(lsl)"><span class="keyword">val</span> (lsl)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Multiplication by a power of two <code class="code">mul_2exp</code>.<br> </div> <pre><span id="VAL(asr)"><span class="keyword">val</span> (asr)</span> : <code class="type"><a href="Q.html#TYPEt">t</a> -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Division by a power of two <code class="code">shift_right</code>.<br> </div> <pre><span id="VAL(~$)"><span class="keyword">val</span> (~$)</span> : <code class="type">int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Conversion from <code class="code">int</code>.<br> </div> <pre><span id="VAL(//)"><span class="keyword">val</span> (//)</span> : <code class="type">int -> int -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Creates a rational from two <code class="code">int</code>s.<br> </div> <pre><span id="VAL(~$$)"><span class="keyword">val</span> (~$$)</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Conversion from <code class="code">Z.t</code>.<br> </div> <pre><span id="VAL(///)"><span class="keyword">val</span> (///)</span> : <code class="type"><a href="Z.html#TYPEt">Z.t</a> -> <a href="Z.html#TYPEt">Z.t</a> -> <a href="Q.html#TYPEt">t</a></code></pre><div class="info "> Creates a rational from two <code class="code">Z.t</code>.<br> </div> </body></html>