<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman, Inc. This material may
be distributed only subject to the terms and conditions set forth in
the Open Publication License, v1.0 or later (the latest version is
presently available at http://www.opencontent.org/openpub/).
<p/>
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
<p/>
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.
</copyright>
<class name="Numeric" super="Object" type="class" ref="xref-000001">
<p/>
  <subclasses>Float, Integer</subclasses>
<p/>
  Numeric is the fundamental base type for the concrete number classes 
  <classname>Float</classname>, <classname>Fixnum</classname>, and <classname>Bignum</classname>.
  <mixins>
    <mixin name="Comparable">
   &lt;, &lt;=, ==, &gt;=, &gt;, between?</mixin>
  </mixins>
<p/>
  <methods type="instance">
        <method name="+@" ref="_pl_at">
      <callseq>
        +<obj>num</obj> <returns><obj>num</obj></returns>
      </callseq>
      <desc>
<p/>
      Unary Plus---Returns the receiver's value.      
<p/>
      </desc>
    </method>
<p/>
        <method name="--@" ref="_mi_mi_at">
      <callseq>
        --<obj>num</obj> <returns><obj>aNumeric</obj></returns>
      </callseq>
      <desc>
<p/>
      Unary Minus---Returns the receiver's value, negated.
<p/>
      </desc>
    </method>
<p/>
        <method name="abs" ref="abs">
      <callseq>
        <obj>num</obj>.abs <returns><obj>aNumeric</obj></returns>
      </callseq>
      <desc>
<p/>
      Returns the absolute value of <obj>num</obj>.
<p/>
<codefragment>
<fullcode><![CDATA[         12.abs
         (-34.56).abs
         -34.56.abs  
]]></fullcode><rubycode>
<tr>
  <td><tt>12.abs</tt></td>
  <td>&#187;</td>
  <td><tt>12</tt></td>
</tr>
<tr>
  <td><tt>(-34.56).abs</tt></td>
  <td>&#187;</td>
  <td><tt>34.56</tt></td>
</tr>
<tr>
  <td><tt>-34.56.abs</tt></td>
  <td>&#187;</td>
  <td><tt>34.56</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="coerce" ref="coerce">
      <callseq>
        <obj>num</obj>.coerce( <obj>aNumeric</obj> )
        <returns><obj>anArray</obj></returns>
      </callseq>
      <desc>
<p/>
      If <obj>aNumeric</obj> is the same type as <obj>num</obj>, returns an array
      containing <obj>aNumeric</obj> and <obj>num</obj>. Otherwise, returns an
      array with both <obj>aNumeric</obj> and <obj>num</obj> represented as
      <classname>Float</classname> objects.
<p/>
<codefragment>
<fullcode><![CDATA[         1.coerce(2.5)
         1.2.coerce(3)
         1.coerce(2)
]]></fullcode><rubycode>
<tr>
  <td><tt>1.coerce(2.5)</tt></td>
  <td>&#187;</td>
  <td><tt>[2.5,<nbsp/>1.0]</tt></td>
</tr>
<tr>
  <td><tt>1.2.coerce(3)</tt></td>
  <td>&#187;</td>
  <td><tt>[3.0,<nbsp/>1.2]</tt></td>
</tr>
<tr>
  <td><tt>1.coerce(2)</tt></td>
  <td>&#187;</td>
  <td><tt>[2,<nbsp/>1]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
    <figure type="table">
        <caption>Difference between modulo and
          remainder. The modulo operator
          (``%'') always has the sign of the divisor, whereas
          <meth>remainder</meth> has the sign of the dividend.</caption> 
        <center>
<p/>
          <table>
<th>
  <td><b>a</b></td>
  <td><b>b</b></td>
<td colspan="2"><b>a.divmod(b)</b></td>
<td colspan="1"><b>a / b</b></td>
<td colspan="1"><b>a.modulo(b)</b></td>
<td colspan="1"><b>a.remainder(b)</b></td>
</th>
<tr>
  <td>13</td>
  <td>4</td>
  <td>3,</td>
  <td>1</td>
  <td>3</td>
  <td>1</td>
  <td>1</td>
</tr>
<tr>
  <td>13</td>
  <td>-4</td>
  <td>-4,</td>
  <td>-3</td>
  <td>-4</td>
  <td>-3</td>
  <td>1</td>
</tr>
<tr>
  <td>-13</td>
  <td>4</td>
  <td>-4,</td>
  <td>3</td>
  <td>-4</td>
  <td>3</td>
  <td>-1</td>
</tr>
<tr>
  <td>-13</td>
  <td>-4</td>
  <td>3,</td>
  <td>-1</td>
  <td>3</td>
  <td>-1</td>
  <td>-1</td>
</tr>
<tr>
  <td>11.5</td>
  <td>4</td>
  <td>2.0,</td>
  <td>3.5</td>
  <td>2.875</td>
  <td>3.5</td>
  <td>3.5</td>
</tr>
<tr>
  <td>11.5</td>
  <td>-4</td>
  <td>-3.0,</td>
  <td>-0.5</td>
  <td>-2.875</td>
  <td>-0.5</td>
  <td>3.5</td>
</tr>
<tr>
  <td>-11.5</td>
  <td>4</td>
  <td>-3.0,</td>
  <td>0.5</td>
  <td>-2.875</td>
  <td>0.5</td>
  <td>-3.5</td>
</tr>
<tr>
  <td>-11.5</td>
  <td>-4</td>
  <td>2.0,</td>
  <td>-3.5</td>
  <td>2.875</td>
  <td>-3.5</td>
  <td>-3.5</td>
</tr>
<bottomrule/></table>
<p/>
        </center>
    </figure>
<p/>
        <method name="divmod" ref="divmod">
      <callseq>
        <obj>num</obj>.divmod( <obj>aNumeric</obj> )
        <returns><obj>anArray</obj></returns>
      </callseq>
      <desc>
<p/>
      Returns an array containing the quotient and modulus obtained by
      dividing <obj>num</obj> by <obj>aNumeric</obj>.
      If <tt>q,<nbsp/>r<nbsp/>=<nbsp/>x.divmod(y)</tt>,
      <table>
<tr>
  <td>q</td>
  <td>=</td>
  <td>floor(float(x) /  float(y))</td>
</tr>
<tr>
  <td>x</td>
  <td>=</td>
  <td>q * y + r</td>
</tr>
</table>
<p/>
      The quotient is rounded toward -infinity. See Table
      22.6 on page 350.
<p/>
<codefragment>
<fullcode><![CDATA[         11.divmod(3)
         11.divmod(-3)
         11.divmod(3.5)
         (-11).divmod(3.5)
         (11.5).divmod(3.5)
]]></fullcode><rubycode>
<tr>
  <td><tt>11.divmod(3)</tt></td>
  <td>&#187;</td>
  <td><tt>[3,<nbsp/>2]</tt></td>
</tr>
<tr>
  <td><tt>11.divmod(-3)</tt></td>
  <td>&#187;</td>
  <td><tt>[-4,<nbsp/>-1]</tt></td>
</tr>
<tr>
  <td><tt>11.divmod(3.5)</tt></td>
  <td>&#187;</td>
  <td><tt>[3.0,<nbsp/>0.5]</tt></td>
</tr>
<tr>
  <td><tt>(-11).divmod(3.5)</tt></td>
  <td>&#187;</td>
  <td><tt>[-4.0,<nbsp/>3.0]</tt></td>
</tr>
<tr>
  <td><tt>(11.5).divmod(3.5)</tt></td>
  <td>&#187;</td>
  <td><tt>[3.0,<nbsp/>1.0]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="eql?" ref="eql_qm">
      <callseq>
        <obj>num</obj>.eql?( <obj>aNumeric</obj> )
        <returns><const>true</const> or <const>false</const></returns>
      </callseq>
      <desc>
<p/>
      Returns <const>true</const> if <obj>num</obj> and <obj>aNumeric</obj> are the same
      type and have equal values.
<p/>
<codefragment>
<fullcode><![CDATA[         1 == 1.0
         1.eql?(1.0)
         (1.0).eql?(1.0)
]]></fullcode><rubycode>
<tr>
  <td><tt>1<nbsp/>==<nbsp/>1.0</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>1.eql?(1.0)</tt></td>
  <td>&#187;</td>
  <td><tt>false</tt></td>
</tr>
<tr>
  <td><tt>(1.0).eql?(1.0)</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="integer?" ref="integer_qm">
      <callseq>
        <obj>num</obj>.integer? <returns><const>true</const> or <const>false</const></returns>
      </callseq>
      <desc>
<p/>
      Returns <const>true</const> if <obj>num</obj> is an <classname>Integer</classname> (including
      <classname>Fixnum</classname> and <classname>Bignum</classname>).
<p/>
      </desc>
    </method>
<p/>
        <method name="modulo" ref="modulo">
      <callseq>
        <obj>num</obj>.modulo( <obj>aNumeric</obj> )
        <returns><obj>aNumeric</obj></returns>
      </callseq>
      <desc>
<p/>
      Equivalent to <obj>num</obj>.<tt>divmod(</tt><obj>aNumeric</obj><tt>)[1]</tt>.
<p/>
      </desc>
    </method>
<p/>
        <method name="nonzero?" ref="nonzero_qm">
      <callseq>
        <obj>num</obj>.nonzero?
        <returns><obj>num</obj> or <tt>nil</tt></returns>
      </callseq>
      <desc>
<p/>
      Returns <obj>num</obj> if <obj>num</obj> is not zero, <tt>nil</tt> otherwise. This
      behavior is useful when chaining comparisons:
<p/>
<codefragment>
<fullcode><![CDATA[        a = %w( z Bb bB bb BB a aA Aa AA A )
        b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
        b
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>a<nbsp/>=<nbsp/>%w(<nbsp/>z<nbsp/>Bb<nbsp/>bB<nbsp/>bb<nbsp/>BB<nbsp/>a<nbsp/>aA<nbsp/>Aa<nbsp/>AA<nbsp/>A<nbsp/>)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>b<nbsp/>=<nbsp/>a.sort<nbsp/>{|a,b|<nbsp/>(a.downcase<nbsp/>&lt;=&gt;<nbsp/>b.downcase).nonzero?<nbsp/>||<nbsp/>a<nbsp/>&lt;=&gt;<nbsp/>b<nbsp/>}</tt></td>
</tr>
<tr>
  <td><tt>b</tt></td>
  <td>&#187;</td>
  <td><tt>["A",<nbsp/>"a",<nbsp/>"AA",<nbsp/>"Aa",<nbsp/>"aA",<nbsp/>"BB",<nbsp/>"Bb",<nbsp/>"bB",<nbsp/>"bb",<nbsp/>"z"]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="remainder" ref="remainder">
      <callseq>
        <obj>num</obj>.remainder( <obj>aNumeric</obj> )
        <returns><obj>aNumeric</obj></returns>
      </callseq>
      <desc>
<p/>
      If <obj>num</obj> and <obj>aNumeric</obj> have different signs, returns
       <em>mod</em>-<obj>aNumeric</obj>; otherwise, returns
      <em>mod</em>. In both cases 
      <em>mod</em> is the value <obj>num</obj>.<tt>modulo(</tt><obj>aNumeric</obj><tt>)</tt>.  The
      differences between <meth>remainder</meth> and modulo (<tt>%</tt>) are
      shown in Table 22.6 on page 350.
<p/>
      </desc>
    </method>
<p/>
        <method name="zero?" ref="zero_qm">
      <callseq>
        <obj>num</obj>.zero? <returns><const>true</const> or <const>false</const></returns>
      </callseq>
      <desc>
<p/>
      Returns <const>true</const> if <obj>num</obj> has a zero value.
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
</class>
</ppdoc>
