BEGIN LICENSE BLOCK *****
Version: EPL 1.0/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Eclipse Public
License Version 1.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.eclipse.org/legal/epl-v10.html
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
Copyright (C) 2002-2004 Thomas E Enebo <enebo@acm.org>
Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
Copyright (C) 2006 Antti Karanta <Antti.Karanta@napa.fi>
Alternatively, the contents of this file may be used under the terms of
either of the GNU General Public License Version 2 or later (the "GPL"),
or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the EPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the EPL, the GPL or the LGPL.
END LICENSE BLOCK ***
import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.util.Numeric.f_abs;
import static org.jruby.util.Numeric.f_arg;
import static org.jruby.util.Numeric.f_mul;
import static org.jruby.util.Numeric.f_negative_p;
import static org.jruby.CompatVersion.*;
import static org.jruby.runtime.Helpers.invokedynamic;
Base class for all numerical types in ruby.
public static final double DBL_EPSILON=2.2204460492503131e-16;
return RubyFixnum.newFixnum(runtime, (long) val);
super(runtime, metaClass);
super(runtime, metaClass, useObjectSpace);
super(runtime, metaClass, useObjectSpace, canBeTainted);
if (value != null && value.length() > 0) return (byte) value.charAt(0);
rb_num2long and FIX2LONG (numeric.c)
rb_dbl2big + LONG2FIX at once (numeric.c)
return a == b ? RubyFixnum.zero(runtime) : a > b ?
return str2inum(runtime,str,base,false);
Converts a string representation of an integer to the integer value.
Parsing starts at the beginning of the string (after leading and
trailing whitespace have been removed), and stops at the end or at the
first character that can't be part of an integer. Leading signs are
allowed. If
base
is zero, strings that begin with '0[xX]',
'0[bB]', or '0' (optionally preceded by a sign) will be treated as hex,
binary, or octal numbers, respectively. If a non-zero base is given,
only the prefix (if any) that is appropriate to that base will be
parsed correctly. For example, if the base is zero or 16, the string
"0xff" will be converted to 256, but if the base is 10, it will come out
as zero, since 'x' is not a valid decimal digit. If the string fails
to parse as a number, zero is returned.
- Parameters:
runtime
the ruby runtimestr
the string to be convertedbase
the expected base of the number (for example, 2, 8, 10, 16),
or 0 if the method should determine the base automatically
(defaults to 10). Values 0 and 2-36 are permitted. Any other
value will result in an ArgumentError.strict
if true, enforce the strict criteria for String encoding of
numeric values, as required by Integer('n'), and raise an
exception when those criteria are not met. Otherwise, allow
lax expression of values, as permitted by String#to_i, and
return a value in almost all cases (excepting illegal radix).
TODO: describe the rules/criteria- Returns:
- a RubyFixnum or (if necessary) a RubyBignum representing
the result of the conversion, which will be zero if the
conversion failed.
Converts a string representation of a floating-point number to the
numeric value. Parsing starts at the beginning of the string (after
leading and trailing whitespace have been removed), and stops at the
end or at the first character that can't be part of a number. If
the string fails to parse as a number, 0.0 is returned.
- Parameters:
runtime
the ruby runtimearg
the string to be convertedstrict
if true, enforce the strict criteria for String encoding of
numeric values, as required by Float('n'), and raise an
exception when those criteria are not met. Otherwise, allow
lax expression of values, as permitted by String#to_f, and
return a value in all cases.
TODO: describe the rules/criteria- Returns:
- a RubyFloat representing the result of the conversion, which
will be 0.0 if the conversion failed.
result = other.callMethod(context, "coerce", this);
return args[0].callMethod(context, method, args[1]);
return args[0].callMethod(context, method, args[1]);
rb_num_coerce_bin
coercion taking two arguments
rb_num_coerce_cmp
coercion used for comparisons
rb_num_coerce_relop
coercion used for relative operators
return RubyComparable.cmperr(this, other);
return RubyComparable.cmperr(this, other);
return RubyComplex.newComplexRaw(context.runtime, RubyFixnum.zero(context.runtime), this);
num_quo (1.8)
quo and fdiv in 1.8 just invokes "/"
return Helpers.invoke(context, this, "to_i");
Ruby runtime = context.runtime;
floatStep19(context, runtime, this, to, step, false, block);
duckStep(context, runtime, this, to, step, block);
for (i = from; i <= tov; i += step) {
for (i = from; i >= tov; i += step) {
double n = (end - beg)/unit;
if (err > 0.5) err = 0.5;
n = Math.floor(n + err) + 1;
for (long i = 0; i < n; i++) {
block.yield(context, RubyFloat.newFloat(runtime, i * unit + beg));
double n = (end - beg)/unit;
if (unit > 0) block.yield(context, RubyFloat.newFloat(runtime, beg));
if (err > 0.5) err = 0.5;
for (long i = 0; i < n; i++){
block.yield(context, RubyFloat.newFloat(runtime, i * unit + beg));
num_equal, doesn't override RubyObject.op_equal
return RubyFixnum.zero(context.runtime);
return f_mul(context, this, this);
if (Double.isNaN(value)) {
return RubyFixnum.zero(context.runtime);
return context.runtime.newArray(this, RubyFixnum.zero(context.runtime));