1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

modulo, frexp, ldexp

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 1998-03-17 10:06:57 +00:00
parent a946791350
commit 4c3d81d323
6 changed files with 137 additions and 17 deletions

View file

@ -1,3 +1,27 @@
Tue Mar 17 18:23:06 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* math.c (math_frexp): newly added.
* math.c (math_ldexp): ditto.
* bignum.c (bigdivmod): calculates modulo.
* numeric.c (fix_remainder): returns reminder, formerly known as
modulo.
* numeric.c (fix_modulo): calculates proper `modulo'.
* bignum.c (bigdivmod): wrong sign for reminder.
Mon Mar 16 17:07:28 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* experimental release 1.1b9_03.
Mon Mar 16 16:33:53 1998 WATANABE Hirofumi <watanabe@ase.ptg.sony.co.jp>
* io.c (pipe_finalize): needed to add pipe_finalize to pipes on
cygwin32.
Mon Mar 16 14:11:06 1998 Yukihiro Matsumoto <matz@netlab.co.jp> Mon Mar 16 14:11:06 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* class.c (ins_methods_i): needed to consider NOEX_UNDEF. * class.c (ins_methods_i): needed to consider NOEX_UNDEF.

View file

@ -662,12 +662,13 @@ big_mul(x, y)
} }
static void static void
bigdivmod(x, y, div, mod) bigdivmod(x, y, div, mod, modulo)
VALUE x, y; VALUE x, y;
VALUE *div, *mod; VALUE *div, *mod;
int modulo;
{ {
UINT nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len, i, j; UINT nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len, i, j;
VALUE z; VALUE yy, z;
USHORT *xds, *yds, *zds, *tds; USHORT *xds, *yds, *zds, *tds;
unsigned long t2; unsigned long t2;
long num; long num;
@ -703,8 +704,8 @@ bigdivmod(x, y, div, mod)
if (nx==ny) zds[nx+1] = 0; if (nx==ny) zds[nx+1] = 0;
while (!yds[ny-1]) ny--; while (!yds[ny-1]) ny--;
if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) { if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) {
y = big_clone(y); yy = big_clone(y);
tds = BDIGITS(y); tds = BDIGITS(yy);
j = 0; j = 0;
num = 0; num = 0;
while (j<ny) { while (j<ny) {
@ -776,7 +777,16 @@ bigdivmod(x, y, div, mod)
} }
} }
RBIGNUM(*mod)->len = ny; RBIGNUM(*mod)->len = ny;
RBIGNUM(*mod)->sign = RBIGNUM(y)->sign; RBIGNUM(*mod)->sign = RBIGNUM(x)->sign;
if (modulo && RBIGNUM(x)->sign != RBIGNUM(y)->sign) {
int len = ny;
zds = BDIGITS(*mod);
while (len-- && !zds[len]);
if (len > 0) {
*mod = bigadd(*mod, y, 1);
return;
}
}
*mod = bignorm(*mod); *mod = bignorm(*mod);
} }
} }
@ -801,14 +811,16 @@ big_div(x, y)
default: default:
return num_coerce_bin(x, y); return num_coerce_bin(x, y);
} }
bigdivmod(x, y, &z, 0); bigdivmod(x, y, &z, 0, 0);
return z; return z;
} }
static VALUE static VALUE
big_mod(x, y) big_modulo(x, y, modulo)
VALUE x, y; VALUE x, y;
int modulo;
{ {
VALUE z; VALUE z;
@ -827,11 +839,25 @@ big_mod(x, y)
default: default:
return num_coerce_bin(x, y); return num_coerce_bin(x, y);
} }
bigdivmod(x, y, 0, &z); bigdivmod(x, y, 0, &z, modulo);
return z; return z;
} }
static VALUE
big_mod(x, y)
VALUE x, y;
{
return big_modulo(x, y, 1);
}
static VALUE
big_remainder(x, y)
VALUE x, y;
{
return big_modulo(x, y, 0);
}
static VALUE static VALUE
big_divmod(x, y) big_divmod(x, y)
VALUE x, y; VALUE x, y;
@ -853,7 +879,7 @@ big_divmod(x, y)
default: default:
return num_coerce_bin(x, y); return num_coerce_bin(x, y);
} }
bigdivmod(x, y, &div, &mod); bigdivmod(x, y, &div, &mod, 1);
return assoc_new(div, mod);; return assoc_new(div, mod);;
} }

7
io.c
View file

@ -941,22 +941,24 @@ pipe_atexit()
} }
} }
#if !defined (__CYGWIN32__)
static void static void
pipe_finalize(fptr) pipe_finalize(fptr)
OpenFile *fptr; OpenFile *fptr;
{ {
#if !defined (__CYGWIN32__)
if (fptr->f != NULL) { if (fptr->f != NULL) {
pclose(fptr->f); pclose(fptr->f);
} }
if (fptr->f2 != NULL) { if (fptr->f2 != NULL) {
pclose(fptr->f2); pclose(fptr->f2);
} }
#else
fptr_finalize(fptr);
#endif
fptr->f = fptr->f2 = NULL; fptr->f = fptr->f2 = NULL;
pipe_del_fptr(fptr); pipe_del_fptr(fptr);
} }
#endif #endif
#endif
void void
io_unbuffered(fptr) io_unbuffered(fptr)
@ -1076,6 +1078,7 @@ pipe_open(pname, mode)
else fptr->f = f; else fptr->f = f;
} }
#if defined (__CYGWIN32__) #if defined (__CYGWIN32__)
fptr->finalize = pipe_finalize;
pipe_add_fptr(fptr); pipe_add_fptr(fptr);
#endif #endif
return (VALUE)port; return (VALUE)port;

28
math.c
View file

@ -90,6 +90,30 @@ math_sqrt(obj, x)
return float_new(sqrt(RFLOAT(x)->value)); return float_new(sqrt(RFLOAT(x)->value));
} }
static VALUE
math_frexp(obj, x)
VALUE obj, x;
{
double d;
int exp;
Need_Float(x);
d = frexp(RFLOAT(x)->value, &exp);
return assoc_new(float_new(d), INT2NUM(exp));
}
static VALUE
math_ldexp(obj, x, n)
VALUE obj, x, n;
{
double d;
int exp;
Need_Float(x);
return float_new(d = ldexp(RFLOAT(x)->value, NUM2INT(n)));
}
void void
Init_Math() Init_Math()
{ {
@ -115,5 +139,7 @@ Init_Math()
rb_define_module_function(mMath, "exp", math_exp, 1); rb_define_module_function(mMath, "exp", math_exp, 1);
rb_define_module_function(mMath, "log", math_log, 1); rb_define_module_function(mMath, "log", math_log, 1);
rb_define_module_function(mMath, "log10", math_log10, 1); rb_define_module_function(mMath, "log10", math_log10, 1);
rb_define_module_function(mMath, "sqrt", math_sqrt, 1);
rb_define_module_function(mMath, "frexp", math_frexp, 1);
rb_define_module_function(mMath, "ldexp", math_ldexp, 2);
} }

View file

@ -275,8 +275,9 @@ flo_div(x, y)
} }
static VALUE static VALUE
flo_mod(x, y) flo_modulo(x, y, modulo)
VALUE x, y; VALUE x, y;
int modulo;
{ {
double value; double value;
@ -293,6 +294,7 @@ flo_mod(x, y)
default: default:
return num_coerce_bin(x, y); return num_coerce_bin(x, y);
} }
#ifdef HAVE_FMOD #ifdef HAVE_FMOD
value = fmod(RFLOAT(x)->value, value); value = fmod(RFLOAT(x)->value, value);
#else #else
@ -304,10 +306,28 @@ flo_mod(x, y)
value = value1 - value2 * value; value = value1 - value2 * value;
} }
#endif #endif
if (modulo &&
(RFLOAT(x)->value < 0.0) != (RFLOAT(y)->value < 0.0) &&
value != 0.0) {
value += RFLOAT(y)->value;
}
return float_new(value); return float_new(value);
} }
static VALUE
flo_mod(x, y)
VALUE x, y;
{
return flo_modulo(x,y,1);
}
static VALUE
flo_remainder(x, y)
VALUE x, y;
{
return flo_modulo(x,y,0);
}
VALUE VALUE
flo_pow(x, y) flo_pow(x, y)
VALUE x, y; VALUE x, y;
@ -754,7 +774,7 @@ fix_div(x, y)
} }
static VALUE static VALUE
fix_mod(x, y) fix_modulo(x, y, modulo)
VALUE x, y; VALUE x, y;
{ {
INT i; INT i;
@ -763,11 +783,30 @@ fix_mod(x, y)
i = FIX2INT(y); i = FIX2INT(y);
if (i == 0) num_zerodiv(); if (i == 0) num_zerodiv();
i = FIX2INT(x)%i; i = FIX2INT(x)%i;
if (modulo &&
(FIX2INT(x) < 0) != (FIX2INT(y) < 0) &&
i != 0) {
i += FIX2INT(y);
}
return INT2FIX(i); return INT2FIX(i);
} }
return num_coerce_bin(x, y); return num_coerce_bin(x, y);
} }
static VALUE
fix_mod(x, y)
VALUE x, y;
{
return fix_modulo(x, y, 1);
}
static VALUE
fix_remainder(x, y)
VALUE x, y;
{
return fix_modulo(x, y, 0);
}
static VALUE static VALUE
fix_pow(x, y) fix_pow(x, y)
VALUE x, y; VALUE x, y;
@ -1218,6 +1257,7 @@ Init_Numeric()
rb_define_method(cFixnum, "*", fix_mul, 1); rb_define_method(cFixnum, "*", fix_mul, 1);
rb_define_method(cFixnum, "/", fix_div, 1); rb_define_method(cFixnum, "/", fix_div, 1);
rb_define_method(cFixnum, "%", fix_mod, 1); rb_define_method(cFixnum, "%", fix_mod, 1);
rb_define_method(cFixnum, "remainder", fix_remainder, 1);
rb_define_method(cFixnum, "**", fix_pow, 1); rb_define_method(cFixnum, "**", fix_pow, 1);
rb_define_method(cFixnum, "abs", fix_abs, 0); rb_define_method(cFixnum, "abs", fix_abs, 0);
@ -1262,6 +1302,7 @@ Init_Numeric()
rb_define_method(cFloat, "*", flo_mul, 1); rb_define_method(cFloat, "*", flo_mul, 1);
rb_define_method(cFloat, "/", flo_div, 1); rb_define_method(cFloat, "/", flo_div, 1);
rb_define_method(cFloat, "%", flo_mod, 1); rb_define_method(cFloat, "%", flo_mod, 1);
rb_define_method(cFloat, "remainder", flo_remainder, 1);
rb_define_method(cFloat, "**", flo_pow, 1); rb_define_method(cFloat, "**", flo_pow, 1);
rb_define_method(cFloat, "==", flo_eq, 1); rb_define_method(cFloat, "==", flo_eq, 1);
rb_define_method(cFloat, "<=>", flo_cmp, 1); rb_define_method(cFloat, "<=>", flo_cmp, 1);

View file

@ -1,2 +1,2 @@
#define RUBY_VERSION "1.1b9_02" #define RUBY_VERSION "1.1b9_03"
#define VERSION_DATE "98/03/13" #define VERSION_DATE "98/03/16"