2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
numeric.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Aug 13 18:33:09 JST 1993
|
|
|
|
|
2003-01-16 02:34:03 -05:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
2003-07-25 01:36:55 -04:00
|
|
|
#include "env.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <math.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
#include <stdio.h>
|
2002-12-19 04:20:20 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ < 4
|
1999-08-13 01:45:20 -04:00
|
|
|
#include <floatingpoint.h>
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
|
|
|
|
2002-12-24 03:53:56 -05: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 05:20:29 -05:00
|
|
|
#define DBL_MAX_10_EXP 308
|
2002-12-24 03:53:56 -05:00
|
|
|
#endif
|
|
|
|
#ifndef DBL_DIG
|
|
|
|
#define DBL_DIG 15
|
|
|
|
#endif
|
|
|
|
#ifndef DBL_MANT_DIG
|
|
|
|
#define DBL_MANT_DIG 53
|
|
|
|
#endif
|
2002-12-19 04:20:20 -05:00
|
|
|
#ifndef DBL_EPSILON
|
2002-12-24 03:53:56 -05:00
|
|
|
#define DBL_EPSILON 2.2204460492503131e-16
|
2002-12-19 04:20:20 -05:00
|
|
|
#endif
|
|
|
|
|
2003-07-21 14:48:58 -04:00
|
|
|
static ID id_coerce, id_to_i, id_eq;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_cNumeric;
|
|
|
|
VALUE rb_cFloat;
|
|
|
|
VALUE rb_cInteger;
|
|
|
|
VALUE rb_cFixnum;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE rb_eZeroDivError;
|
|
|
|
VALUE rb_eFloatDomainError;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_num_zerodiv()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eZeroDivError, "divided by 0");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_coerce(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (CLASS_OF(x) == CLASS_OF(y))
|
2000-02-17 02:11:22 -05:00
|
|
|
return rb_assoc_new(y, x);
|
|
|
|
return rb_assoc_new(rb_Float(y), rb_Float(x));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
coerce_body(x)
|
|
|
|
VALUE *x;
|
|
|
|
{
|
2001-08-05 23:05:23 -04:00
|
|
|
return rb_funcall(x[1], id_coerce, 1, x[0]);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
coerce_rescue(x)
|
|
|
|
VALUE *x;
|
|
|
|
{
|
2003-04-14 05:04:43 -04:00
|
|
|
volatile VALUE v = rb_inspect(x[1]);
|
2003-04-11 02:37:48 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eTypeError, "%s can't be coerced into %s",
|
|
|
|
rb_special_const_p(x[1])?
|
2003-04-14 05:04:43 -04:00
|
|
|
RSTRING(v)->ptr:
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(x[1]),
|
|
|
|
rb_obj_classname(x[0]));
|
1999-12-06 04:04:03 -05:00
|
|
|
return Qnil; /* dummy */
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
static int
|
|
|
|
do_coerce(x, y, err)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *x, *y;
|
2002-12-19 04:20:20 -05:00
|
|
|
int err;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE ary;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE a[2];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
a[0] = *x; a[1] = *y;
|
2001-07-24 05:07:33 -04:00
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
ary = rb_rescue(coerce_body, (VALUE)a, err?coerce_rescue:0, (VALUE)a);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (TYPE(ary) != T_ARRAY || RARRAY(ary)->len != 2) {
|
2002-12-19 04:20:20 -05:00
|
|
|
if (err) {
|
|
|
|
rb_raise(rb_eTypeError, "coerce must return [x, y]");
|
|
|
|
}
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
*x = RARRAY(ary)->ptr[0];
|
|
|
|
*y = RARRAY(ary)->ptr[1];
|
2002-12-19 04:20:20 -05:00
|
|
|
return Qtrue;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
|
|
|
rb_num_coerce_bin(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2002-12-19 04:20:20 -05:00
|
|
|
do_coerce(&x, &y, Qtrue);
|
2003-07-25 01:36:55 -04:00
|
|
|
return rb_funcall(x, ruby_frame->orig_func, 1, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
VALUE
|
|
|
|
rb_num_coerce_cmp(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (do_coerce(&x, &y, Qfalse))
|
2003-07-25 01:36:55 -04:00
|
|
|
return rb_funcall(x, ruby_frame->orig_func, 1, y);
|
2002-12-19 04:20:20 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-07-31 04:42:44 -04:00
|
|
|
VALUE
|
|
|
|
rb_num_coerce_relop(x, y)
|
2003-05-07 23:56:12 -04:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE c, x0 = x, y0 = y;
|
|
|
|
|
|
|
|
if (!do_coerce(&x, &y, Qfalse) ||
|
2003-07-25 01:36:55 -04:00
|
|
|
NIL_P(c = rb_funcall(x, ruby_frame->orig_func, 1, y))) {
|
2003-05-07 23:56:12 -04:00
|
|
|
rb_cmperr(x0, y0);
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2003-05-19 01:41:08 -04:00
|
|
|
num_init_copy(x, y)
|
2002-08-27 04:31:08 -04:00
|
|
|
VALUE x, y;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2001-10-10 04:21:13 -04:00
|
|
|
/* Numerics are immutable values, which should not be copied */
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_raise(rb_eTypeError, "can't copy %s", rb_obj_classname(x));
|
2001-10-10 04:21:13 -04:00
|
|
|
return Qnil; /* not reached */
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
num_uplus(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_uminus(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE zero;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
zero = INT2FIX(0);
|
2002-12-19 04:20:20 -05:00
|
|
|
do_coerce(&zero, &num, Qtrue);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_funcall(zero, '-', 1, num);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:22:50 -05:00
|
|
|
static VALUE
|
|
|
|
num_quo(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
return rb_funcall(x, '/', 1, y);
|
|
|
|
}
|
|
|
|
|
2001-07-31 02:24:45 -04:00
|
|
|
static VALUE
|
|
|
|
num_div(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2003-01-23 01:22:50 -05:00
|
|
|
return rb_Integer(rb_funcall(x, '/', 1, y));
|
2001-07-31 02:24:45 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
num_divmod(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2003-01-23 01:22:50 -05:00
|
|
|
return rb_assoc_new(num_div(x, y), rb_funcall(x, '%', 1, y));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-07-03 01:46:36 -04:00
|
|
|
static VALUE
|
2000-07-06 03:21:26 -04:00
|
|
|
num_modulo(x, y)
|
2000-07-03 01:46:36 -04:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
return rb_funcall(x, '%', 1, y);
|
|
|
|
}
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
static VALUE
|
|
|
|
num_remainder(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE z = rb_funcall(x, '%', 1, y);
|
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
if ((!rb_equal(z, INT2FIX(0))) &&
|
2001-10-29 00:07:26 -05:00
|
|
|
((RTEST(rb_funcall(x, '<', 1, INT2FIX(0))) &&
|
|
|
|
RTEST(rb_funcall(y, '>', 1, INT2FIX(0)))) ||
|
|
|
|
(RTEST(rb_funcall(x, '>', 1, INT2FIX(0))) &&
|
|
|
|
RTEST(rb_funcall(y, '<', 1, INT2FIX(0)))))) {
|
2000-07-06 03:21:26 -04:00
|
|
|
return rb_funcall(z, '-', 1, y);
|
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
num_int_p(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
num_abs(num)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE num;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) {
|
|
|
|
return rb_funcall(num, rb_intern("-@"), 0);
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
num_zero_p(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
if (rb_equal(num, INT2FIX(0))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
num_nonzero_p(num)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE num;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RTEST(rb_funcall(num, rb_intern("zero?"), 0, 0))) {
|
1999-08-13 01:45:20 -04:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2002-07-29 02:14:10 -04:00
|
|
|
static VALUE
|
|
|
|
num_to_int(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
return rb_funcall(num, id_to_i, 0, 0);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_float_new(d)
|
1998-01-16 07:13:05 -05:00
|
|
|
double d;
|
|
|
|
{
|
|
|
|
NEWOBJ(flt, struct RFloat);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(flt, rb_cFloat, T_FLOAT);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
flt->value = d;
|
|
|
|
return (VALUE)flt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_to_s(flt)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE flt;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-08-04 01:28:50 -04:00
|
|
|
char buf[32];
|
2003-03-20 02:15:29 -05:00
|
|
|
char *fmt = "%.15g";
|
1999-08-13 01:45:20 -04:00
|
|
|
double value = RFLOAT(flt)->value;
|
2001-09-08 10:17:53 -04:00
|
|
|
double avalue, d1, d2;
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
if (isinf(value))
|
|
|
|
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
|
|
|
|
else if(isnan(value))
|
|
|
|
return rb_str_new2("NaN");
|
2001-09-03 01:37:42 -04:00
|
|
|
|
2001-09-08 10:17:53 -04:00
|
|
|
avalue = fabs(value);
|
|
|
|
if (avalue == 0.0) {
|
|
|
|
fmt = "%.1f";
|
|
|
|
}
|
|
|
|
else if (avalue < 1.0e-3) {
|
|
|
|
d1 = avalue;
|
2001-09-03 01:37:42 -04:00
|
|
|
while (d1 < 1.0) d1 *= 10.0;
|
|
|
|
d1 = modf(d1, &d2);
|
|
|
|
if (d1 == 0) fmt = "%.1e";
|
|
|
|
}
|
2002-05-14 02:22:31 -04:00
|
|
|
else if (avalue >= 1.0e15) {
|
2001-09-08 10:17:53 -04:00
|
|
|
d1 = avalue;
|
2001-09-03 01:37:42 -04:00
|
|
|
while (d1 > 10.0) d1 /= 10.0;
|
|
|
|
d1 = modf(d1, &d2);
|
|
|
|
if (d1 == 0) fmt = "%.1e";
|
2002-05-14 02:22:31 -04:00
|
|
|
else fmt = "%.16e";
|
2001-09-03 01:37:42 -04:00
|
|
|
}
|
|
|
|
else if ((d1 = modf(value, &d2)) == 0) {
|
|
|
|
fmt = "%.1f";
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-09-03 01:37:42 -04:00
|
|
|
sprintf(buf, fmt, value);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_new2(buf);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_coerce(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_assoc_new(rb_Float(y), x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_uminus(flt)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE flt;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(-RFLOAT(flt)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_plus(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value + (double)FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value + rb_big2dbl(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value + RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_minus(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value - (double)FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value - rb_big2dbl(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value - RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_mul(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value * (double)FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value * rb_big2dbl(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value * RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_div(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
long f_y;
|
1998-01-16 07:13:05 -05:00
|
|
|
double d;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
f_y = FIX2LONG(y);
|
|
|
|
return rb_float_new(RFLOAT(x)->value / (double)f_y);
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
d = rb_big2dbl(y);
|
|
|
|
return rb_float_new(RFLOAT(x)->value / d);
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(RFLOAT(x)->value / RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-03 01:46:36 -04:00
|
|
|
static void
|
|
|
|
flodivmod(x, y, divp, modp)
|
|
|
|
double x, y;
|
|
|
|
double *divp, *modp;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
double div, mod;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#ifdef HAVE_FMOD
|
2000-07-03 01:46:36 -04:00
|
|
|
mod = fmod(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
double z;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-11-27 04:23:38 -05:00
|
|
|
modf(x/y, &z);
|
2001-05-02 00:22:21 -04:00
|
|
|
mod = x - z * y;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
#endif
|
2000-07-06 03:21:26 -04:00
|
|
|
div = (x - mod) / y;
|
|
|
|
if (y*mod < 0) {
|
|
|
|
mod += y;
|
|
|
|
div -= 1.0;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (modp) *modp = mod;
|
|
|
|
if (divp) *divp = div;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
flo_mod(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
double fy, mod;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-03 01:46:36 -04:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
fy = (double)FIX2LONG(y);
|
|
|
|
break;
|
|
|
|
case T_BIGNUM:
|
|
|
|
fy = rb_big2dbl(y);
|
|
|
|
break;
|
|
|
|
case T_FLOAT:
|
|
|
|
fy = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return rb_num_coerce_bin(x, y);
|
|
|
|
}
|
|
|
|
flodivmod(RFLOAT(x)->value, fy, 0, &mod);
|
|
|
|
return rb_float_new(mod);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
static VALUE
|
|
|
|
flo_divmod(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-07-04 00:17:26 -04:00
|
|
|
double fy, div, mod;
|
2000-01-31 22:12:21 -05:00
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
2000-07-03 01:46:36 -04:00
|
|
|
fy = (double)FIX2LONG(y);
|
2000-01-31 22:12:21 -05:00
|
|
|
break;
|
|
|
|
case T_BIGNUM:
|
2000-07-03 01:46:36 -04:00
|
|
|
fy = rb_big2dbl(y);
|
2000-01-31 22:12:21 -05:00
|
|
|
break;
|
|
|
|
case T_FLOAT:
|
2000-07-03 01:46:36 -04:00
|
|
|
fy = RFLOAT(y)->value;
|
2000-01-31 22:12:21 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return rb_num_coerce_bin(x, y);
|
|
|
|
}
|
2000-07-03 01:46:36 -04:00
|
|
|
flodivmod(RFLOAT(x)->value, fy, &div, &mod);
|
2000-01-31 22:12:21 -05:00
|
|
|
return rb_assoc_new(rb_float_new(div), rb_float_new(mod));
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
flo_pow(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(pow(RFLOAT(x)->value, (double)FIX2LONG(y)));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(pow(RFLOAT(x)->value, rb_big2dbl(y)));
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(pow(RFLOAT(x)->value, RFLOAT(y)->value));
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
num_eql(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (TYPE(x) != TYPE(y)) return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return rb_equal(x, y);
|
|
|
|
}
|
|
|
|
|
2002-11-22 04:14:24 -05:00
|
|
|
static VALUE
|
|
|
|
num_cmp(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (x == y) return INT2FIX(0);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
num_equal(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2003-07-21 14:48:58 -04:00
|
|
|
if (x == y) return Qtrue;
|
|
|
|
return rb_funcall(y, id_eq, 1, x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_eq(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
double a, b;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
2003-04-11 02:37:48 -04:00
|
|
|
b = FIX2LONG(y);
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_BIGNUM:
|
2003-04-11 02:37:48 -04:00
|
|
|
b = rb_big2dbl(y);
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
2003-04-11 02:37:48 -04:00
|
|
|
b = RFLOAT(y)->value;
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
|
|
|
return num_equal(x, y);
|
|
|
|
}
|
2003-04-11 02:37:48 -04:00
|
|
|
a = RFLOAT(x)->value;
|
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
|
|
|
return (a == b)?Qtrue:Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_hash(num)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE num;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
double d;
|
|
|
|
char *c;
|
|
|
|
int i, hash;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
d = RFLOAT(num)->value;
|
2001-05-02 00:22:21 -04:00
|
|
|
if (d == 0) d = fabs(d);
|
1998-01-16 07:13:05 -05:00
|
|
|
c = (char*)&d;
|
|
|
|
for (hash=0, i=0; i<sizeof(double);i++) {
|
|
|
|
hash += c[i] * 971;
|
|
|
|
}
|
|
|
|
if (hash < 0) hash = -hash;
|
|
|
|
return INT2FIX(hash);
|
|
|
|
}
|
|
|
|
|
2002-08-19 01:56:09 -04:00
|
|
|
VALUE
|
|
|
|
rb_dbl_cmp(a, b)
|
|
|
|
double a, b;
|
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isnan(a) || isnan(b)) return Qnil;
|
2002-08-19 01: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 04:20:20 -05:00
|
|
|
return Qnil;
|
2002-08-19 01:56:09 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
flo_cmp(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
a = RFLOAT(x)->value;
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
b = (double)FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
b = rb_big2dbl(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1998-01-16 07:19:22 -05:00
|
|
|
b = RFLOAT(y)->value;
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-12-19 04:20:20 -05:00
|
|
|
return rb_num_coerce_cmp(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-08-19 01:56:09 -04:00
|
|
|
return rb_dbl_cmp(a, b);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
flo_gt(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
|
|
|
a = RFLOAT(x)->value;
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
b = (double)FIX2LONG(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
b = rb_big2dbl(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
b = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
return (a > b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_ge(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
|
|
|
a = RFLOAT(x)->value;
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
b = (double)FIX2LONG(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
b = rb_big2dbl(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
b = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
return (a >= b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_lt(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
|
|
|
a = RFLOAT(x)->value;
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
b = (double)FIX2LONG(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
b = rb_big2dbl(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
b = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
return (a < b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_le(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double a, b;
|
|
|
|
|
|
|
|
a = RFLOAT(x)->value;
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
b = (double)FIX2LONG(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
b = rb_big2dbl(y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
b = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
return (a <= b)?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
flo_eql(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
if (TYPE(y) == T_FLOAT) {
|
|
|
|
double a = RFLOAT(x)->value;
|
|
|
|
double b = RFLOAT(y)->value;
|
|
|
|
|
|
|
|
if (isnan(a) || isnan(b)) return Qfalse;
|
|
|
|
if (a == b) return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-07-06 03:21:26 -04:00
|
|
|
flo_to_f(num)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE num;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
return num;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
static VALUE
|
|
|
|
flo_abs(flt)
|
|
|
|
VALUE flt;
|
|
|
|
{
|
|
|
|
double val = fabs(RFLOAT(flt)->value);
|
|
|
|
return rb_float_new(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_zero_p(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
if (RFLOAT(num)->value == 0.0) {
|
|
|
|
return Qtrue;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2002-06-04 03:34:19 -04:00
|
|
|
static VALUE
|
|
|
|
flo_is_nan_p(num)
|
2000-07-06 03:21:26 -04:00
|
|
|
VALUE num;
|
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
double value = RFLOAT(num)->value;
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2003-04-11 02:37:48 -04:00
|
|
|
return isnan(value) ? Qtrue : Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2002-06-04 03:34:19 -04:00
|
|
|
static VALUE
|
|
|
|
flo_is_infinite_p(num)
|
2000-07-06 03:21:26 -04:00
|
|
|
VALUE num;
|
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
double value = RFLOAT(num)->value;
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isinf(value)) {
|
|
|
|
return INT2FIX( value < 0 ? -1 : 1 );
|
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2003-04-11 02:37:48 -04:00
|
|
|
return Qnil;
|
2000-07-06 03:21:26 -04:00
|
|
|
}
|
|
|
|
|
2002-06-04 03:34:19 -04:00
|
|
|
static VALUE
|
|
|
|
flo_is_finite_p(num)
|
2000-07-06 03:21:26 -04:00
|
|
|
VALUE num;
|
|
|
|
{
|
2003-04-11 02:37:48 -04:00
|
|
|
double value = RFLOAT(num)->value;
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2003-04-21 08:26:08 -04:00
|
|
|
#if HAVE_FINITE
|
|
|
|
if (!finite(value))
|
|
|
|
return Qfalse;
|
|
|
|
#else
|
2003-04-11 02:37:48 -04:00
|
|
|
if (isinf(value) || isnan(value))
|
|
|
|
return Qfalse;
|
2003-04-21 08:26:08 -04:00
|
|
|
#endif
|
|
|
|
|
2003-04-11 02:37:48 -04:00
|
|
|
return Qtrue;
|
2000-07-06 03:21:26 -04:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
flo_floor(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
double f = floor(RFLOAT(num)->value);
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (!FIXABLE(f)) {
|
|
|
|
return rb_dbl2big(f);
|
|
|
|
}
|
|
|
|
val = f;
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(val);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_ceil(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
double f = ceil(RFLOAT(num)->value);
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (!FIXABLE(f)) {
|
|
|
|
return rb_dbl2big(f);
|
|
|
|
}
|
|
|
|
val = f;
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(val);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
flo_round(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
double f = RFLOAT(num)->value;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (f > 0.0) f = floor(f+0.5);
|
|
|
|
if (f < 0.0) f = ceil(f-0.5);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (!FIXABLE(f)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_dbl2big(f);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
val = f;
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-07-06 03:21:26 -04:00
|
|
|
flo_truncate(num)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE num;
|
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
double f = RFLOAT(num)->value;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (f > 0.0) f = floor(f);
|
|
|
|
if (f < 0.0) f = ceil(f);
|
|
|
|
|
|
|
|
if (!FIXABLE(f)) {
|
|
|
|
return rb_dbl2big(f);
|
|
|
|
}
|
|
|
|
val = f;
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-07-06 03:21:26 -04:00
|
|
|
num_floor(num)
|
|
|
|
VALUE num;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
return flo_floor(rb_Float(num));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-07-06 03:21:26 -04:00
|
|
|
num_ceil(num)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE num;
|
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
return flo_ceil(rb_Float(num));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
static VALUE
|
|
|
|
num_round(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
return flo_round(rb_Float(num));
|
2000-06-12 03:48:31 -04:00
|
|
|
}
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
static VALUE
|
|
|
|
num_truncate(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
return flo_truncate(rb_Float(num));
|
2000-06-12 03:48:31 -04:00
|
|
|
}
|
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
static VALUE
|
|
|
|
num_step(argc, argv, from)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE from;
|
|
|
|
{
|
|
|
|
VALUE to, step;
|
|
|
|
|
2003-01-08 01:05:08 -05:00
|
|
|
if (argc == 1) {
|
|
|
|
to = argv[0];
|
2002-04-24 00:54:16 -04:00
|
|
|
step = INT2FIX(1);
|
|
|
|
}
|
2003-01-08 01:05:08 -05:00
|
|
|
else {
|
|
|
|
if (argc == 2) {
|
|
|
|
to = argv[0];
|
|
|
|
step = argv[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments");
|
|
|
|
}
|
|
|
|
if (rb_equal(step, INT2FIX(0))) {
|
|
|
|
rb_raise(rb_eArgError, "step cannot be 0");
|
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
|
|
|
|
long i, end, diff;
|
|
|
|
|
|
|
|
i = FIX2LONG(from);
|
|
|
|
end = FIX2LONG(to);
|
|
|
|
diff = FIX2LONG(step);
|
|
|
|
|
|
|
|
if (diff > 0) {
|
|
|
|
while (i <= end) {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 00:54:16 -04:00
|
|
|
i += diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (i >= end) {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 00:54:16 -04:00
|
|
|
i += diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-24 01:08:04 -04:00
|
|
|
else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
|
2003-05-08 05:45:52 -04:00
|
|
|
const double epsilon = DBL_EPSILON;
|
2002-04-24 01:08:04 -04:00
|
|
|
double beg = NUM2DBL(from);
|
|
|
|
double end = NUM2DBL(to);
|
|
|
|
double unit = NUM2DBL(step);
|
2002-05-01 05:41:50 -04:00
|
|
|
double n = (end - beg)/unit;
|
2003-05-08 05:45:52 -04:00
|
|
|
double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
|
2002-05-01 05:41:50 -04:00
|
|
|
long i;
|
2002-04-24 00:54:16 -04:00
|
|
|
|
2003-05-08 05:45:52 -04:00
|
|
|
if (err>0.5) err=0.5;
|
|
|
|
n = floor(n + err) + 1;
|
2002-05-01 05:41:50 -04:00
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
rb_yield(rb_float_new(i*unit+beg));
|
2002-04-24 01:08:04 -04:00
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-04-24 01:08:04 -04:00
|
|
|
VALUE i = from;
|
|
|
|
ID cmp;
|
|
|
|
|
|
|
|
if (RTEST(rb_funcall(step, '>', 1, INT2FIX(0)))) {
|
|
|
|
cmp = '>';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmp = '<';
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (RTEST(rb_funcall(i, cmp, 1, to))) break;
|
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '+', 1, step);
|
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
long
|
|
|
|
rb_num2long(val)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(val)) {
|
2002-07-29 02:14:10 -04:00
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(val)) return FIX2LONG(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
switch (TYPE(val)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RFLOAT(val)->value <= (double)LONG_MAX
|
|
|
|
&& RFLOAT(val)->value >= (double)LONG_MIN) {
|
|
|
|
return (long)(RFLOAT(val)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
char buf[24];
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
sprintf(buf, "%-.10g", RFLOAT(val)->value);
|
|
|
|
if (s = strchr(buf, ' ')) *s = '\0';
|
2001-10-02 00:31:23 -04:00
|
|
|
rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big2long(val);
|
|
|
|
|
2003-03-04 04:31:02 -05:00
|
|
|
case T_SYMBOL:
|
|
|
|
rb_warning("treating Symbol as an integer");
|
|
|
|
/* fall through */
|
1998-01-16 07:13:05 -05:00
|
|
|
default:
|
2000-11-14 02:10:31 -05:00
|
|
|
val = rb_to_int(val);
|
1999-01-19 23:59:39 -05:00
|
|
|
return NUM2LONG(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
unsigned long
|
|
|
|
rb_num2ulong(val)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (TYPE(val) == T_BIGNUM) {
|
|
|
|
return rb_big2ulong(val);
|
|
|
|
}
|
|
|
|
return (unsigned long)rb_num2long(val);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
2003-05-30 09:28:10 -04:00
|
|
|
static void
|
|
|
|
check_int(num)
|
|
|
|
long num;
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
if (num < INT_MIN) {
|
|
|
|
s = "small";
|
|
|
|
}
|
|
|
|
else if (num > INT_MAX) {
|
|
|
|
s = "big";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rb_raise(rb_eRangeError, "integer %ld too %s to convert to `int'", num, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_uint(num)
|
|
|
|
unsigned long num;
|
|
|
|
{
|
|
|
|
if (num > INT_MAX) {
|
|
|
|
rb_raise(rb_eRangeError, "integer %lu too big to convert to `int'", num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
int
|
|
|
|
rb_num2int(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
long num = rb_num2long(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-05-30 09:28:10 -04:00
|
|
|
check_int(num);
|
1999-01-19 23:59:39 -05:00
|
|
|
return (int)num;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_fix2int(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
|
|
|
|
|
2003-05-30 09:28:10 -04:00
|
|
|
check_int(num);
|
|
|
|
return (int)num;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
rb_num2uint(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
unsigned long num = rb_num2ulong(val);
|
|
|
|
|
|
|
|
check_uint(num);
|
|
|
|
return (int)num;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2003-06-16 05:10:17 -04:00
|
|
|
rb_fix2uint(val)
|
2003-05-30 09:28:10 -04:00
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
unsigned long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2ulong(val);
|
|
|
|
|
|
|
|
check_uint(num);
|
1999-01-19 23:59:39 -05:00
|
|
|
return (int)num;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int
|
|
|
|
rb_num2int(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
return rb_num2long(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_fix2int(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
return FIX2INT(val);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_num2fix(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
long v;
|
|
|
|
|
|
|
|
if (FIXNUM_P(val)) return val;
|
|
|
|
|
|
|
|
v = rb_num2long(val);
|
|
|
|
if (!FIXABLE(v))
|
2002-05-28 14:11:07 -04:00
|
|
|
rb_raise(rb_eRangeError, "integer %ld out of range of fixnum", v);
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(v);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-03-14 01:23:46 -05:00
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
|
* 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 03:51:31 -05:00
|
|
|
LONG_LONG
|
2002-03-14 01:23:46 -05:00
|
|
|
rb_num2ll(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
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 03:51:31 -05:00
|
|
|
if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val);
|
2002-03-14 01:23:46 -05:00
|
|
|
|
|
|
|
switch (TYPE(val)) {
|
|
|
|
case T_FLOAT:
|
|
|
|
if (RFLOAT(val)->value <= (double)LLONG_MAX
|
|
|
|
&& RFLOAT(val)->value >= (double)LLONG_MIN) {
|
* 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 03:51:31 -05:00
|
|
|
return (LONG_LONG)(RFLOAT(val)->value);
|
2002-03-14 01:23:46 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char buf[24];
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
sprintf(buf, "%-.10g", RFLOAT(val)->value);
|
|
|
|
if (s = strchr(buf, ' ')) *s = '\0';
|
|
|
|
rb_raise(rb_eRangeError, "float %s out of range of long long", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
return rb_big2ll(val);
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from string");
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
|
|
|
|
case T_TRUE:
|
|
|
|
case T_FALSE:
|
|
|
|
rb_raise(rb_eTypeError, "no implicit conversion from boolean");
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
|
|
|
|
default:
|
|
|
|
val = rb_to_int(val);
|
|
|
|
return NUM2LL(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* 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 03:51:31 -05:00
|
|
|
unsigned LONG_LONG
|
2002-03-14 01:23:46 -05:00
|
|
|
rb_num2ull(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
if (TYPE(val) == T_BIGNUM) {
|
|
|
|
return rb_big2ull(val);
|
|
|
|
}
|
* 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 03:51:31 -05:00
|
|
|
return (unsigned LONG_LONG)rb_num2ll(val);
|
2002-03-14 01:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
static VALUE
|
|
|
|
int_to_i(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
int_int_p(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_succ(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
2002-03-22 02:26:42 -05:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
long i = FIX2LONG(num) + 1;
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(i);
|
2002-03-22 02:26:42 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return rb_funcall(num, '+', 1, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
int_chr(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
long i = NUM2LONG(num);
|
|
|
|
|
|
|
|
if (i < 0 || 0xff < i)
|
2002-05-28 14:11:07 -04:00
|
|
|
rb_raise(rb_eRangeError, "%ld out of char range", i);
|
1999-01-19 23:59:39 -05:00
|
|
|
c = i;
|
|
|
|
return rb_str_new(&c, 1);
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
rb_fix_induced_from(klass, x)
|
|
|
|
VALUE klass, x;
|
|
|
|
{
|
|
|
|
return rb_num2fix(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_int_induced_from(klass, x)
|
|
|
|
VALUE klass, x;
|
|
|
|
{
|
|
|
|
switch (TYPE(x)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
return x;
|
|
|
|
case T_FLOAT:
|
2002-07-29 02:14:10 -04:00
|
|
|
return rb_funcall(x, id_to_i, 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eTypeError, "failed to convert %s into Integer",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(x));
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_flo_induced_from(klass, x)
|
|
|
|
VALUE klass, x;
|
|
|
|
{
|
|
|
|
switch (TYPE(x)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
return rb_funcall(x, rb_intern("to_f"), 0);
|
|
|
|
case T_FLOAT:
|
|
|
|
return x;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eTypeError, "failed to convert %s into Float",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(x));
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
fix_uminus(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(-FIX2LONG(num));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_fix2str(x, base)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x;
|
|
|
|
int base;
|
|
|
|
{
|
2003-04-14 02:54:27 -04:00
|
|
|
extern const char ruby_digitmap[];
|
2003-04-15 03:05:15 -04:00
|
|
|
char buf[SIZEOF_LONG*CHAR_BIT + 2], *b = buf + sizeof buf;
|
2002-01-11 04:18:54 -05:00
|
|
|
long val = FIX2LONG(x);
|
2003-04-14 02:54:27 -04:00
|
|
|
int neg = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-04-14 02:54:27 -04:00
|
|
|
if (base < 2 || 36 < base) {
|
|
|
|
rb_raise(rb_eArgError, "illegal radix %d", base);
|
|
|
|
}
|
|
|
|
if (val == 0) {
|
|
|
|
return rb_str_new2("0");
|
|
|
|
}
|
2002-01-11 04:18:54 -05:00
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
2003-04-14 02:54:27 -04:00
|
|
|
neg = 1;
|
|
|
|
}
|
|
|
|
*--b = '\0';
|
|
|
|
do {
|
|
|
|
*--b = ruby_digitmap[(int)(val % base)];
|
|
|
|
} while (val /= base);
|
|
|
|
if (neg) {
|
|
|
|
*--b = '-';
|
2002-01-11 04:18:54 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-04-14 02:54:27 -04:00
|
|
|
return rb_str_new2(b);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2001-11-19 00:03:03 -05:00
|
|
|
fix_to_s(argc, argv, x)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-11-19 00:03:03 -05:00
|
|
|
VALUE b;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &b);
|
|
|
|
if (argc == 0) base = 10;
|
|
|
|
else base = NUM2INT(b);
|
|
|
|
|
|
|
|
if (base == 2) {
|
|
|
|
/* rb_fix2str() does not handle binary */
|
|
|
|
return rb_big2str(rb_int2big(FIX2INT(x)), 2);
|
|
|
|
}
|
|
|
|
return rb_fix2str(x, base);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_plus(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long a, b, c;
|
|
|
|
VALUE r;
|
|
|
|
|
|
|
|
a = FIX2LONG(x);
|
|
|
|
b = FIX2LONG(y);
|
|
|
|
c = a + b;
|
2002-08-21 11:47:54 -04:00
|
|
|
r = LONG2FIX(c);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (FIX2LONG(r) != c) {
|
|
|
|
r = rb_big_plus(rb_int2big(a), rb_int2big(b));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (TYPE(y) == T_FLOAT) {
|
|
|
|
return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_minus(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long a, b, c;
|
|
|
|
VALUE r;
|
|
|
|
|
|
|
|
a = FIX2LONG(x);
|
|
|
|
b = FIX2LONG(y);
|
|
|
|
c = a - b;
|
2002-08-21 11:47:54 -04:00
|
|
|
r = LONG2FIX(c);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (FIX2LONG(r) != c) {
|
|
|
|
r = rb_big_minus(rb_int2big(a), rb_int2big(b));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (TYPE(y) == T_FLOAT) {
|
|
|
|
return rb_float_new((double)FIX2LONG(x) - RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_mul(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long a, b, c;
|
|
|
|
VALUE r;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
a = FIX2LONG(x);
|
|
|
|
if (a == 0) return x;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
b = FIX2LONG(y);
|
|
|
|
c = a * b;
|
2002-08-21 11:47:54 -04:00
|
|
|
r = LONG2FIX(c);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIX2LONG(r) != c || c/a != b) {
|
|
|
|
r = rb_big_mul(rb_int2big(a), rb_int2big(b));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (TYPE(y) == T_FLOAT) {
|
|
|
|
return rb_float_new((double)FIX2LONG(x) * RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-07-03 01:46:36 -04:00
|
|
|
static void
|
|
|
|
fixdivmod(x, y, divp, modp)
|
|
|
|
long x, y;
|
|
|
|
long *divp, *modp;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
long div, mod;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-03 01:46:36 -04:00
|
|
|
if (y == 0) rb_num_zerodiv();
|
|
|
|
if (y < 0) {
|
|
|
|
if (x < 0)
|
|
|
|
div = -x / -y;
|
|
|
|
else
|
|
|
|
div = - (x / -y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-03 01:46:36 -04:00
|
|
|
else {
|
|
|
|
if (x < 0)
|
|
|
|
div = - (-x / y);
|
|
|
|
else
|
|
|
|
div = x / y;
|
|
|
|
}
|
|
|
|
mod = x - div*y;
|
2000-07-06 03:21:26 -04:00
|
|
|
if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
|
|
|
|
mod += y;
|
|
|
|
div -= 1;
|
|
|
|
}
|
2000-07-03 01:46:36 -04:00
|
|
|
if (divp) *divp = div;
|
|
|
|
if (modp) *modp = mod;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:22:50 -05:00
|
|
|
static VALUE
|
|
|
|
fix_quo(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y));
|
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2000-07-03 01:46:36 -04:00
|
|
|
fix_div(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(y)) {
|
2000-07-03 01:46:36 -04:00
|
|
|
long div;
|
|
|
|
|
|
|
|
fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2NUM(div);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_mod(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long mod;
|
|
|
|
|
|
|
|
fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2NUM(mod);
|
2000-07-03 01:46:36 -04:00
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-07-03 01:46:36 -04:00
|
|
|
fix_divmod(x, y)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
if (FIXNUM_P(y)) {
|
|
|
|
long div, mod;
|
|
|
|
|
|
|
|
fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
|
|
|
|
|
2002-08-21 11:47:54 -04:00
|
|
|
return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
|
2000-07-03 01:46:36 -04:00
|
|
|
}
|
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_pow(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a, b;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (b == 0) return INT2FIX(1);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (b == 1) return x;
|
1999-01-19 23:59:39 -05:00
|
|
|
a = FIX2LONG(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (b > 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big_pow(rb_int2big(a), y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(pow((double)a, (double)b));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_equal(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return (FIX2LONG(x) == FIX2LONG(y))?Qtrue:Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return num_equal(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_cmp(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a = FIX2LONG(x), b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (a == b) return INT2FIX(0);
|
|
|
|
if (a > b) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
else {
|
2002-12-19 04:20:20 -05:00
|
|
|
return rb_num_coerce_cmp(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_gt(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a = FIX2LONG(x), b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (a > b) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_ge(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a = FIX2LONG(x), b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (a >= b) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_lt(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a = FIX2LONG(x), b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (a < b) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_le(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
long a = FIX2LONG(x), b = FIX2LONG(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (a <= b) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2003-07-31 04:42:44 -04:00
|
|
|
return rb_num_coerce_relop(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_rev(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long val = FIX2LONG(num);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
val = ~val;
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_and(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (TYPE(y) == T_BIGNUM) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big_and(y, x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
val = FIX2LONG(x) & NUM2LONG(y);
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_or(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (TYPE(y) == T_BIGNUM) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big_or(y, x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
val = FIX2LONG(x) | NUM2LONG(y);
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_xor(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
|
|
|
|
if (TYPE(y) == T_BIGNUM) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big_xor(y, x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
val = FIX2LONG(x) ^ NUM2LONG(y);
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-06-10 06:06:12 -04:00
|
|
|
static VALUE fix_rshift _((VALUE, VALUE));
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
fix_lshift(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-07-03 01:46:36 -04:00
|
|
|
long val, width;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
val = NUM2LONG(x);
|
2000-07-03 01:46:36 -04:00
|
|
|
width = NUM2LONG(y);
|
2002-06-10 06:06:12 -04:00
|
|
|
if (width < 0)
|
2002-08-21 11:47:54 -04:00
|
|
|
return fix_rshift(x, LONG2FIX(-width));
|
1998-01-16 07:13:05 -05:00
|
|
|
if (width > (sizeof(VALUE)*CHAR_BIT-1)
|
1999-01-19 23:59:39 -05:00
|
|
|
|| ((unsigned long)val)>>(sizeof(VALUE)*CHAR_BIT-1-width) > 0) {
|
|
|
|
return rb_big_lshift(rb_int2big(val), y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
val = val << width;
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_rshift(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
long i, val;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
i = NUM2LONG(y);
|
1999-12-06 04:04:03 -05:00
|
|
|
if (i < 0)
|
2002-08-21 11:47:54 -04:00
|
|
|
return fix_lshift(x, LONG2FIX(-i));
|
2000-07-03 01:46:36 -04:00
|
|
|
if (i == 0) return x;
|
2002-06-10 06:06:12 -04:00
|
|
|
val = FIX2LONG(x);
|
2000-07-03 01:46:36 -04:00
|
|
|
if (i >= sizeof(long)*CHAR_BIT-1) {
|
2002-06-10 06:06:12 -04:00
|
|
|
if (val < 0) return INT2FIX(-1);
|
2000-07-03 01:46:36 -04:00
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
2002-06-10 06:06:12 -04:00
|
|
|
val = RSHIFT(val, i);
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_aref(fix, idx)
|
|
|
|
VALUE fix, idx;
|
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
long val = FIX2LONG(fix);
|
2002-08-13 05:21:18 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-10-29 00:07:26 -05:00
|
|
|
if (TYPE(idx) == T_BIGNUM) {
|
2002-08-13 05:21:18 -04:00
|
|
|
idx = rb_big_norm(idx);
|
|
|
|
if (!FIXNUM_P(idx)) {
|
|
|
|
if (!RBIGNUM(idx)->sign || val >= 0)
|
|
|
|
return INT2FIX(0);
|
|
|
|
return INT2FIX(1);
|
|
|
|
}
|
2001-10-29 00:07:26 -05:00
|
|
|
}
|
2002-08-13 05:21:18 -04:00
|
|
|
i = NUM2LONG(idx);
|
2001-10-29 00:07:26 -05:00
|
|
|
|
2002-08-13 05:21:18 -04:00
|
|
|
if (i < 0) return INT2FIX(0);
|
|
|
|
if (sizeof(VALUE)*CHAR_BIT-1 < i) {
|
|
|
|
if (val < 0) return INT2FIX(1);
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2FIX(0);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2002-08-13 05:21:18 -04:00
|
|
|
if (val & (1L<<i))
|
|
|
|
return INT2FIX(1);
|
|
|
|
return INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_to_f(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
double val;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
val = (double)FIX2LONG(num);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_abs(fix)
|
|
|
|
VALUE fix;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
long i = FIX2LONG(fix);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (i < 0) i = -i;
|
|
|
|
|
2002-08-28 04:05:23 -04:00
|
|
|
return LONG2NUM(i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fix_id2name(fix)
|
|
|
|
VALUE fix;
|
|
|
|
{
|
|
|
|
char *name = rb_id2name(FIX2UINT(fix));
|
1999-01-19 23:59:39 -05:00
|
|
|
if (name) return rb_str_new2(name);
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-11-03 06:04:35 -05:00
|
|
|
static VALUE
|
|
|
|
fix_to_sym(fix)
|
|
|
|
VALUE fix;
|
|
|
|
{
|
|
|
|
ID id = FIX2UINT(fix);
|
|
|
|
|
|
|
|
if (rb_id2name(id)) {
|
|
|
|
return ID2SYM(id);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
fix_size(fix)
|
|
|
|
VALUE fix;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return INT2FIX(sizeof(long));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
int_upto(from, to)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE from, to;
|
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to)) {
|
|
|
|
long i, end;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
end = FIX2LONG(to);
|
|
|
|
for (i = FIX2LONG(from); i <= end; i++) {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 00:54:16 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-04-24 00:54:16 -04:00
|
|
|
else {
|
2003-05-07 23:56:12 -04:00
|
|
|
VALUE i = from, c;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-05-07 23:56:12 -04:00
|
|
|
while (!(c = rb_funcall(i, '>', 1, to))) {
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '+', 1, INT2FIX(1));
|
|
|
|
}
|
2003-05-07 23:56:12 -04:00
|
|
|
if (NIL_P(c)) rb_cmperr(i, to);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-04-24 00:54:16 -04:00
|
|
|
int_downto(from, to)
|
|
|
|
VALUE from, to;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
if (FIXNUM_P(from) && FIXNUM_P(to)) {
|
|
|
|
long i, end;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
end = FIX2LONG(to);
|
|
|
|
for (i=FIX2LONG(from); i >= end; i--) {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_yield(LONG2FIX(i));
|
2002-04-24 00:54:16 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2003-05-07 23:56:12 -04:00
|
|
|
VALUE i = from, c;
|
2002-04-24 00:54:16 -04:00
|
|
|
|
2003-05-07 23:56:12 -04:00
|
|
|
while (!(c = rb_funcall(i, '<', 1, to))) {
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '-', 1, INT2FIX(1));
|
|
|
|
}
|
2003-05-07 23:56:12 -04:00
|
|
|
if (NIL_P(c)) rb_cmperr(i, to);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
int_dotimes(num)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE num;
|
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
if (FIXNUM_P(num)) {
|
|
|
|
long i, end;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
end = FIX2LONG(num);
|
|
|
|
for (i=0; i<end; i++) {
|
2002-08-21 11:47:54 -04:00
|
|
|
rb_yield(LONG2FIX(i));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-04-24 00:54:16 -04:00
|
|
|
VALUE i = INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
for (;;) {
|
|
|
|
if (!RTEST(rb_funcall(i, '<', 1, num))) break;
|
|
|
|
rb_yield(i);
|
|
|
|
i = rb_funcall(i, '+', 1, INT2FIX(1));
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
fix_zero_p(num)
|
|
|
|
VALUE num;
|
|
|
|
{
|
|
|
|
if (FIX2LONG(num) == 0) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
Init_Numeric()
|
|
|
|
{
|
2000-05-01 05:42:38 -04:00
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ < 4
|
1999-08-13 01:45:20 -04:00
|
|
|
/* allow divide by zero -- Inf */
|
|
|
|
fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
|
|
|
|
#endif
|
2001-08-05 23:05:23 -04:00
|
|
|
id_coerce = rb_intern("coerce");
|
|
|
|
id_to_i = rb_intern("to_i");
|
2003-07-21 14:48:58 -04:00
|
|
|
id_eq = rb_intern("==");
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
|
2000-05-01 05:42:38 -04:00
|
|
|
rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_cNumeric = rb_define_class("Numeric", rb_cObject);
|
|
|
|
|
|
|
|
rb_include_module(rb_cNumeric, rb_mComparable);
|
2003-05-19 01:41:08 -04:00
|
|
|
rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1);
|
2003-04-03 00:25:00 -05:00
|
|
|
rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
|
2002-11-22 04:14:24 -05:00
|
|
|
rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
|
2003-01-23 01:22:50 -05:00
|
|
|
rb_define_method(rb_cNumeric, "quo", num_quo, 1);
|
|
|
|
rb_define_method(rb_cNumeric, "div", num_div, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
|
2000-07-04 00:17:26 -04:00
|
|
|
rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cNumeric, "abs", num_abs, 0);
|
2002-07-29 02:14:10 -04:00
|
|
|
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
|
1999-01-19 23:59:39 -05: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);
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cNumeric, "floor", num_floor, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "ceil", num_ceil, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "round", num_round, 0);
|
|
|
|
rb_define_method(rb_cNumeric, "truncate", num_truncate, 0);
|
2002-04-24 00:54:16 -04:00
|
|
|
rb_define_method(rb_cNumeric, "step", num_step, -1);
|
2000-07-06 03:21:26 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_cInteger = rb_define_class("Integer", rb_cNumeric);
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_undef_alloc_func(rb_cInteger);
|
2001-08-29 02:28:51 -04:00
|
|
|
rb_undef_method(CLASS_OF(rb_cInteger), "new");
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
|
|
|
|
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);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_include_module(rb_cInteger, rb_mPrecision);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
|
|
|
rb_define_method(rb_cInteger, "next", int_succ, 0);
|
|
|
|
rb_define_method(rb_cInteger, "chr", int_chr, 0);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
|
2000-11-13 00:39:35 -05:00
|
|
|
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cInteger, "floor", int_to_i, 0);
|
|
|
|
rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
|
|
|
|
rb_define_method(rb_cInteger, "round", int_to_i, 0);
|
|
|
|
rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_include_module(rb_cFixnum, rb_mPrecision);
|
|
|
|
rb_define_singleton_method(rb_cFixnum, "induced_from", rb_fix_induced_from, 1);
|
|
|
|
rb_define_singleton_method(rb_cInteger, "induced_from", rb_int_induced_from, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2001-11-19 00:03:03 -05:00
|
|
|
rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "id2name", fix_id2name, 0);
|
2002-11-03 06:04:35 -05:00
|
|
|
rb_define_method(rb_cFixnum, "to_sym", fix_to_sym, 0);
|
1999-01-19 23:59:39 -05: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);
|
2001-07-31 02:24:45 -04:00
|
|
|
rb_define_method(rb_cFixnum, "div", fix_div, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cFixnum, "%", fix_mod, 1);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cFixnum, "modulo", fix_mod, 1);
|
2000-07-03 01:46:36 -04:00
|
|
|
rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1);
|
2003-01-23 01:22:50 -05:00
|
|
|
rb_define_method(rb_cFixnum, "quo", fix_quo, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "==", fix_equal, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
|
|
|
|
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);
|
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "~", fix_rev, 0);
|
|
|
|
rb_define_method(rb_cFixnum, "&", fix_and, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "|", fix_or, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "^", fix_xor, 1);
|
|
|
|
rb_define_method(rb_cFixnum, "[]", fix_aref, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "<<", fix_lshift, 1);
|
|
|
|
rb_define_method(rb_cFixnum, ">>", fix_rshift, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
|
|
|
|
rb_define_method(rb_cFixnum, "size", fix_size, 0);
|
|
|
|
rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
|
|
|
|
|
|
|
|
rb_cFloat = rb_define_class("Float", rb_cNumeric);
|
|
|
|
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_undef_alloc_func(rb_cFloat);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_undef_method(CLASS_OF(rb_cFloat), "new");
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_singleton_method(rb_cFloat, "induced_from", rb_flo_induced_from, 1);
|
|
|
|
rb_include_module(rb_cFloat, rb_mPrecision);
|
|
|
|
|
2002-12-24 03:53:56 -05:00
|
|
|
rb_define_const(rb_cFloat, "ROUNDS", INT2FIX(FLT_ROUNDS));
|
|
|
|
rb_define_const(rb_cFloat, "RADIX", INT2FIX(FLT_RADIX));
|
|
|
|
rb_define_const(rb_cFloat, "MANT_DIG", INT2FIX(DBL_MANT_DIG));
|
|
|
|
rb_define_const(rb_cFloat, "DIG", INT2FIX(DBL_DIG));
|
|
|
|
rb_define_const(rb_cFloat, "MIN_EXP", INT2FIX(DBL_MIN_EXP));
|
|
|
|
rb_define_const(rb_cFloat, "MAX_EXP", INT2FIX(DBL_MAX_EXP));
|
|
|
|
rb_define_const(rb_cFloat, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP));
|
|
|
|
rb_define_const(rb_cFloat, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP));
|
|
|
|
rb_define_const(rb_cFloat, "MIN", rb_float_new(DBL_MIN));
|
|
|
|
rb_define_const(rb_cFloat, "MAX", rb_float_new(DBL_MAX));
|
|
|
|
rb_define_const(rb_cFloat, "EPSILON", rb_float_new(DBL_EPSILON));
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
|
|
|
|
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);
|
|
|
|
rb_define_method(rb_cFloat, "%", flo_mod, 1);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cFloat, "**", flo_pow, 1);
|
|
|
|
rb_define_method(rb_cFloat, "==", flo_eq, 1);
|
|
|
|
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);
|
|
|
|
rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cFloat, "to_i", flo_truncate, 0);
|
2002-07-29 02:14:10 -04:00
|
|
|
rb_define_method(rb_cFloat, "to_int", flo_truncate, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cFloat, "floor", flo_floor, 0);
|
|
|
|
rb_define_method(rb_cFloat, "ceil", flo_ceil, 0);
|
|
|
|
rb_define_method(rb_cFloat, "round", flo_round, 0);
|
2000-07-06 03:21:26 -04:00
|
|
|
rb_define_method(rb_cFloat, "truncate", flo_truncate, 0);
|
2000-06-12 03: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);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|