2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
numeric.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri Aug 13 18:33:09 JST 1993
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2011-05-29 12:55:54 +04:00
|
|
|
#include "internal.h"
|
2014-11-15 14:49:06 +03:00
|
|
|
#include "ruby/util.h"
|
2012-12-02 13:57:47 +04:00
|
|
|
#include "id.h"
|
2004-06-16 18:21:34 +04:00
|
|
|
#include <ctype.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <math.h>
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <stdio.h>
|
2002-12-19 12:20:20 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ < 4
|
1999-08-13 09:45:20 +04:00
|
|
|
#include <floatingpoint.h>
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-12-19 12:20:20 +03:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
|
|
|
|
2003-12-11 05:39:59 +03:00
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-05 16:33:42 +04:00
|
|
|
#if !defined HAVE_ISFINITE && !defined isfinite
|
2013-10-10 10:33:34 +04:00
|
|
|
#if defined HAVE_FINITE && !defined finite && !defined _WIN32
|
2013-07-08 18:12:52 +04:00
|
|
|
extern int finite(double);
|
2014-01-05 16:33:42 +04:00
|
|
|
# define HAVE_ISFINITE 1
|
|
|
|
# define isfinite(x) finite(x)
|
|
|
|
#endif
|
2013-07-08 18:12:52 +04:00
|
|
|
#endif
|
|
|
|
|
2002-12-24 11:53:56 +03:00
|
|
|
/* use IEEE 64bit values if not defined */
|
|
|
|
#ifndef FLT_RADIX
|
|
|
|
#define FLT_RADIX 2
|
|
|
|
#endif
|
|
|
|
#ifndef FLT_ROUNDS
|
|
|
|
#define FLT_ROUNDS 1
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MIN
|
|
|
|
#define DBL_MIN 2.2250738585072014e-308
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MAX
|
|
|
|
#define DBL_MAX 1.7976931348623157e+308
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MIN_EXP
|
|
|
|
#define DBL_MIN_EXP (-1021)
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MAX_EXP
|
|
|
|
#define DBL_MAX_EXP 1024
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MIN_10_EXP
|
|
|
|
#define DBL_MIN_10_EXP (-307)
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MAX_10_EXP
|
2002-12-24 13:20:29 +03:00
|
|
|
#define DBL_MAX_10_EXP 308
|
2002-12-24 11:53:56 +03:00
|
|
|
#endif
|
|
|
|
#ifndef DBL_DIG
|
|
|
|
#define DBL_DIG 15
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MANT_DIG
|
|
|
|
#define DBL_MANT_DIG 53
|
|
|
|
#endif
|
2002-12-19 12:20:20 +03:00
|
|
|
#ifndef DBL_EPSILON
|
2002-12-24 11:53:56 +03:00
|
|
|
#define DBL_EPSILON 2.2204460492503131e-16
|
2002-12-19 12:20:20 +03:00
|
|
|
#endif
|
|
|
|
|
2009-12-29 10:05:39 +03:00
|
|
|
#ifdef HAVE_INFINITY
|
2011-08-05 17:28:50 +04:00
|
|
|
#elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
|
2011-10-23 13:03:33 +04:00
|
|
|
const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}};
|
2009-12-29 10:05:39 +03:00
|
|
|
#else
|
2011-10-23 13:03:33 +04:00
|
|
|
const union bytesequence4_or_float rb_infinity = {{0x7f, 0x80, 0x00, 0x00}};
|
2009-12-29 10:05:39 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_NAN
|
2011-08-05 17:28:50 +04:00
|
|
|
#elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
|
2011-10-23 13:03:33 +04:00
|
|
|
const union bytesequence4_or_float rb_nan = {{0x00, 0x00, 0xc0, 0x7f}};
|
2009-12-29 10:05:39 +03:00
|
|
|
#else
|
2011-10-23 13:03:33 +04:00
|
|
|
const union bytesequence4_or_float rb_nan = {{0x7f, 0xc0, 0x00, 0x00}};
|
2009-12-29 10:05:39 +03:00
|
|
|
#endif
|
|
|
|
|
2007-11-13 19:34:45 +03:00
|
|
|
#ifndef HAVE_ROUND
|
|
|
|
double
|
|
|
|
round(double x)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
if (x > 0.0) {
|
|
|
|
f = floor(x);
|
|
|
|
x = f + (x - f >= 0.5);
|
|
|
|
}
|
|
|
|
else if (x < 0.0) {
|
|
|
|
f = ceil(x);
|
|
|
|
x = f - (f - x >= 0.5);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-29 10:09:08 +04:00
|
|
|
static VALUE fix_uminus(VALUE num);
|
2011-03-23 02:07:36 +03:00
|
|
|
static VALUE fix_mul(VALUE x, VALUE y);
|
2016-04-03 01:38:44 +03:00
|
|
|
static VALUE fix_lshift(long, unsigned long);
|
|
|
|
static VALUE fix_rshift(long, unsigned long);
|
2011-03-23 02:07:36 +03:00
|
|
|
static VALUE int_pow(long x, unsigned long y);
|
2016-03-26 04:54:50 +03:00
|
|
|
static VALUE int_cmp(VALUE x, VALUE y);
|
2016-04-13 08:12:01 +03:00
|
|
|
static int int_round_zero_p(VALUE num, int ndigits);
|
2016-04-13 09:47:55 +03:00
|
|
|
VALUE rb_int_floor(VALUE num, int ndigits);
|
2016-04-13 09:50:24 +03:00
|
|
|
VALUE rb_int_ceil(VALUE num, int ndigits);
|
2016-04-18 06:56:33 +03:00
|
|
|
static VALUE flo_to_i(VALUE num);
|
2016-04-13 05:41:24 +03:00
|
|
|
static int float_invariant_round(double number, int ndigits, VALUE *num);
|
2011-03-23 02:07:36 +03:00
|
|
|
|
2015-10-12 03:08:58 +03:00
|
|
|
static ID id_coerce, id_div, id_divmod;
|
2014-05-20 12:28:33 +04:00
|
|
|
#define id_to_i idTo_i
|
2014-02-28 19:47:52 +04:00
|
|
|
#define id_eq idEq
|
|
|
|
#define id_cmp idCmp
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cNumeric;
|
|
|
|
VALUE rb_cFloat;
|
|
|
|
VALUE rb_cInteger;
|
|
|
|
VALUE rb_cFixnum;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE rb_eZeroDivError;
|
|
|
|
VALUE rb_eFloatDomainError;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2014-07-01 18:36:03 +04:00
|
|
|
static ID id_to, id_by;
|
2013-09-02 18:56:06 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num_zerodiv(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eZeroDivError, "divided by 0");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2010-10-13 07:56:31 +04:00
|
|
|
/* experimental API */
|
|
|
|
int
|
|
|
|
rb_num_to_uint(VALUE val, unsigned int *ret)
|
|
|
|
{
|
|
|
|
#define NUMERR_TYPE 1
|
|
|
|
#define NUMERR_NEGATIVE 2
|
|
|
|
#define NUMERR_TOOLARGE 3
|
|
|
|
if (FIXNUM_P(val)) {
|
|
|
|
long v = FIX2LONG(val);
|
2010-10-13 09:58:35 +04:00
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
|
|
|
if (v > (long)UINT_MAX) return NUMERR_TOOLARGE;
|
|
|
|
#endif
|
2010-10-13 07:56:31 +04:00
|
|
|
if (v < 0) return NUMERR_NEGATIVE;
|
|
|
|
*ret = (unsigned int)v;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(val, T_BIGNUM)) {
|
2014-02-16 01:17:34 +04:00
|
|
|
if (BIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE;
|
2010-10-13 07:56:31 +04:00
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
|
|
|
/* long is 64bit */
|
|
|
|
return NUMERR_TOOLARGE;
|
|
|
|
#else
|
|
|
|
/* long is 32bit */
|
2013-06-08 18:57:28 +04:00
|
|
|
if (rb_absint_size(val, NULL) > sizeof(int)) return NUMERR_TOOLARGE;
|
2010-10-13 07:56:31 +04:00
|
|
|
*ret = (unsigned int)rb_big2ulong((VALUE)val);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return NUMERR_TYPE;
|
|
|
|
}
|
2003-12-27 08:46:46 +03:00
|
|
|
|
2012-06-20 10:31:35 +04:00
|
|
|
#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
|
|
|
|
|
2015-05-17 08:59:36 +03:00
|
|
|
static VALUE
|
|
|
|
compare_with_zero(VALUE num, ID mid)
|
|
|
|
{
|
|
|
|
VALUE zero = INT2FIX(0);
|
|
|
|
VALUE r = rb_check_funcall(num, mid, 1, &zero);
|
|
|
|
if (r == Qundef) {
|
2016-02-16 12:25:08 +03:00
|
|
|
rb_cmperr(num, zero);
|
2015-05-17 08:59:36 +03:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-03-26 04:52:00 +03:00
|
|
|
#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0))
|
|
|
|
#define FIXNUM_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0)
|
|
|
|
#define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0))
|
|
|
|
|
2016-03-26 04:54:50 +03:00
|
|
|
#if 0
|
|
|
|
static inline int
|
|
|
|
int_pos_p(VALUE num)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
2016-03-28 15:25:41 +03:00
|
|
|
return FIXNUM_POSITIVE_P(num);
|
2016-03-26 04:54:50 +03:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
2016-03-28 15:25:41 +03:00
|
|
|
return BIGNUM_POSITIVE_P(num);
|
2016-03-26 04:54:50 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
int_neg_p(VALUE num)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return FIXNUM_NEGATIVE_P(num);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return BIGNUM_NEGATIVE_P(num);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-06-20 10:31:32 +04:00
|
|
|
static inline int
|
|
|
|
positive_int_p(VALUE num)
|
|
|
|
{
|
|
|
|
const ID mid = '>';
|
2012-06-20 10:31:35 +04:00
|
|
|
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (method_basic_p(rb_cFixnum))
|
2016-03-26 04:52:00 +03:00
|
|
|
return FIXNUM_POSITIVE_P(num);
|
2012-06-20 10:31:35 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
if (method_basic_p(rb_cBignum))
|
2014-02-16 01:17:34 +04:00
|
|
|
return BIGNUM_POSITIVE_P(num);
|
2012-06-20 10:31:35 +04:00
|
|
|
}
|
2015-05-17 08:59:36 +03:00
|
|
|
return RTEST(compare_with_zero(num, mid));
|
2012-06-20 10:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
negative_int_p(VALUE num)
|
|
|
|
{
|
|
|
|
const ID mid = '<';
|
2012-06-20 10:31:35 +04:00
|
|
|
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (method_basic_p(rb_cFixnum))
|
2016-03-26 04:52:00 +03:00
|
|
|
return FIXNUM_NEGATIVE_P(num);
|
2012-06-20 10:31:35 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
if (method_basic_p(rb_cBignum))
|
2014-02-16 01:17:34 +04:00
|
|
|
return BIGNUM_NEGATIVE_P(num);
|
2012-06-20 10:31:35 +04:00
|
|
|
}
|
2015-05-17 08:59:36 +03:00
|
|
|
return RTEST(compare_with_zero(num, mid));
|
2012-06-20 10:31:32 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 07:46:47 +04:00
|
|
|
int
|
|
|
|
rb_num_negative_p(VALUE num)
|
|
|
|
{
|
|
|
|
return negative_int_p(num);
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.coerce(numeric) -> array
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2015-07-22 03:45:33 +03:00
|
|
|
* If a +numeric+ is the same type as +num+, returns an array containing
|
2013-07-15 22:27:23 +04:00
|
|
|
* +numeric+ and +num+. Otherwise, returns an array with both a +numeric+ and
|
|
|
|
* +num+ represented as Float objects.
|
|
|
|
*
|
|
|
|
* 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.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1.coerce(2.5) #=> [2.5, 1.0]
|
|
|
|
* 1.2.coerce(3) #=> [3.0, 1.2]
|
|
|
|
* 1.coerce(2) #=> [2, 1]
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_coerce(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (CLASS_OF(x) == CLASS_OF(y))
|
2000-02-17 10:11:22 +03:00
|
|
|
return rb_assoc_new(y, x);
|
2008-06-30 20:03:41 +04:00
|
|
|
x = rb_Float(x);
|
|
|
|
y = rb_Float(y);
|
|
|
|
return rb_assoc_new(y, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2013-01-12 12:47:18 +04:00
|
|
|
static VALUE
|
2016-02-17 05:38:09 +03:00
|
|
|
coerce_body(VALUE arg)
|
2013-01-12 12:47:18 +04:00
|
|
|
{
|
2016-02-17 05:38:09 +03:00
|
|
|
VALUE *x = (VALUE *)arg;
|
2013-01-12 12:47:18 +04:00
|
|
|
return rb_funcall(x[1], id_coerce, 1, x[0]);
|
|
|
|
}
|
|
|
|
|
2014-01-15 12:16:37 +04:00
|
|
|
NORETURN(static void coerce_failed(VALUE x, VALUE y));
|
|
|
|
static void
|
|
|
|
coerce_failed(VALUE x, VALUE y)
|
|
|
|
{
|
2015-01-13 15:07:14 +03:00
|
|
|
if (SPECIAL_CONST_P(y) || BUILTIN_TYPE(y) == T_FLOAT) {
|
|
|
|
y = rb_inspect(y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
y = rb_obj_class(y);
|
|
|
|
}
|
2014-01-15 12:16:37 +04:00
|
|
|
rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
|
2015-01-13 15:07:14 +03:00
|
|
|
y, rb_obj_class(x));
|
2014-01-15 12:16:37 +04:00
|
|
|
}
|
|
|
|
|
2013-01-12 12:47:18 +04:00
|
|
|
static VALUE
|
2016-02-17 05:54:50 +03:00
|
|
|
coerce_rescue(VALUE arg, VALUE errinfo)
|
2013-01-12 12:47:18 +04:00
|
|
|
{
|
2016-02-17 05:38:09 +03:00
|
|
|
VALUE *x = (VALUE *)arg;
|
2014-01-15 12:16:37 +04:00
|
|
|
coerce_failed(x[0], x[1]);
|
2013-01-12 12:47:18 +04:00
|
|
|
return Qnil; /* dummy */
|
|
|
|
}
|
|
|
|
|
2014-06-07 17:16:01 +04:00
|
|
|
static VALUE
|
2016-02-17 05:54:50 +03:00
|
|
|
coerce_rescue_quiet(VALUE arg, VALUE errinfo)
|
2014-06-07 17:16:01 +04:00
|
|
|
{
|
|
|
|
return Qundef;
|
|
|
|
}
|
|
|
|
|
2002-12-19 12:20:20 +03:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
do_coerce(VALUE *x, VALUE *y, int err)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE ary;
|
2013-01-12 12:47:18 +04:00
|
|
|
VALUE a[2];
|
|
|
|
|
|
|
|
a[0] = *x; a[1] = *y;
|
2001-07-24 13:07:33 +04:00
|
|
|
|
2013-01-12 12:47:18 +04:00
|
|
|
if (!rb_respond_to(*y, id_coerce)) {
|
2013-01-10 10:30:23 +04:00
|
|
|
if (err) {
|
2016-02-17 05:38:09 +03:00
|
|
|
coerce_failed(*x, *y);
|
2013-01-10 12:12:58 +04:00
|
|
|
}
|
|
|
|
return FALSE;
|
2013-01-10 10:30:23 +04:00
|
|
|
}
|
|
|
|
|
2014-06-07 17:16:01 +04:00
|
|
|
ary = rb_rescue(coerce_body, (VALUE)a, err ? coerce_rescue : coerce_rescue_quiet, (VALUE)a);
|
|
|
|
if (ary == Qundef) {
|
|
|
|
rb_warn("Numerical comparison operators will no more rescue exceptions of #coerce");
|
|
|
|
rb_warn("in the next release. Return nil in #coerce if the coercion is impossible.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
|
2002-12-19 12:20:20 +03:00
|
|
|
if (err) {
|
|
|
|
rb_raise(rb_eTypeError, "coerce must return [x, y]");
|
2015-09-28 05:40:46 +03:00
|
|
|
}
|
|
|
|
else if (!NIL_P(ary)) {
|
2014-06-07 17:16:01 +04:00
|
|
|
rb_warn("Bad return value for #coerce, called by numerical comparison operators.");
|
|
|
|
rb_warn("#coerce must return [x, y]. The next release will raise an error for this.");
|
2002-12-19 12:20:20 +03:00
|
|
|
}
|
2009-07-18 12:05:32 +04:00
|
|
|
return FALSE;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2013-05-13 13:56:22 +04:00
|
|
|
*x = RARRAY_AREF(ary, 0);
|
|
|
|
*y = RARRAY_AREF(ary, 1);
|
2009-07-18 12:05:32 +04:00
|
|
|
return TRUE;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE
|
2008-02-12 05:46:21 +03:00
|
|
|
rb_num_coerce_bin(VALUE x, VALUE y, ID func)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
do_coerce(&x, &y, TRUE);
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_funcall(x, func, 1, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-12-19 12:20:20 +03:00
|
|
|
VALUE
|
2008-02-12 05:46:21 +03:00
|
|
|
rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
|
2002-12-19 12:20:20 +03:00
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
if (do_coerce(&x, &y, FALSE))
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_funcall(x, func, 1, y);
|
2002-12-19 12:20:20 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-07-31 12:42:44 +04:00
|
|
|
VALUE
|
2008-02-12 05:46:21 +03:00
|
|
|
rb_num_coerce_relop(VALUE x, VALUE y, ID func)
|
2003-05-08 07:56:12 +04:00
|
|
|
{
|
|
|
|
VALUE c, x0 = x, y0 = y;
|
|
|
|
|
2009-07-18 12:05:32 +04:00
|
|
|
if (!do_coerce(&x, &y, FALSE) ||
|
2008-02-12 05:46:21 +03:00
|
|
|
NIL_P(c = rb_funcall(x, func, 1, y))) {
|
2003-05-08 07:56:12 +04:00
|
|
|
rb_cmperr(x0, y0);
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Trap attempts to add methods to Numeric objects. Always raises a TypeError.
|
|
|
|
*
|
|
|
|
* Numerics should be values; singleton_methods should not be added to them.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2003-12-01 16:16:09 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_sadded(VALUE x, VALUE name)
|
2003-12-01 16:16:09 +03:00
|
|
|
{
|
2009-08-27 13:31:11 +04:00
|
|
|
ID mid = rb_to_id(name);
|
* call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
eval_error.h, eval_jump.h, eval_load.c, eval_thread.c, gc.c,
insnhelper.h, insns.def, iseq.c, main.c, numeric.c, parse.y,
range.c, regenc.h, ruby.h, signal.c, thread.c, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fixed indents and non-C90 comments.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-02 19:26:04 +03:00
|
|
|
/* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
|
2009-08-27 13:31:11 +04:00
|
|
|
rb_remove_method_id(rb_singleton_class(x), mid);
|
2003-12-01 16:16:09 +03:00
|
|
|
rb_raise(rb_eTypeError,
|
2014-01-15 12:16:40 +04:00
|
|
|
"can't define singleton method \"%"PRIsVALUE"\" for %"PRIsVALUE,
|
|
|
|
rb_id2str(mid),
|
|
|
|
rb_obj_class(x));
|
2012-04-14 04:36:26 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2003-12-01 16:16:09 +03:00
|
|
|
}
|
|
|
|
|
2013-07-15 22:27:23 +04:00
|
|
|
/*
|
|
|
|
* Numerics are immutable values, which should not be copied.
|
|
|
|
*
|
|
|
|
* Any attempt to use this method on a Numeric will raise a TypeError.
|
|
|
|
*/
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_init_copy(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2014-01-15 12:16:42 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't copy %"PRIsVALUE, rb_obj_class(x));
|
2012-04-14 04:36:26 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* +num -> num
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* Unary Plus---Returns the receiver's value.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_uplus(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2009-08-17 02:28:48 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* num.i -> Complex(0,num)
|
|
|
|
*
|
|
|
|
* Returns the corresponding imaginary number.
|
|
|
|
* Not available for complex numbers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_imaginary(VALUE num)
|
|
|
|
{
|
|
|
|
return rb_complex_new(INT2FIX(0), num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* -num -> numeric
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* Unary Minus---Returns the receiver's value, negated.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_uminus(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE zero;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
zero = INT2FIX(0);
|
2009-07-18 12:05:32 +04:00
|
|
|
do_coerce(&zero, &num, TRUE);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_funcall(zero, '-', 1, num);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
2008-04-02 18:13:53 +04:00
|
|
|
* call-seq:
|
2013-06-06 05:04:11 +04:00
|
|
|
* num.fdiv(numeric) -> float
|
2008-04-02 18:13:53 +04:00
|
|
|
*
|
2013-06-06 05:04:11 +04:00
|
|
|
* Returns float division.
|
2008-04-02 18:13:53 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2013-06-06 05:04:11 +04:00
|
|
|
num_fdiv(VALUE x, VALUE y)
|
2008-04-02 18:13:53 +04:00
|
|
|
{
|
2013-06-06 05:04:11 +04:00
|
|
|
return rb_funcall(rb_Float(x), '/', 1, y);
|
2008-04-02 18:13:53 +04:00
|
|
|
}
|
2003-12-27 08:46:46 +03:00
|
|
|
|
2008-04-07 17:52:26 +04:00
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.div(numeric) -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Uses +/+ to perform division, then converts the result to an integer.
|
|
|
|
* +numeric+ does not define the +/+ operator; this is left to subclasses.
|
2009-06-19 17:37:04 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Equivalent to <code>num.divmod(numeric)[0]</code>.
|
2009-06-19 17:37:04 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* See Numeric#divmod.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2001-07-31 10:24:45 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_div(VALUE x, VALUE y)
|
2001-07-31 10:24:45 +04:00
|
|
|
{
|
2008-05-27 16:51:28 +04:00
|
|
|
if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
|
2009-06-20 16:37:13 +04:00
|
|
|
return rb_funcall(rb_funcall(x, '/', 1, y), rb_intern("floor"), 0);
|
2001-07-31 10:24:45 +04:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
|
2009-06-20 16:37:13 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.modulo(numeric) -> real
|
2009-06-20 16:37:13 +04:00
|
|
|
*
|
|
|
|
* x.modulo(y) means x-y*(x/y).floor
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Equivalent to <code>num.divmod(numeric)[1]</code>.
|
2009-06-20 16:37:13 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* See Numeric#divmod.
|
2009-06-20 16:37:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_modulo(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
return rb_funcall(x, '-', 1,
|
|
|
|
rb_funcall(y, '*', 1,
|
2015-10-12 03:08:58 +03:00
|
|
|
rb_funcall(x, id_div, 1, y)));
|
2009-06-20 16:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.remainder(numeric) -> real
|
2009-06-20 16:37:13 +04:00
|
|
|
*
|
|
|
|
* x.remainder(y) means x-y*(x/y).truncate
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* See Numeric#divmod.
|
2009-06-20 16:37:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_remainder(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
VALUE z = rb_funcall(x, '%', 1, y);
|
|
|
|
|
|
|
|
if ((!rb_equal(z, INT2FIX(0))) &&
|
2012-06-20 10:31:32 +04:00
|
|
|
((negative_int_p(x) &&
|
|
|
|
positive_int_p(y)) ||
|
|
|
|
(positive_int_p(x) &&
|
|
|
|
negative_int_p(y)))) {
|
2009-06-20 16:37:13 +04:00
|
|
|
return rb_funcall(z, '-', 1, y);
|
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.divmod(numeric) -> array
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns an array containing the quotient and modulus obtained by dividing
|
|
|
|
* +num+ by +numeric+.
|
|
|
|
*
|
|
|
|
* If <code>q, r = * x.divmod(y)</code>, then
|
2003-12-27 08:46:46 +03:00
|
|
|
*
|
2009-06-19 17:37:04 +04:00
|
|
|
* q = floor(x/y)
|
|
|
|
* x = q*y+r
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* The quotient is rounded toward -infinity, as shown in the following table:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
|
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
|
|
|
* 13 | 4 | 3, 1 | 3 | 1 | 1
|
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
2009-08-11 16:42:53 +04:00
|
|
|
* 13 | -4 | -4, -3 | -4 | -3 | 1
|
2003-12-27 08:46:46 +03:00
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
|
|
|
* -13 | 4 | -4, 3 | -4 | 3 | -1
|
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
|
|
|
* -13 | -4 | 3, -1 | 3 | -1 | -1
|
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
* 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
|
2003-12-27 08:46:46 +03:00
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
* 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
|
2003-12-27 08:46:46 +03:00
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
* -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
|
2003-12-27 08:46:46 +03:00
|
|
|
* ------+-----+---------------+---------+-------------+---------------
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
* -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
|
2003-12-27 08:46:46 +03:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Examples
|
2008-03-09 04:04:46 +03:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 11.divmod(3) #=> [3, 2]
|
|
|
|
* 11.divmod(-3) #=> [-4, -1]
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
* 11.divmod(3.5) #=> [3, 0.5]
|
|
|
|
* (-11).divmod(3.5) #=> [-4, 3.0]
|
|
|
|
* (11.5).divmod(3.5) #=> [3, 1.0]
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_divmod(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-06-20 16:37:13 +04:00
|
|
|
return rb_assoc_new(num_div(x, y), num_modulo(x, y));
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
|
|
|
|
2005-12-07 11:41:59 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.real? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +num+ is a Real number. (i.e. not Complex).
|
2005-12-07 11:41:59 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2008-09-17 02:04:19 +04:00
|
|
|
num_real_p(VALUE num)
|
2005-12-07 11:41:59 +03:00
|
|
|
{
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.integer? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-02-22 06:01:28 +04:00
|
|
|
* Returns +true+ if +num+ is an Integer (including Fixnum and Bignum).
|
|
|
|
*
|
|
|
|
* (1.0).integer? #=> false
|
|
|
|
* (1).integer? #=> true
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_int_p(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.abs -> numeric
|
|
|
|
* num.magnitude -> numeric
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the absolute value of +num+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 12.abs #=> 12
|
|
|
|
* (-34.56).abs #=> 34.56
|
|
|
|
* -34.56.abs #=> 34.56
|
2013-07-15 22:27:23 +04:00
|
|
|
*
|
|
|
|
* Numeric#magnitude is an alias of Numeric#abs.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_abs(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-06-20 10:31:32 +04:00
|
|
|
if (negative_int_p(num)) {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(num, idUMinus, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.zero? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +num+ has a zero value.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_zero_p(VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2016-03-17 20:11:42 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (FIX2LONG(num) == 0) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_bigzero_p(num);
|
|
|
|
}
|
|
|
|
else if (rb_equal(num, INT2FIX(0))) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.nonzero? -> self or nil
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +self+ if +num+ is not zero, +nil+ otherwise.
|
|
|
|
*
|
|
|
|
* This behavior is useful when chaining comparisons:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 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 #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_nonzero_p(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2015-02-16 07:08:52 +03:00
|
|
|
if (RTEST(rb_funcallv(num, rb_intern("zero?"), 0, 0))) {
|
1999-08-13 09:45:20 +04:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.to_int -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-02-22 06:01:28 +04:00
|
|
|
* Invokes the child class's +to_i+ method to convert +num+ to an integer.
|
|
|
|
*
|
|
|
|
* 1.0.class => Float
|
|
|
|
* 1.0.to_int.class => Fixnum
|
|
|
|
* 1.0.to_i.class => Fixnum
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2002-07-29 10:14:10 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_to_int(VALUE num)
|
2002-07-29 10:14:10 +04:00
|
|
|
{
|
2015-02-16 07:08:52 +03:00
|
|
|
return rb_funcallv(num, id_to_i, 0, 0);
|
2002-07-29 10:14:10 +04:00
|
|
|
}
|
|
|
|
|
2015-05-17 09:01:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* num.positive? -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +num+ is greater than 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_positive_p(VALUE num)
|
|
|
|
{
|
|
|
|
const ID mid = '>';
|
|
|
|
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (method_basic_p(rb_cFixnum))
|
2015-05-19 07:13:22 +03:00
|
|
|
return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0) ? Qtrue : Qfalse;
|
2015-05-17 09:01:47 +03:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
if (method_basic_p(rb_cBignum))
|
2015-05-19 07:13:22 +03:00
|
|
|
return BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num) ? Qtrue : Qfalse;
|
2015-05-17 09:01:47 +03:00
|
|
|
}
|
2015-05-19 07:13:22 +03:00
|
|
|
return compare_with_zero(num, mid);
|
2015-05-17 09:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2015-05-24 18:33:08 +03:00
|
|
|
* num.negative? -> true or false
|
2015-05-17 09:01:47 +03:00
|
|
|
*
|
|
|
|
* Returns +true+ if +num+ is less than 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_negative_p(VALUE num)
|
|
|
|
{
|
|
|
|
return negative_int_p(num) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
|
|
|
|
/********************************************************************
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* Document-class: Float
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Float objects represent inexact real numbers using the native
|
|
|
|
* architecture's double-precision floating point representation.
|
2010-08-29 04:42:05 +04:00
|
|
|
*
|
2013-08-10 09:02:56 +04:00
|
|
|
* Floating point has a different arithmetic and is an inexact number.
|
2010-08-29 04:42:05 +04:00
|
|
|
* So you should know its esoteric system. see following:
|
2011-05-15 15:55:52 +04:00
|
|
|
*
|
2010-08-29 04:42:05 +04:00
|
|
|
* - http://docs.sun.com/source/806-3568/ncg_goldberg.html
|
2013-04-24 09:08:47 +04:00
|
|
|
* - http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#wiki-floats_imprecise
|
2010-08-29 04:42:05 +04:00
|
|
|
* - http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
2012-08-23 11:22:40 +04:00
|
|
|
rb_float_new_in_heap(double d)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* gc.c: support RGENGC. [ruby-trunk - Feature #8339]
See this ticet about RGENGC.
* gc.c: Add several flags:
* RGENGC_DEBUG: if >0, then prints debug information.
* RGENGC_CHECK_MODE: if >0, add assertions.
* RGENGC_PROFILE: if >0, add profiling features.
check GC.stat and GC::Profiler.
* include/ruby/ruby.h: disable RGENGC by default (USE_RGENGC == 0).
* array.c: add write barriers for T_ARRAY and generate sunny objects.
* include/ruby/ruby.h (RARRAY_PTR_USE): added. Use this macro if
you want to access raw pointers. If you modify the contents which
pointer pointed, then you need to care write barrier.
* bignum.c, marshal.c, random.c: generate T_BIGNUM sunny objects.
* complex.c, include/ruby/ruby.h: add write barriers for T_COMPLEX
and generate sunny objects.
* rational.c (nurat_s_new_internal), include/ruby/ruby.h: add write
barriers for T_RATIONAL and generate sunny objects.
* internal.h: add write barriers for RBasic::klass.
* numeric.c (rb_float_new_in_heap): generate sunny T_FLOAT objects.
* object.c (rb_class_allocate_instance), range.c:
generate sunny T_OBJECT objects.
* string.c: add write barriers for T_STRING and generate sunny objects.
* variable.c: add write barriers for ivars.
* vm_insnhelper.c (vm_setivar): ditto.
* include/ruby/ruby.h, debug.c: use two flags
FL_WB_PROTECTED and FL_OLDGEN.
* node.h (NODE_FL_CREF_PUSHED_BY_EVAL, NODE_FL_CREF_OMOD_SHARED):
move flag bits.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40703 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 22:07:47 +04:00
|
|
|
NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT | (RGENGC_WB_PROTECTED_FLOAT ? FL_WB_PROTECTED : 0));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-11-20 14:35:12 +03:00
|
|
|
flt->float_value = d;
|
2012-10-27 06:10:53 +04:00
|
|
|
OBJ_FREEZE(flt);
|
1998-01-16 15:13:05 +03:00
|
|
|
return (VALUE)flt;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.to_s -> string
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns a string containing a representation of self. As well as a fixed or
|
|
|
|
* exponential form of the +float+, the call may return +NaN+, +Infinity+, and
|
|
|
|
* +-Infinity+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_to_s(VALUE flt)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-03-05 12:36:39 +03:00
|
|
|
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
|
2009-04-06 20:08:23 +04:00
|
|
|
enum {float_dig = DBL_DIG+1};
|
2009-03-05 12:36:39 +03:00
|
|
|
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double value = RFLOAT_VALUE(flt);
|
2010-05-21 07:26:00 +04:00
|
|
|
VALUE s;
|
|
|
|
char *p, *e;
|
|
|
|
int sign, decpt, digs;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2016-02-24 05:20:45 +03:00
|
|
|
if (isinf(value)) {
|
|
|
|
static const char minf[] = "-Infinity";
|
|
|
|
const int pos = (value > 0); /* skip "-" */
|
|
|
|
return rb_usascii_str_new(minf+pos, strlen(minf)-pos);
|
|
|
|
}
|
2010-01-14 07:07:00 +03:00
|
|
|
else if (isnan(value))
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
return rb_usascii_str_new2("NaN");
|
2004-05-07 12:44:24 +04:00
|
|
|
|
2010-05-12 05:57:08 +04:00
|
|
|
p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
|
2010-05-21 07:26:00 +04:00
|
|
|
s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
|
|
|
|
if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
|
|
|
|
memcpy(buf, p, digs);
|
|
|
|
xfree(p);
|
|
|
|
if (decpt > 0) {
|
2010-05-13 08:30:07 +04:00
|
|
|
if (decpt < digs) {
|
2010-05-21 07:26:00 +04:00
|
|
|
memmove(buf + decpt + 1, buf + decpt, digs - decpt);
|
|
|
|
buf[decpt] = '.';
|
|
|
|
rb_str_cat(s, buf, digs + 1);
|
|
|
|
}
|
2012-07-16 17:52:10 +04:00
|
|
|
else if (decpt <= DBL_DIG) {
|
2010-05-21 07:26:00 +04:00
|
|
|
long len;
|
|
|
|
char *ptr;
|
|
|
|
rb_str_cat(s, buf, digs);
|
|
|
|
rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
|
|
|
|
ptr = RSTRING_PTR(s) + len;
|
|
|
|
if (decpt > digs) {
|
|
|
|
memset(ptr, '0', decpt - digs);
|
|
|
|
ptr += decpt - digs;
|
|
|
|
}
|
|
|
|
memcpy(ptr, ".0", 2);
|
2010-05-13 08:30:07 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-05-21 07:26:00 +04:00
|
|
|
goto exp;
|
2010-05-13 08:30:07 +04:00
|
|
|
}
|
2010-05-21 07:26:00 +04:00
|
|
|
}
|
|
|
|
else if (decpt > -4) {
|
|
|
|
long len;
|
|
|
|
char *ptr;
|
|
|
|
rb_str_cat(s, "0.", 2);
|
|
|
|
rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
|
|
|
|
ptr = RSTRING_PTR(s);
|
|
|
|
memset(ptr += len, '0', -decpt);
|
|
|
|
memcpy(ptr -= decpt, buf, digs);
|
2004-05-07 12:44:24 +04:00
|
|
|
}
|
2010-05-12 05:57:08 +04:00
|
|
|
else {
|
2010-05-21 07:26:00 +04:00
|
|
|
exp:
|
2010-05-12 05:57:08 +04:00
|
|
|
if (digs > 1) {
|
2010-05-21 07:26:00 +04:00
|
|
|
memmove(buf + 2, buf + 1, digs - 1);
|
2010-05-12 05:57:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[2] = '0';
|
2010-05-21 07:26:00 +04:00
|
|
|
digs++;
|
2004-05-17 11:14:45 +04:00
|
|
|
}
|
2010-05-21 07:26:00 +04:00
|
|
|
buf[1] = '.';
|
|
|
|
rb_str_cat(s, buf, digs + 1);
|
|
|
|
rb_str_catf(s, "e%+03d", decpt - 1);
|
2004-05-17 11:14:45 +04:00
|
|
|
}
|
2010-05-21 07:26:00 +04:00
|
|
|
return s;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2011-05-13 14:27:10 +04:00
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.coerce(numeric) -> array
|
|
|
|
*
|
|
|
|
* Returns an array with both a +numeric+ and a +float+ represented as Float
|
|
|
|
* objects.
|
2011-05-13 14:27:10 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* This is achieved by converting a +numeric+ to a Float.
|
2011-05-13 14:27:10 +04:00
|
|
|
*
|
|
|
|
* 1.2.coerce(3) #=> [3.0, 1.2]
|
|
|
|
* 2.5.coerce(1.1) #=> [1.1, 2.5]
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_coerce(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_assoc_new(rb_Float(y), x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* -float -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* Returns float, negated.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_uminus(VALUE flt)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(-RFLOAT_VALUE(flt));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* float + other -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns a new float which is the sum of +float+ and +other+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_plus(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '+');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-02-01 11:09:54 +03:00
|
|
|
* float - other -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns a new float which is the difference of +float+ and +other+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_minus(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '-');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* float * other -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns a new float which is the product of +float+ and +other+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_mul(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '*');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* float / other -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns a new float which is the result of dividing +float+ by +other+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_div(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
long f_y;
|
1998-01-16 15:13:05 +03:00
|
|
|
double d;
|
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
f_y = FIX2LONG(y);
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
d = rb_big2dbl(y);
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) / d);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '/');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 10:41:40 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-04-12 06:59:07 +04:00
|
|
|
* float.fdiv(numeric) -> float
|
2009-09-05 10:41:40 +04:00
|
|
|
* float.quo(numeric) -> float
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns <code>float / numeric</code>, same as Float#/.
|
2009-09-05 10:41:40 +04:00
|
|
|
*/
|
|
|
|
|
2008-03-16 03:23:43 +03:00
|
|
|
static VALUE
|
2008-04-07 17:52:26 +04:00
|
|
|
flo_quo(VALUE x, VALUE y)
|
2008-03-16 03:23:43 +03:00
|
|
|
{
|
|
|
|
return rb_funcall(x, '/', 1, y);
|
|
|
|
}
|
|
|
|
|
2000-07-03 09:46:36 +04:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flodivmod(double x, double y, double *divp, double *modp)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-07-06 11:21:26 +04:00
|
|
|
double div, mod;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2014-08-01 11:35:48 +04:00
|
|
|
if (isnan(y)) {
|
|
|
|
/* y is NaN so all results are NaN */
|
|
|
|
if (modp) *modp = y;
|
|
|
|
if (divp) *divp = y;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-27 19:01:54 +03:00
|
|
|
if (y == 0.0) rb_num_zerodiv();
|
2012-12-01 19:25:28 +04:00
|
|
|
if ((x == 0.0) || (isinf(y) && !isinf(x)))
|
2012-03-14 10:10:01 +04:00
|
|
|
mod = x;
|
|
|
|
else {
|
2012-03-14 12:56:42 +04:00
|
|
|
#ifdef HAVE_FMOD
|
|
|
|
mod = fmod(x, y);
|
|
|
|
#else
|
2000-07-03 09:46:36 +04:00
|
|
|
double z;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-11-27 12:23:38 +03:00
|
|
|
modf(x/y, &z);
|
2001-05-02 08:22:21 +04:00
|
|
|
mod = x - z * y;
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2012-03-14 12:56:42 +04:00
|
|
|
}
|
2014-08-01 11:35:48 +04:00
|
|
|
if (isinf(x) && !isinf(y))
|
2007-11-13 10:33:09 +03:00
|
|
|
div = x;
|
2016-04-03 03:34:31 +03:00
|
|
|
else {
|
2007-11-13 10:33:09 +03:00
|
|
|
div = (x - mod) / y;
|
2016-04-03 03:34:31 +03:00
|
|
|
if (modp && divp) div = round(div);
|
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (y*mod < 0) {
|
|
|
|
mod += y;
|
|
|
|
div -= 1.0;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (modp) *modp = mod;
|
|
|
|
if (divp) *divp = div;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-03-14 10:10:01 +04:00
|
|
|
/*
|
|
|
|
* Returns the modulo of division of x by y.
|
|
|
|
* An error will be raised if y == 0.
|
|
|
|
*/
|
|
|
|
|
2012-03-15 05:39:00 +04:00
|
|
|
double
|
|
|
|
ruby_float_mod(double x, double y)
|
|
|
|
{
|
2012-03-14 10:10:01 +04:00
|
|
|
double mod;
|
|
|
|
flodivmod(x, y, 0, &mod);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-02-22 06:01:28 +04:00
|
|
|
* float % other -> float
|
|
|
|
* float.modulo(other) -> float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-02-22 06:01:28 +04:00
|
|
|
* Return the modulo after division of +float+ by +other+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 6543.21.modulo(137) #=> 104.21
|
|
|
|
* 6543.21.modulo(137.24) #=> 92.9299999999996
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_mod(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2012-03-14 10:10:01 +04:00
|
|
|
double fy;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2000-07-03 09:46:36 +04:00
|
|
|
fy = (double)FIX2LONG(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2000-07-03 09:46:36 +04:00
|
|
|
fy = rb_big2dbl(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
fy = RFLOAT_VALUE(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '%');
|
2000-07-03 09:46:36 +04:00
|
|
|
}
|
2012-03-14 10:10:01 +04:00
|
|
|
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2008-03-13 19:37:54 +03:00
|
|
|
static VALUE
|
|
|
|
dbl2ival(double d)
|
|
|
|
{
|
|
|
|
if (FIXABLE(d)) {
|
|
|
|
return LONG2FIX((long)d);
|
|
|
|
}
|
2009-07-04 04:46:14 +04:00
|
|
|
return rb_dbl2big(d);
|
2008-03-13 19:37:54 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-02-22 06:01:28 +04:00
|
|
|
* float.divmod(numeric) -> array
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-02-22 06:01:28 +04:00
|
|
|
* See Numeric#divmod.
|
|
|
|
*
|
|
|
|
* 42.0.divmod 6 #=> [7, 0.0]
|
|
|
|
* 42.0.divmod 5 #=> [8, 2.0]
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_divmod(VALUE x, VALUE y)
|
2000-02-01 06:12:21 +03:00
|
|
|
{
|
2008-03-14 08:03:28 +03:00
|
|
|
double fy, div, mod;
|
2004-11-17 05:27:38 +03:00
|
|
|
volatile VALUE a, b;
|
2000-02-01 06:12:21 +03:00
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2000-07-03 09:46:36 +04:00
|
|
|
fy = (double)FIX2LONG(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2000-07-03 09:46:36 +04:00
|
|
|
fy = rb_big2dbl(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
fy = RFLOAT_VALUE(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_bin(x, y, id_divmod);
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
|
2008-03-13 19:37:54 +03:00
|
|
|
a = dbl2ival(div);
|
2008-09-05 22:24:21 +04:00
|
|
|
b = DBL2NUM(mod);
|
2004-11-17 05:27:38 +03:00
|
|
|
return rb_assoc_new(a, b);
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* float ** other -> float
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Raises +float+ to the power of +other+.
|
2009-06-19 18:44:03 +04:00
|
|
|
*
|
|
|
|
* 2.0**3 #=> 8.0
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
2007-05-09 08:11:41 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_pow(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2015-10-12 03:08:44 +03:00
|
|
|
double dx, dy;
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM)) {
|
2015-10-12 03:08:44 +03:00
|
|
|
dx = RFLOAT_VALUE(x);
|
|
|
|
dy = (double)FIX2LONG(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2015-10-12 03:08:44 +03:00
|
|
|
dx = RFLOAT_VALUE(x);
|
|
|
|
dy = rb_big2dbl(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2015-10-12 03:08:44 +03:00
|
|
|
dx = RFLOAT_VALUE(x);
|
|
|
|
dy = RFLOAT_VALUE(y);
|
|
|
|
if (dx < 0 && dy != round(dy))
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(rb_complex_raw1(x), idPow, 1, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_bin(x, y, idPow);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2015-10-12 03:08:44 +03:00
|
|
|
return DBL2NUM(pow(dx, dy));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.eql?(numeric) -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +num+ and +numeric+ are the same type and have equal
|
2016-03-18 16:11:09 +03:00
|
|
|
* values. Contrast this with <code>Numeric#==</code>, which performs
|
|
|
|
* type conversions.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1 == 1.0 #=> true
|
|
|
|
* 1.eql?(1.0) #=> false
|
|
|
|
* (1.0).eql?(1.0) #=> true
|
2016-03-18 16:11:09 +03:00
|
|
|
* 68719476736.eql?(68719476736.0) #=> false
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_eql(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (TYPE(x) != TYPE(y)) return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2016-03-18 16:11:09 +03:00
|
|
|
if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_eql(x, y);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return rb_equal(x, y);
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-02-23 07:35:38 +04:00
|
|
|
* number <=> other -> 0 or nil
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-02-23 07:35:38 +04:00
|
|
|
* Returns zero if +number+ equals +other+, otherwise +nil+ is returned if the
|
|
|
|
* two values are incomparable.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2002-11-22 12:14:24 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_cmp(VALUE x, VALUE y)
|
2002-11-22 12:14:24 +03:00
|
|
|
{
|
|
|
|
if (x == y) return INT2FIX(0);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_equal(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-07-21 22:48:58 +04:00
|
|
|
if (x == y) return Qtrue;
|
|
|
|
return rb_funcall(y, id_eq, 1, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float == obj -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ only if +obj+ has the same value as +float+. Contrast this
|
|
|
|
* with Float#eql?, which requires obj to be a Float.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN == NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.0 == 1 #=> true
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_eq(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2004-06-04 13:56:25 +04:00
|
|
|
volatile double a, b;
|
2003-04-11 10:37:48 +04:00
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 14:39:42 +04:00
|
|
|
return rb_integer_float_eq(y, x);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(b)) return Qfalse;
|
|
|
|
#endif
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:13:05 +03:00
|
|
|
return num_equal(x, y);
|
|
|
|
}
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a)) return Qfalse;
|
|
|
|
#endif
|
2003-04-11 10:37:48 +04:00
|
|
|
return (a == b)?Qtrue:Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.hash -> integer
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* Returns a hash code for this float.
|
2014-03-14 05:27:43 +04:00
|
|
|
*
|
|
|
|
* See also Object#hash.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_hash(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2015-03-18 06:01:58 +03:00
|
|
|
return rb_dbl_hash(RFLOAT_VALUE(num));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dbl_hash(double d)
|
|
|
|
{
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2009-08-03 09:15:54 +04:00
|
|
|
/* normalize -0.0 to 0.0 */
|
|
|
|
if (d == 0.0) d = 0.0;
|
2006-09-22 02:52:38 +04:00
|
|
|
hash = rb_memhash(&d, sizeof(d));
|
2010-01-14 07:07:00 +03:00
|
|
|
return LONG2FIX(hash);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-08-19 09:56:09 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_dbl_cmp(double a, double b)
|
2002-08-19 09:56:09 +04:00
|
|
|
{
|
2003-04-11 10:37:48 +04:00
|
|
|
if (isnan(a) || isnan(b)) return Qnil;
|
2002-08-19 09:56:09 +04:00
|
|
|
if (a == b) return INT2FIX(0);
|
|
|
|
if (a > b) return INT2FIX(1);
|
|
|
|
if (a < b) return INT2FIX(-1);
|
2002-12-19 12:20:20 +03:00
|
|
|
return Qnil;
|
2002-08-19 09:56:09 +04:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-02-23 07:35:38 +04:00
|
|
|
* float <=> real -> -1, 0, +1 or nil
|
|
|
|
*
|
|
|
|
* Returns -1, 0, +1 or nil depending on whether +float+ is less than, equal
|
|
|
|
* to, or greater than +real+. This is the basis for the tests in Comparable.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN <=> NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2013-02-23 07:35:38 +04:00
|
|
|
*
|
|
|
|
* +nil+ is returned if the two values are incomparable.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_cmp(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-07-27 11:27:48 +04:00
|
|
|
double a, b;
|
|
|
|
VALUE i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2009-06-19 12:19:14 +04:00
|
|
|
if (isnan(a)) return Qnil;
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
VALUE rel = rb_integer_float_cmp(y, x);
|
2012-07-16 10:02:21 +04:00
|
|
|
if (FIXNUM_P(rel))
|
|
|
|
return INT2FIX(-FIX2INT(rel));
|
|
|
|
return rel;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-07-25 00:37:31 +04:00
|
|
|
if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
|
|
|
|
if (RTEST(i)) {
|
|
|
|
int j = rb_cmpint(i, x, y);
|
|
|
|
j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
|
|
|
|
return INT2FIX(j);
|
|
|
|
}
|
2009-06-19 12:19:14 +04:00
|
|
|
if (a > 0.0) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
2014-02-28 06:04:28 +04:00
|
|
|
return rb_num_coerce_cmp(x, y, id_cmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-08-19 09:56:09 +04:00
|
|
|
return rb_dbl_cmp(a, b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float > real -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is greater than +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN > NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_gt(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
VALUE rel = rb_integer_float_cmp(y, x);
|
2012-07-16 10:02:21 +04:00
|
|
|
if (FIXNUM_P(rel))
|
|
|
|
return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
|
|
|
|
return Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(b)) return Qfalse;
|
|
|
|
#endif
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_relop(x, y, '>');
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a)) return Qfalse;
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
return (a > b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float >= real -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is greater than or equal to +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN >= NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_ge(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
VALUE rel = rb_integer_float_cmp(y, x);
|
2012-07-16 10:02:21 +04:00
|
|
|
if (FIXNUM_P(rel))
|
|
|
|
return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
|
|
|
|
return Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(b)) return Qfalse;
|
|
|
|
#endif
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_relop(x, y, idGE);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a)) return Qfalse;
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
return (a >= b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float < real -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is less than +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN < NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_lt(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
VALUE rel = rb_integer_float_cmp(y, x);
|
2012-07-16 10:02:21 +04:00
|
|
|
if (FIXNUM_P(rel))
|
|
|
|
return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
|
|
|
|
return Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(b)) return Qfalse;
|
|
|
|
#endif
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_relop(x, y, '<');
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a)) return Qfalse;
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
return (a < b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float <= real -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is less than or equal to +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN <= NaN</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_le(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
a = RFLOAT_VALUE(x);
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
VALUE rel = rb_integer_float_cmp(y, x);
|
2012-07-16 10:02:21 +04:00
|
|
|
if (FIXNUM_P(rel))
|
|
|
|
return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
|
|
|
|
return Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
b = RFLOAT_VALUE(y);
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(b)) return Qfalse;
|
|
|
|
#endif
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_relop(x, y, idLE);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2009-02-27 08:42:06 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a)) return Qfalse;
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
return (a <= b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.eql?(obj) -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ only if +obj+ is a Float with the same value as +float+.
|
|
|
|
* Contrast this with Float#==, which performs type conversions.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2012-11-08 04:34:55 +04:00
|
|
|
* The result of <code>NaN.eql?(NaN)</code> is undefined, so the
|
|
|
|
* implementation-dependent value is returned.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.0.eql?(1) #=> false
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_eql(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(y, T_FLOAT)) {
|
2009-02-27 08:42:06 +03:00
|
|
|
double a = RFLOAT_VALUE(x);
|
|
|
|
double b = RFLOAT_VALUE(y);
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
|
|
|
#endif
|
|
|
|
if (a == b)
|
2009-02-23 20:15:17 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.to_f -> self
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Since +float+ is already a float, returns +self+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_to_f(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-07-06 11:21:26 +04:00
|
|
|
return num;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.abs -> float
|
|
|
|
* float.magnitude -> float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the absolute value of +float+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* (-34.56).abs #=> 34.56
|
|
|
|
* -34.56.abs #=> 34.56
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_abs(VALUE flt)
|
2000-07-06 11:21:26 +04:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double val = fabs(RFLOAT_VALUE(flt));
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(val);
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.zero? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +float+ is 0.0.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_zero_p(VALUE num)
|
2000-07-06 11:21:26 +04:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
if (RFLOAT_VALUE(num) == 0.0) {
|
2000-07-06 11:21:26 +04:00
|
|
|
return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.nan? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +float+ is an invalid IEEE floating point number.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* a = -1.0 #=> -1.0
|
|
|
|
* a.nan? #=> false
|
|
|
|
* a = 0.0/0.0 #=> NaN
|
|
|
|
* a.nan? #=> true
|
|
|
|
*/
|
|
|
|
|
2002-06-04 11:34:19 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_is_nan_p(VALUE num)
|
2007-05-09 08:11:41 +04:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double value = RFLOAT_VALUE(num);
|
2000-07-06 11:21:26 +04:00
|
|
|
|
2003-04-11 10:37:48 +04:00
|
|
|
return isnan(value) ? Qtrue : Qfalse;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.infinite? -> nil, -1, +1
|
|
|
|
*
|
|
|
|
* Return values corresponding to the value of +float+:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* +finite+:: +nil+
|
|
|
|
* +-Infinity+:: +-1+
|
|
|
|
* ++Infinity+:: +1+
|
|
|
|
*
|
|
|
|
* For example:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* (0.0).infinite? #=> nil
|
|
|
|
* (-1.0/0.0).infinite? #=> -1
|
|
|
|
* (+1.0/0.0).infinite? #=> 1
|
|
|
|
*/
|
|
|
|
|
2002-06-04 11:34:19 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_is_infinite_p(VALUE num)
|
2007-05-09 08:11:41 +04:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double value = RFLOAT_VALUE(num);
|
2000-07-06 11:21:26 +04:00
|
|
|
|
2003-04-11 10:37:48 +04:00
|
|
|
if (isinf(value)) {
|
|
|
|
return INT2FIX( value < 0 ? -1 : 1 );
|
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
|
2003-04-11 10:37:48 +04:00
|
|
|
return Qnil;
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.finite? -> true or false
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +float+ is a valid IEEE floating point number (it is not
|
|
|
|
* infinite, and Float#nan? is +false+).
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2002-06-04 11:34:19 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
flo_is_finite_p(VALUE num)
|
2007-05-09 08:11:41 +04:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double value = RFLOAT_VALUE(num);
|
2000-07-06 11:21:26 +04:00
|
|
|
|
2014-09-04 12:50:31 +04:00
|
|
|
#ifdef HAVE_ISFINITE
|
2014-01-05 16:33:42 +04:00
|
|
|
if (!isfinite(value))
|
2003-04-21 16:26:08 +04:00
|
|
|
return Qfalse;
|
|
|
|
#else
|
2003-04-11 10:37:48 +04:00
|
|
|
if (isinf(value) || isnan(value))
|
|
|
|
return Qfalse;
|
2003-04-21 16:26:08 +04:00
|
|
|
#endif
|
|
|
|
|
2003-04-11 10:37:48 +04:00
|
|
|
return Qtrue;
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
|
|
|
|
2014-05-18 04:37:10 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* float.next_float -> float
|
|
|
|
*
|
|
|
|
* Returns the next representable floating-point number.
|
|
|
|
*
|
|
|
|
* Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY.
|
|
|
|
*
|
|
|
|
* Float::NAN.next_float is Float::NAN.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* p 0.01.next_float #=> 0.010000000000000002
|
|
|
|
* p 1.0.next_float #=> 1.0000000000000002
|
|
|
|
* p 100.0.next_float #=> 100.00000000000001
|
|
|
|
*
|
|
|
|
* p 0.01.next_float - 0.01 #=> 1.734723475976807e-18
|
|
|
|
* p 1.0.next_float - 1.0 #=> 2.220446049250313e-16
|
|
|
|
* p 100.0.next_float - 100.0 #=> 1.4210854715202004e-14
|
|
|
|
*
|
|
|
|
* f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
|
|
|
|
* #=> 0x1.47ae147ae147bp-7 0.01
|
|
|
|
* # 0x1.47ae147ae147cp-7 0.010000000000000002
|
|
|
|
* # 0x1.47ae147ae147dp-7 0.010000000000000004
|
|
|
|
* # 0x1.47ae147ae147ep-7 0.010000000000000005
|
|
|
|
* # 0x1.47ae147ae147fp-7 0.010000000000000007
|
|
|
|
* # 0x1.47ae147ae148p-7 0.010000000000000009
|
|
|
|
* # 0x1.47ae147ae1481p-7 0.01000000000000001
|
|
|
|
* # 0x1.47ae147ae1482p-7 0.010000000000000012
|
|
|
|
* # 0x1.47ae147ae1483p-7 0.010000000000000014
|
|
|
|
* # 0x1.47ae147ae1484p-7 0.010000000000000016
|
|
|
|
* # 0x1.47ae147ae1485p-7 0.010000000000000018
|
|
|
|
* # 0x1.47ae147ae1486p-7 0.01000000000000002
|
|
|
|
* # 0x1.47ae147ae1487p-7 0.010000000000000021
|
|
|
|
* # 0x1.47ae147ae1488p-7 0.010000000000000023
|
|
|
|
* # 0x1.47ae147ae1489p-7 0.010000000000000024
|
|
|
|
* # 0x1.47ae147ae148ap-7 0.010000000000000026
|
|
|
|
* # 0x1.47ae147ae148bp-7 0.010000000000000028
|
|
|
|
* # 0x1.47ae147ae148cp-7 0.01000000000000003
|
|
|
|
* # 0x1.47ae147ae148dp-7 0.010000000000000031
|
|
|
|
* # 0x1.47ae147ae148ep-7 0.010000000000000033
|
|
|
|
*
|
|
|
|
* f = 0.0
|
|
|
|
* 100.times { f += 0.1 }
|
|
|
|
* p f #=> 9.99999999999998 # should be 10.0 in the ideal world.
|
|
|
|
* p 10-f #=> 1.9539925233402755e-14 # the floating-point error.
|
|
|
|
* p(10.0.next_float-10) #=> 1.7763568394002505e-15 # 1 ulp (units in the last place).
|
|
|
|
* p((10-f)/(10.0.next_float-10)) #=> 11.0 # the error is 11 ulp.
|
|
|
|
* p((10-f)/(10*Float::EPSILON)) #=> 8.8 # approximation of the above.
|
|
|
|
* p "%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
flo_next_float(VALUE vx)
|
|
|
|
{
|
|
|
|
double x, y;
|
|
|
|
x = NUM2DBL(vx);
|
|
|
|
y = nextafter(x, INFINITY);
|
|
|
|
return DBL2NUM(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* float.prev_float -> float
|
|
|
|
*
|
2015-12-14 05:52:14 +03:00
|
|
|
* Returns the previous representable floating-point number.
|
2014-05-18 04:37:10 +04:00
|
|
|
*
|
|
|
|
* (-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
|
|
|
|
*
|
|
|
|
* Float::NAN.prev_float is Float::NAN.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* p 0.01.prev_float #=> 0.009999999999999998
|
|
|
|
* p 1.0.prev_float #=> 0.9999999999999999
|
|
|
|
* p 100.0.prev_float #=> 99.99999999999999
|
|
|
|
*
|
|
|
|
* p 0.01 - 0.01.prev_float #=> 1.734723475976807e-18
|
|
|
|
* p 1.0 - 1.0.prev_float #=> 1.1102230246251565e-16
|
|
|
|
* p 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14
|
|
|
|
*
|
|
|
|
* f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
|
|
|
|
* #=> 0x1.47ae147ae147bp-7 0.01
|
|
|
|
* # 0x1.47ae147ae147ap-7 0.009999999999999998
|
|
|
|
* # 0x1.47ae147ae1479p-7 0.009999999999999997
|
|
|
|
* # 0x1.47ae147ae1478p-7 0.009999999999999995
|
|
|
|
* # 0x1.47ae147ae1477p-7 0.009999999999999993
|
|
|
|
* # 0x1.47ae147ae1476p-7 0.009999999999999992
|
|
|
|
* # 0x1.47ae147ae1475p-7 0.00999999999999999
|
|
|
|
* # 0x1.47ae147ae1474p-7 0.009999999999999988
|
|
|
|
* # 0x1.47ae147ae1473p-7 0.009999999999999986
|
|
|
|
* # 0x1.47ae147ae1472p-7 0.009999999999999985
|
|
|
|
* # 0x1.47ae147ae1471p-7 0.009999999999999983
|
|
|
|
* # 0x1.47ae147ae147p-7 0.009999999999999981
|
|
|
|
* # 0x1.47ae147ae146fp-7 0.00999999999999998
|
|
|
|
* # 0x1.47ae147ae146ep-7 0.009999999999999978
|
|
|
|
* # 0x1.47ae147ae146dp-7 0.009999999999999976
|
|
|
|
* # 0x1.47ae147ae146cp-7 0.009999999999999974
|
|
|
|
* # 0x1.47ae147ae146bp-7 0.009999999999999972
|
|
|
|
* # 0x1.47ae147ae146ap-7 0.00999999999999997
|
|
|
|
* # 0x1.47ae147ae1469p-7 0.009999999999999969
|
|
|
|
* # 0x1.47ae147ae1468p-7 0.009999999999999967
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
flo_prev_float(VALUE vx)
|
|
|
|
{
|
|
|
|
double x, y;
|
|
|
|
x = NUM2DBL(vx);
|
|
|
|
y = nextafter(x, -INFINITY);
|
|
|
|
return DBL2NUM(y);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-04-13 09:54:38 +03:00
|
|
|
* float.floor([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-04-13 09:54:38 +03:00
|
|
|
* Returns the largest number less than or equal to +float+ in
|
|
|
|
* decimal digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and floor down for negative.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.2.floor #=> 1
|
|
|
|
* 2.0.floor #=> 2
|
|
|
|
* (-1.2).floor #=> -2
|
|
|
|
* (-2.0).floor #=> -2
|
2016-04-13 09:54:38 +03:00
|
|
|
*
|
|
|
|
* 1.234567.floor(2) #=> 1.23
|
|
|
|
* 1.234567.floor(3) #=> 1.234
|
|
|
|
* 1.234567.floor(4) #=> 1.2345
|
|
|
|
* 1.234567.floor(5) #=> 1.23456
|
|
|
|
*
|
|
|
|
* 34567.89.floor(-5) #=> 0
|
|
|
|
* 34567.89.floor(-4) #=> 30000
|
|
|
|
* 34567.89.floor(-3) #=> 34000
|
|
|
|
* 34567.89.floor(-2) #=> 34500
|
|
|
|
* 34567.89.floor(-1) #=> 34560
|
|
|
|
* 34567.89.floor(0) #=> 34567
|
|
|
|
* 34567.89.floor(1) #=> 34567.8
|
|
|
|
* 34567.89.floor(2) #=> 34567.89
|
|
|
|
* 34567.89.floor(3) #=> 34567.89
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2016-04-13 09:54:38 +03:00
|
|
|
flo_floor(int argc, VALUE *argv, VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2016-04-13 09:54:38 +03:00
|
|
|
double number, f;
|
1999-01-20 07:59:39 +03:00
|
|
|
long val;
|
2016-04-13 09:54:38 +03:00
|
|
|
int ndigits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-04-13 09:54:38 +03:00
|
|
|
if (rb_check_arity(argc, 0, 1)) {
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
|
|
|
}
|
|
|
|
if (ndigits < 0) {
|
2016-04-18 06:56:33 +03:00
|
|
|
return rb_int_floor(flo_to_i(num), ndigits);
|
2016-04-13 09:54:38 +03:00
|
|
|
}
|
|
|
|
number = RFLOAT_VALUE(num);
|
|
|
|
if (ndigits > 0) {
|
|
|
|
if (float_invariant_round(number, ndigits, &num)) return num;
|
|
|
|
f = pow(10, ndigits);
|
|
|
|
f = floor(number * f) / f;
|
|
|
|
return DBL2NUM(f);
|
|
|
|
}
|
|
|
|
f = floor(number);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!FIXABLE(f)) {
|
|
|
|
return rb_dbl2big(f);
|
|
|
|
}
|
2009-03-10 08:43:14 +03:00
|
|
|
val = (long)f;
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2FIX(val);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-04-13 09:56:36 +03:00
|
|
|
* float.ceil([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-04-13 09:56:36 +03:00
|
|
|
* Returns the smallest number greater than or equal to +float+ in decimal
|
|
|
|
* digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and ceil up for negative.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.2.ceil #=> 2
|
|
|
|
* 2.0.ceil #=> 2
|
|
|
|
* (-1.2).ceil #=> -1
|
|
|
|
* (-2.0).ceil #=> -2
|
2016-04-13 09:56:36 +03:00
|
|
|
* 1.234567.ceil(2) #=> 1.24
|
|
|
|
* 1.234567.ceil(3) #=> 1.235
|
|
|
|
* 1.234567.ceil(4) #=> 1.2346
|
|
|
|
* 1.234567.ceil(5) #=> 1.23457
|
|
|
|
*
|
|
|
|
* 34567.89.ceil(-5) #=> 100000
|
|
|
|
* 34567.89.ceil(-4) #=> 40000
|
|
|
|
* 34567.89.ceil(-3) #=> 35000
|
|
|
|
* 34567.89.ceil(-2) #=> 34600
|
|
|
|
* 34567.89.ceil(-1) #=> 34570
|
|
|
|
* 34567.89.ceil(0) #=> 34568
|
|
|
|
* 34567.89.ceil(1) #=> 34567.9
|
|
|
|
* 34567.89.ceil(2) #=> 34567.89
|
|
|
|
* 34567.89.ceil(3) #=> 34567.89
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2016-04-13 09:56:36 +03:00
|
|
|
flo_ceil(int argc, VALUE *argv, VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2016-04-13 09:56:36 +03:00
|
|
|
double number, f;
|
|
|
|
int ndigits = 0;
|
|
|
|
|
|
|
|
if (rb_check_arity(argc, 0, 1)) {
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
|
|
|
}
|
|
|
|
number = RFLOAT_VALUE(num);
|
|
|
|
if (ndigits < 0) {
|
|
|
|
return rb_int_ceil(dbl2ival(ceil(number)), ndigits);
|
|
|
|
}
|
|
|
|
if (ndigits == 0) {
|
|
|
|
return dbl2ival(ceil(number));
|
|
|
|
}
|
|
|
|
if (float_invariant_round(number, ndigits, &num)) return num;
|
|
|
|
f = pow(10, ndigits);
|
|
|
|
return DBL2NUM(ceil(number * f) / f);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 08:12:01 +03:00
|
|
|
static int
|
|
|
|
int_round_zero_p(VALUE num, int ndigits)
|
|
|
|
{
|
|
|
|
long bytes;
|
|
|
|
/* If 10**N / 2 > num, then return 0 */
|
|
|
|
/* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
bytes = sizeof(long);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
bytes = rb_big_size(num);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bytes = NUM2LONG(rb_funcall(num, idSize, 0));
|
|
|
|
}
|
|
|
|
return (-0.415241 * ndigits - 0.125 > bytes);
|
|
|
|
}
|
|
|
|
|
2011-09-05 00:14:00 +04:00
|
|
|
/*
|
|
|
|
* Assumes num is an Integer, ndigits <= 0
|
|
|
|
*/
|
2016-03-26 04:54:50 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_round(VALUE num, int ndigits)
|
2011-09-05 00:14:00 +04:00
|
|
|
{
|
|
|
|
VALUE n, f, h, r;
|
2016-04-13 08:12:01 +03:00
|
|
|
|
|
|
|
if (int_round_zero_p(num, ndigits)) {
|
2011-09-05 00:14:00 +04:00
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
f = int_pow(10, -ndigits);
|
|
|
|
if (FIXNUM_P(num) && FIXNUM_P(f)) {
|
|
|
|
SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
|
|
|
|
int neg = x < 0;
|
|
|
|
if (neg) x = -x;
|
|
|
|
x = (x + y / 2) / y * y;
|
|
|
|
if (neg) x = -x;
|
|
|
|
return LONG2NUM(x);
|
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(f, T_FLOAT)) {
|
2011-09-05 00:14:00 +04:00
|
|
|
/* then int_pow overflow */
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
2016-03-26 04:54:50 +03:00
|
|
|
h = rb_int_idiv(f, INT2FIX(2));
|
|
|
|
r = rb_int_modulo(num, f);
|
|
|
|
n = rb_int_minus(num, r);
|
|
|
|
r = int_cmp(r, h);
|
|
|
|
if (FIXNUM_POSITIVE_P(r) || (FIXNUM_ZERO_P(r) && !int_neg_p(num))) {
|
|
|
|
n = rb_int_plus(n, f);
|
2011-09-05 00:14:00 +04:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-04-13 09:47:55 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_floor(VALUE num, int ndigits)
|
|
|
|
{
|
|
|
|
VALUE f;
|
|
|
|
|
|
|
|
if (int_round_zero_p(num, ndigits))
|
|
|
|
return INT2FIX(0);
|
|
|
|
f = int_pow(10, -ndigits);
|
|
|
|
if (FIXNUM_P(num) && FIXNUM_P(f)) {
|
|
|
|
SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
|
|
|
|
int neg = x < 0;
|
|
|
|
if (neg) x = -x + y - 1;
|
|
|
|
x = x / y * y;
|
|
|
|
if (neg) x = -x;
|
|
|
|
return LONG2NUM(x);
|
|
|
|
}
|
|
|
|
if (RB_TYPE_P(f, T_FLOAT)) {
|
|
|
|
/* then int_pow overflow */
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
return rb_int_minus(num, rb_int_modulo(num, f));
|
|
|
|
}
|
2011-09-05 00:14:29 +04:00
|
|
|
|
2016-04-13 09:50:24 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_ceil(VALUE num, int ndigits)
|
|
|
|
{
|
|
|
|
VALUE f;
|
|
|
|
|
|
|
|
if (int_round_zero_p(num, ndigits))
|
|
|
|
return INT2FIX(0);
|
|
|
|
f = int_pow(10, -ndigits);
|
|
|
|
if (FIXNUM_P(num) && FIXNUM_P(f)) {
|
|
|
|
SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
|
|
|
|
int neg = x < 0;
|
|
|
|
if (neg) x = -x;
|
|
|
|
else x += y - 1;
|
|
|
|
x = (x / y) * y;
|
|
|
|
if (neg) x = -x;
|
|
|
|
return LONG2NUM(x);
|
|
|
|
}
|
|
|
|
if (RB_TYPE_P(f, T_FLOAT)) {
|
|
|
|
/* then int_pow overflow */
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
return rb_int_plus(num, rb_int_minus(f, rb_int_modulo(num, f)));
|
|
|
|
}
|
|
|
|
|
2016-04-18 06:55:33 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_truncate(VALUE num, int ndigits)
|
|
|
|
{
|
|
|
|
VALUE f;
|
|
|
|
VALUE m;
|
|
|
|
|
|
|
|
if (int_round_zero_p(num, ndigits))
|
|
|
|
return INT2FIX(0);
|
|
|
|
f = int_pow(10, -ndigits);
|
|
|
|
if (FIXNUM_P(num) && FIXNUM_P(f)) {
|
|
|
|
SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
|
|
|
|
int neg = x < 0;
|
|
|
|
if (neg) x = -x;
|
|
|
|
x = x / y * y;
|
|
|
|
if (neg) x = -x;
|
|
|
|
return LONG2NUM(x);
|
|
|
|
}
|
|
|
|
if (RB_TYPE_P(f, T_FLOAT)) {
|
|
|
|
/* then int_pow overflow */
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
m = rb_int_modulo(num, f);
|
|
|
|
if (int_neg_p(num)) {
|
|
|
|
return rb_int_plus(num, rb_int_minus(f, m));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_int_minus(num, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.round([ndigits]) -> integer or float
|
|
|
|
*
|
|
|
|
* Rounds +float+ to a given precision in decimal digits (default 0 digits).
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
2010-02-11 16:59:34 +03:00
|
|
|
* is more than zero.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2010-02-11 16:30:55 +03:00
|
|
|
* 1.4.round #=> 1
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.5.round #=> 2
|
2010-02-11 16:30:55 +03:00
|
|
|
* 1.6.round #=> 2
|
2003-12-23 19:21:17 +03:00
|
|
|
* (-1.5).round #=> -2
|
2010-02-11 16:30:55 +03:00
|
|
|
*
|
|
|
|
* 1.234567.round(2) #=> 1.23
|
|
|
|
* 1.234567.round(3) #=> 1.235
|
|
|
|
* 1.234567.round(4) #=> 1.2346
|
|
|
|
* 1.234567.round(5) #=> 1.23457
|
|
|
|
*
|
|
|
|
* 34567.89.round(-5) #=> 0
|
|
|
|
* 34567.89.round(-4) #=> 30000
|
|
|
|
* 34567.89.round(-3) #=> 35000
|
|
|
|
* 34567.89.round(-2) #=> 34600
|
|
|
|
* 34567.89.round(-1) #=> 34570
|
|
|
|
* 34567.89.round(0) #=> 34568
|
|
|
|
* 34567.89.round(1) #=> 34567.9
|
|
|
|
* 34567.89.round(2) #=> 34567.89
|
|
|
|
* 34567.89.round(3) #=> 34567.89
|
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2007-05-31 21:01:15 +04:00
|
|
|
flo_round(int argc, VALUE *argv, VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2007-05-31 21:01:15 +04:00
|
|
|
double number, f;
|
2011-03-22 04:08:49 +03:00
|
|
|
int ndigits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-04-02 09:33:26 +03:00
|
|
|
if (rb_check_arity(argc, 0, 1)) {
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
2007-05-31 21:01:15 +04:00
|
|
|
}
|
2011-09-05 00:14:29 +04:00
|
|
|
if (ndigits < 0) {
|
2016-04-18 06:56:33 +03:00
|
|
|
return rb_int_round(flo_to_i(num), ndigits);
|
2011-09-05 00:14:29 +04:00
|
|
|
}
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
number = RFLOAT_VALUE(num);
|
2011-09-05 00:14:29 +04:00
|
|
|
if (ndigits == 0) {
|
2016-04-03 03:34:31 +03:00
|
|
|
return dbl2ival(round(number));
|
2011-09-05 00:14:29 +04:00
|
|
|
}
|
2016-04-13 05:41:24 +03:00
|
|
|
if (float_invariant_round(number, ndigits, &num)) return num;
|
|
|
|
f = pow(10, ndigits);
|
|
|
|
return DBL2NUM(round(number * f) / f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
float_invariant_round(double number, int ndigits, VALUE *num)
|
|
|
|
{
|
|
|
|
enum {float_dig = DBL_DIG+2};
|
|
|
|
int binexp;
|
|
|
|
|
2011-09-01 20:07:16 +04:00
|
|
|
frexp(number, &binexp);
|
2011-08-31 08:13:00 +04:00
|
|
|
|
|
|
|
/* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
|
|
|
|
i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp
|
2011-09-01 20:07:16 +04:00
|
|
|
Recall that up to float_dig digits can be needed to represent a double,
|
|
|
|
so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
|
2011-08-31 08:13:00 +04:00
|
|
|
will be an integer and thus the result is the original number.
|
|
|
|
If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
|
|
|
|
if ndigits + exp < 0, the result is 0.
|
|
|
|
We have:
|
|
|
|
2 ** (binexp-1) <= |number| < 2 ** binexp
|
|
|
|
10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
|
|
|
|
If binexp >= 0, and since log_2(10) = 3.322259:
|
|
|
|
10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
|
2011-09-06 01:44:42 +04:00
|
|
|
floor(binexp/4) <= exp <= ceil(binexp/3)
|
2011-08-31 08:13:00 +04:00
|
|
|
If binexp <= 0, swap the /4 and the /3
|
2011-09-06 01:44:42 +04:00
|
|
|
So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
|
|
|
|
If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
|
2011-08-31 08:13:00 +04:00
|
|
|
*/
|
2011-09-05 00:14:29 +04:00
|
|
|
if (isinf(number) || isnan(number) ||
|
2011-09-06 01:44:42 +04:00
|
|
|
(ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
|
2016-04-13 05:41:24 +03:00
|
|
|
return TRUE;
|
2011-08-31 08:13:00 +04:00
|
|
|
}
|
2011-09-06 01:44:42 +04:00
|
|
|
if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
|
2016-04-13 05:41:24 +03:00
|
|
|
*num = DBL2NUM(0);
|
|
|
|
return TRUE;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2016-04-13 05:41:24 +03:00
|
|
|
return FALSE;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-07-15 22:27:23 +04:00
|
|
|
* float.to_i -> integer
|
|
|
|
* float.to_int -> integer
|
|
|
|
*
|
|
|
|
* Returns the +float+ truncated to an Integer.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-04-18 06:56:33 +03:00
|
|
|
* Synonyms are #to_i and #to_int
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-04-18 06:56:33 +03:00
|
|
|
flo_to_i(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
double f = RFLOAT_VALUE(num);
|
2000-07-06 11:21:26 +04:00
|
|
|
long val;
|
|
|
|
|
|
|
|
if (f > 0.0) f = floor(f);
|
|
|
|
if (f < 0.0) f = ceil(f);
|
|
|
|
|
|
|
|
if (!FIXABLE(f)) {
|
|
|
|
return rb_dbl2big(f);
|
|
|
|
}
|
2009-03-10 08:43:14 +03:00
|
|
|
val = (long)f;
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2FIX(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-18 06:56:33 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* float.truncate([ndigits]) -> integer or float
|
|
|
|
*
|
|
|
|
* Truncates +float+ to a given precision in decimal digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is more than zero.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
flo_truncate(int argc, VALUE *argv, VALUE num)
|
|
|
|
{
|
|
|
|
if (signbit(RFLOAT_VALUE(num)))
|
|
|
|
return flo_ceil(argc, argv, num);
|
|
|
|
else
|
|
|
|
return flo_floor(argc, argv, num);
|
|
|
|
}
|
|
|
|
|
2015-05-17 09:01:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* float.positive? -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is greater than 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_positive_p(VALUE num)
|
|
|
|
{
|
|
|
|
double f = RFLOAT_VALUE(num);
|
|
|
|
return f > 0.0 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* float.negative? -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +float+ is less than 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_negative_p(VALUE num)
|
|
|
|
{
|
|
|
|
double f = RFLOAT_VALUE(num);
|
|
|
|
return f < 0.0 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-04-18 06:57:34 +03:00
|
|
|
* num.floor([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the largest integer less than or equal to +num+.
|
|
|
|
*
|
|
|
|
* Numeric implements this by converting an Integer to a Float and invoking
|
|
|
|
* Float#floor.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1.floor #=> 1
|
|
|
|
* (-1).floor #=> -1
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-04-18 06:57:34 +03:00
|
|
|
num_floor(int argc, VALUE *argv, VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-04-18 06:57:34 +03:00
|
|
|
return flo_floor(argc, argv, rb_Float(num));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-04-18 06:57:34 +03:00
|
|
|
* num.ceil([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the smallest possible Integer that is greater than or equal to
|
|
|
|
* +num+.
|
|
|
|
*
|
|
|
|
* Numeric achieves this by converting itself to a Float then invoking
|
|
|
|
* Float#ceil.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1.ceil #=> 1
|
|
|
|
* 1.2.ceil #=> 2
|
|
|
|
* (-1.2).ceil #=> -1
|
|
|
|
* (-1.0).ceil #=> -1
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2016-04-18 06:57:34 +03:00
|
|
|
num_ceil(int argc, VALUE *argv, VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2016-04-18 06:57:34 +03:00
|
|
|
return flo_ceil(argc, argv, rb_Float(num));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* num.round([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Rounds +num+ to a given precision in decimal digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is more than zero.
|
|
|
|
*
|
|
|
|
* Numeric implements this by converting itself to a Float and invoking
|
|
|
|
* Float#round.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
2007-05-31 21:01:15 +04:00
|
|
|
num_round(int argc, VALUE* argv, VALUE num)
|
2000-07-06 11:21:26 +04:00
|
|
|
{
|
2007-06-01 06:32:17 +04:00
|
|
|
return flo_round(argc, argv, rb_Float(num));
|
2000-06-12 11:48:31 +04:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-04-18 06:57:34 +03:00
|
|
|
* num.truncate([ndigits]) -> integer or float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +num+ truncated to an Integer.
|
|
|
|
*
|
|
|
|
* Numeric implements this by converting its value to a Float and invoking
|
|
|
|
* Float#truncate.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
2016-04-18 06:57:34 +03:00
|
|
|
num_truncate(int argc, VALUE *argv, VALUE num)
|
2000-07-06 11:21:26 +04:00
|
|
|
{
|
2016-04-18 06:57:34 +03:00
|
|
|
return flo_truncate(argc, argv, rb_Float(num));
|
2000-06-12 11:48:31 +04:00
|
|
|
}
|
|
|
|
|
2012-11-06 21:14:16 +04:00
|
|
|
static double
|
2012-11-07 02:50:30 +04:00
|
|
|
ruby_float_step_size(double beg, double end, double unit, int excl)
|
|
|
|
{
|
2012-11-06 21:14:16 +04:00
|
|
|
const double epsilon = DBL_EPSILON;
|
|
|
|
double n = (end - beg)/unit;
|
|
|
|
double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
|
|
|
|
|
|
|
|
if (isinf(unit)) {
|
|
|
|
return unit > 0 ? beg <= end : beg >= end;
|
|
|
|
}
|
2014-02-28 06:04:59 +04:00
|
|
|
if (unit == 0) {
|
|
|
|
return INFINITY;
|
|
|
|
}
|
2012-11-06 21:14:16 +04:00
|
|
|
if (err>0.5) err=0.5;
|
|
|
|
if (excl) {
|
|
|
|
if (n<=0) return 0;
|
|
|
|
if (n<1)
|
|
|
|
n = 0;
|
|
|
|
else
|
|
|
|
n = floor(n - err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (n<0) return 0;
|
|
|
|
n = floor(n + err);
|
|
|
|
}
|
|
|
|
return n+1;
|
|
|
|
}
|
2003-12-27 08:46:46 +03:00
|
|
|
|
2009-01-04 05:58:45 +03:00
|
|
|
int
|
|
|
|
ruby_float_step(VALUE from, VALUE to, VALUE step, int excl)
|
|
|
|
{
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
|
2009-01-04 05:58:45 +03:00
|
|
|
double beg = NUM2DBL(from);
|
|
|
|
double end = NUM2DBL(to);
|
|
|
|
double unit = NUM2DBL(step);
|
2012-11-06 21:14:16 +04:00
|
|
|
double n = ruby_float_step_size(beg, end, unit, excl);
|
2009-01-04 05:58:45 +03:00
|
|
|
long i;
|
|
|
|
|
2012-11-07 11:03:50 +04:00
|
|
|
if (isinf(unit)) {
|
|
|
|
/* if unit is infinity, i*unit+beg is NaN */
|
|
|
|
if (n) rb_yield(DBL2NUM(beg));
|
|
|
|
}
|
2014-02-28 06:04:59 +04:00
|
|
|
else if (unit == 0) {
|
|
|
|
VALUE val = DBL2NUM(beg);
|
|
|
|
for (;;)
|
|
|
|
rb_yield(val);
|
|
|
|
}
|
2012-11-07 11:03:50 +04:00
|
|
|
else {
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
double d = i*unit+beg;
|
|
|
|
if (unit >= 0 ? end < d : d < end) d = end;
|
|
|
|
rb_yield(DBL2NUM(d));
|
|
|
|
}
|
2009-01-04 05:58:45 +03:00
|
|
|
}
|
2009-07-18 12:05:32 +04:00
|
|
|
return TRUE;
|
2009-01-04 05:58:45 +03:00
|
|
|
}
|
2009-07-18 12:05:32 +04:00
|
|
|
return FALSE;
|
2009-01-04 05:58:45 +03:00
|
|
|
}
|
|
|
|
|
2012-11-06 21:14:31 +04:00
|
|
|
VALUE
|
2013-03-06 10:30:03 +04:00
|
|
|
ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
|
2012-11-07 02:50:30 +04:00
|
|
|
{
|
2012-11-06 21:14:31 +04:00
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
|
2014-02-28 06:03:30 +04:00
|
|
|
long delta, diff;
|
2012-11-06 21:14:31 +04:00
|
|
|
|
|
|
|
diff = FIX2LONG(step);
|
2014-02-28 06:04:59 +04:00
|
|
|
if (diff == 0) {
|
|
|
|
return DBL2NUM(INFINITY);
|
|
|
|
}
|
2012-11-06 21:14:31 +04:00
|
|
|
delta = FIX2LONG(to) - FIX2LONG(from);
|
2014-02-28 06:03:30 +04:00
|
|
|
if (diff < 0) {
|
|
|
|
diff = -diff;
|
|
|
|
delta = -delta;
|
|
|
|
}
|
2012-11-06 21:14:31 +04:00
|
|
|
if (excl) {
|
2014-02-28 06:03:30 +04:00
|
|
|
delta--;
|
2012-11-06 21:14:31 +04:00
|
|
|
}
|
2014-02-28 06:03:30 +04:00
|
|
|
if (delta < 0) {
|
|
|
|
return INT2FIX(0);
|
2014-02-27 07:10:12 +04:00
|
|
|
}
|
2014-02-28 08:59:49 +04:00
|
|
|
return ULONG2NUM(delta / diff + 1UL);
|
2012-11-06 21:14:31 +04:00
|
|
|
}
|
2012-12-29 16:22:01 +04:00
|
|
|
else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
|
2012-11-06 21:14:31 +04:00
|
|
|
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
|
|
|
|
|
|
|
|
if (isinf(n)) return DBL2NUM(n);
|
2014-02-28 09:11:44 +04:00
|
|
|
if (POSFIXABLE(n)) return LONG2FIX(n);
|
|
|
|
return rb_dbl2big(n);
|
2012-11-06 21:14:31 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE result;
|
2014-02-28 06:04:59 +04:00
|
|
|
ID cmp = '>';
|
|
|
|
switch (rb_cmpint(rb_num_coerce_cmp(step, INT2FIX(0), id_cmp), step, INT2FIX(0))) {
|
2014-05-07 11:34:33 +04:00
|
|
|
case 0: return DBL2NUM(INFINITY);
|
|
|
|
case -1: cmp = '<'; break;
|
2014-02-28 06:04:59 +04:00
|
|
|
}
|
2012-11-06 21:14:31 +04:00
|
|
|
if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
|
|
|
|
result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step);
|
|
|
|
if (!excl || RTEST(rb_funcall(rb_funcall(from, '+', 1, rb_funcall(result, '*', 1, step)), cmp, 1, to))) {
|
|
|
|
result = rb_funcall(result, '+', 1, INT2FIX(1));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 08:41:37 +03:00
|
|
|
static VALUE
|
|
|
|
num_step_compare_with_zero(VALUE num)
|
|
|
|
{
|
|
|
|
VALUE zero = INT2FIX(0);
|
|
|
|
return rb_check_funcall(num, '>', 1, &zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
num_step_negative_p(VALUE num)
|
|
|
|
{
|
|
|
|
const ID mid = '<';
|
|
|
|
VALUE r;
|
|
|
|
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (method_basic_p(rb_cFixnum))
|
|
|
|
return (SIGNED_VALUE)num < 0;
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
if (method_basic_p(rb_cBignum))
|
|
|
|
return BIGNUM_NEGATIVE_P(num);
|
|
|
|
}
|
|
|
|
r = rb_rescue(num_step_compare_with_zero, num, coerce_rescue_quiet, Qnil);
|
|
|
|
if (r == Qundef) {
|
|
|
|
coerce_failed(num, INT2FIX(0));
|
|
|
|
}
|
|
|
|
return !RTEST(r);
|
|
|
|
}
|
|
|
|
|
2014-05-07 12:05:00 +04:00
|
|
|
static int
|
|
|
|
num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step)
|
|
|
|
{
|
|
|
|
VALUE hash;
|
|
|
|
int desc;
|
|
|
|
|
|
|
|
argc = rb_scan_args(argc, argv, "02:", to, step, &hash);
|
|
|
|
if (!NIL_P(hash)) {
|
2014-05-07 12:24:09 +04:00
|
|
|
ID keys[2];
|
|
|
|
VALUE values[2];
|
2014-07-01 18:36:03 +04:00
|
|
|
keys[0] = id_to;
|
|
|
|
keys[1] = id_by;
|
2014-05-07 12:24:09 +04:00
|
|
|
rb_get_kwargs(hash, keys, 0, 2, values);
|
|
|
|
if (values[0] != Qundef) {
|
|
|
|
if (argc > 0) rb_raise(rb_eArgError, "to is given twice");
|
|
|
|
*to = values[0];
|
|
|
|
}
|
|
|
|
if (values[1] != Qundef) {
|
|
|
|
if (argc > 1) rb_raise(rb_eArgError, "step is given twice");
|
|
|
|
*step = values[1];
|
|
|
|
}
|
2014-05-07 12:05:00 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* compatibility */
|
|
|
|
if (argc > 1 && NIL_P(*step)) {
|
|
|
|
rb_raise(rb_eTypeError, "step must be numeric");
|
|
|
|
}
|
|
|
|
if (rb_equal(*step, INT2FIX(0))) {
|
|
|
|
rb_raise(rb_eArgError, "step can't be 0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NIL_P(*step)) {
|
|
|
|
*step = INT2FIX(1);
|
|
|
|
}
|
2016-02-26 08:41:37 +03:00
|
|
|
desc = num_step_negative_p(*step);
|
2014-05-07 12:05:00 +04:00
|
|
|
if (NIL_P(*to)) {
|
|
|
|
*to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
|
|
|
|
}
|
|
|
|
return desc;
|
|
|
|
}
|
2013-09-04 17:57:02 +04:00
|
|
|
|
2012-11-06 21:14:31 +04:00
|
|
|
static VALUE
|
2013-06-26 17:43:22 +04:00
|
|
|
num_step_size(VALUE from, VALUE args, VALUE eobj)
|
2012-11-07 02:50:30 +04:00
|
|
|
{
|
2014-05-07 12:05:00 +04:00
|
|
|
VALUE to, step;
|
2013-09-02 18:56:06 +04:00
|
|
|
int argc = args ? RARRAY_LENINT(args) : 0;
|
2014-03-17 08:20:16 +04:00
|
|
|
const VALUE *argv = args ? RARRAY_CONST_PTR(args) : 0;
|
2013-09-02 18:56:06 +04:00
|
|
|
|
2014-05-07 12:05:00 +04:00
|
|
|
num_step_scan_args(argc, argv, &to, &step);
|
2013-09-02 18:56:06 +04:00
|
|
|
|
2013-03-06 10:30:03 +04:00
|
|
|
return ruby_num_interval_step_size(from, to, step, FALSE);
|
2012-11-06 21:14:31 +04:00
|
|
|
}
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-02-01 01:05:52 +04:00
|
|
|
* num.step(by: step, to: limit) {|i| block } -> self
|
|
|
|
* num.step(by: step, to: limit) -> an_enumerator
|
2013-09-02 18:56:06 +04:00
|
|
|
* num.step(limit=nil, step=1) {|i| block } -> self
|
|
|
|
* num.step(limit=nil, step=1) -> an_enumerator
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Invokes the given block with the sequence of numbers starting at +num+,
|
|
|
|
* incremented by +step+ (defaulted to +1+) on each call.
|
|
|
|
*
|
|
|
|
* The loop finishes when the value to be passed to the block is greater than
|
|
|
|
* +limit+ (if +step+ is positive) or less than +limit+ (if +step+ is
|
2013-09-02 18:56:06 +04:00
|
|
|
* negative), where <i>limit</i> is defaulted to infinity.
|
|
|
|
*
|
|
|
|
* In the recommended keyword argument style, either or both of
|
|
|
|
* +step+ and +limit+ (default infinity) can be omitted. In the
|
2014-02-28 06:19:21 +04:00
|
|
|
* fixed position argument style, zero as a step
|
2013-09-02 18:56:06 +04:00
|
|
|
* (i.e. num.step(limit, 0)) is not allowed for historical
|
|
|
|
* compatibility reasons.
|
2013-07-15 22:27:23 +04:00
|
|
|
*
|
|
|
|
* If all the arguments are integers, the loop operates using an integer
|
|
|
|
* counter.
|
|
|
|
*
|
|
|
|
* If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed the following expression:
|
|
|
|
*
|
|
|
|
* floor(n + n*epsilon)+ 1
|
|
|
|
*
|
|
|
|
* Where the +n+ is the following:
|
|
|
|
*
|
|
|
|
* n = (limit - num)/step
|
|
|
|
*
|
|
|
|
* Otherwise, the loop starts at +num+, uses either the less-than (<) or
|
|
|
|
* greater-than (>) operator to compare the counter against +limit+, and
|
|
|
|
* increments itself using the <code>+</code> operator.
|
|
|
|
*
|
|
|
|
* If no block is given, an Enumerator is returned instead.
|
|
|
|
*
|
|
|
|
* For example:
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2013-09-02 18:56:06 +04:00
|
|
|
* p 1.step.take(4)
|
|
|
|
* p 10.step(by: -1).take(4)
|
|
|
|
* 3.step(to: 5) { |i| print i, " " }
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1.step(10, 2) { |i| print i, " " }
|
2013-09-02 18:56:06 +04:00
|
|
|
* Math::E.step(to: Math::PI, by: 0.2) { |f| print f, " " }
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Will produce:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-09-02 18:56:06 +04:00
|
|
|
* [1, 2, 3, 4]
|
|
|
|
* [10, 9, 8, 7]
|
|
|
|
* 3 4 5
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1 3 5 7 9
|
|
|
|
* 2.71828182845905 2.91828182845905 3.11828182845905
|
|
|
|
*/
|
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
num_step(int argc, VALUE *argv, VALUE from)
|
2002-04-24 08:54:16 +04:00
|
|
|
{
|
2014-05-07 12:05:00 +04:00
|
|
|
VALUE to, step;
|
2013-09-02 18:56:06 +04:00
|
|
|
int desc, inf;
|
2002-04-24 08:54:16 +04:00
|
|
|
|
2012-11-06 21:14:31 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size);
|
2002-04-24 08:54:16 +04:00
|
|
|
|
2014-05-07 12:05:00 +04:00
|
|
|
desc = num_step_scan_args(argc, argv, &to, &step);
|
2016-03-17 18:18:59 +03:00
|
|
|
if (rb_equal(step, INT2FIX(0))) {
|
2014-02-28 06:04:59 +04:00
|
|
|
inf = 1;
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(to, T_FLOAT)) {
|
2014-02-28 06:04:43 +04:00
|
|
|
double f = RFLOAT_VALUE(to);
|
|
|
|
inf = isinf(f) && (signbit(f) ? desc : !desc);
|
|
|
|
}
|
|
|
|
else inf = 0;
|
2002-04-24 08:54:16 +04:00
|
|
|
|
2013-09-02 18:56:06 +04:00
|
|
|
if (FIXNUM_P(from) && (inf || FIXNUM_P(to)) && FIXNUM_P(step)) {
|
|
|
|
long i = FIX2LONG(from);
|
|
|
|
long diff = FIX2LONG(step);
|
2002-04-24 08:54:16 +04:00
|
|
|
|
2013-09-02 18:56:06 +04:00
|
|
|
if (inf) {
|
|
|
|
for (;; i += diff)
|
2002-08-21 19:47:54 +04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 08:54:16 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-09-02 18:56:06 +04:00
|
|
|
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));
|
2002-04-24 08:54:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-18 12:05:32 +04:00
|
|
|
else if (!ruby_float_step(from, to, step, FALSE)) {
|
2002-04-24 09:08:04 +04:00
|
|
|
VALUE i = from;
|
|
|
|
|
2013-09-02 18:56:06 +04:00
|
|
|
if (inf) {
|
|
|
|
for (;; i = rb_funcall(i, '+', 1, step))
|
|
|
|
rb_yield(i);
|
2002-04-24 09:08:04 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-09-02 18:56:06 +04:00
|
|
|
ID cmp = desc ? '<' : '>';
|
|
|
|
|
|
|
|
for (; !RTEST(rb_funcall(i, cmp, 1, to)); i = rb_funcall(i, '+', 1, step))
|
|
|
|
rb_yield(i);
|
2002-04-24 09:08:04 +04:00
|
|
|
}
|
2002-04-24 08:54:16 +04:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2014-01-15 12:16:38 +04:00
|
|
|
static char *
|
|
|
|
out_of_range_float(char (*pbuf)[24], VALUE val)
|
|
|
|
{
|
|
|
|
char *const buf = *pbuf;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(*pbuf), "%-.10g", RFLOAT_VALUE(val));
|
|
|
|
if ((s = strchr(buf, ' ')) != 0) *s = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FLOAT_OUT_OF_RANGE(val, type) do { \
|
|
|
|
char buf[24]; \
|
|
|
|
rb_raise(rb_eRangeError, "float %s out of range of "type, \
|
|
|
|
out_of_range_float(&buf, (val))); \
|
|
|
|
} while (0)
|
|
|
|
|
2010-07-02 00:39:32 +04:00
|
|
|
#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
|
|
|
|
#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
|
|
|
|
#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
|
2013-03-27 17:12:27 +04:00
|
|
|
#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
|
|
|
|
(LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
|
|
|
|
LONG_MIN <= (n): \
|
|
|
|
LONG_MIN_MINUS_ONE < (n))
|
2010-07-02 00:39:32 +04:00
|
|
|
|
2014-04-18 19:46:32 +04:00
|
|
|
long
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num2long(VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-03-11 13:48:12 +03:00
|
|
|
again:
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(val)) {
|
2002-07-29 10:14:10 +04:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(val)) return FIX2LONG(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(val, T_FLOAT)) {
|
2010-07-02 00:39:32 +04:00
|
|
|
if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE
|
2013-03-27 17:12:27 +04:00
|
|
|
&& LONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) {
|
2013-04-01 15:08:44 +04:00
|
|
|
return (long)RFLOAT_VALUE(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-01-15 12:16:38 +04:00
|
|
|
FLOAT_OUT_OF_RANGE(val, "integer");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_BIGNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_big2long(val);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2000-11-14 10:10:31 +03:00
|
|
|
val = rb_to_int(val);
|
2008-03-11 13:48:12 +03:00
|
|
|
goto again;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-31 08:51:29 +04:00
|
|
|
static unsigned long
|
|
|
|
rb_num2ulong_internal(VALUE val, int *wrap_p)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-05-19 13:22:59 +04:00
|
|
|
again:
|
|
|
|
if (NIL_P(val)) {
|
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
|
|
|
}
|
|
|
|
|
2013-03-31 08:51:29 +04:00
|
|
|
if (FIXNUM_P(val)) {
|
|
|
|
long l = FIX2LONG(val); /* this is FIX2LONG, inteneded */
|
|
|
|
if (wrap_p)
|
|
|
|
*wrap_p = l < 0;
|
2013-04-01 15:08:44 +04:00
|
|
|
return (unsigned long)l;
|
2013-03-31 08:51:29 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(val, T_FLOAT)) {
|
2010-07-02 00:39:32 +04:00
|
|
|
if (RFLOAT_VALUE(val) < ULONG_MAX_PLUS_ONE
|
2013-03-27 17:12:27 +04:00
|
|
|
&& LONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) {
|
2013-03-31 08:51:29 +04:00
|
|
|
double d = RFLOAT_VALUE(val);
|
|
|
|
if (wrap_p)
|
|
|
|
*wrap_p = d <= -1.0; /* NUM2ULONG(v) uses v.to_int conceptually. */
|
2013-04-01 16:14:30 +04:00
|
|
|
if (0 <= d)
|
|
|
|
return (unsigned long)d;
|
|
|
|
return (unsigned long)(long)d;
|
2010-05-19 13:22:59 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-01-15 12:16:38 +04:00
|
|
|
FLOAT_OUT_OF_RANGE(val, "integer");
|
2010-05-19 13:22:59 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_BIGNUM)) {
|
2013-03-31 08:51:29 +04:00
|
|
|
{
|
|
|
|
unsigned long ul = rb_big2ulong(val);
|
|
|
|
if (wrap_p)
|
2014-02-16 01:17:34 +04:00
|
|
|
*wrap_p = BIGNUM_NEGATIVE_P(val);
|
2013-03-31 08:51:29 +04:00
|
|
|
return ul;
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-04-01 15:08:44 +04:00
|
|
|
val = rb_to_int(val);
|
|
|
|
goto again;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2014-04-18 19:46:32 +04:00
|
|
|
unsigned long
|
2013-03-31 08:51:29 +04:00
|
|
|
rb_num2ulong(VALUE val)
|
|
|
|
{
|
|
|
|
return rb_num2ulong_internal(val, NULL);
|
|
|
|
}
|
|
|
|
|
2013-04-01 15:08:44 +04:00
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
2009-05-20 20:43:41 +04:00
|
|
|
void
|
|
|
|
rb_out_of_int(SIGNED_VALUE num)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `int'",
|
|
|
|
num, num < 0 ? "small" : "big");
|
|
|
|
}
|
|
|
|
|
2003-05-30 17:28:10 +04:00
|
|
|
static void
|
2013-04-01 15:08:44 +04:00
|
|
|
check_int(long num)
|
2003-05-30 17:28:10 +04:00
|
|
|
{
|
2013-04-01 15:08:44 +04:00
|
|
|
if ((long)(int)num != num) {
|
2009-05-20 20:43:41 +04:00
|
|
|
rb_out_of_int(num);
|
2003-05-30 17:28:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-01 07:06:09 +04:00
|
|
|
check_uint(unsigned long num, int sign)
|
2003-05-30 17:28:10 +04:00
|
|
|
{
|
2012-06-20 10:31:32 +04:00
|
|
|
if (sign) {
|
2008-07-18 19:29:17 +04:00
|
|
|
/* minus */
|
2013-04-01 07:06:09 +04:00
|
|
|
if (num < (unsigned long)INT_MIN)
|
|
|
|
rb_raise(rb_eRangeError, "integer %ld too small to convert to `unsigned int'", (long)num);
|
2008-07-18 19:29:17 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* plus */
|
2013-04-01 07:06:09 +04:00
|
|
|
if (UINT_MAX < num)
|
|
|
|
rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned int'", num);
|
2003-05-30 17:28:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
long
|
2006-07-11 10:47:09 +04:00
|
|
|
rb_num2int(VALUE val)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
long num = rb_num2long(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-05-30 17:28:10 +04:00
|
|
|
check_int(num);
|
2003-12-01 11:42:53 +03:00
|
|
|
return num;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
long
|
2006-07-11 10:47:09 +04:00
|
|
|
rb_fix2int(VALUE val)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
|
|
|
|
|
2003-05-30 17:28:10 +04:00
|
|
|
check_int(num);
|
2003-12-01 11:42:53 +03:00
|
|
|
return num;
|
2003-05-30 17:28:10 +04:00
|
|
|
}
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
unsigned long
|
2006-07-11 10:47:09 +04:00
|
|
|
rb_num2uint(VALUE val)
|
2003-05-30 17:28:10 +04:00
|
|
|
{
|
2013-03-31 08:51:29 +04:00
|
|
|
int wrap;
|
|
|
|
unsigned long num = rb_num2ulong_internal(val, &wrap);
|
2003-05-30 17:28:10 +04:00
|
|
|
|
2013-03-31 08:51:29 +04:00
|
|
|
check_uint(num, wrap);
|
|
|
|
return num;
|
2003-05-30 17:28:10 +04:00
|
|
|
}
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
unsigned long
|
2006-07-11 10:47:09 +04:00
|
|
|
rb_fix2uint(VALUE val)
|
2003-05-30 17:28:10 +04:00
|
|
|
{
|
2003-12-01 11:42:53 +03:00
|
|
|
unsigned long num;
|
2003-05-30 17:28:10 +04:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
if (!FIXNUM_P(val)) {
|
2007-06-29 10:43:24 +04:00
|
|
|
return rb_num2uint(val);
|
2003-12-01 11:42:53 +03:00
|
|
|
}
|
|
|
|
num = FIX2ULONG(val);
|
2008-07-17 05:23:35 +04:00
|
|
|
|
2012-06-20 10:31:32 +04:00
|
|
|
check_uint(num, negative_int_p(val));
|
2003-12-01 11:42:53 +03:00
|
|
|
return num;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
#else
|
2003-12-01 11:42:53 +03:00
|
|
|
long
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num2int(VALUE val)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
return rb_num2long(val);
|
|
|
|
}
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
long
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_fix2int(VALUE val)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
return FIX2INT(val);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-14 07:54:34 +04:00
|
|
|
void
|
|
|
|
rb_out_of_short(SIGNED_VALUE num)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'",
|
|
|
|
num, num < 0 ? "small" : "big");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-01 15:08:44 +04:00
|
|
|
check_short(long num)
|
2011-11-14 07:54:34 +04:00
|
|
|
{
|
2013-04-01 15:08:44 +04:00
|
|
|
if ((long)(short)num != num) {
|
2011-11-14 07:54:34 +04:00
|
|
|
rb_out_of_short(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-01 07:06:09 +04:00
|
|
|
check_ushort(unsigned long num, int sign)
|
2011-11-14 07:54:34 +04:00
|
|
|
{
|
2012-06-20 10:31:32 +04:00
|
|
|
if (sign) {
|
2011-11-14 07:54:34 +04:00
|
|
|
/* minus */
|
2013-04-01 07:06:09 +04:00
|
|
|
if (num < (unsigned long)SHRT_MIN)
|
|
|
|
rb_raise(rb_eRangeError, "integer %ld too small to convert to `unsigned short'", (long)num);
|
2011-11-14 07:54:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* plus */
|
2013-04-01 07:06:09 +04:00
|
|
|
if (USHRT_MAX < num)
|
|
|
|
rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned short'", num);
|
2011-11-14 07:54:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
short
|
|
|
|
rb_num2short(VALUE val)
|
|
|
|
{
|
|
|
|
long num = rb_num2long(val);
|
|
|
|
|
|
|
|
check_short(num);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
short
|
|
|
|
rb_fix2short(VALUE val)
|
|
|
|
{
|
|
|
|
long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
|
|
|
|
|
|
|
|
check_short(num);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short
|
|
|
|
rb_num2ushort(VALUE val)
|
|
|
|
{
|
2013-03-31 08:51:29 +04:00
|
|
|
int wrap;
|
|
|
|
unsigned long num = rb_num2ulong_internal(val, &wrap);
|
2011-11-14 07:54:34 +04:00
|
|
|
|
2013-03-31 08:51:29 +04:00
|
|
|
check_ushort(num, wrap);
|
|
|
|
return num;
|
2011-11-14 07:54:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short
|
|
|
|
rb_fix2ushort(VALUE val)
|
|
|
|
{
|
|
|
|
unsigned long num;
|
|
|
|
|
|
|
|
if (!FIXNUM_P(val)) {
|
2011-11-14 09:53:59 +04:00
|
|
|
return rb_num2ushort(val);
|
2011-11-14 07:54:34 +04:00
|
|
|
}
|
|
|
|
num = FIX2ULONG(val);
|
|
|
|
|
2012-06-20 10:31:32 +04:00
|
|
|
check_ushort(num, negative_int_p(val));
|
2011-11-14 07:54:34 +04:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num2fix(VALUE val)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2013-04-01 15:08:44 +04:00
|
|
|
long v;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
if (FIXNUM_P(val)) return val;
|
|
|
|
|
|
|
|
v = rb_num2long(val);
|
|
|
|
if (!FIXABLE(v))
|
2013-04-01 15:08:44 +04:00
|
|
|
rb_raise(rb_eRangeError, "integer %ld out of range of fixnum", v);
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2FIX(v);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-03-14 09:23:46 +03:00
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
|
2010-07-02 00:39:32 +04:00
|
|
|
#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
|
|
|
|
#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
|
|
|
|
#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
|
2011-07-09 11:54:38 +04:00
|
|
|
#ifndef ULLONG_MAX
|
|
|
|
#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
|
|
|
|
#endif
|
2013-03-27 17:12:27 +04:00
|
|
|
#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
|
|
|
|
(LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
|
|
|
|
LLONG_MIN <= (n): \
|
|
|
|
LLONG_MIN_MINUS_ONE < (n))
|
2010-07-02 00:39:32 +04:00
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
LONG_LONG
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num2ll(VALUE val)
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
|
|
|
if (NIL_P(val)) {
|
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from nil");
|
|
|
|
}
|
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val);
|
2002-03-14 09:23:46 +03:00
|
|
|
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(val, T_FLOAT)) {
|
2010-07-02 00:39:32 +04:00
|
|
|
if (RFLOAT_VALUE(val) < LLONG_MAX_PLUS_ONE
|
2013-03-27 17:12:27 +04:00
|
|
|
&& (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val)))) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
return (LONG_LONG)(RFLOAT_VALUE(val));
|
2002-03-14 09:23:46 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-01-15 12:16:38 +04:00
|
|
|
FLOAT_OUT_OF_RANGE(val, "long long");
|
2002-03-14 09:23:46 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_BIGNUM)) {
|
2002-03-14 09:23:46 +03:00
|
|
|
return rb_big2ll(val);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_STRING)) {
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from string");
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_TRUE) || RB_TYPE_P(val, T_FALSE)) {
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from boolean");
|
|
|
|
}
|
2012-04-14 04:36:26 +04:00
|
|
|
|
2012-04-15 04:06:13 +04:00
|
|
|
val = rb_to_int(val);
|
|
|
|
return NUM2LL(val);
|
2002-03-14 09:23:46 +03:00
|
|
|
}
|
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_num2ull(VALUE val)
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
2013-09-11 12:23:06 +04:00
|
|
|
if (RB_TYPE_P(val, T_NIL)) {
|
2011-07-07 10:23:40 +04:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from nil");
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_FIXNUM)) {
|
2011-07-11 10:03:41 +04:00
|
|
|
return (LONG_LONG)FIX2LONG(val); /* this is FIX2LONG, inteneded */
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_FLOAT)) {
|
2011-07-07 10:23:40 +04:00
|
|
|
if (RFLOAT_VALUE(val) < ULLONG_MAX_PLUS_ONE
|
2013-03-27 17:12:27 +04:00
|
|
|
&& LLONG_MIN_MINUS_ONE_IS_LESS_THAN(RFLOAT_VALUE(val))) {
|
2013-04-01 16:14:30 +04:00
|
|
|
if (0 <= RFLOAT_VALUE(val))
|
|
|
|
return (unsigned LONG_LONG)(RFLOAT_VALUE(val));
|
2013-03-29 01:49:55 +04:00
|
|
|
return (unsigned LONG_LONG)(LONG_LONG)(RFLOAT_VALUE(val));
|
2011-07-07 10:23:40 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-01-15 12:16:38 +04:00
|
|
|
FLOAT_OUT_OF_RANGE(val, "unsigned long long");
|
2011-07-07 10:23:40 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_BIGNUM)) {
|
2002-03-14 09:23:46 +03:00
|
|
|
return rb_big2ull(val);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_STRING)) {
|
2011-07-07 10:23:40 +04:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from string");
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(val, T_TRUE) || RB_TYPE_P(val, T_FALSE)) {
|
2011-07-07 10:23:40 +04:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from boolean");
|
2002-03-14 09:23:46 +03:00
|
|
|
}
|
2012-04-14 04:36:26 +04:00
|
|
|
|
2012-04-15 04:06:13 +04:00
|
|
|
val = rb_to_int(val);
|
|
|
|
return NUM2ULL(val);
|
2002-03-14 09:23:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-class: Integer
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* This class is the basis for the two concrete classes that hold whole
|
|
|
|
* numbers, Bignum and Fixnum.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.to_i -> integer
|
2003-12-27 08:46:46 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* As +int+ is already an Integer, all these methods simply return the receiver.
|
|
|
|
*
|
2016-04-18 06:55:33 +03:00
|
|
|
* Synonyms is #to_int
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
int_to_i(VALUE num)
|
2000-07-06 11:21:26 +04:00
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.integer? -> true
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Since +int+ is already an Integer, this always returns +true+.
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
int_int_p(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2006-09-21 10:09:26 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.odd? -> true or false
|
2006-12-31 18:02:22 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +int+ is an odd number.
|
2006-09-21 10:09:26 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_odd_p(VALUE num)
|
|
|
|
{
|
2016-03-17 20:11:42 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (num & 2) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_odd_p(num);
|
|
|
|
}
|
|
|
|
else if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
|
2007-06-29 10:43:24 +04:00
|
|
|
return Qtrue;
|
2006-09-21 10:09:26 +04:00
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.even? -> true or false
|
2006-12-31 18:02:22 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if +int+ is an even number.
|
2006-09-21 10:09:26 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_even_p(VALUE num)
|
|
|
|
{
|
2016-03-17 20:15:33 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if ((num & 2) == 0) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_even_p(num);
|
|
|
|
}
|
|
|
|
else if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
|
2007-06-29 10:43:24 +04:00
|
|
|
return Qtrue;
|
2006-09-21 10:09:26 +04:00
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-08-06 07:33:22 +04:00
|
|
|
* int.next -> integer
|
|
|
|
* int.succ -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the Integer equal to +int+ + 1.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2006-12-31 18:02:22 +03:00
|
|
|
* 1.next #=> 2
|
|
|
|
* (-1).next #=> 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_succ(VALUE num)
|
|
|
|
{
|
|
|
|
long i = FIX2LONG(num) + 1;
|
|
|
|
return LONG2NUM(i);
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.next -> integer
|
|
|
|
* int.succ -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the Integer equal to +int+ + 1, same as Fixnum#next.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 1.next #=> 2
|
|
|
|
* (-1).next #=> 0
|
|
|
|
*/
|
|
|
|
|
2013-03-05 05:20:20 +04:00
|
|
|
VALUE
|
|
|
|
rb_int_succ(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-03-22 10:26:42 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
long i = FIX2LONG(num) + 1;
|
2002-08-28 12:05:23 +04:00
|
|
|
return LONG2NUM(i);
|
2002-03-22 10:26:42 +03:00
|
|
|
}
|
2013-03-05 05:18:55 +04:00
|
|
|
if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_plus(num, INT2FIX(1));
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return rb_funcall(num, '+', 1, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
2013-03-05 05:20:20 +04:00
|
|
|
#define int_succ rb_int_succ
|
|
|
|
|
2007-01-30 07:27:04 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.pred -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the Integer equal to +int+ - 1.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2007-01-30 07:27:04 +03:00
|
|
|
* 1.pred #=> 0
|
|
|
|
* (-1).pred #=> -2
|
|
|
|
*/
|
|
|
|
|
2013-03-05 05:20:20 +04:00
|
|
|
VALUE
|
|
|
|
rb_int_pred(VALUE num)
|
2007-01-30 07:27:04 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
long i = FIX2LONG(num) - 1;
|
|
|
|
return LONG2NUM(i);
|
|
|
|
}
|
2013-03-05 05:18:55 +04:00
|
|
|
if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_minus(num, INT2FIX(1));
|
|
|
|
}
|
2007-01-30 07:27:04 +03:00
|
|
|
return rb_funcall(num, '-', 1, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
2013-03-05 05:20:20 +04:00
|
|
|
#define int_pred rb_int_pred
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#chr
|
|
|
|
* call-seq:
|
|
|
|
* int.chr([encoding]) -> string
|
|
|
|
*
|
|
|
|
* Returns a string containing the character represented by the +int+'s value
|
|
|
|
* according to +encoding+.
|
|
|
|
*
|
|
|
|
* 65.chr #=> "A"
|
|
|
|
* 230.chr #=> "\346"
|
|
|
|
* 255.chr(Encoding::UTF_8) #=> "\303\277"
|
|
|
|
*/
|
|
|
|
|
2010-10-12 10:18:08 +04:00
|
|
|
VALUE
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
|
2010-10-12 10:18:08 +04:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
VALUE str;
|
2012-01-09 00:42:45 +04:00
|
|
|
switch (n = rb_enc_codelen(code, enc)) {
|
|
|
|
case ONIGERR_INVALID_CODE_POINT_VALUE:
|
|
|
|
rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
|
|
|
|
break;
|
|
|
|
case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
|
|
|
|
case 0:
|
2011-06-11 11:52:34 +04:00
|
|
|
rb_raise(rb_eRangeError, "%u out of char range", code);
|
2012-01-09 00:42:45 +04:00
|
|
|
break;
|
2010-10-12 10:18:08 +04:00
|
|
|
}
|
|
|
|
str = rb_enc_str_new(0, n, enc);
|
|
|
|
rb_enc_mbcput(code, RSTRING_PTR(str), enc);
|
2012-01-09 00:42:45 +04:00
|
|
|
if (rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_END(str), enc) != n) {
|
|
|
|
rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
|
|
|
|
}
|
2010-10-12 10:18:08 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2007-10-26 12:38:14 +04:00
|
|
|
int_chr(int argc, VALUE *argv, VALUE num)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
char c;
|
2010-10-13 16:13:53 +04:00
|
|
|
unsigned int i;
|
2007-10-26 12:38:14 +04:00
|
|
|
rb_encoding *enc;
|
|
|
|
|
2010-10-13 16:13:53 +04:00
|
|
|
if (rb_num_to_uint(num, &i) == 0) {
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(num)) {
|
|
|
|
rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRangeError, "bignum out of char range");
|
|
|
|
}
|
|
|
|
|
2007-10-26 12:38:14 +04:00
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
2009-06-26 22:18:12 +04:00
|
|
|
if (0xff < i) {
|
|
|
|
enc = rb_default_internal_encoding();
|
2010-10-12 12:50:30 +04:00
|
|
|
if (!enc) {
|
|
|
|
rb_raise(rb_eRangeError, "%d out of char range", i);
|
|
|
|
}
|
2009-06-26 22:18:12 +04:00
|
|
|
goto decode;
|
|
|
|
}
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
c = (char)i;
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
if (i < 0x80) {
|
|
|
|
return rb_usascii_str_new(&c, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_str_new(&c, 1);
|
|
|
|
}
|
2007-10-26 12:38:14 +04:00
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
default:
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 0, 1);
|
2007-10-26 12:38:14 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
enc = rb_to_encoding(argv[0]);
|
2007-12-23 02:47:18 +03:00
|
|
|
if (!enc) enc = rb_ascii8bit_encoding();
|
2009-06-26 22:18:12 +04:00
|
|
|
decode:
|
2010-10-12 10:18:08 +04:00
|
|
|
return rb_enc_uint_chr(i, enc);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:32:11 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.ord -> self
|
2008-12-22 15:32:11 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the +int+ itself.
|
2008-12-22 15:32:11 +03:00
|
|
|
*
|
|
|
|
* ?a.ord #=> 97
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* This method is intended for compatibility to character constant in Ruby
|
|
|
|
* 1.9.
|
|
|
|
*
|
2008-12-22 15:32:11 +03:00
|
|
|
* For example, ?a.ord returns 97 both in 1.8 and 1.9.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2011-05-09 06:53:55 +04:00
|
|
|
int_ord(VALUE num)
|
2008-12-22 15:32:11 +03:00
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/********************************************************************
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* Document-class: Fixnum
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Holds Integer values that can be represented in a native machine word
|
|
|
|
* (minus 1 bit). If any operation on a Fixnum exceeds this range, the value
|
|
|
|
* is automatically converted to a Bignum.
|
|
|
|
*
|
|
|
|
* Fixnum objects have immediate value. This means that when they are assigned
|
|
|
|
* or passed as parameters, the actual object is passed, rather than a
|
|
|
|
* reference to that object.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Assignment does not alias Fixnum objects. There is effectively only one
|
|
|
|
* Fixnum object instance for any given integer value, so, for example, you
|
|
|
|
* cannot add a singleton method to a Fixnum. Any attempt to add a singleton
|
|
|
|
* method to a Fixnum object will raise a TypeError.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#-@
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* -fix -> integer
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Negates +fix+, which may return a Bignum.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_uminus(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-28 12:05:23 +04:00
|
|
|
return LONG2NUM(-FIX2LONG(num));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_uminus(VALUE num)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return fix_uminus(num);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_uminus(num);
|
|
|
|
}
|
|
|
|
return rb_funcall(num, idUMinus, 0, 0);
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#to_s
|
|
|
|
* call-seq:
|
|
|
|
* int.to_s(base=10) -> string
|
|
|
|
*
|
|
|
|
* Returns a string containing the representation of +int+ radix +base+
|
|
|
|
* (between 2 and 36).
|
|
|
|
*
|
|
|
|
* 12345.to_s #=> "12345"
|
|
|
|
* 12345.to_s(2) #=> "11000000111001"
|
|
|
|
* 12345.to_s(8) #=> "30071"
|
|
|
|
* 12345.to_s(10) #=> "12345"
|
|
|
|
* 12345.to_s(16) #=> "3039"
|
|
|
|
* 12345.to_s(36) #=> "9ix"
|
|
|
|
* 78546939656932.to_s(36) #=> "rubyrules"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_fix2str(VALUE x, int base)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-02-24 05:20:45 +03:00
|
|
|
char buf[SIZEOF_VALUE*CHAR_BIT + 1], *const e = buf + sizeof buf, *b = e;
|
2002-01-11 12:18:54 +03:00
|
|
|
long val = FIX2LONG(x);
|
2016-03-14 07:41:16 +03:00
|
|
|
unsigned long u;
|
2003-04-14 10:54:27 +04:00
|
|
|
int neg = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-04-14 10:54:27 +04:00
|
|
|
if (base < 2 || 36 < base) {
|
* regerror.c, string.c, io.c, lib/getoptlong.rb, lib/net/imap.rb,
compile.c, sprintf.c, parse.y, ext/win32ole/win32ole.c,
ext/tk/sample/demos-en/entry3.rb, ext/tk/lib/tcltk.rb,
ext/openssl/ossl_bn.c, numeric.c, vm.c,
benchmark/bm_so_meteor_contest.rb, bignum.c, ruby.c: don't "illegal"
for non law violation context.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-12-21 05:31:11 +03:00
|
|
|
rb_raise(rb_eArgError, "invalid radix %d", base);
|
2003-04-14 10:54:27 +04:00
|
|
|
}
|
|
|
|
if (val == 0) {
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
return rb_usascii_str_new2("0");
|
2003-04-14 10:54:27 +04:00
|
|
|
}
|
2002-01-11 12:18:54 +03:00
|
|
|
if (val < 0) {
|
2016-03-14 07:41:16 +03:00
|
|
|
u = 1 + (unsigned long)(-(val + 1)); /* u = -val avoiding overflow */
|
2003-04-14 10:54:27 +04:00
|
|
|
neg = 1;
|
|
|
|
}
|
2016-03-14 07:41:16 +03:00
|
|
|
else {
|
|
|
|
u = val;
|
|
|
|
}
|
2003-04-14 10:54:27 +04:00
|
|
|
do {
|
2016-03-14 07:41:16 +03:00
|
|
|
*--b = ruby_digitmap[(int)(u % base)];
|
|
|
|
} while (u /= base);
|
2003-04-14 10:54:27 +04:00
|
|
|
if (neg) {
|
|
|
|
*--b = '-';
|
2002-01-11 12:18:54 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2016-02-24 05:20:45 +03:00
|
|
|
return rb_usascii_str_new(b, e - b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2016-03-18 15:33:28 +03:00
|
|
|
int_to_s(int argc, VALUE *argv, VALUE x)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-11-19 08:03:03 +03:00
|
|
|
int base;
|
|
|
|
|
2016-04-02 09:33:26 +03:00
|
|
|
if (rb_check_arity(argc, 0, 1))
|
|
|
|
base = NUM2INT(argv[0]);
|
|
|
|
else
|
|
|
|
base = 10;
|
2016-03-26 04:55:14 +03:00
|
|
|
return rb_int2str(x, base);
|
|
|
|
}
|
2001-11-19 08:03:03 +03:00
|
|
|
|
2016-03-26 04:55:14 +03:00
|
|
|
VALUE
|
|
|
|
rb_int2str(VALUE x, int base)
|
|
|
|
{
|
2016-03-18 15:57:40 +03:00
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return rb_fix2str(x, base);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big2str(x, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_any_to_s(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#+
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix + numeric -> numeric_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Performs addition: the class of the resulting object depends on the class of
|
|
|
|
* +numeric+ and on the magnitude of the result. It may return a Bignum.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_plus(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long a, b, c;
|
|
|
|
VALUE r;
|
|
|
|
|
|
|
|
a = FIX2LONG(x);
|
|
|
|
b = FIX2LONG(y);
|
|
|
|
c = a + b;
|
2006-09-05 00:10:45 +04:00
|
|
|
r = LONG2NUM(c);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
return rb_big_plus(y, x);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
2015-02-24 16:59:06 +03:00
|
|
|
else if (RB_TYPE_P(y, T_COMPLEX)) {
|
|
|
|
VALUE rb_nucomp_add(VALUE, VALUE);
|
|
|
|
return rb_nucomp_add(y, x);
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '+');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 12:50:19 +03:00
|
|
|
VALUE
|
|
|
|
rb_fix_plus(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
return fix_plus(x, y);
|
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_plus(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_plus(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_plus(x, y);
|
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y, '+');
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#-
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix - numeric -> numeric_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Performs subtraction: the class of the resulting object depends on the class
|
|
|
|
* of +numeric+ and on the magnitude of the result. It may return a Bignum.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_minus(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long a, b, c;
|
|
|
|
VALUE r;
|
|
|
|
|
|
|
|
a = FIX2LONG(x);
|
|
|
|
b = FIX2LONG(y);
|
|
|
|
c = a - b;
|
2006-09-05 00:10:45 +04:00
|
|
|
r = LONG2NUM(c);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
x = rb_int2big(FIX2LONG(x));
|
|
|
|
return rb_big_minus(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '-');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_minus(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_minus(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_minus(x, y);
|
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-24 10:52:39 +03:00
|
|
|
#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
|
|
|
|
/*tests if N*N would overflow*/
|
|
|
|
#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#*
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix * numeric -> numeric_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Performs multiplication: the class of the resulting object depends on the
|
|
|
|
* class of +numeric+ and on the magnitude of the result. It may return a
|
|
|
|
* Bignum.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_mul(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(y)) {
|
2016-03-20 14:10:43 +03:00
|
|
|
return rb_fix_mul_fix(x, y);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
return rb_big_mul(y, x);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
2015-02-24 16:59:06 +03:00
|
|
|
else if (RB_TYPE_P(y, T_COMPLEX)) {
|
|
|
|
VALUE rb_nucomp_mul(VALUE, VALUE);
|
|
|
|
return rb_nucomp_mul(y, x);
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '*');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_mul(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_mul(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_mul(x, y);
|
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y, '*');
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-30 07:20:56 +03:00
|
|
|
* Document-method: Integer#fdiv
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-30 07:20:56 +03:00
|
|
|
* integer.fdiv(numeric) -> float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the floating point result of dividing +fix+ by +numeric+.
|
2008-04-07 17:52:26 +04:00
|
|
|
*
|
2008-05-07 08:14:57 +04:00
|
|
|
* 654321.fdiv(13731) #=> 47.6528293642124
|
|
|
|
* 654321.fdiv(13731.24) #=> 47.6519964693647
|
2008-04-07 17:52:26 +04:00
|
|
|
*
|
2016-04-30 07:20:56 +03:00
|
|
|
* -1234567890987654321.fdiv(13731) #=> -89910996357705.5
|
|
|
|
* -1234567890987654321.fdiv(13731.24) #=> -89909424858035.7
|
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2008-03-16 03:23:43 +03:00
|
|
|
static VALUE
|
|
|
|
fix_fdiv(VALUE x, VALUE y)
|
2003-01-23 09:22:50 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM((double)FIX2LONG(x) / (double)FIX2LONG(y));
|
2003-01-23 09:22:50 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2009-06-17 16:55:16 +04:00
|
|
|
return rb_big_fdiv(rb_int2big(FIX2LONG(x)), y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-03-16 03:23:43 +03:00
|
|
|
return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
|
2005-08-03 11:09:48 +04:00
|
|
|
}
|
2003-01-23 09:22:50 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 07:20:56 +03:00
|
|
|
static VALUE
|
|
|
|
int_fdiv(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_fdiv(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_fdiv(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2016-04-30 07:22:27 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Fixnum#/
|
|
|
|
* call-seq:
|
|
|
|
* fix / numeric -> numeric_result
|
|
|
|
*
|
|
|
|
* Performs division: the class of the resulting object depends on the class of
|
|
|
|
* +numeric+ and on the magnitude of the result. It may return a Bignum.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2008-05-01 19:00:01 +04:00
|
|
|
fix_divide(VALUE x, VALUE y, ID op)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(y)) {
|
2016-03-08 12:15:18 +03:00
|
|
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
2016-03-21 16:36:03 +03:00
|
|
|
return rb_fix_div_fix(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
x = rb_int2big(FIX2LONG(x));
|
|
|
|
return rb_big_div(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-05-01 19:00:01 +04:00
|
|
|
{
|
2008-05-27 16:51:28 +04:00
|
|
|
double div;
|
|
|
|
|
2008-05-01 19:00:01 +04:00
|
|
|
if (op == '/') {
|
2008-05-27 16:51:28 +04:00
|
|
|
div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(div);
|
2008-05-01 19:00:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-05-27 16:51:28 +04:00
|
|
|
if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv();
|
|
|
|
div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
|
2008-05-07 17:23:24 +04:00
|
|
|
return rb_dbl2big(floor(div));
|
2008-05-01 19:00:01 +04:00
|
|
|
}
|
2005-10-05 20:15:16 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (RB_TYPE_P(y, T_RATIONAL) &&
|
|
|
|
op == '/' && FIX2LONG(x) == 1)
|
2009-07-12 15:46:40 +04:00
|
|
|
return rb_rational_reciprocal(y);
|
2008-05-01 19:00:01 +04:00
|
|
|
return rb_num_coerce_bin(x, y, op);
|
2005-08-03 11:09:48 +04:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2005-10-05 20:15:16 +04:00
|
|
|
static VALUE
|
|
|
|
fix_div(VALUE x, VALUE y)
|
|
|
|
{
|
2008-05-01 19:00:01 +04:00
|
|
|
return fix_divide(x, y, '/');
|
2005-10-05 20:15:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-04-30 08:43:15 +03:00
|
|
|
* Document-method: Integer#div
|
2005-10-05 20:15:16 +04:00
|
|
|
* call-seq:
|
2016-04-30 08:43:15 +03:00
|
|
|
* int.div(numeric) -> integer
|
2005-10-05 20:15:16 +04:00
|
|
|
*
|
2016-04-30 08:43:15 +03:00
|
|
|
* Performs integer division: returns integer result of dividing +int+ by
|
2013-07-15 22:27:23 +04:00
|
|
|
* +numeric+.
|
2005-10-05 20:15:16 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_idiv(VALUE x, VALUE y)
|
|
|
|
{
|
2015-10-12 03:08:58 +03:00
|
|
|
return fix_divide(x, y, id_div);
|
2005-10-05 20:15:16 +04:00
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_idiv(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_idiv(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_idiv(x, y);
|
|
|
|
}
|
|
|
|
return num_div(x, y);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#%
|
2016-04-30 08:26:52 +03:00
|
|
|
* Document-method: Integer#modulo
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix % other -> real
|
|
|
|
* fix.modulo(other) -> real
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +fix+ modulo +other+.
|
|
|
|
*
|
|
|
|
* See Numeric#divmod for more information.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_mod(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2000-07-03 09:46:36 +04:00
|
|
|
if (FIXNUM_P(y)) {
|
2016-03-08 12:15:18 +03:00
|
|
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
2016-03-21 16:36:03 +03:00
|
|
|
return rb_fix_mod_fix(x, y);
|
2000-07-03 09:46:36 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
x = rb_int2big(FIX2LONG(x));
|
|
|
|
return rb_big_modulo(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2012-03-14 10:10:01 +04:00
|
|
|
return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_bin(x, y, '%');
|
2005-08-03 11:09:48 +04:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2016-03-26 04:54:16 +03:00
|
|
|
VALUE
|
|
|
|
rb_int_modulo(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_mod(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_modulo(x, y);
|
|
|
|
}
|
|
|
|
return num_modulo(x, y);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-30 08:05:54 +03:00
|
|
|
* Document-method: Integer#divmod
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-30 08:05:54 +03:00
|
|
|
* integer.divmod(numeric) -> array
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-04-30 08:05:54 +03:00
|
|
|
* See <code>Numeric#divmod</code>.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_divmod(VALUE x, VALUE y)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2000-07-03 09:46:36 +04:00
|
|
|
if (FIXNUM_P(y)) {
|
2016-03-21 16:36:03 +03:00
|
|
|
VALUE div, mod;
|
2016-03-08 12:15:18 +03:00
|
|
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
2016-03-21 16:36:03 +03:00
|
|
|
rb_fix_divmod_fix(x, y, &div, &mod);
|
|
|
|
return rb_assoc_new(div, mod);
|
2000-07-03 09:46:36 +04:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
x = rb_int2big(FIX2LONG(x));
|
|
|
|
return rb_big_divmod(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2005-08-03 11:09:48 +04:00
|
|
|
{
|
|
|
|
double div, mod;
|
|
|
|
volatile VALUE a, b;
|
|
|
|
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod);
|
2008-03-13 19:37:54 +03:00
|
|
|
a = dbl2ival(div);
|
2008-09-05 22:24:21 +04:00
|
|
|
b = DBL2NUM(mod);
|
2005-08-03 11:09:48 +04:00
|
|
|
return rb_assoc_new(a, b);
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_bin(x, y, id_divmod);
|
2005-08-03 11:09:48 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 08:05:54 +03:00
|
|
|
static VALUE
|
|
|
|
int_divmod(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_divmod(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_divmod(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
2016-04-30 06:59:02 +03:00
|
|
|
* Document-method: Integer#**
|
2016-04-27 15:46:46 +03:00
|
|
|
* call-seq:
|
2016-04-30 06:59:02 +03:00
|
|
|
* integer ** numeric -> numeric_result
|
2016-04-27 15:46:46 +03:00
|
|
|
*
|
2016-04-30 06:59:02 +03:00
|
|
|
* Raises +integer+ to the power of +numeric+, which may be negative or
|
2016-04-27 15:46:46 +03:00
|
|
|
* fractional.
|
2016-04-30 06:59:02 +03:00
|
|
|
* The result may be a Fixnum, Bignum, or Float
|
2016-04-27 15:46:46 +03:00
|
|
|
*
|
|
|
|
* 2 ** 3 #=> 8
|
|
|
|
* 2 ** -1 #=> (1/2)
|
|
|
|
* 2 ** 0.5 #=> 1.4142135623731
|
2016-04-30 06:59:02 +03:00
|
|
|
*
|
|
|
|
* 123456789 ** 2 #=> 15241578750190521
|
|
|
|
* 123456789 ** 1.2 #=> 5126464716.09932
|
|
|
|
* 123456789 ** -2 #=> 6.5610001194102e-17
|
|
|
|
*
|
2016-04-27 15:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2007-04-26 12:30:10 +04:00
|
|
|
static VALUE
|
|
|
|
int_pow(long x, unsigned long y)
|
|
|
|
{
|
|
|
|
int neg = x < 0;
|
|
|
|
long z = 1;
|
|
|
|
|
|
|
|
if (neg) x = -x;
|
2007-07-05 11:38:41 +04:00
|
|
|
if (y & 1)
|
|
|
|
z = x;
|
|
|
|
else
|
|
|
|
neg = 0;
|
2007-04-26 12:30:10 +04:00
|
|
|
y &= ~1;
|
|
|
|
do {
|
|
|
|
while (y % 2 == 0) {
|
2007-07-13 21:29:24 +04:00
|
|
|
if (!FIT_SQRT_LONG(x)) {
|
2007-04-27 07:53:43 +04:00
|
|
|
VALUE v;
|
2007-04-26 12:30:10 +04:00
|
|
|
bignum:
|
2007-07-05 11:38:41 +04:00
|
|
|
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
|
|
|
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
2007-04-27 07:53:43 +04:00
|
|
|
return v;
|
2007-04-26 12:30:10 +04:00
|
|
|
}
|
2007-07-13 21:29:24 +04:00
|
|
|
x = x * x;
|
2007-04-26 12:30:10 +04:00
|
|
|
y >>= 1;
|
|
|
|
}
|
|
|
|
{
|
2013-04-09 15:39:53 +04:00
|
|
|
if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
|
2007-04-26 12:30:10 +04:00
|
|
|
goto bignum;
|
|
|
|
}
|
2013-04-09 15:39:53 +04:00
|
|
|
z = x * z;
|
2007-04-26 12:30:10 +04:00
|
|
|
}
|
|
|
|
} while (--y);
|
2007-07-05 11:38:41 +04:00
|
|
|
if (neg) z = -z;
|
2007-04-26 12:30:10 +04:00
|
|
|
return LONG2NUM(z);
|
|
|
|
}
|
|
|
|
|
2013-08-02 18:48:55 +04:00
|
|
|
VALUE
|
|
|
|
rb_int_positive_pow(long x, unsigned long y)
|
|
|
|
{
|
|
|
|
return int_pow(x, y);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_pow(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-07-14 18:31:21 +04:00
|
|
|
long a = FIX2LONG(x);
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (FIXNUM_P(y)) {
|
2007-07-14 18:31:21 +04:00
|
|
|
long b = FIX2LONG(y);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-02-05 09:39:49 +04:00
|
|
|
if (a == 1) return INT2FIX(1);
|
|
|
|
if (a == -1) {
|
|
|
|
if (b % 2 == 0)
|
|
|
|
return INT2FIX(1);
|
|
|
|
else
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
2008-03-16 03:23:43 +03:00
|
|
|
if (b < 0)
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(rb_rational_raw1(x), idPow, 1, y);
|
2008-03-16 03:23:43 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (b == 0) return INT2FIX(1);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (b == 1) return x;
|
2007-10-26 12:01:41 +04:00
|
|
|
if (a == 0) {
|
|
|
|
if (b > 0) return INT2FIX(0);
|
2009-12-29 10:05:39 +03:00
|
|
|
return DBL2NUM(INFINITY);
|
2007-10-26 12:01:41 +04:00
|
|
|
}
|
2008-03-16 03:23:43 +03:00
|
|
|
return int_pow(a, b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2007-07-14 18:31:21 +04:00
|
|
|
if (a == 1) return INT2FIX(1);
|
|
|
|
if (a == -1) {
|
|
|
|
if (int_even_p(y)) return INT2FIX(1);
|
|
|
|
else return INT2FIX(-1);
|
|
|
|
}
|
2013-02-05 09:39:49 +04:00
|
|
|
if (negative_int_p(y))
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(rb_rational_raw1(x), idPow, 1, y);
|
2013-02-05 09:39:49 +04:00
|
|
|
if (a == 0) return INT2FIX(0);
|
2005-08-03 11:09:48 +04:00
|
|
|
x = rb_int2big(FIX2LONG(x));
|
|
|
|
return rb_big_pow(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2008-09-05 22:24:21 +04:00
|
|
|
if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
|
2007-10-26 12:01:41 +04:00
|
|
|
if (a == 0) {
|
2009-12-29 10:05:39 +03:00
|
|
|
return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0);
|
2007-10-26 12:01:41 +04:00
|
|
|
}
|
2008-09-05 22:24:21 +04:00
|
|
|
if (a == 1) return DBL2NUM(1.0);
|
2009-08-17 03:03:45 +04:00
|
|
|
{
|
|
|
|
double dy = RFLOAT_VALUE(y);
|
|
|
|
if (a < 0 && dy != round(dy))
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(rb_complex_raw1(x), idPow, 1, y);
|
2009-08-17 03:03:45 +04:00
|
|
|
return DBL2NUM(pow((double)a, dy));
|
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_bin(x, y, idPow);
|
2005-08-03 11:09:48 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 06:59:02 +03:00
|
|
|
static VALUE
|
|
|
|
rb_int_pow(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_pow(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_pow(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#==
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix == other -> true or false
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Return +true+ if +fix+ equals +other+ numerically.
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* 1 == 2 #=> false
|
|
|
|
* 1 == 1.0 #=> true
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_equal(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-02-21 16:30:17 +03:00
|
|
|
if (x == y) return Qtrue;
|
|
|
|
if (FIXNUM_P(y)) return Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2005-08-12 12:13:28 +04:00
|
|
|
return rb_big_eq(y, x);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2012-07-16 14:39:42 +04:00
|
|
|
return rb_integer_float_eq(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:13:05 +03:00
|
|
|
return num_equal(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#<=>
|
|
|
|
* call-seq:
|
|
|
|
* int <=> numeric -> -1, 0, +1 or nil
|
|
|
|
*
|
|
|
|
* Comparison---Returns +-1+, +0+, ++1+ or +nil+ depending on whether +fix+ is
|
|
|
|
* less than, equal to, or greater than +numeric+.
|
|
|
|
*
|
|
|
|
* This is the basis for the tests in the Comparable module.
|
|
|
|
*
|
|
|
|
* +nil+ is returned if the two values are incomparable.
|
|
|
|
*/
|
|
|
|
|
2016-03-19 12:28:12 +03:00
|
|
|
static VALUE
|
|
|
|
fix_cmp(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (x == y) return INT2FIX(0);
|
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2016-03-19 15:53:36 +03:00
|
|
|
VALUE cmp = rb_big_cmp(y, x);
|
|
|
|
switch (cmp) {
|
|
|
|
case INT2FIX(+1): return INT2FIX(-1);
|
|
|
|
case INT2FIX(-1): return INT2FIX(+1);
|
|
|
|
}
|
|
|
|
return cmp;
|
2016-03-19 12:28:12 +03:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
|
|
|
return rb_integer_float_cmp(x, y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_num_coerce_cmp(x, y, id_cmp);
|
|
|
|
}
|
|
|
|
return rb_num_coerce_cmp(x, y, id_cmp);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-03-19 12:28:12 +03:00
|
|
|
int_cmp(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-03-19 12:28:12 +03:00
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_cmp(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
2016-03-19 12:28:12 +03:00
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_cmp(x, y);
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2016-03-19 12:28:12 +03:00
|
|
|
rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#>
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix > real -> true or false
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if the value of +fix+ is greater than that of +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_gt(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
2007-02-21 11:53:15 +03:00
|
|
|
if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2016-03-20 12:53:50 +03:00
|
|
|
return rb_big_cmp(y, x) == INT2FIX(-1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_relop(x, y, '>');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#>=
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix >= real -> true or false
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if the value of +fix+ is greater than or equal to that of
|
|
|
|
* +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_ge(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
2007-02-21 11:53:15 +03:00
|
|
|
if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2016-03-20 12:53:50 +03:00
|
|
|
return rb_big_cmp(y, x) != INT2FIX(+1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
|
|
|
VALUE rel = rb_integer_float_cmp(x, y);
|
|
|
|
return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_relop(x, y, idGE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#<
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* fix < real -> true or false
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if the value of +fix+ is less than that of +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_lt(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
2007-02-21 11:53:15 +03:00
|
|
|
if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2016-03-20 12:53:50 +03:00
|
|
|
return rb_big_cmp(y, x) == INT2FIX(+1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
2012-07-16 13:41:25 +04:00
|
|
|
return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-02-12 05:46:21 +03:00
|
|
|
return rb_num_coerce_relop(x, y, '<');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Fixnum#<=
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2009-11-03 20:46:28 +03:00
|
|
|
* fix <= real -> true or false
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns +true+ if the value of +fix+ is less than or equal to that of
|
|
|
|
* +real+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_le(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
2007-02-21 11:53:15 +03:00
|
|
|
if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-11 12:23:06 +04:00
|
|
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
2016-03-20 12:53:50 +03:00
|
|
|
return rb_big_cmp(y, x) != INT2FIX(-1) ? Qtrue : Qfalse;
|
2013-09-11 12:23:06 +04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(y, T_FLOAT)) {
|
|
|
|
VALUE rel = rb_integer_float_cmp(x, y);
|
|
|
|
return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
else {
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_num_coerce_relop(x, y, idLE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-30 06:27:20 +03:00
|
|
|
* Document-method: Integer#~
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-30 06:27:20 +03:00
|
|
|
* ~integer -> integer
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* One's complement: returns a number where each bit is flipped.
|
2016-04-30 06:27:20 +03:00
|
|
|
*
|
|
|
|
* Inverts the bits in a integer. As Integers are conceptually infinite
|
|
|
|
* length, the result acts as if it had an infinite number of one
|
|
|
|
* bits to the left. In hex representations, this is displayed
|
|
|
|
* as two periods to the left of the digits.
|
|
|
|
*
|
|
|
|
* sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
|
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-04-30 06:30:53 +03:00
|
|
|
fix_comp(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2010-07-07 14:59:44 +04:00
|
|
|
return ~num | FIXNUM_FLAG;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 06:27:20 +03:00
|
|
|
static VALUE
|
|
|
|
int_comp(VALUE num)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
2016-04-30 06:30:53 +03:00
|
|
|
return fix_comp(num);
|
2016-04-30 06:27:20 +03:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
2016-04-30 06:30:53 +03:00
|
|
|
return rb_big_comp(num);
|
2016-04-30 06:27:20 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2012-12-22 19:06:22 +04:00
|
|
|
static int
|
2014-06-07 13:16:21 +04:00
|
|
|
bit_coerce(VALUE *x, VALUE *y)
|
2012-12-22 19:06:22 +04:00
|
|
|
{
|
|
|
|
if (!FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
|
2015-01-12 12:56:14 +03:00
|
|
|
VALUE orig = *x;
|
2014-06-07 13:16:21 +04:00
|
|
|
do_coerce(x, y, TRUE);
|
2012-12-22 19:06:22 +04:00
|
|
|
if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM)
|
|
|
|
&& !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
|
2015-01-12 12:56:14 +03:00
|
|
|
coerce_failed(orig, *y);
|
2012-12-22 19:06:22 +04:00
|
|
|
}
|
2008-03-11 13:48:12 +03:00
|
|
|
}
|
2012-12-22 19:06:22 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_num_coerce_bit(VALUE x, VALUE y, ID func)
|
|
|
|
{
|
2014-06-07 13:16:21 +04:00
|
|
|
bit_coerce(&x, &y);
|
2012-12-22 19:06:22 +04:00
|
|
|
return rb_funcall(x, func, 1, y);
|
2008-03-11 13:48:12 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-30 06:08:53 +03:00
|
|
|
* Document-method: Integer#&
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-30 06:08:53 +03:00
|
|
|
* integer & integer -> integer_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* Bitwise AND.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_and(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-12-22 19:06:22 +04:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long val = FIX2LONG(x) & FIX2LONG(y);
|
|
|
|
return LONG2NUM(val);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-12-22 19:06:22 +04:00
|
|
|
if (RB_TYPE_P(y, T_BIGNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_big_and(y, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2012-12-22 19:06:22 +04:00
|
|
|
|
2014-06-07 13:16:21 +04:00
|
|
|
bit_coerce(&x, &y);
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(x, '&', 1, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 06:08:53 +03:00
|
|
|
static VALUE
|
|
|
|
int_and(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_and(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_and(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-30 05:54:14 +03:00
|
|
|
* Document-method: Integer#|
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-30 05:54:14 +03:00
|
|
|
* integer | integer -> integer_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* Bitwise OR.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_or(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-12-22 19:06:22 +04:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long val = FIX2LONG(x) | FIX2LONG(y);
|
|
|
|
return LONG2NUM(val);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-12-22 19:06:22 +04:00
|
|
|
if (RB_TYPE_P(y, T_BIGNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_big_or(y, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2012-12-22 19:06:22 +04:00
|
|
|
|
2014-06-07 13:16:21 +04:00
|
|
|
bit_coerce(&x, &y);
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(x, '|', 1, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 05:54:14 +03:00
|
|
|
static VALUE
|
|
|
|
int_or(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_or(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_or(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 18:35:23 +03:00
|
|
|
* Document-method: Integer#^
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-27 18:35:23 +03:00
|
|
|
* integer ^ integer -> integer_result
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
|
|
|
* Bitwise EXCLUSIVE OR.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_xor(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-12-22 19:06:22 +04:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long val = FIX2LONG(x) ^ FIX2LONG(y);
|
|
|
|
return LONG2NUM(val);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-12-22 19:06:22 +04:00
|
|
|
if (RB_TYPE_P(y, T_BIGNUM)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_big_xor(y, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2012-12-22 19:06:22 +04:00
|
|
|
|
2014-06-07 13:16:21 +04:00
|
|
|
bit_coerce(&x, &y);
|
2015-10-12 03:08:58 +03:00
|
|
|
return rb_funcall(x, '^', 1, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 18:35:23 +03:00
|
|
|
static VALUE
|
|
|
|
int_xor(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return fix_xor(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_xor(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#<<
|
|
|
|
* call-seq:
|
|
|
|
* int << count -> integer
|
|
|
|
*
|
|
|
|
* Shifts +int+ left +count+ positions, or right if +count+ is negative.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2007-07-19 09:38:48 +04:00
|
|
|
rb_fix_lshift(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-07-03 09:46:36 +04:00
|
|
|
long val, width;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
val = NUM2LONG(x);
|
2007-07-19 09:38:48 +04:00
|
|
|
if (!FIXNUM_P(y))
|
|
|
|
return rb_big_lshift(rb_int2big(val), y);
|
|
|
|
width = FIX2LONG(y);
|
2002-06-10 14:06:12 +04:00
|
|
|
if (width < 0)
|
2007-07-19 09:38:48 +04:00
|
|
|
return fix_rshift(val, (unsigned long)-width);
|
|
|
|
return fix_lshift(val, width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_lshift(long val, unsigned long width)
|
|
|
|
{
|
2007-12-24 21:12:24 +03:00
|
|
|
if (width > (SIZEOF_LONG*CHAR_BIT-1)
|
|
|
|
|| ((unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
|
2007-07-19 09:38:48 +04:00
|
|
|
return rb_big_lshift(rb_int2big(val), ULONG2NUM(width));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
val = val << width;
|
2002-08-28 12:05:23 +04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 15:23:05 +03:00
|
|
|
static VALUE
|
|
|
|
rb_int_lshift(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return rb_fix_lshift(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_lshift(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#>>
|
|
|
|
* call-seq:
|
|
|
|
* int >> count -> integer
|
|
|
|
*
|
|
|
|
* Shifts +int+ right +count+ positions, or left if +count+ is negative.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2007-07-19 09:38:48 +04:00
|
|
|
rb_fix_rshift(VALUE x, VALUE y)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
long i, val;
|
|
|
|
|
2002-06-10 14:06:12 +04:00
|
|
|
val = FIX2LONG(x);
|
2007-07-19 09:38:48 +04:00
|
|
|
if (!FIXNUM_P(y))
|
|
|
|
return rb_big_rshift(rb_int2big(val), y);
|
|
|
|
i = FIX2LONG(y);
|
|
|
|
if (i == 0) return x;
|
|
|
|
if (i < 0)
|
|
|
|
return fix_lshift(val, (unsigned long)-i);
|
|
|
|
return fix_rshift(val, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_rshift(long val, unsigned long i)
|
|
|
|
{
|
2000-07-03 09:46:36 +04:00
|
|
|
if (i >= sizeof(long)*CHAR_BIT-1) {
|
2002-06-10 14:06:12 +04:00
|
|
|
if (val < 0) return INT2FIX(-1);
|
2000-07-03 09:46:36 +04:00
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
2002-06-10 14:06:12 +04:00
|
|
|
val = RSHIFT(val, i);
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2FIX(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 15:09:40 +03:00
|
|
|
static VALUE
|
|
|
|
rb_int_rshift(VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(x)) {
|
|
|
|
return rb_fix_rshift(x, y);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(x, T_BIGNUM)) {
|
|
|
|
return rb_big_rshift(x, y);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
/*
|
|
|
|
* Document-method: Integer#[]
|
|
|
|
* call-seq:
|
2016-04-27 15:59:59 +03:00
|
|
|
* int[n] -> 0, 1
|
2016-04-27 15:46:46 +03:00
|
|
|
*
|
|
|
|
* Bit Reference---Returns the +n+th bit in the binary representation of
|
2016-04-27 15:59:59 +03:00
|
|
|
* +int+, where <code>int[0]</code> is the least significant bit.
|
2016-04-27 15:46:46 +03:00
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* a = 0b11001100101010
|
|
|
|
* 30.downto(0) do |n| print a[n] end
|
|
|
|
* #=> 0000000000000000011001100101010
|
|
|
|
*
|
|
|
|
* a = 9**15
|
|
|
|
* 50.downto(0) do |n|
|
|
|
|
* print a[n]
|
|
|
|
* end
|
|
|
|
* #=> 000101110110100000111000011110010100111100010111001
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
fix_aref(VALUE fix, VALUE idx)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
long val = FIX2LONG(fix);
|
2002-08-13 13:21:18 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-05-07 17:24:55 +04:00
|
|
|
idx = rb_to_int(idx);
|
|
|
|
if (!FIXNUM_P(idx)) {
|
2002-08-13 13:21:18 +04:00
|
|
|
idx = rb_big_norm(idx);
|
|
|
|
if (!FIXNUM_P(idx)) {
|
2014-02-16 01:17:34 +04:00
|
|
|
if (!BIGNUM_SIGN(idx) || val >= 0)
|
2002-08-13 13:21:18 +04:00
|
|
|
return INT2FIX(0);
|
|
|
|
return INT2FIX(1);
|
|
|
|
}
|
2001-10-29 08:07:26 +03:00
|
|
|
}
|
2008-03-11 13:48:12 +03:00
|
|
|
i = FIX2LONG(idx);
|
2001-10-29 08:07:26 +03:00
|
|
|
|
2002-08-13 13:21:18 +04:00
|
|
|
if (i < 0) return INT2FIX(0);
|
2013-10-09 19:08:41 +04:00
|
|
|
if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
|
2002-08-13 13:21:18 +04:00
|
|
|
if (val < 0) return INT2FIX(1);
|
1998-01-16 15:13:05 +03:00
|
|
|
return INT2FIX(0);
|
2001-05-02 08:22:21 +04:00
|
|
|
}
|
2002-08-13 13:21:18 +04:00
|
|
|
if (val & (1L<<i))
|
|
|
|
return INT2FIX(1);
|
|
|
|
return INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 14:56:03 +03:00
|
|
|
static VALUE
|
|
|
|
int_aref(VALUE num, VALUE idx)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return fix_aref(num, idx);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_aref(num, idx);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Integer#to_f
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-03-18 17:52:46 +03:00
|
|
|
* int.to_f -> float
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-03-18 18:02:45 +03:00
|
|
|
* Converts +int+ to a +Float+. If +int+ doesn't fit in a +Float+,
|
|
|
|
* the result is infinity.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-03-18 17:52:46 +03:00
|
|
|
int_to_f(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
double val;
|
|
|
|
|
2016-03-18 17:52:46 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
val = (double)FIX2LONG(num);
|
|
|
|
}
|
2016-03-18 18:02:45 +03:00
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
val = rb_big2dbl(num);
|
|
|
|
}
|
2016-03-18 17:52:46 +03:00
|
|
|
else {
|
2016-03-19 12:43:35 +03:00
|
|
|
rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
|
2016-03-18 17:52:46 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#abs
|
|
|
|
* Document-method: Integer#magnitude
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-26 13:59:27 +03:00
|
|
|
* int.abs -> integer
|
|
|
|
* int.magnitude -> integer
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2016-04-26 13:59:27 +03:00
|
|
|
* Returns the absolute value of +int+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* -12345.abs #=> 12345
|
|
|
|
* 12345.abs #=> 12345
|
2016-04-26 13:59:27 +03:00
|
|
|
* -1234567890987654321.abs #=> 1234567890987654321
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
fix_abs(VALUE fix)
|
|
|
|
{
|
|
|
|
long i = FIX2LONG(fix);
|
|
|
|
|
|
|
|
if (i < 0) i = -i;
|
|
|
|
|
|
|
|
return LONG2NUM(i);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-04-26 13:59:27 +03:00
|
|
|
int_abs(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-04-26 13:59:27 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return fix_abs(num);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_abs(num);
|
|
|
|
}
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#size
|
2003-12-23 19:21:17 +03:00
|
|
|
* call-seq:
|
2016-04-26 14:47:14 +03:00
|
|
|
* int.size -> int
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Returns the number of bytes in the machine representation of +fix+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2003-12-23 19:21:17 +03:00
|
|
|
* 1.size #=> 4
|
|
|
|
* -1.size #=> 4
|
|
|
|
* 2147483647.size #=> 4
|
2016-04-26 14:47:14 +03:00
|
|
|
* (256**10 - 1).size #=> 12
|
|
|
|
* (256**20 - 1).size #=> 20
|
|
|
|
* (256**40 - 1).size #=> 40
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
fix_size(VALUE fix)
|
|
|
|
{
|
|
|
|
return INT2FIX(sizeof(long));
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2016-04-26 14:47:14 +03:00
|
|
|
int_size(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-04-26 14:47:14 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return fix_size(num);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_size_m(num);
|
|
|
|
}
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2013-09-01 05:31:16 +04:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#bit_length
|
2013-09-01 05:31:16 +04:00
|
|
|
* call-seq:
|
|
|
|
* int.bit_length -> integer
|
|
|
|
*
|
|
|
|
* Returns the number of bits of the value of <i>int</i>.
|
|
|
|
*
|
|
|
|
* "the number of bits" means that
|
|
|
|
* the bit position of the highest bit which is different to the sign bit.
|
|
|
|
* (The bit position of the bit 2**n is n+1.)
|
|
|
|
* If there is no such bit (zero or minus one), zero is returned.
|
|
|
|
*
|
|
|
|
* I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
|
2016-04-27 15:46:46 +03:00
|
|
|
*
|
2016-04-26 14:17:37 +03:00
|
|
|
* (-2**10000-1).bit_length #=> 10001
|
|
|
|
* (-2**10000).bit_length #=> 10000
|
|
|
|
* (-2**10000+1).bit_length #=> 10000
|
|
|
|
* (-2**1000-1).bit_length #=> 1001
|
|
|
|
* (-2**1000).bit_length #=> 1000
|
|
|
|
* (-2**1000+1).bit_length #=> 1000
|
2013-09-01 05:31:16 +04:00
|
|
|
* (-2**12-1).bit_length #=> 13
|
|
|
|
* (-2**12).bit_length #=> 12
|
|
|
|
* (-2**12+1).bit_length #=> 12
|
|
|
|
* -0x101.bit_length #=> 9
|
|
|
|
* -0x100.bit_length #=> 8
|
|
|
|
* -0xff.bit_length #=> 8
|
|
|
|
* -2.bit_length #=> 1
|
|
|
|
* -1.bit_length #=> 0
|
|
|
|
* 0.bit_length #=> 0
|
|
|
|
* 1.bit_length #=> 1
|
|
|
|
* 0xff.bit_length #=> 8
|
|
|
|
* 0x100.bit_length #=> 9
|
|
|
|
* (2**12-1).bit_length #=> 12
|
|
|
|
* (2**12).bit_length #=> 13
|
|
|
|
* (2**12+1).bit_length #=> 13
|
2016-04-26 14:17:37 +03:00
|
|
|
* (2**1000-1).bit_length #=> 1000
|
|
|
|
* (2**1000).bit_length #=> 1001
|
|
|
|
* (2**1000+1).bit_length #=> 1001
|
|
|
|
* (2**10000-1).bit_length #=> 10000
|
|
|
|
* (2**10000).bit_length #=> 10001
|
|
|
|
* (2**10000+1).bit_length #=> 10001
|
2014-02-11 20:40:41 +04:00
|
|
|
*
|
|
|
|
* This method can be used to detect overflow in Array#pack as follows.
|
|
|
|
*
|
|
|
|
* if n.bit_length < 32
|
|
|
|
* [n].pack("l") # no overflow
|
|
|
|
* else
|
|
|
|
* raise "overflow"
|
|
|
|
* end
|
2013-09-01 05:31:16 +04:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
rb_fix_bit_length(VALUE fix)
|
|
|
|
{
|
|
|
|
long v = FIX2LONG(fix);
|
|
|
|
if (v < 0)
|
|
|
|
v = ~v;
|
|
|
|
return LONG2FIX(bit_length(v));
|
|
|
|
}
|
|
|
|
|
2013-09-01 05:31:16 +04:00
|
|
|
static VALUE
|
2016-04-26 14:17:37 +03:00
|
|
|
rb_int_bit_length(VALUE num)
|
2013-09-01 05:31:16 +04:00
|
|
|
{
|
2016-04-26 14:17:37 +03:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
return rb_fix_bit_length(num);
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
|
|
|
return rb_big_bit_length(num);
|
|
|
|
}
|
|
|
|
return Qnil;
|
2013-09-01 05:31:16 +04:00
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#upto
|
2003-12-27 08:46:46 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.upto(limit) {|i| block } -> self
|
2010-05-13 09:49:55 +04:00
|
|
|
* int.upto(limit) -> an_enumerator
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Iterates the given block, passing in integer values from +int+ up to and
|
|
|
|
* including +limit+.
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* If no block is given, an Enumerator is returned instead.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* For example:
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* 5.upto(10) { |i| print i, " " }
|
|
|
|
* #=> 5 6 7 8 9 10
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
int_upto_size(VALUE from, VALUE args, VALUE eobj)
|
|
|
|
{
|
|
|
|
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(1), FALSE);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
int_upto(VALUE from, VALUE to)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-11-06 21:15:15 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
|
2002-04-24 08:54:16 +04:00
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to)) {
|
|
|
|
long i, end;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
end = FIX2LONG(to);
|
|
|
|
for (i = FIX2LONG(from); i <= end; i++) {
|
2002-08-21 19:47:54 +04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 08:54:16 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-04-24 08:54:16 +04:00
|
|
|
else {
|
2003-05-08 07:56:12 +04:00
|
|
|
VALUE i = from, c;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-05-08 07:56:12 +04:00
|
|
|
while (!(c = rb_funcall(i, '>', 1, to))) {
|
2002-04-24 08:54:16 +04:00
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '+', 1, INT2FIX(1));
|
|
|
|
}
|
2003-05-08 07:56:12 +04:00
|
|
|
if (NIL_P(c)) rb_cmperr(i, to);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#downto
|
2003-12-27 08:46:46 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.downto(limit) {|i| block } -> self
|
2010-05-13 09:49:55 +04:00
|
|
|
* int.downto(limit) -> an_enumerator
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Iterates the given block, passing decreasing values from +int+ down to and
|
|
|
|
* including +limit+.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* If no block is given, an Enumerator is returned instead.
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 5.downto(1) { |n| print n, ".. " }
|
|
|
|
* print " Liftoff!\n"
|
2013-07-15 22:27:23 +04:00
|
|
|
* #=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
int_downto_size(VALUE from, VALUE args, VALUE eobj)
|
|
|
|
{
|
|
|
|
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(-1), FALSE);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
int_downto(VALUE from, VALUE to)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-11-06 21:15:15 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
|
2002-04-24 08:54:16 +04:00
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to)) {
|
|
|
|
long i, end;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
end = FIX2LONG(to);
|
|
|
|
for (i=FIX2LONG(from); i >= end; i--) {
|
2002-08-21 19:47:54 +04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 08:54:16 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2003-05-08 07:56:12 +04:00
|
|
|
VALUE i = from, c;
|
2002-04-24 08:54:16 +04:00
|
|
|
|
2003-05-08 07:56:12 +04:00
|
|
|
while (!(c = rb_funcall(i, '<', 1, to))) {
|
2002-04-24 08:54:16 +04:00
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '-', 1, INT2FIX(1));
|
|
|
|
}
|
2003-05-08 07:56:12 +04:00
|
|
|
if (NIL_P(c)) rb_cmperr(i, to);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2003-12-27 08:46:46 +03:00
|
|
|
/*
|
2016-04-27 15:46:46 +03:00
|
|
|
* Document-method: Integer#times
|
2003-12-27 08:46:46 +03:00
|
|
|
* call-seq:
|
2009-06-27 11:46:57 +04:00
|
|
|
* int.times {|i| block } -> self
|
2010-05-13 09:49:55 +04:00
|
|
|
* int.times -> an_enumerator
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Iterates the given block +int+ times, passing in values from zero to
|
|
|
|
* <code>int - 1</code>.
|
2007-05-09 08:11:41 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* If no block is given, an Enumerator is returned instead.
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2003-12-27 08:46:46 +03:00
|
|
|
* 5.times do |i|
|
|
|
|
* print i, " "
|
|
|
|
* end
|
2013-07-15 22:27:23 +04:00
|
|
|
* #=> 0 1 2 3 4
|
2003-12-27 08:46:46 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-27 15:46:46 +03:00
|
|
|
static VALUE
|
|
|
|
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
if (NUM2LONG(num) <= 0) return INT2FIX(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
int_dotimes(VALUE num)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-11-06 21:15:30 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-06-29 10:43:24 +04:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
long i, end;
|
2006-12-31 18:02:22 +03:00
|
|
|
|
2007-06-29 10:43:24 +04:00
|
|
|
end = FIX2LONG(num);
|
|
|
|
for (i=0; i<end; i++) {
|
2015-10-11 00:22:54 +03:00
|
|
|
rb_yield_1(LONG2FIX(i));
|
2007-06-29 10:43:24 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2007-06-29 10:43:24 +04:00
|
|
|
else {
|
|
|
|
VALUE i = INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2007-06-29 10:43:24 +04:00
|
|
|
for (;;) {
|
|
|
|
if (!RTEST(rb_funcall(i, '<', 1, num))) break;
|
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '+', 1, INT2FIX(1));
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2007-06-29 10:43:24 +04:00
|
|
|
return num;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2009-09-05 10:41:40 +04:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Integer#round
|
2009-09-05 10:41:40 +04:00
|
|
|
* call-seq:
|
2011-08-25 02:57:42 +04:00
|
|
|
* int.round([ndigits]) -> integer or float
|
2009-09-05 10:41:40 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Rounds +int+ to a given precision in decimal digits (default 0 digits).
|
|
|
|
*
|
2009-09-05 10:41:40 +04:00
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and round down for negative.
|
|
|
|
*
|
|
|
|
* 1.round #=> 1
|
|
|
|
* 1.round(2) #=> 1.0
|
|
|
|
* 15.round(-1) #=> 20
|
|
|
|
*/
|
|
|
|
|
2007-06-05 09:31:05 +04:00
|
|
|
static VALUE
|
|
|
|
int_round(int argc, VALUE* argv, VALUE num)
|
|
|
|
{
|
|
|
|
int ndigits;
|
|
|
|
|
2016-04-02 09:33:26 +03:00
|
|
|
if (!rb_check_arity(argc, 0, 1)) return num;
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
2007-06-05 09:31:05 +04:00
|
|
|
if (ndigits > 0) {
|
|
|
|
return rb_Float(num);
|
|
|
|
}
|
2007-06-29 10:44:01 +04:00
|
|
|
if (ndigits == 0) {
|
|
|
|
return num;
|
|
|
|
}
|
2016-03-26 04:54:50 +03:00
|
|
|
return rb_int_round(num, ndigits);
|
2007-06-05 09:31:05 +04:00
|
|
|
}
|
|
|
|
|
2016-04-13 09:47:55 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Integer#floor
|
2016-04-13 09:47:55 +03:00
|
|
|
* call-seq:
|
|
|
|
* int.floor([ndigits]) -> integer or float
|
|
|
|
*
|
|
|
|
* Returns the largest number less than or equal to +int+ in decimal
|
|
|
|
* digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and floor down for negative.
|
|
|
|
*
|
|
|
|
* 1.floor #=> 1
|
|
|
|
* 1.floor(2) #=> 1.0
|
|
|
|
* 15.floor(-1) #=> 10
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_floor(int argc, VALUE* argv, VALUE num)
|
|
|
|
{
|
|
|
|
int ndigits;
|
|
|
|
|
|
|
|
if (!rb_check_arity(argc, 0, 1)) return num;
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
|
|
|
if (ndigits > 0) {
|
|
|
|
return rb_Float(num);
|
|
|
|
}
|
|
|
|
if (ndigits == 0) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
return rb_int_floor(num, ndigits);
|
|
|
|
}
|
|
|
|
|
2016-04-13 09:50:24 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Integer#ceil
|
2016-04-13 09:50:24 +03:00
|
|
|
* call-seq:
|
|
|
|
* int.ceil([ndigits]) -> integer or float
|
|
|
|
*
|
|
|
|
* Returns the smallest number than or equal to +int+ in decimal
|
|
|
|
* digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and ceil up for negative.
|
|
|
|
*
|
|
|
|
* 1.ceil #=> 1
|
|
|
|
* 1.ceil(2) #=> 1.0
|
|
|
|
* 15.ceil(-1) #=> 20
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_ceil(int argc, VALUE* argv, VALUE num)
|
|
|
|
{
|
|
|
|
int ndigits;
|
|
|
|
|
|
|
|
if (!rb_check_arity(argc, 0, 1)) return num;
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
|
|
|
if (ndigits > 0) {
|
|
|
|
return rb_Float(num);
|
|
|
|
}
|
|
|
|
if (ndigits == 0) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
return rb_int_ceil(num, ndigits);
|
|
|
|
}
|
|
|
|
|
2016-04-18 06:55:33 +03:00
|
|
|
/*
|
2016-04-27 16:05:09 +03:00
|
|
|
* Document-method: Integer#truncate
|
2016-04-18 06:55:33 +03:00
|
|
|
* call-seq:
|
|
|
|
* int.truncate([ndigits]) -> integer or float
|
|
|
|
*
|
|
|
|
* Returns the smallest number than or equal to +int+ in decimal
|
|
|
|
* digits (default 0 digits).
|
|
|
|
*
|
|
|
|
* Precision may be negative. Returns a floating point number when +ndigits+
|
|
|
|
* is positive, +self+ for zero, and truncate up for negative.
|
|
|
|
*
|
|
|
|
* 1.truncate #=> 1
|
|
|
|
* 1.truncate(2) #=> 1.0
|
|
|
|
* 15.truncate(-1) #=> 10
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_truncate(int argc, VALUE* argv, VALUE num)
|
|
|
|
{
|
|
|
|
int ndigits;
|
|
|
|
|
|
|
|
if (!rb_check_arity(argc, 0, 1)) return num;
|
|
|
|
ndigits = NUM2INT(argv[0]);
|
|
|
|
if (ndigits > 0) {
|
|
|
|
return rb_Float(num);
|
|
|
|
}
|
|
|
|
if (ndigits == 0) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
return rb_int_truncate(num, ndigits);
|
|
|
|
}
|
|
|
|
|
2010-05-08 08:50:09 +04:00
|
|
|
/*
|
|
|
|
* Document-class: ZeroDivisionError
|
|
|
|
*
|
|
|
|
* Raised when attempting to divide an integer by 0.
|
|
|
|
*
|
|
|
|
* 42 / 0
|
2013-07-15 22:27:23 +04:00
|
|
|
* #=> ZeroDivisionError: divided by 0
|
2010-05-08 08:50:09 +04:00
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Note that only division by an exact 0 will raise the exception:
|
2010-05-08 08:50:09 +04:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* 42 / 0.0 #=> Float::INFINITY
|
|
|
|
* 42 / -0.0 #=> -Float::INFINITY
|
|
|
|
* 0 / 0.0 #=> NaN
|
2010-05-08 08:50:09 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: FloatDomainError
|
|
|
|
*
|
2013-07-15 22:27:23 +04:00
|
|
|
* Raised when attempting to convert special float values (in particular
|
|
|
|
* +infinite+ or +NaN+) to numerical classes which don't support them.
|
2010-05-08 08:50:09 +04:00
|
|
|
*
|
|
|
|
* Float::INFINITY.to_r
|
2013-07-15 22:27:23 +04:00
|
|
|
* #=> FloatDomainError: Infinity
|
2010-05-08 08:50:09 +04:00
|
|
|
*/
|
|
|
|
|
2013-07-15 06:33:58 +04:00
|
|
|
/*
|
2015-10-05 09:42:14 +03:00
|
|
|
* Document-class: Numeric
|
|
|
|
*
|
|
|
|
* Numeric is the class from which all higher-level numeric classes should inherit.
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as
|
|
|
|
* Integer are implemented as immediates, which means that each Integer is a single immutable
|
|
|
|
* object which is always passed by value.
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* a = 1
|
|
|
|
* puts 1.object_id == a.object_id #=> true
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* There can only ever be one instance of the integer +1+, for example. Ruby ensures this
|
|
|
|
* by preventing instantiation and duplication.
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
|
|
|
|
* 1.dup #=> TypeError: can't dup Fixnum
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* For this reason, Numeric should be used when defining other numeric classes.
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* Classes which inherit from Numeric must implement +coerce+, which returns a two-member
|
|
|
|
* Array containing an object that has been coerced into an instance of the new class
|
|
|
|
* and +self+ (see #coerce).
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* Inheriting classes should also implement arithmetic operator methods (<code>+</code>,
|
|
|
|
* <code>-</code>, <code>*</code> and <code>/</code>) and the <code><=></code> operator (see
|
|
|
|
* Comparable). These methods may rely on +coerce+ to ensure interoperability with
|
|
|
|
* instances of other numeric classes.
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* class Tally < Numeric
|
|
|
|
* def initialize(string)
|
|
|
|
* @string = string
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def to_s
|
|
|
|
* @string
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def to_i
|
|
|
|
* @string.size
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def coerce(other)
|
|
|
|
* [self.class.new('|' * other.to_i), self]
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def <=>(other)
|
|
|
|
* to_i <=> other.to_i
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def +(other)
|
|
|
|
* self.class.new('|' * (to_i + other.to_i))
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def -(other)
|
|
|
|
* self.class.new('|' * (to_i - other.to_i))
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def *(other)
|
|
|
|
* self.class.new('|' * (to_i * other.to_i))
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* def /(other)
|
|
|
|
* self.class.new('|' * (to_i / other.to_i))
|
|
|
|
* end
|
|
|
|
* end
|
2015-10-05 09:42:18 +03:00
|
|
|
*
|
2015-10-05 09:42:14 +03:00
|
|
|
* tally = Tally.new('||')
|
|
|
|
* puts tally * 2 #=> "||||"
|
|
|
|
* puts tally > 1 #=> true
|
2013-07-15 06:33:58 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
Init_Numeric(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
#undef rb_intern
|
2008-08-16 04:20:31 +04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2008-06-09 13:25:32 +04:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ < 4
|
1999-08-13 09:45:20 +04:00
|
|
|
/* allow divide by zero -- Inf */
|
|
|
|
fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
|
2003-12-20 18:45:15 +03:00
|
|
|
#elif defined(_UNICOSMP)
|
|
|
|
/* Turn off floating point exceptions for divide by zero, etc. */
|
|
|
|
_set_Creg(0, 0);
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
2001-08-06 07:05:23 +04:00
|
|
|
id_coerce = rb_intern("coerce");
|
2012-11-06 21:14:31 +04:00
|
|
|
id_div = rb_intern("div");
|
2015-10-12 03:08:58 +03:00
|
|
|
id_divmod = rb_intern("divmod");
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cNumeric = rb_define_class("Numeric", rb_cObject);
|
|
|
|
|
2003-12-01 16:16:09 +03:00
|
|
|
rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_include_module(rb_cNumeric, rb_mComparable);
|
2003-05-19 09:41:08 +04:00
|
|
|
rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1);
|
2003-04-03 09:25:00 +04:00
|
|
|
rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2009-08-17 02:28:48 +04:00
|
|
|
rb_define_method(rb_cNumeric, "i", num_imaginary, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
|
2002-11-22 12:14:24 +03:00
|
|
|
rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
|
2008-05-27 16:47:14 +04:00
|
|
|
rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1);
|
2003-01-23 09:22:50 +03:00
|
|
|
rb_define_method(rb_cNumeric, "div", num_div, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
|
2009-06-20 16:37:13 +04:00
|
|
|
rb_define_method(rb_cNumeric, "%", num_modulo, 1);
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
|
2000-07-04 08:17:26 +04:00
|
|
|
rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cNumeric, "abs", num_abs, 0);
|
2008-08-29 17:41:41 +04:00
|
|
|
rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
|
2002-07-29 10:14:10 +04:00
|
|
|
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-09-17 02:04:19 +04:00
|
|
|
rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
|
|
|
|
|
2016-04-18 06:57:34 +03:00
|
|
|
rb_define_method(rb_cNumeric, "floor", num_floor, -1);
|
|
|
|
rb_define_method(rb_cNumeric, "ceil", num_ceil, -1);
|
2007-05-31 21:01:15 +04:00
|
|
|
rb_define_method(rb_cNumeric, "round", num_round, -1);
|
2016-04-18 06:57:34 +03:00
|
|
|
rb_define_method(rb_cNumeric, "truncate", num_truncate, -1);
|
2002-04-24 08:54:16 +04:00
|
|
|
rb_define_method(rb_cNumeric, "step", num_step, -1);
|
2015-05-17 09:01:47 +03:00
|
|
|
rb_define_method(rb_cNumeric, "positive?", num_positive_p, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "negative?", num_negative_p, 0);
|
2000-07-06 11:21:26 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cInteger = rb_define_class("Integer", rb_cNumeric);
|
2002-12-20 11:33:17 +03:00
|
|
|
rb_undef_alloc_func(rb_cInteger);
|
2001-08-29 10:28:51 +04:00
|
|
|
rb_undef_method(CLASS_OF(rb_cInteger), "new");
|
|
|
|
|
2016-03-18 15:33:28 +03:00
|
|
|
rb_define_method(rb_cInteger, "to_s", int_to_s, -1);
|
|
|
|
rb_define_alias(rb_cInteger, "inspect", "to_s");
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
|
2006-09-21 10:09:26 +04:00
|
|
|
rb_define_method(rb_cInteger, "odd?", int_odd_p, 0);
|
|
|
|
rb_define_method(rb_cInteger, "even?", int_even_p, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cInteger, "upto", int_upto, 1);
|
|
|
|
rb_define_method(rb_cInteger, "downto", int_downto, 1);
|
|
|
|
rb_define_method(rb_cInteger, "times", int_dotimes, 0);
|
|
|
|
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
|
|
|
rb_define_method(rb_cInteger, "next", int_succ, 0);
|
2007-01-30 07:27:04 +03:00
|
|
|
rb_define_method(rb_cInteger, "pred", int_pred, 0);
|
2007-10-26 12:38:14 +04:00
|
|
|
rb_define_method(rb_cInteger, "chr", int_chr, -1);
|
2008-12-22 15:32:11 +03:00
|
|
|
rb_define_method(rb_cInteger, "ord", int_ord, 0);
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
|
2000-11-13 08:39:35 +03:00
|
|
|
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
|
2016-03-18 17:52:46 +03:00
|
|
|
rb_define_method(rb_cInteger, "to_f", int_to_f, 0);
|
2016-04-13 09:47:55 +03:00
|
|
|
rb_define_method(rb_cInteger, "floor", int_floor, -1);
|
2016-04-13 09:50:24 +03:00
|
|
|
rb_define_method(rb_cInteger, "ceil", int_ceil, -1);
|
2016-04-18 06:55:33 +03:00
|
|
|
rb_define_method(rb_cInteger, "truncate", int_truncate, -1);
|
2007-06-01 20:27:05 +04:00
|
|
|
rb_define_method(rb_cInteger, "round", int_round, -1);
|
2016-03-19 12:28:12 +03:00
|
|
|
rb_define_method(rb_cInteger, "<=>", int_cmp, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-12-29 14:57:06 +03:00
|
|
|
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
|
|
|
|
rb_define_method(rb_cFixnum, "+", fix_plus, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "-", fix_minus, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "*", fix_mul, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "/", fix_div, 1);
|
2016-04-30 08:43:15 +03:00
|
|
|
rb_define_method(rb_cInteger, "div", rb_int_idiv, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFixnum, "%", fix_mod, 1);
|
2016-04-30 08:26:52 +03:00
|
|
|
rb_define_method(rb_cInteger, "modulo", rb_int_modulo, 1);
|
2016-04-30 08:05:54 +03:00
|
|
|
rb_define_method(rb_cInteger, "divmod", int_divmod, 1);
|
2016-04-30 07:20:56 +03:00
|
|
|
rb_define_method(rb_cInteger, "fdiv", int_fdiv, 1);
|
2016-04-30 06:59:02 +03:00
|
|
|
rb_define_method(rb_cInteger, "**", rb_int_pow, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-04-26 13:59:27 +03:00
|
|
|
rb_define_method(rb_cInteger, "abs", int_abs, 0);
|
|
|
|
rb_define_method(rb_cInteger, "magnitude", int_abs, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "==", fix_equal, 1);
|
2009-08-12 09:55:06 +04:00
|
|
|
rb_define_method(rb_cFixnum, "===", fix_equal, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFixnum, ">", fix_gt, 1);
|
|
|
|
rb_define_method(rb_cFixnum, ">=", fix_ge, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "<", fix_lt, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "<=", fix_le, 1);
|
|
|
|
|
2016-04-30 06:27:20 +03:00
|
|
|
rb_define_method(rb_cInteger, "~", int_comp, 0);
|
2016-04-30 06:08:53 +03:00
|
|
|
rb_define_method(rb_cInteger, "&", int_and, 1);
|
2016-04-30 05:54:14 +03:00
|
|
|
rb_define_method(rb_cInteger, "|", int_or, 1);
|
2016-04-27 18:35:23 +03:00
|
|
|
rb_define_method(rb_cInteger, "^", int_xor, 1);
|
2016-04-27 14:56:03 +03:00
|
|
|
rb_define_method(rb_cInteger, "[]", int_aref, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-04-26 15:23:05 +03:00
|
|
|
rb_define_method(rb_cInteger, "<<", rb_int_lshift, 1);
|
2016-04-26 15:09:40 +03:00
|
|
|
rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-04-26 14:47:14 +03:00
|
|
|
rb_define_method(rb_cInteger, "size", int_size, 0);
|
2016-04-26 14:17:37 +03:00
|
|
|
rb_define_method(rb_cInteger, "bit_length", rb_int_bit_length, 0);
|
2006-12-31 18:02:22 +03:00
|
|
|
rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_cFloat = rb_define_class("Float", rb_cNumeric);
|
|
|
|
|
2002-12-20 11:33:17 +03:00
|
|
|
rb_undef_alloc_func(rb_cFloat);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_undef_method(CLASS_OF(rb_cFloat), "new");
|
|
|
|
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* Represents the rounding mode for floating point addition.
|
|
|
|
*
|
|
|
|
* Usually defaults to 1, rounding to the nearest number.
|
|
|
|
*
|
|
|
|
* Other modes include:
|
|
|
|
*
|
|
|
|
* -1:: Indeterminable
|
|
|
|
* 0:: Rounding towards zero
|
|
|
|
* 1:: Rounding to the nearest number
|
|
|
|
* 2:: Rounding towards positive infinity
|
|
|
|
* 3:: Rounding towards negative infinity
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "ROUNDS", INT2FIX(FLT_ROUNDS));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The base of the floating point, or number of unique digits used to
|
|
|
|
* represent the number.
|
|
|
|
*
|
|
|
|
* Usually defaults to 2 on most systems, which would represent a base-10 decimal.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "RADIX", INT2FIX(FLT_RADIX));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The number of base digits for the +double+ data type.
|
|
|
|
*
|
|
|
|
* Usually defaults to 53.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "MANT_DIG", INT2FIX(DBL_MANT_DIG));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
2014-03-02 05:55:02 +04:00
|
|
|
* The minimum number of significant decimal digits in a double-precision
|
|
|
|
* floating point.
|
2013-02-03 04:59:51 +04:00
|
|
|
*
|
|
|
|
* Usually defaults to 15.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "DIG", INT2FIX(DBL_DIG));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The smallest posable exponent value in a double-precision floating
|
|
|
|
* point.
|
|
|
|
*
|
|
|
|
* Usually defaults to -1021.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "MIN_EXP", INT2FIX(DBL_MIN_EXP));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The largest possible exponent value in a double-precision floating
|
|
|
|
* point.
|
|
|
|
*
|
|
|
|
* Usually defaults to 1024.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "MAX_EXP", INT2FIX(DBL_MAX_EXP));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The smallest negative exponent in a double-precision floating point
|
|
|
|
* where 10 raised to this power minus 1.
|
|
|
|
*
|
|
|
|
* Usually defaults to -307.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The largest positive exponent in a double-precision floating point where
|
|
|
|
* 10 raised to this power minus 1.
|
|
|
|
*
|
|
|
|
* Usually defaults to 308.
|
|
|
|
*/
|
2002-12-24 11:53:56 +03:00
|
|
|
rb_define_const(rb_cFloat, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
2015-08-11 17:01:22 +03:00
|
|
|
* The smallest positive normalized number in a double-precision floating point.
|
2013-02-03 04:59:51 +04:00
|
|
|
*
|
|
|
|
* Usually defaults to 2.2250738585072014e-308.
|
2015-08-11 17:01:22 +03:00
|
|
|
*
|
|
|
|
* If the platform supports denormalized numbers,
|
|
|
|
* there are numbers between zero and Float::MIN.
|
|
|
|
* 0.0.next_float returns the smallest positive floating point number
|
|
|
|
* including denormalized numbers.
|
2013-02-03 04:59:51 +04:00
|
|
|
*/
|
2008-09-05 22:24:21 +04:00
|
|
|
rb_define_const(rb_cFloat, "MIN", DBL2NUM(DBL_MIN));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The largest possible integer in a double-precision floating point number.
|
|
|
|
*
|
|
|
|
* Usually defaults to 1.7976931348623157e+308.
|
|
|
|
*/
|
2008-09-05 22:24:21 +04:00
|
|
|
rb_define_const(rb_cFloat, "MAX", DBL2NUM(DBL_MAX));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* The difference between 1 and the smallest double-precision floating
|
2015-08-11 17:01:22 +03:00
|
|
|
* point number greater than 1.
|
2013-02-03 04:59:51 +04:00
|
|
|
*
|
|
|
|
* Usually defaults to 2.2204460492503131e-16.
|
|
|
|
*/
|
2008-09-05 22:24:21 +04:00
|
|
|
rb_define_const(rb_cFloat, "EPSILON", DBL2NUM(DBL_EPSILON));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* An expression representing positive infinity.
|
|
|
|
*/
|
2009-12-29 10:05:39 +03:00
|
|
|
rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY));
|
2013-02-03 04:59:51 +04:00
|
|
|
/*
|
|
|
|
* An expression representing a value which is "not a number".
|
|
|
|
*/
|
2009-12-29 10:05:39 +03:00
|
|
|
rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
|
2002-12-24 11:53:56 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
|
2012-08-15 15:50:01 +04:00
|
|
|
rb_define_alias(rb_cFloat, "inspect", "to_s");
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
|
|
|
|
rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
|
|
|
|
rb_define_method(rb_cFloat, "+", flo_plus, 1);
|
|
|
|
rb_define_method(rb_cFloat, "-", flo_minus, 1);
|
|
|
|
rb_define_method(rb_cFloat, "*", flo_mul, 1);
|
|
|
|
rb_define_method(rb_cFloat, "/", flo_div, 1);
|
2008-04-07 17:52:26 +04:00
|
|
|
rb_define_method(rb_cFloat, "quo", flo_quo, 1);
|
|
|
|
rb_define_method(rb_cFloat, "fdiv", flo_quo, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "%", flo_mod, 1);
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
|
2000-02-01 06:12:21 +03:00
|
|
|
rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "**", flo_pow, 1);
|
|
|
|
rb_define_method(rb_cFloat, "==", flo_eq, 1);
|
2009-08-12 09:55:06 +04:00
|
|
|
rb_define_method(rb_cFloat, "===", flo_eq, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
|
|
|
|
rb_define_method(rb_cFloat, ">", flo_gt, 1);
|
|
|
|
rb_define_method(rb_cFloat, ">=", flo_ge, 1);
|
|
|
|
rb_define_method(rb_cFloat, "<", flo_lt, 1);
|
|
|
|
rb_define_method(rb_cFloat, "<=", flo_le, 1);
|
|
|
|
rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
|
|
|
|
rb_define_method(rb_cFloat, "hash", flo_hash, 0);
|
|
|
|
rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
|
|
|
|
rb_define_method(rb_cFloat, "abs", flo_abs, 0);
|
2008-08-29 17:41:41 +04:00
|
|
|
rb_define_method(rb_cFloat, "magnitude", flo_abs, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
|
|
|
|
|
2016-04-18 06:56:33 +03:00
|
|
|
rb_define_method(rb_cFloat, "to_i", flo_to_i, 0);
|
|
|
|
rb_define_method(rb_cFloat, "to_int", flo_to_i, 0);
|
2016-04-13 09:54:38 +03:00
|
|
|
rb_define_method(rb_cFloat, "floor", flo_floor, -1);
|
2016-04-13 09:56:36 +03:00
|
|
|
rb_define_method(rb_cFloat, "ceil", flo_ceil, -1);
|
2007-05-31 21:01:15 +04:00
|
|
|
rb_define_method(rb_cFloat, "round", flo_round, -1);
|
2016-04-18 06:56:33 +03:00
|
|
|
rb_define_method(rb_cFloat, "truncate", flo_truncate, -1);
|
2000-06-12 11:48:31 +04:00
|
|
|
|
|
|
|
rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
|
|
|
|
rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
|
|
|
|
rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0);
|
2014-05-18 04:37:10 +04:00
|
|
|
rb_define_method(rb_cFloat, "next_float", flo_next_float, 0);
|
|
|
|
rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0);
|
2015-05-17 09:01:47 +03:00
|
|
|
rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0);
|
|
|
|
rb_define_method(rb_cFloat, "negative?", flo_negative_p, 0);
|
2013-09-02 18:56:06 +04:00
|
|
|
|
2014-07-01 18:36:03 +04:00
|
|
|
id_to = rb_intern("to");
|
|
|
|
id_by = rb_intern("by");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2013-09-25 11:58:49 +04:00
|
|
|
|
|
|
|
#undef rb_float_value
|
|
|
|
double
|
|
|
|
rb_float_value(VALUE v)
|
|
|
|
{
|
|
|
|
return rb_float_value_inline(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef rb_float_new
|
|
|
|
VALUE
|
|
|
|
rb_float_new(double d)
|
|
|
|
{
|
|
|
|
return rb_float_new_inline(d);
|
|
|
|
}
|