class Numeric
Public Instance Methods
cbrt → num
click to toggle source
Cube root.
27.cbrt #=> 3.0
VALUE
rb_num_cbrt( VALUE num)
{
#if HAVE_FUNC_CBRT
return rb_float_new( cbrt( RFLOAT_VALUE( rb_Float( num))));
#else
double n;
int neg;
int i;
n = RFLOAT_VALUE( rb_Float( num));
if ((neg = n < 0))
n = -n;
n = sqrt( sqrt( n));
i = 2;
for (;;) {
double w = n;
int j;
for (j=i;j;--j) w = sqrt( w);
i *= 2;
w *= n;
if (n == w) break;
n = w;
}
return rb_float_new( neg ? -n : n);
#endif
}
grammatical sing, plu → str
click to toggle source
Singular or plural
1.grammatical "line", "lines" #=> "line" 6.grammatical "child", "children" #=> "children"
VALUE
rb_num_grammatical( VALUE num, VALUE sing, VALUE plu)
{
long l;
double d;
switch (TYPE( num)) {
case T_FIXNUM:
l = NUM2LONG( num);
if (l == 1l || l == -1l)
return sing;
break;
case T_BIGNUM:
/* 1 is not a Bignum */
break;
case T_FLOAT:
d = RFLOAT_VALUE( num);
if (d == 1.0 || d == -1.0)
return sing;
break;
default:
l = NUM2LONG( rb_funcall( num, id_cmp, 1, INT2FIX( 1)));
if (l == 0)
return sing;
l = NUM2LONG( rb_funcall( num, id_cmp, 1, INT2FIX(-1)));
if (l == 0)
return sing;
break;
}
return plu;
}
neg? → true or false
click to toggle source
Check whether num is negative.
VALUE
rb_num_neg_p( VALUE num)
{
VALUE r = Qfalse;
switch (TYPE( num)) {
case T_FIXNUM:
if (NUM2LONG( num) < 0)
r = Qtrue;
break;
case T_BIGNUM:
if (!RBIGNUM_SIGN( num)) /* 0 is not a Bignum. */
r = Qtrue;
break;
case T_FLOAT:
if (RFLOAT_VALUE( num) < 0.0)
r = Qtrue;
break;
default:
return rb_num_neg_p( rb_funcall( num, id_cmp, 1, INT2FIX( 0)));
break;
}
return r;
}
pos? → true or false
click to toggle source
Check whether num is positive.
VALUE
rb_num_pos_p( VALUE num)
{
VALUE r = Qfalse;
switch (TYPE( num)) {
case T_FIXNUM:
if (NUM2LONG( num) > 0)
r = Qtrue;
break;
case T_BIGNUM:
if (RBIGNUM_SIGN( num)) /* 0 is not a Bignum. */
r = Qtrue;
break;
case T_FLOAT:
if (RFLOAT_VALUE( num) > 0.0)
r = Qtrue;
break;
default:
return rb_num_neg_p( rb_funcall( INT2FIX( 0), id_cmp, 1, num));
break;
}
return r;
}
sqrt → num
click to toggle source
Square root.
144.sqrt #=> 12.0
VALUE
rb_num_sqrt( VALUE num)
{
return rb_float_new( sqrt( RFLOAT_VALUE( rb_Float( num))));
}