File: C:/Ruby27-x64/share/doc/ruby/html/Numeric.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Numeric - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Object.html">Object</a>
</div>
<div id="includes-section" class="nav-section">
<h3>Included Modules</h3>
<ul class="link-list">
<li><a class="include" href="Comparable.html">Comparable</a>
</ul>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-i-25">#%</a>
<li ><a href="#method-i-2B-40">#+@</a>
<li ><a href="#method-i-2D-40">#-@</a>
<li ><a href="#method-i-3C-3D-3E">#<=></a>
<li ><a href="#method-i-abs">#abs</a>
<li ><a href="#method-i-abs2">#abs2</a>
<li ><a href="#method-i-angle">#angle</a>
<li ><a href="#method-i-arg">#arg</a>
<li ><a href="#method-i-ceil">#ceil</a>
<li ><a href="#method-i-clone">#clone</a>
<li ><a href="#method-i-coerce">#coerce</a>
<li ><a href="#method-i-conj">#conj</a>
<li ><a href="#method-i-conjugate">#conjugate</a>
<li ><a href="#method-i-denominator">#denominator</a>
<li ><a href="#method-i-div">#div</a>
<li ><a href="#method-i-divmod">#divmod</a>
<li ><a href="#method-i-dup">#dup</a>
<li ><a href="#method-i-eql-3F">#eql?</a>
<li ><a href="#method-i-fdiv">#fdiv</a>
<li ><a href="#method-i-finite-3F">#finite?</a>
<li ><a href="#method-i-floor">#floor</a>
<li ><a href="#method-i-i">#i</a>
<li ><a href="#method-i-imag">#imag</a>
<li ><a href="#method-i-imaginary">#imaginary</a>
<li ><a href="#method-i-infinite-3F">#infinite?</a>
<li ><a href="#method-i-integer-3F">#integer?</a>
<li ><a href="#method-i-magnitude">#magnitude</a>
<li ><a href="#method-i-modulo">#modulo</a>
<li ><a href="#method-i-negative-3F">#negative?</a>
<li ><a href="#method-i-nonzero-3F">#nonzero?</a>
<li ><a href="#method-i-numerator">#numerator</a>
<li ><a href="#method-i-phase">#phase</a>
<li ><a href="#method-i-polar">#polar</a>
<li ><a href="#method-i-positive-3F">#positive?</a>
<li ><a href="#method-i-quo">#quo</a>
<li ><a href="#method-i-real">#real</a>
<li ><a href="#method-i-real-3F">#real?</a>
<li ><a href="#method-i-rect">#rect</a>
<li ><a href="#method-i-rectangular">#rectangular</a>
<li ><a href="#method-i-remainder">#remainder</a>
<li ><a href="#method-i-round">#round</a>
<li ><a href="#method-i-step">#step</a>
<li ><a href="#method-i-to_c">#to_c</a>
<li ><a href="#method-i-to_int">#to_int</a>
<li ><a href="#method-i-truncate">#truncate</a>
<li ><a href="#method-i-zero-3F">#zero?</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Numeric">
<h1 id="class-Numeric" class="class">
class Numeric
</h1>
<section class="description">
<p><a href="Numeric.html"><code>Numeric</code></a> is the class from which all higher-level numeric classes should inherit.</p>
<p><a href="Numeric.html"><code>Numeric</code></a> allows instantiation of heap-allocated objects. Other core numeric classes such as <a href="Integer.html"><code>Integer</code></a> are implemented as immediates, which means that each <a href="Integer.html"><code>Integer</code></a> is a single immutable object which is always passed by value.</p>
<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">object_id</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">object_id</span> <span class="ruby-comment">#=> true</span>
</pre>
<p>There can only ever be one instance of the integer <code>1</code>, for example. Ruby ensures this by preventing instantiation. If duplication is attempted, the same instance is returned.</p>
<pre class="ruby"><span class="ruby-constant">Integer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">1</span>) <span class="ruby-comment">#=> NoMethodError: undefined method `new' for Integer:Class</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">dup</span> <span class="ruby-comment">#=> 1</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">object_id</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">object_id</span> <span class="ruby-comment">#=> true</span>
</pre>
<p>For this reason, <a href="Numeric.html"><code>Numeric</code></a> should be used when defining other numeric classes.</p>
<p>Classes which inherit from <a href="Numeric.html"><code>Numeric</code></a> must implement <code>coerce</code>, which returns a two-member <a href="Array.html"><code>Array</code></a> containing an object that has been coerced into an instance of the new class and <code>self</code> (see <a href="Numeric.html#method-i-coerce"><code>coerce</code></a>).</p>
<p>Inheriting classes should also implement arithmetic operator methods (<code>+</code>, <code>-</code>, <code>*</code> and <code>/</code>) and the <code><=></code> operator (see <a href="Comparable.html"><code>Comparable</code></a>). These methods may rely on <code>coerce</code> to ensure interoperability with instances of other numeric classes.</p>
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Tally</span> <span class="ruby-operator"><</span> <span class="ruby-constant">Numeric</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-ivar">@string</span> = <span class="ruby-identifier">string</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_s</span>
<span class="ruby-ivar">@string</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_i</span>
<span class="ruby-ivar">@string</span>.<span class="ruby-identifier">size</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">coerce</span>(<span class="ruby-identifier">other</span>)
[<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'|'</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>), <span class="ruby-keyword">self</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title"><=></span>(<span class="ruby-identifier">other</span>)
<span class="ruby-identifier">to_i</span> <span class="ruby-operator"><=></span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">+</span>(<span class="ruby-identifier">other</span>)
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'|'</span> <span class="ruby-operator">*</span> (<span class="ruby-identifier">to_i</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>))
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">-</span>(<span class="ruby-identifier">other</span>)
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'|'</span> <span class="ruby-operator">*</span> (<span class="ruby-identifier">to_i</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>))
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">*</span>(<span class="ruby-identifier">other</span>)
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'|'</span> <span class="ruby-operator">*</span> (<span class="ruby-identifier">to_i</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>))
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">/</span>(<span class="ruby-identifier">other</span>)
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'|'</span> <span class="ruby-operator">*</span> (<span class="ruby-identifier">to_i</span> <span class="ruby-operator">/</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_i</span>))
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">tally</span> = <span class="ruby-constant">Tally</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'||'</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">tally</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span> <span class="ruby-comment">#=> "||||"</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">tally</span> <span class="ruby-operator">></span> <span class="ruby-value">1</span> <span class="ruby-comment">#=> true</span>
</pre>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-25" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
modulo(numeric) → real
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p><code>x.modulo(y)</code> means <code>x-y*(x/y).floor</code>.</p>
<p>Equivalent to <code>num.divmod(numeric)[1]</code>.</p>
<p>See <a href="Numeric.html#method-i-divmod"><code>Numeric#divmod</code></a>.</p>
<div class="method-source-code" id="25-source">
<pre>static VALUE
num_modulo(VALUE x, VALUE y)
{
VALUE q = num_funcall1(x, id_div, y);
return rb_funcall(x, '-', 1,
rb_funcall(y, '*', 1, q));
}</pre>
</div>
</div>
</div>
<div id="method-i-2B-40" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
+num → num
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Unary Plus—Returns the receiver.</p>
<div class="method-source-code" id="2B-40-source">
<pre>static VALUE
num_uplus(VALUE num)
{
return num;
}</pre>
</div>
</div>
</div>
<div id="method-i-2D-40" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
-num → numeric
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Unary Minus—Returns the receiver, negated.</p>
<div class="method-source-code" id="2D-40-source">
<pre>static VALUE
num_uminus(VALUE num)
{
VALUE zero;
zero = INT2FIX(0);
do_coerce(&zero, &num, TRUE);
return num_funcall1(zero, '-', num);
}</pre>
</div>
</div>
</div>
<div id="method-i-3C-3D-3E" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
number <=> other → 0 or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns zero if <code>number</code> equals <code>other</code>, otherwise returns <code>nil</code>.</p>
<div class="method-source-code" id="3C-3D-3E-source">
<pre>static VALUE
num_cmp(VALUE x, VALUE y)
{
if (x == y) return INT2FIX(0);
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-abs" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
abs → numeric
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the absolute value of <code>num</code>.</p>
<pre class="ruby"><span class="ruby-value">12</span>.<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 12</span>
(<span class="ruby-value">-34.56</span>).<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 34.56</span>
<span class="ruby-value">-34.56</span>.<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 34.56</span>
</pre>
<p><a href="Numeric.html#method-i-magnitude"><code>Numeric#magnitude</code></a> is an alias for <a href="Numeric.html#method-i-abs"><code>Numeric#abs</code></a>.</p>
<div class="method-source-code" id="abs-source">
<pre>static VALUE
num_abs(VALUE num)
{
if (rb_num_negative_int_p(num)) {
return num_funcall0(num, idUMinus);
}
return num;
}</pre>
</div>
</div>
</div>
<div id="method-i-abs2" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
abs2 → real
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns square of self.</p>
<div class="method-source-code" id="abs2-source">
<pre>static VALUE
numeric_abs2(VALUE self)
{
return f_mul(self, self);
}</pre>
</div>
</div>
</div>
<div id="method-i-angle" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
angle → 0 or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns 0 if the value is positive, pi otherwise.</p>
<div class="method-source-code" id="angle-source">
<pre>static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
}</pre>
</div>
</div>
</div>
<div id="method-i-arg" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
arg → 0 or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns 0 if the value is positive, pi otherwise.</p>
<div class="method-source-code" id="arg-source">
<pre>static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
}</pre>
</div>
</div>
</div>
<div id="method-i-ceil" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
ceil([ndigits]) → integer or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the smallest number greater than or equal to <code>num</code> with a precision of <code>ndigits</code> decimal digits (default: 0).</p>
<p><a href="Numeric.html"><code>Numeric</code></a> implements this by converting its value to a <a href="Float.html"><code>Float</code></a> and invoking <a href="Float.html#method-i-ceil"><code>Float#ceil</code></a>.</p>
<div class="method-source-code" id="ceil-source">
<pre>static VALUE
num_ceil(int argc, VALUE *argv, VALUE num)
{
return flo_ceil(argc, argv, rb_Float(num));
}</pre>
</div>
</div>
</div>
<div id="method-i-clone" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
clone(freeze: true) → num
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the receiver. <code>freeze</code> cannot be <code>false</code>.</p>
<div class="method-source-code" id="clone-source">
<pre>static VALUE
num_clone(int argc, VALUE *argv, VALUE x)
{
return rb_immutable_obj_clone(argc, argv, x);
}</pre>
</div>
</div>
</div>
<div id="method-i-coerce" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
coerce(numeric) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>If <code>numeric</code> is the same type as <code>num</code>, returns an array <code>[numeric, num]</code>. Otherwise, returns an array with both <code>numeric</code> and <code>num</code> represented as <a href="Float.html"><code>Float</code></a> objects.</p>
<p>This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.</p>
<pre class="ruby"><span class="ruby-value">1</span>.<span class="ruby-identifier">coerce</span>(<span class="ruby-value">2.5</span>) <span class="ruby-comment">#=> [2.5, 1.0]</span>
<span class="ruby-value">1.2</span>.<span class="ruby-identifier">coerce</span>(<span class="ruby-value">3</span>) <span class="ruby-comment">#=> [3.0, 1.2]</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">coerce</span>(<span class="ruby-value">2</span>) <span class="ruby-comment">#=> [2, 1]</span>
</pre>
<div class="method-source-code" id="coerce-source">
<pre>static VALUE
num_coerce(VALUE x, VALUE y)
{
if (CLASS_OF(x) == CLASS_OF(y))
return rb_assoc_new(y, x);
x = rb_Float(x);
y = rb_Float(y);
return rb_assoc_new(y, x);
}</pre>
</div>
</div>
</div>
<div id="method-i-conj" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
conj → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
conjugate → self
</span>
</div>
<div class="method-description">
<p>Returns self.</p>
<div class="method-source-code" id="conj-source">
<pre>static VALUE
numeric_conj(VALUE self)
{
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-conjugate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
conjugate → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns self.</p>
<div class="method-source-code" id="conjugate-source">
<pre>static VALUE
numeric_conj(VALUE self)
{
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-denominator" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
denominator → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the denominator (always positive).</p>
<div class="method-source-code" id="denominator-source">
<pre>static VALUE
numeric_denominator(VALUE self)
{
return f_denominator(f_to_r(self));
}</pre>
</div>
</div>
</div>
<div id="method-i-div" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
div(numeric) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Uses <code>/</code> to perform division, then converts the result to an integer. <a href="Numeric.html"><code>Numeric</code></a> does not define the <code>/</code> operator; this is left to subclasses.</p>
<p>Equivalent to <code>num.divmod(numeric)[0]</code>.</p>
<p>See <a href="Numeric.html#method-i-divmod"><code>Numeric#divmod</code></a>.</p>
<div class="method-source-code" id="div-source">
<pre>static VALUE
num_div(VALUE x, VALUE y)
{
if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
return rb_funcall(num_funcall1(x, '/', y), rb_intern("floor"), 0);
}</pre>
</div>
</div>
</div>
<div id="method-i-divmod" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
divmod(numeric) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array containing the quotient and modulus obtained by dividing <code>num</code> by <code>numeric</code>.</p>
<p>If <code>q, r = x.divmod(y)</code>, then</p>
<pre class="ruby"><span class="ruby-identifier">q</span> = <span class="ruby-identifier">floor</span>(<span class="ruby-identifier">x</span><span class="ruby-operator">/</span><span class="ruby-identifier">y</span>)
<span class="ruby-identifier">x</span> = <span class="ruby-identifier">q</span><span class="ruby-operator">*</span><span class="ruby-identifier">y</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">r</span>
</pre>
<p>The quotient is rounded toward negative infinity, as shown in the following table:</p>
<pre> a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
------+-----+---------------+---------+-------------+---------------
13 | 4 | 3, 1 | 3 | 1 | 1
------+-----+---------------+---------+-------------+---------------
13 | -4 | -4, -3 | -4 | -3 | 1
------+-----+---------------+---------+-------------+---------------
-13 | 4 | -4, 3 | -4 | 3 | -1
------+-----+---------------+---------+-------------+---------------
-13 | -4 | 3, -1 | 3 | -1 | -1
------+-----+---------------+---------+-------------+---------------
11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
------+-----+---------------+---------+-------------+---------------
11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5</pre>
<p>Examples</p>
<pre class="ruby"><span class="ruby-value">11</span>.<span class="ruby-identifier">divmod</span>(<span class="ruby-value">3</span>) <span class="ruby-comment">#=> [3, 2]</span>
<span class="ruby-value">11</span>.<span class="ruby-identifier">divmod</span>(<span class="ruby-value">-3</span>) <span class="ruby-comment">#=> [-4, -1]</span>
<span class="ruby-value">11</span>.<span class="ruby-identifier">divmod</span>(<span class="ruby-value">3.5</span>) <span class="ruby-comment">#=> [3, 0.5]</span>
(<span class="ruby-value">-11</span>).<span class="ruby-identifier">divmod</span>(<span class="ruby-value">3.5</span>) <span class="ruby-comment">#=> [-4, 3.0]</span>
<span class="ruby-value">11.5</span>.<span class="ruby-identifier">divmod</span>(<span class="ruby-value">3.5</span>) <span class="ruby-comment">#=> [3, 1.0]</span>
</pre>
<div class="method-source-code" id="divmod-source">
<pre>static VALUE
num_divmod(VALUE x, VALUE y)
{
return rb_assoc_new(num_div(x, y), num_modulo(x, y));
}</pre>
</div>
</div>
</div>
<div id="method-i-dup" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
dup → num
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the receiver.</p>
<div class="method-source-code" id="dup-source">
<pre>static VALUE
num_dup(VALUE x)
{
return x;
}</pre>
</div>
</div>
</div>
<div id="method-i-eql-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
eql?(numeric) → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> and <code>numeric</code> are the same type and have equal values. Contrast this with Numeric#==, which performs type conversions.</p>
<pre class="ruby"><span class="ruby-value">1</span> <span class="ruby-operator">==</span> <span class="ruby-value">1.0</span> <span class="ruby-comment">#=> true</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">eql?</span>(<span class="ruby-value">1.0</span>) <span class="ruby-comment">#=> false</span>
<span class="ruby-value">1.0</span>.<span class="ruby-identifier">eql?</span>(<span class="ruby-value">1.0</span>) <span class="ruby-comment">#=> true</span>
</pre>
<div class="method-source-code" id="eql-3F-source">
<pre>static VALUE
num_eql(VALUE x, VALUE y)
{
if (TYPE(x) != TYPE(y)) return Qfalse;
if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_eql(x, y);
}
return rb_equal(x, y);
}</pre>
</div>
</div>
</div>
<div id="method-i-fdiv" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fdiv(numeric) → float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns float division.</p>
<div class="method-source-code" id="fdiv-source">
<pre>static VALUE
num_fdiv(VALUE x, VALUE y)
{
return rb_funcall(rb_Float(x), '/', 1, y);
}</pre>
</div>
</div>
</div>
<div id="method-i-finite-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
finite? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> is a finite number, otherwise returns <code>false</code>.</p>
<div class="method-source-code" id="finite-3F-source">
<pre>static VALUE
num_finite_p(VALUE num)
{
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-floor" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
floor([ndigits]) → integer or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the largest number less than or equal to <code>num</code> with a precision of <code>ndigits</code> decimal digits (default: 0).</p>
<p><a href="Numeric.html"><code>Numeric</code></a> implements this by converting its value to a <a href="Float.html"><code>Float</code></a> and invoking <a href="Float.html#method-i-floor"><code>Float#floor</code></a>.</p>
<div class="method-source-code" id="floor-source">
<pre>static VALUE
num_floor(int argc, VALUE *argv, VALUE num)
{
return flo_floor(argc, argv, rb_Float(num));
}</pre>
</div>
</div>
</div>
<div id="method-i-i" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
i → Complex(0, num)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the corresponding imaginary number. Not available for complex numbers.</p>
<pre class="ruby"><span class="ruby-value">-42</span>.<span class="ruby-identifier">i</span> <span class="ruby-comment">#=> (0-42i)</span>
<span class="ruby-value">2.0</span>.<span class="ruby-identifier">i</span> <span class="ruby-comment">#=> (0+2.0i)</span>
</pre>
<div class="method-source-code" id="i-source">
<pre>static VALUE
num_imaginary(VALUE num)
{
return rb_complex_new(INT2FIX(0), num);
}</pre>
</div>
</div>
</div>
<div id="method-i-imag" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
imag → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
imaginary → 0
</span>
</div>
<div class="method-description">
<p>Returns zero.</p>
<div class="method-source-code" id="imag-source">
<pre>static VALUE
numeric_imag(VALUE self)
{
return INT2FIX(0);
}</pre>
</div>
</div>
</div>
<div id="method-i-imaginary" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
imaginary → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns zero.</p>
<div class="method-source-code" id="imaginary-source">
<pre>static VALUE
numeric_imag(VALUE self)
{
return INT2FIX(0);
}</pre>
</div>
</div>
</div>
<div id="method-i-infinite-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
infinite? → -1, 1, or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>nil</code>, -1, or 1 depending on whether the value is finite, <code>-Infinity</code>, or <code>+Infinity</code>.</p>
<div class="method-source-code" id="infinite-3F-source">
<pre>static VALUE
num_infinite_p(VALUE num)
{
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-integer-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
integer? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> is an <a href="Integer.html"><code>Integer</code></a>.</p>
<pre class="ruby"><span class="ruby-value">1.0</span>.<span class="ruby-identifier">integer?</span> <span class="ruby-comment">#=> false</span>
<span class="ruby-value">1</span>.<span class="ruby-identifier">integer?</span> <span class="ruby-comment">#=> true</span>
</pre>
<div class="method-source-code" id="integer-3F-source">
<pre>static VALUE
num_int_p(VALUE num)
{
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-magnitude" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
magnitude → numeric
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the absolute value of <code>num</code>.</p>
<pre class="ruby"><span class="ruby-value">12</span>.<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 12</span>
(<span class="ruby-value">-34.56</span>).<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 34.56</span>
<span class="ruby-value">-34.56</span>.<span class="ruby-identifier">abs</span> <span class="ruby-comment">#=> 34.56</span>
</pre>
<p><a href="Numeric.html#method-i-magnitude"><code>Numeric#magnitude</code></a> is an alias for <a href="Numeric.html#method-i-abs"><code>Numeric#abs</code></a>.</p>
<div class="method-source-code" id="magnitude-source">
<pre>static VALUE
num_abs(VALUE num)
{
if (rb_num_negative_int_p(num)) {
return num_funcall0(num, idUMinus);
}
return num;
}</pre>
</div>
</div>
</div>
<div id="method-i-modulo" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
modulo(numeric) → real
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p><code>x.modulo(y)</code> means <code>x-y*(x/y).floor</code>.</p>
<p>Equivalent to <code>num.divmod(numeric)[1]</code>.</p>
<p>See <a href="Numeric.html#method-i-divmod"><code>Numeric#divmod</code></a>.</p>
<div class="method-source-code" id="modulo-source">
<pre>static VALUE
num_modulo(VALUE x, VALUE y)
{
VALUE q = num_funcall1(x, id_div, y);
return rb_funcall(x, '-', 1,
rb_funcall(y, '*', 1, q));
}</pre>
</div>
</div>
</div>
<div id="method-i-negative-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
negative? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> is less than 0.</p>
<div class="method-source-code" id="negative-3F-source">
<pre>static VALUE
num_negative_p(VALUE num)
{
return rb_num_negative_int_p(num) ? Qtrue : Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-nonzero-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
nonzero? → self or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>self</code> if <code>num</code> is not zero, <code>nil</code> otherwise.</p>
<p>This behavior is useful when chaining comparisons:</p>
<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-node">%w( z Bb bB bb BB a aA Aa AA A )</span>
<span class="ruby-identifier">b</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">a</span>.<span class="ruby-identifier">downcase</span> <span class="ruby-operator"><=></span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">downcase</span>).<span class="ruby-identifier">nonzero?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">a</span> <span class="ruby-operator"><=></span> <span class="ruby-identifier">b</span> }
<span class="ruby-identifier">b</span> <span class="ruby-comment">#=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]</span>
</pre>
<div class="method-source-code" id="nonzero-3F-source">
<pre>static VALUE
num_nonzero_p(VALUE num)
{
if (RTEST(num_funcall0(num, rb_intern("zero?")))) {
return Qnil;
}
return num;
}</pre>
</div>
</div>
</div>
<div id="method-i-numerator" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
numerator → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the numerator.</p>
<div class="method-source-code" id="numerator-source">
<pre>static VALUE
numeric_numerator(VALUE self)
{
return f_numerator(f_to_r(self));
}</pre>
</div>
</div>
</div>
<div id="method-i-phase" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
phase → 0 or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns 0 if the value is positive, pi otherwise.</p>
<div class="method-source-code" id="phase-source">
<pre>static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
}</pre>
</div>
</div>
</div>
<div id="method-i-polar" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
polar → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array; [num.abs, num.arg].</p>
<div class="method-source-code" id="polar-source">
<pre>static VALUE
numeric_polar(VALUE self)
{
VALUE abs, arg;
if (RB_INTEGER_TYPE_P(self)) {
abs = rb_int_abs(self);
arg = numeric_arg(self);
}
else if (RB_FLOAT_TYPE_P(self)) {
abs = rb_float_abs(self);
arg = float_arg(self);
}
else if (RB_TYPE_P(self, T_RATIONAL)) {
abs = rb_rational_abs(self);
arg = numeric_arg(self);
}
else {
abs = f_abs(self);
arg = f_arg(self);
}
return rb_assoc_new(abs, arg);
}</pre>
</div>
</div>
</div>
<div id="method-i-positive-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
positive? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> is greater than 0.</p>
<div class="method-source-code" id="positive-3F-source">
<pre>static VALUE
num_positive_p(VALUE num)
{
const ID mid = '>';
if (FIXNUM_P(num)) {
if (method_basic_p(rb_cInteger))
return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0) ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(num, T_BIGNUM)) {
if (method_basic_p(rb_cInteger))
return BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num) ? Qtrue : Qfalse;
}
return rb_num_compare_with_zero(num, mid);
}</pre>
</div>
</div>
</div>
<div id="method-i-quo" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
quo(int_or_rat) → rat
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
quo(flo) → flo
</span>
</div>
<div class="method-description">
<p>Returns the most exact division (rational for integers, float for floats).</p>
<div class="method-source-code" id="quo-source">
<pre>VALUE
rb_numeric_quo(VALUE x, VALUE y)
{
if (RB_TYPE_P(x, T_COMPLEX)) {
return rb_complex_div(x, y);
}
if (RB_FLOAT_TYPE_P(y)) {
return rb_funcallv(x, idFdiv, 1, &y);
}
if (canonicalization) {
x = rb_rational_raw1(x);
}
else {
x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r");
}
return rb_rational_div(x, y);
}</pre>
</div>
</div>
</div>
<div id="method-i-real" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
real → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns self.</p>
<div class="method-source-code" id="real-source">
<pre>static VALUE
numeric_real(VALUE self)
{
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-real-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
real? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> is a real number (i.e. not <a href="Complex.html"><code>Complex</code></a>).</p>
<div class="method-source-code" id="real-3F-source">
<pre>static VALUE
num_real_p(VALUE num)
{
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-rect" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
rect → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
rectangular → array
</span>
</div>
<div class="method-description">
<p>Returns an array; [num, 0].</p>
<div class="method-source-code" id="rect-source">
<pre>static VALUE
numeric_rect(VALUE self)
{
return rb_assoc_new(self, INT2FIX(0));
}</pre>
</div>
</div>
</div>
<div id="method-i-rectangular" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
rectangular → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array; [num, 0].</p>
<div class="method-source-code" id="rectangular-source">
<pre>static VALUE
numeric_rect(VALUE self)
{
return rb_assoc_new(self, INT2FIX(0));
}</pre>
</div>
</div>
</div>
<div id="method-i-remainder" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
remainder(numeric) → real
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p><code>x.remainder(y)</code> means <code>x-y*(x/y).truncate</code>.</p>
<p>See <a href="Numeric.html#method-i-divmod"><code>Numeric#divmod</code></a>.</p>
<div class="method-source-code" id="remainder-source">
<pre>static VALUE
num_remainder(VALUE x, VALUE y)
{
VALUE z = num_funcall1(x, '%', y);
if ((!rb_equal(z, INT2FIX(0))) &&
((rb_num_negative_int_p(x) &&
rb_num_positive_int_p(y)) ||
(rb_num_positive_int_p(x) &&
rb_num_negative_int_p(y)))) {
return rb_funcall(z, '-', 1, y);
}
return z;
}</pre>
</div>
</div>
</div>
<div id="method-i-round" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
round([ndigits]) → integer or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>num</code> rounded to the nearest value with a precision of <code>ndigits</code> decimal digits (default: 0).</p>
<p><a href="Numeric.html"><code>Numeric</code></a> implements this by converting its value to a <a href="Float.html"><code>Float</code></a> and invoking <a href="Float.html#method-i-round"><code>Float#round</code></a>.</p>
<div class="method-source-code" id="round-source">
<pre>static VALUE
num_round(int argc, VALUE* argv, VALUE num)
{
return flo_round(argc, argv, rb_Float(num));
}</pre>
</div>
</div>
</div>
<div id="method-i-step" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
step(by: step, to: limit) {|i| block } → self
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
step(by: step, to: limit) → an_enumerator
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
step(by: step, to: limit) → an_arithmetic_sequence
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
step(limit=nil, step=1) {|i| block } → self
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
step(limit=nil, step=1) → an_enumerator
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
step(limit=nil, step=1) → an_arithmetic_sequence
</span>
</div>
<div class="method-description">
<p>Invokes the given block with the sequence of numbers starting at <code>num</code>, incremented by <code>step</code> (defaulted to <code>1</code>) on each call.</p>
<p>The loop finishes when the value to be passed to the block is greater than <code>limit</code> (if <code>step</code> is positive) or less than <code>limit</code> (if <code>step</code> is negative), where <code>limit</code> is defaulted to infinity.</p>
<p>In the recommended keyword argument style, either or both of <code>step</code> and <code>limit</code> (default infinity) can be omitted. In the fixed position argument style, zero as a step (i.e. <code>num.step(limit, 0)</code>) is not allowed for historical compatibility reasons.</p>
<p>If all the arguments are integers, the loop operates using an integer counter.</p>
<p>If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed <em>floor(n + n*Float::EPSILON) + 1</em> times, where <em>n = (limit - num)/step</em>.</p>
<p>Otherwise, the loop starts at <code>num</code>, uses either the less-than (<code><</code>) or greater-than (<code>></code>) operator to compare the counter against <code>limit</code>, and increments itself using the <code>+</code> operator.</p>
<p>If no block is given, an <a href="Enumerator.html"><code>Enumerator</code></a> is returned instead. Especially, the enumerator is an <a href="Enumerator/ArithmeticSequence.html"><code>Enumerator::ArithmeticSequence</code></a> if both <code>limit</code> and <code>step</code> are kind of <a href="Numeric.html"><code>Numeric</code></a> or <code>nil</code>.</p>
<p>For example:</p>
<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">step</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">4</span>)
<span class="ruby-identifier">p</span> <span class="ruby-value">10</span>.<span class="ruby-identifier">step</span>(<span class="ruby-value">by:</span> <span class="ruby-value">-1</span>).<span class="ruby-identifier">take</span>(<span class="ruby-value">4</span>)
<span class="ruby-value">3</span>.<span class="ruby-identifier">step</span>(<span class="ruby-value">to:</span> <span class="ruby-value">5</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">print</span> <span class="ruby-identifier">i</span>, <span class="ruby-string">" "</span> }
<span class="ruby-value">1</span>.<span class="ruby-identifier">step</span>(<span class="ruby-value">10</span>, <span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">print</span> <span class="ruby-identifier">i</span>, <span class="ruby-string">" "</span> }
<span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">E</span>.<span class="ruby-identifier">step</span>(<span class="ruby-value">to:</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>, <span class="ruby-value">by:</span> <span class="ruby-value">0.2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">print</span> <span class="ruby-identifier">f</span>, <span class="ruby-string">" "</span> }
</pre>
<p>Will produce:</p>
<pre>[1, 2, 3, 4]
[10, 9, 8, 7]
3 4 5
1 3 5 7 9
2.718281828459045 2.9182818284590453 3.118281828459045</pre>
<div class="method-source-code" id="step-source">
<pre>static VALUE
num_step(int argc, VALUE *argv, VALUE from)
{
VALUE to, step;
int desc, inf;
if (!rb_block_given_p()) {
VALUE by = Qundef;
num_step_extract_args(argc, argv, &to, &step, &by);
if (by != Qundef) {
step = by;
}
if (NIL_P(step)) {
step = INT2FIX(1);
}
if ((NIL_P(to) || rb_obj_is_kind_of(to, rb_cNumeric)) &&
rb_obj_is_kind_of(step, rb_cNumeric)) {
return rb_arith_seq_new(from, ID2SYM(rb_frame_this_func()), argc, argv,
num_step_size, from, to, step, FALSE);
}
return SIZED_ENUMERATOR(from, 2, ((VALUE [2]){to, step}), num_step_size);
}
desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
if (rb_equal(step, INT2FIX(0))) {
inf = 1;
}
else if (RB_TYPE_P(to, T_FLOAT)) {
double f = RFLOAT_VALUE(to);
inf = isinf(f) && (signbit(f) ? desc : !desc);
}
else inf = 0;
if (FIXNUM_P(from) && (inf || FIXNUM_P(to)) && FIXNUM_P(step)) {
long i = FIX2LONG(from);
long diff = FIX2LONG(step);
if (inf) {
for (;; i += diff)
rb_yield(LONG2FIX(i));
}
else {
long end = FIX2LONG(to);
if (desc) {
for (; i >= end; i += diff)
rb_yield(LONG2FIX(i));
}
else {
for (; i <= end; i += diff)
rb_yield(LONG2FIX(i));
}
}
}
else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
VALUE i = from;
if (inf) {
for (;; i = rb_funcall(i, '+', 1, step))
rb_yield(i);
}
else {
ID cmp = desc ? '<' : '>';
for (; !RTEST(rb_funcall(i, cmp, 1, to)); i = rb_funcall(i, '+', 1, step))
rb_yield(i);
}
}
return from;
}</pre>
</div>
</div>
</div>
<div id="method-i-to_c" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
to_c → complex
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the value as a complex.</p>
<div class="method-source-code" id="to_c-source">
<pre>static VALUE
numeric_to_c(VALUE self)
{
return rb_complex_new1(self);
}</pre>
</div>
</div>
</div>
<div id="method-i-to_int" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
to_int → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Invokes the child class's <code>to_i</code> method to convert <code>num</code> to an integer.</p>
<pre class="ruby"><span class="ruby-value">1.0</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment">#=> Float</span>
<span class="ruby-value">1.0</span>.<span class="ruby-identifier">to_int</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment">#=> Integer</span>
<span class="ruby-value">1.0</span>.<span class="ruby-identifier">to_i</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment">#=> Integer</span>
</pre>
<div class="method-source-code" id="to_int-source">
<pre>static VALUE
num_to_int(VALUE num)
{
return num_funcall0(num, id_to_i);
}</pre>
</div>
</div>
</div>
<div id="method-i-truncate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
truncate([ndigits]) → integer or float
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>num</code> truncated (toward zero) to a precision of <code>ndigits</code> decimal digits (default: 0).</p>
<p><a href="Numeric.html"><code>Numeric</code></a> implements this by converting its value to a <a href="Float.html"><code>Float</code></a> and invoking <a href="Float.html#method-i-truncate"><code>Float#truncate</code></a>.</p>
<div class="method-source-code" id="truncate-source">
<pre>static VALUE
num_truncate(int argc, VALUE *argv, VALUE num)
{
return flo_truncate(argc, argv, rb_Float(num));
}</pre>
</div>
</div>
</div>
<div id="method-i-zero-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
zero? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if <code>num</code> has a zero value.</p>
<div class="method-source-code" id="zero-3F-source">
<pre>static VALUE
num_zero_p(VALUE num)
{
if (FIXNUM_P(num)) {
if (FIXNUM_ZERO_P(num)) {
return Qtrue;
}
}
else if (RB_TYPE_P(num, T_BIGNUM)) {
if (rb_bigzero_p(num)) {
/* this should not happen usually */
return Qtrue;
}
}
else if (rb_equal(num, INT2FIX(0))) {
return Qtrue;
}
return Qfalse;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>