| 1 |
/* |
| 2 |
** numeric.c - Numeric, Integer, Float class |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <mruby.h> |
| 8 |
#include <mruby/array.h> |
| 9 |
#include <mruby/numeric.h> |
| 10 |
#include <mruby/string.h> |
| 11 |
#include <mruby/class.h> |
| 12 |
#include <mruby/internal.h> |
| 13 |
#include <mruby/presym.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#ifndef MRB_NO_FLOAT |
| 17 |
#ifdef MRB_USE_FLOAT32 |
| 18 |
#define trunc(f) truncf(f) |
| 19 |
#define fmod(x,y) fmodf(x,y) |
| 20 |
#else |
| 21 |
#endif |
| 22 |
#endif |
| 23 |
|
| 24 |
mrb_noreturn void |
| 25 |
mrb_int_overflow(mrb_state *mrb, const char *reason) |
| 26 |
{ |
| 27 |
mrb_raisef(mrb, E_RANGE_ERROR, "integer overflow in %s", reason); |
| 28 |
} |
| 29 |
|
| 30 |
mrb_noreturn void |
| 31 |
mrb_int_zerodiv(mrb_state *mrb) |
| 32 |
{ |
| 33 |
mrb_raise(mrb, E_ZERODIV_ERROR, "divided by 0"); |
| 34 |
} |
| 35 |
|
| 36 |
static mrb_noreturn void |
| 37 |
mrb_int_noconv(mrb_state *mrb, mrb_value y) |
| 38 |
{ |
| 39 |
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %Y into Integer", y); |
| 40 |
} |
| 41 |
|
| 42 |
mrb_value |
| 43 |
mrb_int_pow(mrb_state *mrb, mrb_value x, mrb_value y) |
| 44 |
{ |
| 45 |
#ifdef MRB_USE_BIGINT |
| 46 |
if (mrb_bigint_p(x)) { |
| 47 |
#ifndef MRB_NO_FLOAT |
| 48 |
if (mrb_float_p(y)) { |
| 49 |
return mrb_float_value(mrb, pow(mrb_bint_as_float(mrb, x), mrb_float(y))); |
| 50 |
} |
| 51 |
#endif |
| 52 |
return mrb_bint_pow(mrb, x, y); |
| 53 |
} |
| 54 |
#endif |
| 55 |
mrb_int base = mrb_integer(x); |
| 56 |
mrb_int result = 1; |
| 57 |
mrb_int exp; |
| 58 |
|
| 59 |
#ifndef MRB_NO_FLOAT |
| 60 |
if (mrb_float_p(y)) { |
| 61 |
return mrb_float_value(mrb, pow((double)base, mrb_float(y))); |
| 62 |
} |
| 63 |
else if (mrb_integer_p(y)) { |
| 64 |
exp = mrb_integer(y); |
| 65 |
} |
| 66 |
else |
| 67 |
#endif |
| 68 |
{ |
| 69 |
exp = mrb_as_int(mrb, y); |
| 70 |
} |
| 71 |
if (exp < 0) { |
| 72 |
#ifndef MRB_NO_FLOAT |
| 73 |
return mrb_float_value(mrb, pow((double)base, (double)exp)); |
| 74 |
#else |
| 75 |
mrb_int_overflow(mrb, "negative power"); |
| 76 |
#endif |
| 77 |
} |
| 78 |
for (;;) { |
| 79 |
if (exp & 1) { |
| 80 |
if (mrb_int_mul_overflow(result, base, &result)) { |
| 81 |
#ifdef MRB_USE_BIGINT |
| 82 |
return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y); |
| 83 |
#else |
| 84 |
mrb_int_overflow(mrb, "power"); |
| 85 |
#endif |
| 86 |
} |
| 87 |
} |
| 88 |
exp >>= 1; |
| 89 |
if (exp == 0) break; |
| 90 |
if (mrb_int_mul_overflow(base, base, &base)) { |
| 91 |
#ifdef MRB_USE_BIGINT |
| 92 |
return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y); |
| 93 |
#else |
| 94 |
mrb_int_overflow(mrb, "power"); |
| 95 |
#endif |
| 96 |
} |
| 97 |
} |
| 98 |
return mrb_int_value(mrb, result); |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* call-seq: |
| 103 |
* |
| 104 |
* num ** other -> num |
| 105 |
* |
| 106 |
* Raises <code>num</code> the <code>other</code> power. |
| 107 |
* |
| 108 |
* 2.0**3 #=> 8.0 |
| 109 |
*/ |
| 110 |
static mrb_value |
| 111 |
int_pow(mrb_state *mrb, mrb_value x) |
| 112 |
{ |
| 113 |
return mrb_int_pow(mrb, x, mrb_get_arg1(mrb)); |
| 114 |
} |
| 115 |
|
| 116 |
mrb_int |
| 117 |
mrb_div_int(mrb_int x, mrb_int y) |
| 118 |
{ |
| 119 |
mrb_int div = x / y; |
| 120 |
|
| 121 |
if ((x ^ y) < 0 && x != div * y) { |
| 122 |
div -= 1; |
| 123 |
} |
| 124 |
return div; |
| 125 |
} |
| 126 |
|
| 127 |
mrb_value |
| 128 |
mrb_div_int_value(mrb_state *mrb, mrb_int x, mrb_int y) |
| 129 |
{ |
| 130 |
if (y == 0) { |
| 131 |
mrb_int_zerodiv(mrb); |
| 132 |
} |
| 133 |
else if (x == MRB_INT_MIN && y == -1) { |
| 134 |
#ifdef MRB_USE_BIGINT |
| 135 |
return mrb_bint_mul_ii(mrb, x, y); |
| 136 |
#else |
| 137 |
mrb_int_overflow(mrb, "division"); |
| 138 |
#endif |
| 139 |
} |
| 140 |
return mrb_int_value(mrb, mrb_div_int(x, y)); |
| 141 |
} |
| 142 |
|
| 143 |
/* 15.2.8.3.6 */ |
| 144 |
/* |
| 145 |
* call-seq: |
| 146 |
* int / num -> num |
| 147 |
* |
| 148 |
* Performs division: the class of the resulting object depends on |
| 149 |
* the class of <code>num</code> and on the magnitude of the |
| 150 |
* result. |
| 151 |
*/ |
| 152 |
static mrb_value |
| 153 |
int_div(mrb_state *mrb, mrb_value x) |
| 154 |
{ |
| 155 |
mrb_value y = mrb_get_arg1(mrb); |
| 156 |
#ifdef MRB_USE_BIGINT |
| 157 |
if (mrb_bigint_p(x)) { |
| 158 |
return mrb_bint_div(mrb, x, y); |
| 159 |
} |
| 160 |
#endif |
| 161 |
mrb_int a = mrb_integer(x); |
| 162 |
|
| 163 |
if (mrb_integer_p(y)) { |
| 164 |
return mrb_div_int_value(mrb, a, mrb_integer(y)); |
| 165 |
} |
| 166 |
switch (mrb_type(y)) { |
| 167 |
#ifdef MRB_USE_BIGINT |
| 168 |
case MRB_TT_BIGINT: |
| 169 |
return mrb_bint_div(mrb, mrb_bint_new_int(mrb, a), y); |
| 170 |
#endif |
| 171 |
#ifdef MRB_USE_RATIONAL |
| 172 |
case MRB_TT_RATIONAL: |
| 173 |
return mrb_rational_div(mrb, mrb_rational_new(mrb, a, 1), y); |
| 174 |
#endif |
| 175 |
#ifdef MRB_USE_COMPLEX |
| 176 |
case MRB_TT_COMPLEX: |
| 177 |
x = mrb_complex_new(mrb, (mrb_float)a, 0); |
| 178 |
return mrb_complex_div(mrb, x, y); |
| 179 |
#endif |
| 180 |
#ifndef MRB_NO_FLOAT |
| 181 |
case MRB_TT_FLOAT: |
| 182 |
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y))); |
| 183 |
#endif |
| 184 |
default: |
| 185 |
mrb_int_noconv(mrb, y); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/* 15.2.9.3.19(x) */ |
| 190 |
/* |
| 191 |
* call-seq: |
| 192 |
* num.quo(numeric) -> real |
| 193 |
* |
| 194 |
* Returns most exact division. |
| 195 |
*/ |
| 196 |
|
| 197 |
/* |
| 198 |
* call-seq: |
| 199 |
* int.div(other) -> int |
| 200 |
* |
| 201 |
* Performs division: resulting integer. |
| 202 |
*/ |
| 203 |
static mrb_value |
| 204 |
int_idiv(mrb_state *mrb, mrb_value x) |
| 205 |
{ |
| 206 |
#ifdef MRB_USE_BIGINT |
| 207 |
if (mrb_bigint_p(x)) { |
| 208 |
return mrb_bint_div(mrb, x, mrb_get_arg1(mrb)); |
| 209 |
} |
| 210 |
#endif |
| 211 |
mrb_int y = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 212 |
return mrb_div_int_value(mrb, mrb_integer(x), y); |
| 213 |
} |
| 214 |
|
| 215 |
static mrb_value |
| 216 |
int_quo(mrb_state *mrb, mrb_value x) |
| 217 |
{ |
| 218 |
#ifndef MRB_USE_RATIONAL |
| 219 |
#ifdef MRB_NO_FLOAT |
| 220 |
return int_idiv(mrb, x); |
| 221 |
#else |
| 222 |
mrb_float y = mrb_as_float(mrb, mrb_get_arg1(mrb)); |
| 223 |
|
| 224 |
if (y == 0) { |
| 225 |
mrb_int_zerodiv(mrb); |
| 226 |
} |
| 227 |
#ifdef MRB_USE_BIGINT |
| 228 |
if (mrb_bigint_p(x)) { |
| 229 |
return mrb_float_value(mrb, mrb_bint_as_float(mrb, x) / y); |
| 230 |
} |
| 231 |
#endif |
| 232 |
return mrb_float_value(mrb, mrb_integer(x) / y); |
| 233 |
#endif |
| 234 |
#else |
| 235 |
mrb_int a = mrb_integer(x); |
| 236 |
mrb_value y = mrb_get_arg1(mrb); |
| 237 |
if (mrb_integer_p(y) && mrb_class_defined_id(mrb, MRB_SYM(Rational))) { |
| 238 |
return mrb_rational_new(mrb, a, mrb_integer(y)); |
| 239 |
} |
| 240 |
switch (mrb_type(y)) { |
| 241 |
case MRB_TT_RATIONAL: |
| 242 |
x = mrb_rational_new(mrb, a, 1); |
| 243 |
return mrb_rational_div(mrb, x, y); |
| 244 |
default: |
| 245 |
#ifndef MRB_NO_FLOAT |
| 246 |
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y))); |
| 247 |
#else |
| 248 |
mrb_int_noconv(mrb, y); |
| 249 |
break; |
| 250 |
#endif |
| 251 |
} |
| 252 |
#endif |
| 253 |
} |
| 254 |
|
| 255 |
static mrb_value |
| 256 |
coerce_step_counter(mrb_state *mrb, mrb_value self) |
| 257 |
{ |
| 258 |
mrb_value num, step; |
| 259 |
|
| 260 |
mrb_get_args(mrb, "oo", &num, &step); |
| 261 |
|
| 262 |
#ifndef MRB_NO_FLOAT |
| 263 |
mrb->c->ci->mid = 0; |
| 264 |
if (mrb_float_p(num) || mrb_float_p(step)) { |
| 265 |
return mrb_ensure_float_type(mrb, self); |
| 266 |
} |
| 267 |
#endif |
| 268 |
|
| 269 |
return self; |
| 270 |
} |
| 271 |
|
| 272 |
#ifndef MRB_NO_FLOAT |
| 273 |
/******************************************************************** |
| 274 |
* |
| 275 |
* Document-class: Float |
| 276 |
* |
| 277 |
* <code>Float</code> objects represent inexact real numbers using |
| 278 |
* the native architecture's double-precision floating-point |
| 279 |
* representation. |
| 280 |
*/ |
| 281 |
|
| 282 |
static mrb_value |
| 283 |
flo_pow(mrb_state *mrb, mrb_value x) |
| 284 |
{ |
| 285 |
mrb_value y = mrb_get_arg1(mrb); |
| 286 |
mrb_float d = pow(mrb_as_float(mrb, x), mrb_as_float(mrb, y)); |
| 287 |
return mrb_float_value(mrb, d); |
| 288 |
} |
| 289 |
|
| 290 |
static mrb_value |
| 291 |
flo_idiv(mrb_state *mrb, mrb_value xv) |
| 292 |
{ |
| 293 |
mrb_float x = mrb_float(xv); |
| 294 |
mrb_check_num_exact(mrb, x); |
| 295 |
mrb_int y = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 296 |
return mrb_div_int_value(mrb, (mrb_int)x, y); |
| 297 |
} |
| 298 |
|
| 299 |
mrb_float |
| 300 |
mrb_div_float(mrb_float x, mrb_float y) |
| 301 |
{ |
| 302 |
if (y != 0.0) { |
| 303 |
return x / y; |
| 304 |
} |
| 305 |
else if (x == 0.0) { |
| 306 |
return NAN; |
| 307 |
} |
| 308 |
else { |
| 309 |
return x * (signbit(y) ? -1.0 : 1.0) * INFINITY; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/* 15.2.9.3.6 */ |
| 314 |
/* |
| 315 |
* call-seq: |
| 316 |
* float / num -> float |
| 317 |
* |
| 318 |
* Returns a new Float which is the result of dividing float by num. |
| 319 |
*/ |
| 320 |
static mrb_value |
| 321 |
flo_div(mrb_state *mrb, mrb_value x) |
| 322 |
{ |
| 323 |
mrb_value y = mrb_get_arg1(mrb); |
| 324 |
mrb_float a = mrb_float(x); |
| 325 |
|
| 326 |
switch(mrb_type(y)) { |
| 327 |
#ifdef MRB_USE_COMPLEX |
| 328 |
case MRB_TT_COMPLEX: |
| 329 |
return mrb_complex_div(mrb, mrb_complex_new(mrb, a, 0), y); |
| 330 |
#endif |
| 331 |
case MRB_TT_FLOAT: |
| 332 |
a = mrb_div_float(a, mrb_float(y)); |
| 333 |
return mrb_float_value(mrb, a); |
| 334 |
default: |
| 335 |
a = mrb_div_float(a, mrb_as_float(mrb, y)); |
| 336 |
return mrb_float_value(mrb, a); |
| 337 |
} |
| 338 |
return mrb_float_value(mrb, a); |
| 339 |
} |
| 340 |
|
| 341 |
/* the argument `fmt` is no longer used; you can pass `NULL` */ |
| 342 |
mrb_value |
| 343 |
mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt) |
| 344 |
{ |
| 345 |
char buf[25]; |
| 346 |
#ifdef MRB_USE_FLOAT32 |
| 347 |
const int prec = 7; |
| 348 |
#else |
| 349 |
const int prec = 15; |
| 350 |
#endif |
| 351 |
|
| 352 |
mrb_format_float(mrb_float(flo), buf, sizeof(buf), 'g', prec, '\0'); |
| 353 |
for (char *p = buf; *p; p++) { |
| 354 |
if (*p == '.') goto exit; |
| 355 |
if (*p == 'e') { |
| 356 |
memmove(p+2, p, strlen(p)+1); |
| 357 |
p[0] = '.'; |
| 358 |
p[1] = '0'; |
| 359 |
goto exit; |
| 360 |
} |
| 361 |
} |
| 362 |
strcat(buf, ".0"); |
| 363 |
exit: |
| 364 |
return mrb_str_new_cstr(mrb, buf); |
| 365 |
} |
| 366 |
|
| 367 |
/* 15.2.9.3.16(x) */ |
| 368 |
/* |
| 369 |
* call-seq: |
| 370 |
* flt.to_s -> string |
| 371 |
* flt.inspect -> string |
| 372 |
* |
| 373 |
* Returns a string containing a representation of self. As well as a |
| 374 |
* fixed or exponential form of the number, the call may return |
| 375 |
* "<code>NaN</code>", "<code>Infinity</code>", and |
| 376 |
* "<code>-Infinity</code>". |
| 377 |
* |
| 378 |
* 3.0.to_s #=> 3.0 |
| 379 |
* 3.25.to_s #=> 3.25 |
| 380 |
*/ |
| 381 |
|
| 382 |
static mrb_value |
| 383 |
flo_to_s(mrb_state *mrb, mrb_value flt) |
| 384 |
{ |
| 385 |
mrb_float f = mrb_float(flt); |
| 386 |
mrb_value str; |
| 387 |
|
| 388 |
if (isinf(f)) { |
| 389 |
str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity") |
| 390 |
: mrb_str_new_lit(mrb, "Infinity"); |
| 391 |
} |
| 392 |
else if (isnan(f)) { |
| 393 |
str = mrb_str_new_lit(mrb, "NaN"); |
| 394 |
} |
| 395 |
else { |
| 396 |
str = mrb_float_to_str(mrb, flt, NULL); |
| 397 |
} |
| 398 |
|
| 399 |
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str)); |
| 400 |
return str; |
| 401 |
} |
| 402 |
|
| 403 |
/* 15.2.9.3.3 */ |
| 404 |
/* |
| 405 |
* call-seq: |
| 406 |
* float + other -> float |
| 407 |
* |
| 408 |
* Returns a new float which is the sum of <code>float</code> |
| 409 |
* and <code>other</code>. |
| 410 |
*/ |
| 411 |
static mrb_value |
| 412 |
flo_add(mrb_state *mrb, mrb_value x) |
| 413 |
{ |
| 414 |
mrb_value y = mrb_get_arg1(mrb); |
| 415 |
mrb_float a = mrb_float(x); |
| 416 |
|
| 417 |
switch (mrb_type(y)) { |
| 418 |
case MRB_TT_FLOAT: |
| 419 |
return mrb_float_value(mrb, a + mrb_float(y)); |
| 420 |
#if defined(MRB_USE_COMPLEX) |
| 421 |
case MRB_TT_COMPLEX: |
| 422 |
return mrb_complex_add(mrb, y, x); |
| 423 |
#endif |
| 424 |
default: |
| 425 |
return mrb_float_value(mrb, a + mrb_as_float(mrb, y)); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* 15.2.9.3.4 */ |
| 430 |
/* |
| 431 |
* call-seq: |
| 432 |
* float - other -> float |
| 433 |
* |
| 434 |
* Returns a new float which is the difference of <code>float</code> |
| 435 |
* and <code>other</code>. |
| 436 |
*/ |
| 437 |
|
| 438 |
static mrb_value |
| 439 |
flo_sub(mrb_state *mrb, mrb_value x) |
| 440 |
{ |
| 441 |
mrb_value y = mrb_get_arg1(mrb); |
| 442 |
mrb_float a = mrb_float(x); |
| 443 |
|
| 444 |
switch (mrb_type(y)) { |
| 445 |
case MRB_TT_FLOAT: |
| 446 |
return mrb_float_value(mrb, a - mrb_float(y)); |
| 447 |
#if defined(MRB_USE_COMPLEX) |
| 448 |
case MRB_TT_COMPLEX: |
| 449 |
return mrb_complex_sub(mrb, mrb_complex_new(mrb, a, 0), y); |
| 450 |
#endif |
| 451 |
default: |
| 452 |
return mrb_float_value(mrb, a - mrb_as_float(mrb, y)); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* 15.2.9.3.5 */ |
| 457 |
/* |
| 458 |
* call-seq: |
| 459 |
* float * other -> float |
| 460 |
* |
| 461 |
* Returns a new float which is the product of <code>float</code> |
| 462 |
* and <code>other</code>. |
| 463 |
*/ |
| 464 |
|
| 465 |
static mrb_value |
| 466 |
flo_mul(mrb_state *mrb, mrb_value x) |
| 467 |
{ |
| 468 |
mrb_value y = mrb_get_arg1(mrb); |
| 469 |
mrb_float a = mrb_float(x); |
| 470 |
|
| 471 |
switch (mrb_type(y)) { |
| 472 |
case MRB_TT_FLOAT: |
| 473 |
return mrb_float_value(mrb, a * mrb_float(y)); |
| 474 |
#if defined(MRB_USE_COMPLEX) |
| 475 |
case MRB_TT_COMPLEX: |
| 476 |
return mrb_complex_mul(mrb, y, x); |
| 477 |
#endif |
| 478 |
default: |
| 479 |
return mrb_float_value(mrb, a * mrb_as_float(mrb, y)); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
static void |
| 484 |
flodivmod(mrb_state *mrb, double x, double y, mrb_float *divp, mrb_float *modp) |
| 485 |
{ |
| 486 |
double div, mod; |
| 487 |
|
| 488 |
if (isnan(y)) { |
| 489 |
/* y is NaN so all results are NaN */ |
| 490 |
div = mod = y; |
| 491 |
goto exit; |
| 492 |
} |
| 493 |
if (y == 0.0) { |
| 494 |
mrb_int_zerodiv(mrb); |
| 495 |
} |
| 496 |
if (isinf(y) && !isinf(x)) { |
| 497 |
mod = x; |
| 498 |
} |
| 499 |
else { |
| 500 |
mod = fmod(x, y); |
| 501 |
} |
| 502 |
if (isinf(x) && !isinf(y)) { |
| 503 |
div = x; |
| 504 |
} |
| 505 |
else { |
| 506 |
div = (x - mod) / y; |
| 507 |
if (modp && divp) div = round(div); |
| 508 |
} |
| 509 |
if (div == 0) div = 0.0; |
| 510 |
if (mod == 0) mod = 0.0; |
| 511 |
if (y*mod < 0) { |
| 512 |
mod += y; |
| 513 |
div -= 1.0; |
| 514 |
} |
| 515 |
exit: |
| 516 |
if (modp) *modp = mod; |
| 517 |
if (divp) *divp = div; |
| 518 |
} |
| 519 |
|
| 520 |
/* 15.2.9.3.5 */ |
| 521 |
/* |
| 522 |
* call-seq: |
| 523 |
* flt % other -> float |
| 524 |
* flt.modulo(other) -> float |
| 525 |
* |
| 526 |
* Return the modulo after division of <code>flt</code> by <code>other</code>. |
| 527 |
* |
| 528 |
* 6543.21.modulo(137) #=> 104.21 |
| 529 |
* 6543.21.modulo(137.24) #=> 92.9299999999996 |
| 530 |
*/ |
| 531 |
|
| 532 |
static mrb_value |
| 533 |
flo_mod(mrb_state *mrb, mrb_value x) |
| 534 |
{ |
| 535 |
mrb_value y = mrb_get_arg1(mrb); |
| 536 |
mrb_float mod; |
| 537 |
|
| 538 |
flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), NULL, &mod); |
| 539 |
return mrb_float_value(mrb, mod); |
| 540 |
} |
| 541 |
#endif |
| 542 |
|
| 543 |
/* 15.2.8.3.16 */ |
| 544 |
/* |
| 545 |
* call-seq: |
| 546 |
* num.eql?(numeric) -> true or false |
| 547 |
* |
| 548 |
* Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the |
| 549 |
* same type and have equal values. |
| 550 |
* |
| 551 |
* 1 == 1.0 #=> true |
| 552 |
* 1.eql?(1.0) #=> false |
| 553 |
* (1.0).eql?(1.0) #=> true |
| 554 |
*/ |
| 555 |
static mrb_value |
| 556 |
num_eql(mrb_state *mrb, mrb_value x) |
| 557 |
{ |
| 558 |
mrb_value y = mrb_get_arg1(mrb); |
| 559 |
|
| 560 |
#ifdef MRB_USE_BIGINT |
| 561 |
if (mrb_bigint_p(x)) { |
| 562 |
return mrb_bool_value(mrb_bint_cmp(mrb, x, y) == 0); |
| 563 |
} |
| 564 |
#endif |
| 565 |
#ifndef MRB_NO_FLOAT |
| 566 |
if (mrb_float_p(x)) { |
| 567 |
if (!mrb_float_p(y)) return mrb_false_value(); |
| 568 |
return mrb_bool_value(mrb_float(x) == mrb_float(y)); |
| 569 |
} |
| 570 |
#endif |
| 571 |
if (mrb_integer_p(x)) { |
| 572 |
if (!mrb_integer_p(y)) return mrb_false_value(); |
| 573 |
return mrb_bool_value(mrb_integer(x) == mrb_integer(y)); |
| 574 |
} |
| 575 |
return mrb_bool_value(mrb_equal(mrb, x, y)); |
| 576 |
} |
| 577 |
|
| 578 |
#ifndef MRB_NO_FLOAT |
| 579 |
/* 15.2.9.3.7 */ |
| 580 |
/* |
| 581 |
* call-seq: |
| 582 |
* flt == obj -> true or false |
| 583 |
* |
| 584 |
* Returns <code>true</code> only if <i>obj</i> has the same value |
| 585 |
* as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which |
| 586 |
* requires <i>obj</i> to be a <code>Float</code>. |
| 587 |
* |
| 588 |
* 1.0 == 1 #=> true |
| 589 |
* |
| 590 |
*/ |
| 591 |
|
| 592 |
static mrb_value |
| 593 |
flo_eq(mrb_state *mrb, mrb_value x) |
| 594 |
{ |
| 595 |
mrb_value y = mrb_get_arg1(mrb); |
| 596 |
|
| 597 |
switch (mrb_type(y)) { |
| 598 |
case MRB_TT_INTEGER: |
| 599 |
return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_integer(y)); |
| 600 |
case MRB_TT_FLOAT: |
| 601 |
return mrb_bool_value(mrb_float(x) == mrb_float(y)); |
| 602 |
#ifdef MRB_USE_RATIONAL |
| 603 |
case MRB_TT_RATIONAL: |
| 604 |
return mrb_bool_value(mrb_float(x) == mrb_as_float(mrb, y)); |
| 605 |
#endif |
| 606 |
#ifdef MRB_USE_COMPLEX |
| 607 |
case MRB_TT_COMPLEX: |
| 608 |
return mrb_bool_value(mrb_equal(mrb, y, x)); |
| 609 |
#endif |
| 610 |
default: |
| 611 |
return mrb_false_value(); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
static int64_t |
| 616 |
value_int64(mrb_state *mrb, mrb_value x) |
| 617 |
{ |
| 618 |
switch (mrb_type(x)) { |
| 619 |
case MRB_TT_INTEGER: |
| 620 |
return (int64_t)mrb_integer(x); |
| 621 |
case MRB_TT_FLOAT: |
| 622 |
{ |
| 623 |
double f = mrb_float(x); |
| 624 |
|
| 625 |
if ((mrb_float)INT64_MAX >= f && f >= (mrb_float)INT64_MIN) |
| 626 |
return (int64_t)f; |
| 627 |
} |
| 628 |
default: |
| 629 |
mrb_raise(mrb, E_TYPE_ERROR, "cannot convert to Integer"); |
| 630 |
break; |
| 631 |
} |
| 632 |
/* not reached */ |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
static mrb_value |
| 637 |
int64_value(mrb_state *mrb, int64_t v) |
| 638 |
{ |
| 639 |
if (!TYPED_FIXABLE(v,int64_t)) { |
| 640 |
mrb_int_overflow(mrb, "bit operation"); |
| 641 |
} |
| 642 |
return mrb_fixnum_value((mrb_int)v); |
| 643 |
} |
| 644 |
|
| 645 |
static mrb_value |
| 646 |
flo_rev(mrb_state *mrb, mrb_value x) |
| 647 |
{ |
| 648 |
int64_t v1 = value_int64(mrb, x); |
| 649 |
return int64_value(mrb, ~v1); |
| 650 |
} |
| 651 |
|
| 652 |
static mrb_value |
| 653 |
flo_and(mrb_state *mrb, mrb_value x) |
| 654 |
{ |
| 655 |
mrb_value y = mrb_get_arg1(mrb); |
| 656 |
int64_t v1, v2; |
| 657 |
|
| 658 |
v1 = value_int64(mrb, x); |
| 659 |
v2 = value_int64(mrb, y); |
| 660 |
return int64_value(mrb, v1 & v2); |
| 661 |
} |
| 662 |
|
| 663 |
static mrb_value |
| 664 |
flo_or(mrb_state *mrb, mrb_value x) |
| 665 |
{ |
| 666 |
mrb_value y = mrb_get_arg1(mrb); |
| 667 |
int64_t v1, v2; |
| 668 |
|
| 669 |
v1 = value_int64(mrb, x); |
| 670 |
v2 = value_int64(mrb, y); |
| 671 |
return int64_value(mrb, v1 | v2); |
| 672 |
} |
| 673 |
|
| 674 |
static mrb_value |
| 675 |
flo_xor(mrb_state *mrb, mrb_value x) |
| 676 |
{ |
| 677 |
mrb_value y = mrb_get_arg1(mrb); |
| 678 |
int64_t v1, v2; |
| 679 |
|
| 680 |
v1 = value_int64(mrb, x); |
| 681 |
v2 = value_int64(mrb, y); |
| 682 |
return int64_value(mrb, v1 ^ v2); |
| 683 |
} |
| 684 |
|
| 685 |
static mrb_value |
| 686 |
flo_shift(mrb_state *mrb, mrb_value x, mrb_int width) |
| 687 |
{ |
| 688 |
mrb_float val; |
| 689 |
|
| 690 |
if (width == 0) { |
| 691 |
return x; |
| 692 |
} |
| 693 |
val = mrb_float(x); |
| 694 |
if (width < -MRB_INT_BIT/2) { |
| 695 |
if (val < 0) return mrb_fixnum_value(-1); |
| 696 |
return mrb_fixnum_value(0); |
| 697 |
} |
| 698 |
if (width < 0) { |
| 699 |
while (width++) { |
| 700 |
val /= 2; |
| 701 |
if (val < 1.0) { |
| 702 |
val = 0; |
| 703 |
break; |
| 704 |
} |
| 705 |
} |
| 706 |
#if defined(_ISOC99_SOURCE) |
| 707 |
val = trunc(val); |
| 708 |
#else |
| 709 |
if (val > 0){ |
| 710 |
val = floor(val); |
| 711 |
} |
| 712 |
else { |
| 713 |
val = ceil(val); |
| 714 |
} |
| 715 |
#endif |
| 716 |
if (val == 0 && mrb_float(x) < 0) { |
| 717 |
return mrb_fixnum_value(-1); |
| 718 |
} |
| 719 |
} |
| 720 |
else { |
| 721 |
while (width--) { |
| 722 |
val *= 2; |
| 723 |
} |
| 724 |
} |
| 725 |
if (FIXABLE_FLOAT(val)) |
| 726 |
return mrb_int_value(mrb, (mrb_int)val); |
| 727 |
return mrb_float_value(mrb, val); |
| 728 |
} |
| 729 |
|
| 730 |
static mrb_value |
| 731 |
flo_rshift(mrb_state *mrb, mrb_value x) |
| 732 |
{ |
| 733 |
mrb_int width = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 734 |
if (width == MRB_INT_MIN) return flo_shift(mrb, x, -MRB_INT_BIT); |
| 735 |
return flo_shift(mrb, x, -width); |
| 736 |
} |
| 737 |
|
| 738 |
static mrb_value |
| 739 |
flo_lshift(mrb_state *mrb, mrb_value x) |
| 740 |
{ |
| 741 |
mrb_int width = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 742 |
return flo_shift(mrb, x, width); |
| 743 |
} |
| 744 |
|
| 745 |
/* 15.2.9.3.13 */ |
| 746 |
/* |
| 747 |
* call-seq: |
| 748 |
* flt.to_f -> self |
| 749 |
* |
| 750 |
* As <code>flt</code> is already a float, returns +self+. |
| 751 |
*/ |
| 752 |
|
| 753 |
static mrb_value |
| 754 |
flo_to_f(mrb_state *mrb, mrb_value num) |
| 755 |
{ |
| 756 |
return num; |
| 757 |
} |
| 758 |
|
| 759 |
/* 15.2.9.3.11 */ |
| 760 |
/* |
| 761 |
* call-seq: |
| 762 |
* flt.infinite? -> nil, -1, +1 |
| 763 |
* |
| 764 |
* Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i> |
| 765 |
* is finite, -infinity, or +infinity. |
| 766 |
* |
| 767 |
* (0.0).infinite? #=> nil |
| 768 |
* (-1.0/0.0).infinite? #=> -1 |
| 769 |
* (+1.0/0.0).infinite? #=> 1 |
| 770 |
*/ |
| 771 |
|
| 772 |
static mrb_value |
| 773 |
flo_infinite_p(mrb_state *mrb, mrb_value num) |
| 774 |
{ |
| 775 |
mrb_float value = mrb_float(num); |
| 776 |
|
| 777 |
if (isinf(value)) { |
| 778 |
return mrb_fixnum_value(value < 0 ? -1 : 1); |
| 779 |
} |
| 780 |
return mrb_nil_value(); |
| 781 |
} |
| 782 |
|
| 783 |
/* 15.2.9.3.9 */ |
| 784 |
/* |
| 785 |
* call-seq: |
| 786 |
* flt.finite? -> true or false |
| 787 |
* |
| 788 |
* Returns <code>true</code> if <i>flt</i> is a valid IEEE floating |
| 789 |
* point number (it is not infinite, and <code>nan?</code> is |
| 790 |
* <code>false</code>). |
| 791 |
* |
| 792 |
*/ |
| 793 |
|
| 794 |
static mrb_value |
| 795 |
flo_finite_p(mrb_state *mrb, mrb_value num) |
| 796 |
{ |
| 797 |
return mrb_bool_value(isfinite(mrb_float(num))); |
| 798 |
} |
| 799 |
|
| 800 |
/* |
| 801 |
* Document-class: FloatDomainError |
| 802 |
* |
| 803 |
* Raised when attempting to convert special float values |
| 804 |
* (in particular infinite or NaN) |
| 805 |
* to numerical classes which don't support them. |
| 806 |
* |
| 807 |
* Float::INFINITY.to_i |
| 808 |
* |
| 809 |
* <em>raises the exception:</em> |
| 810 |
* |
| 811 |
* FloatDomainError: Infinity |
| 812 |
*/ |
| 813 |
/* ------------------------------------------------------------------------*/ |
| 814 |
void |
| 815 |
mrb_check_num_exact(mrb_state *mrb, mrb_float num) |
| 816 |
{ |
| 817 |
if (isinf(num)) { |
| 818 |
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity"); |
| 819 |
} |
| 820 |
if (isnan(num)) { |
| 821 |
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN"); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
static mrb_value |
| 826 |
flo_rounding_int(mrb_state *mrb, mrb_float f) |
| 827 |
{ |
| 828 |
if (!FIXABLE_FLOAT(f)) { |
| 829 |
#ifdef MRB_USE_BIGINT |
| 830 |
return mrb_bint_new_float(mrb, f); |
| 831 |
#else |
| 832 |
mrb_int_overflow(mrb, "rounding"); |
| 833 |
#endif |
| 834 |
} |
| 835 |
return mrb_int_value(mrb, (mrb_int)f); |
| 836 |
} |
| 837 |
|
| 838 |
static mrb_value |
| 839 |
flo_rounding(mrb_state *mrb, mrb_value num, double (*func)(double)) |
| 840 |
{ |
| 841 |
mrb_float f = mrb_float(num); |
| 842 |
mrb_int ndigits = 0; |
| 843 |
#ifdef MRB_USE_FLOAT32 |
| 844 |
const int fprec = 7; |
| 845 |
#else |
| 846 |
const int fprec = 15; |
| 847 |
#endif |
| 848 |
|
| 849 |
mrb_get_args(mrb, "|i", &ndigits); |
| 850 |
if (f == 0.0) { |
| 851 |
return ndigits > 0 ? mrb_float_value(mrb, f) : mrb_fixnum_value(0); |
| 852 |
} |
| 853 |
if (ndigits > 0) { |
| 854 |
if (ndigits > fprec) return num; |
| 855 |
mrb_float d = pow(10, (double)ndigits); |
| 856 |
f = func(f * d) / d; |
| 857 |
mrb_check_num_exact(mrb, f); |
| 858 |
return mrb_float_value(mrb, f); |
| 859 |
} |
| 860 |
if (ndigits < 0) { |
| 861 |
mrb_float d = pow(10, -(double)ndigits); |
| 862 |
f = func(f / d) * d; |
| 863 |
} |
| 864 |
else { /* ndigits == 0 */ |
| 865 |
f = func(f); |
| 866 |
} |
| 867 |
mrb_check_num_exact(mrb, f); |
| 868 |
return flo_rounding_int(mrb, f); |
| 869 |
} |
| 870 |
|
| 871 |
/* 15.2.9.3.10 */ |
| 872 |
/* |
| 873 |
* call-seq: |
| 874 |
* float.floor([ndigits]) -> integer or float |
| 875 |
* |
| 876 |
* Returns the largest number less than or equal to +float+ with |
| 877 |
* a precision of +ndigits+ decimal digits (default: 0). |
| 878 |
* |
| 879 |
* When the precision is negative, the returned value is an integer |
| 880 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 881 |
* |
| 882 |
* Returns a floating-point number when +ndigits+ is positive, |
| 883 |
* otherwise returns an integer. |
| 884 |
* |
| 885 |
* 1.2.floor #=> 1 |
| 886 |
* 2.0.floor #=> 2 |
| 887 |
* (-1.2).floor #=> -2 |
| 888 |
* (-2.0).floor #=> -2 |
| 889 |
* |
| 890 |
* 1.234567.floor(2) #=> 1.23 |
| 891 |
* 1.234567.floor(3) #=> 1.234 |
| 892 |
* 1.234567.floor(4) #=> 1.2345 |
| 893 |
* 1.234567.floor(5) #=> 1.23456 |
| 894 |
* |
| 895 |
* 34567.89.floor(-5) #=> 0 |
| 896 |
* 34567.89.floor(-4) #=> 30000 |
| 897 |
* 34567.89.floor(-3) #=> 34000 |
| 898 |
* 34567.89.floor(-2) #=> 34500 |
| 899 |
* 34567.89.floor(-1) #=> 34560 |
| 900 |
* 34567.89.floor(0) #=> 34567 |
| 901 |
* 34567.89.floor(1) #=> 34567.8 |
| 902 |
* 34567.89.floor(2) #=> 34567.89 |
| 903 |
* 34567.89.floor(3) #=> 34567.89 |
| 904 |
* |
| 905 |
* Note that the limited precision of floating-point arithmetic |
| 906 |
* might lead to surprising results: |
| 907 |
* |
| 908 |
* (0.3 / 0.1).floor #=> 2 (!) |
| 909 |
*/ |
| 910 |
static mrb_value |
| 911 |
flo_floor(mrb_state *mrb, mrb_value num) |
| 912 |
{ |
| 913 |
return flo_rounding(mrb, num, floor); |
| 914 |
} |
| 915 |
|
| 916 |
/* 15.2.9.3.8 */ |
| 917 |
/* |
| 918 |
* call-seq: |
| 919 |
* float.ceil([ndigits]) -> integer or float |
| 920 |
* |
| 921 |
* Returns the smallest number greater than or equal to +float+ with |
| 922 |
* a precision of +ndigits+ decimal digits (default: 0). |
| 923 |
* |
| 924 |
* When the precision is negative, the returned value is an integer |
| 925 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 926 |
* |
| 927 |
* Returns a floating-point number when +ndigits+ is positive, |
| 928 |
* otherwise returns an integer. |
| 929 |
* |
| 930 |
* 1.2.ceil #=> 2 |
| 931 |
* 2.0.ceil #=> 2 |
| 932 |
* (-1.2).ceil #=> -1 |
| 933 |
* (-2.0).ceil #=> -2 |
| 934 |
* |
| 935 |
* 1.234567.ceil(2) #=> 1.24 |
| 936 |
* 1.234567.ceil(3) #=> 1.235 |
| 937 |
* 1.234567.ceil(4) #=> 1.2346 |
| 938 |
* 1.234567.ceil(5) #=> 1.23457 |
| 939 |
* |
| 940 |
* 34567.89.ceil(-5) #=> 100000 |
| 941 |
* 34567.89.ceil(-4) #=> 40000 |
| 942 |
* 34567.89.ceil(-3) #=> 35000 |
| 943 |
* 34567.89.ceil(-2) #=> 34600 |
| 944 |
* 34567.89.ceil(-1) #=> 34570 |
| 945 |
* 34567.89.ceil(0) #=> 34568 |
| 946 |
* 34567.89.ceil(1) #=> 34567.9 |
| 947 |
* 34567.89.ceil(2) #=> 34567.89 |
| 948 |
* 34567.89.ceil(3) #=> 34567.89 |
| 949 |
* |
| 950 |
* Note that the limited precision of floating-point arithmetic |
| 951 |
* might lead to surprising results: |
| 952 |
* |
| 953 |
* (2.1 / 0.7).ceil #=> 4 (!) |
| 954 |
*/ |
| 955 |
|
| 956 |
static mrb_value |
| 957 |
flo_ceil(mrb_state *mrb, mrb_value num) |
| 958 |
{ |
| 959 |
return flo_rounding(mrb, num, ceil); |
| 960 |
} |
| 961 |
|
| 962 |
/* 15.2.9.3.12 */ |
| 963 |
/* |
| 964 |
* call-seq: |
| 965 |
* flt.round([ndigits]) -> integer or float |
| 966 |
* |
| 967 |
* Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits). |
| 968 |
* Precision may be negative. Returns a floating-point number when ndigits |
| 969 |
* is more than zero. |
| 970 |
* |
| 971 |
* 1.4.round #=> 1 |
| 972 |
* 1.5.round #=> 2 |
| 973 |
* 1.6.round #=> 2 |
| 974 |
* (-1.5).round #=> -2 |
| 975 |
* |
| 976 |
* 1.234567.round(2) #=> 1.23 |
| 977 |
* 1.234567.round(3) #=> 1.235 |
| 978 |
* 1.234567.round(4) #=> 1.2346 |
| 979 |
* 1.234567.round(5) #=> 1.23457 |
| 980 |
* |
| 981 |
* 34567.89.round(-5) #=> 0 |
| 982 |
* 34567.89.round(-4) #=> 30000 |
| 983 |
* 34567.89.round(-3) #=> 35000 |
| 984 |
* 34567.89.round(-2) #=> 34600 |
| 985 |
* 34567.89.round(-1) #=> 34570 |
| 986 |
* 34567.89.round(0) #=> 34568 |
| 987 |
* 34567.89.round(1) #=> 34567.9 |
| 988 |
* 34567.89.round(2) #=> 34567.89 |
| 989 |
* 34567.89.round(3) #=> 34567.89 |
| 990 |
* |
| 991 |
*/ |
| 992 |
|
| 993 |
static mrb_value |
| 994 |
flo_round(mrb_state *mrb, mrb_value num) |
| 995 |
{ |
| 996 |
double number, f; |
| 997 |
mrb_int ndigits = 0; |
| 998 |
mrb_int i; |
| 999 |
|
| 1000 |
mrb_get_args(mrb, "|i", &ndigits); |
| 1001 |
number = mrb_float(num); |
| 1002 |
|
| 1003 |
if (0 < ndigits && (isinf(number) || isnan(number))) { |
| 1004 |
return num; |
| 1005 |
} |
| 1006 |
mrb_check_num_exact(mrb, number); |
| 1007 |
|
| 1008 |
f = 1.0; |
| 1009 |
if (ndigits < -DBL_DIG-2) return mrb_fixnum_value(0); |
| 1010 |
i = ndigits >= 0 ? ndigits : -ndigits; |
| 1011 |
if (ndigits > DBL_DIG+2) return num; |
| 1012 |
while (--i >= 0) |
| 1013 |
f = f*10.0; |
| 1014 |
|
| 1015 |
if (isinf(f)) { |
| 1016 |
if (ndigits < 0) number = 0; |
| 1017 |
} |
| 1018 |
else { |
| 1019 |
double d; |
| 1020 |
|
| 1021 |
if (ndigits < 0) number /= f; |
| 1022 |
else number *= f; |
| 1023 |
|
| 1024 |
/* home-made inline implementation of round(3) */ |
| 1025 |
if (number > 0.0) { |
| 1026 |
d = floor(number); |
| 1027 |
number = d + (number - d >= 0.5); |
| 1028 |
} |
| 1029 |
else if (number < 0.0) { |
| 1030 |
d = ceil(number); |
| 1031 |
number = d - (d - number >= 0.5); |
| 1032 |
} |
| 1033 |
|
| 1034 |
if (ndigits < 0) number *= f; |
| 1035 |
else number /= f; |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (ndigits > 0) { |
| 1039 |
if (!isfinite(number)) return num; |
| 1040 |
return mrb_float_value(mrb, number); |
| 1041 |
} |
| 1042 |
if (!FIXABLE_FLOAT(number)) |
| 1043 |
return mrb_float_value(mrb, number); |
| 1044 |
return mrb_int_value(mrb, (mrb_int)number); |
| 1045 |
} |
| 1046 |
|
| 1047 |
/* 15.2.9.3.14 */ |
| 1048 |
static mrb_value |
| 1049 |
flo_to_i(mrb_state *mrb, mrb_value num) |
| 1050 |
{ |
| 1051 |
mrb_float f = mrb_float(num); |
| 1052 |
|
| 1053 |
mrb_check_num_exact(mrb, f); |
| 1054 |
if (!FIXABLE_FLOAT(f)) { |
| 1055 |
#ifdef MRB_USE_BIGINT |
| 1056 |
return mrb_bint_new_float(mrb, f); |
| 1057 |
#else |
| 1058 |
mrb_int_overflow(mrb, "to_f"); |
| 1059 |
#endif |
| 1060 |
} |
| 1061 |
if (f > 0.0) f = floor(f); |
| 1062 |
if (f < 0.0) f = ceil(f); |
| 1063 |
|
| 1064 |
return mrb_int_value(mrb, (mrb_int)f); |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* 15.2.9.3.15 */ |
| 1068 |
/* |
| 1069 |
* call-seq: |
| 1070 |
* flt.to_i -> integer |
| 1071 |
* flt.truncate -> integer |
| 1072 |
* |
| 1073 |
* Returns <i>flt</i> truncated to an <code>Integer</code>. |
| 1074 |
*/ |
| 1075 |
|
| 1076 |
static mrb_value |
| 1077 |
flo_truncate(mrb_state *mrb, mrb_value num) |
| 1078 |
{ |
| 1079 |
if (signbit(mrb_float(num))) return flo_ceil(mrb, num); |
| 1080 |
return flo_floor(mrb, num); |
| 1081 |
} |
| 1082 |
|
| 1083 |
static mrb_value |
| 1084 |
flo_nan_p(mrb_state *mrb, mrb_value num) |
| 1085 |
{ |
| 1086 |
return mrb_bool_value(isnan(mrb_float(num))); |
| 1087 |
} |
| 1088 |
|
| 1089 |
static mrb_value |
| 1090 |
flo_abs(mrb_state *mrb, mrb_value num) |
| 1091 |
{ |
| 1092 |
mrb_float f = mrb_float(num); |
| 1093 |
|
| 1094 |
if (signbit(f)) return mrb_float_value(mrb, -f); |
| 1095 |
return num; |
| 1096 |
} |
| 1097 |
#endif |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* Document-class: Integer |
| 1101 |
* |
| 1102 |
* <code>Integer</code> is hold whole numbers. |
| 1103 |
* |
| 1104 |
*/ |
| 1105 |
|
| 1106 |
/* 15.2.9.3.24 */ |
| 1107 |
/* |
| 1108 |
* call-seq: |
| 1109 |
* int.to_i -> integer |
| 1110 |
* |
| 1111 |
* As <i>int</i> is already an <code>Integer</code>, all these |
| 1112 |
* methods simply return the receiver. |
| 1113 |
*/ |
| 1114 |
|
| 1115 |
static mrb_value |
| 1116 |
int_to_i(mrb_state *mrb, mrb_value num) |
| 1117 |
{ |
| 1118 |
return num; |
| 1119 |
} |
| 1120 |
|
| 1121 |
mrb_value |
| 1122 |
mrb_int_mul(mrb_state *mrb, mrb_value x, mrb_value y) |
| 1123 |
{ |
| 1124 |
mrb_int a; |
| 1125 |
|
| 1126 |
a = mrb_integer(x); |
| 1127 |
if (mrb_integer_p(y)) { |
| 1128 |
mrb_int b, c; |
| 1129 |
|
| 1130 |
if (a == 0) return x; |
| 1131 |
if (a == 1) return y; |
| 1132 |
b = mrb_integer(y); |
| 1133 |
if (b == 0) return y; |
| 1134 |
if (b == 1) return x; |
| 1135 |
if (mrb_int_mul_overflow(a, b, &c)) { |
| 1136 |
#ifdef MRB_USE_BIGINT |
| 1137 |
x = mrb_bint_new_int(mrb, a); |
| 1138 |
return mrb_bint_mul(mrb, x, y); |
| 1139 |
#else |
| 1140 |
mrb_int_overflow(mrb, "multiplication"); |
| 1141 |
#endif |
| 1142 |
} |
| 1143 |
return mrb_int_value(mrb, c); |
| 1144 |
} |
| 1145 |
switch (mrb_type(y)) { |
| 1146 |
#ifdef MRB_USE_BIGINT |
| 1147 |
case MRB_TT_BIGINT: |
| 1148 |
if (a == 0) return x; |
| 1149 |
if (a == 1) return y; |
| 1150 |
return mrb_bint_mul(mrb, y, x); |
| 1151 |
#endif |
| 1152 |
#ifdef MRB_USE_RATIONAL |
| 1153 |
case MRB_TT_RATIONAL: |
| 1154 |
if (a == 0) return x; |
| 1155 |
if (a == 1) return y; |
| 1156 |
return mrb_rational_mul(mrb, y, x); |
| 1157 |
#endif |
| 1158 |
#ifdef MRB_USE_COMPLEX |
| 1159 |
case MRB_TT_COMPLEX: |
| 1160 |
if (a == 0) return x; |
| 1161 |
if (a == 1) return y; |
| 1162 |
return mrb_complex_mul(mrb, y, x); |
| 1163 |
#endif |
| 1164 |
#ifndef MRB_NO_FLOAT |
| 1165 |
case MRB_TT_FLOAT: |
| 1166 |
return mrb_float_value(mrb, (mrb_float)a * mrb_as_float(mrb, y)); |
| 1167 |
#endif |
| 1168 |
default: |
| 1169 |
mrb_int_noconv(mrb, y); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* 15.2.8.3.5 */ |
| 1174 |
/* |
| 1175 |
* call-seq: |
| 1176 |
* int * numeric -> numeric_result |
| 1177 |
* |
| 1178 |
* Performs multiplication: the class of the resulting object depends on |
| 1179 |
* the class of <code>numeric</code> and on the magnitude of the |
| 1180 |
* result. |
| 1181 |
*/ |
| 1182 |
|
| 1183 |
static mrb_value |
| 1184 |
int_mul(mrb_state *mrb, mrb_value x) |
| 1185 |
{ |
| 1186 |
mrb_value y = mrb_get_arg1(mrb); |
| 1187 |
|
| 1188 |
#ifdef MRB_USE_BIGINT |
| 1189 |
if (mrb_bigint_p(x)) { |
| 1190 |
return mrb_bint_mul(mrb, x, y); |
| 1191 |
} |
| 1192 |
#endif |
| 1193 |
return mrb_int_mul(mrb, x, y); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static void |
| 1197 |
intdivmod(mrb_state *mrb, mrb_int x, mrb_int y, mrb_int *divp, mrb_int *modp) |
| 1198 |
{ |
| 1199 |
if (y == 0) { |
| 1200 |
mrb_int_zerodiv(mrb); |
| 1201 |
} |
| 1202 |
else if (x == MRB_INT_MIN && y == -1) { |
| 1203 |
mrb_int_overflow(mrb, "division"); |
| 1204 |
} |
| 1205 |
else { |
| 1206 |
mrb_int div = x / y; |
| 1207 |
mrb_int mod = x - div * y; |
| 1208 |
|
| 1209 |
if ((x ^ y) < 0 && x != div * y) { |
| 1210 |
mod += y; |
| 1211 |
div -= 1; |
| 1212 |
} |
| 1213 |
if (divp) *divp = div; |
| 1214 |
if (modp) *modp = mod; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* 15.2.8.3.7 */ |
| 1219 |
/* |
| 1220 |
* call-seq: |
| 1221 |
* int % num -> num |
| 1222 |
* |
| 1223 |
* Returns <code>int</code> modulo <code>other</code>. |
| 1224 |
* See <code>numeric.divmod</code> for more information. |
| 1225 |
*/ |
| 1226 |
|
| 1227 |
static mrb_value |
| 1228 |
int_mod(mrb_state *mrb, mrb_value x) |
| 1229 |
{ |
| 1230 |
mrb_value y = mrb_get_arg1(mrb); |
| 1231 |
mrb_int a, b; |
| 1232 |
|
| 1233 |
#ifdef MRB_USE_BIGINT |
| 1234 |
if (mrb_bigint_p(x)) { |
| 1235 |
return mrb_bint_mod(mrb, x, y); |
| 1236 |
} |
| 1237 |
#endif |
| 1238 |
a = mrb_integer(x); |
| 1239 |
if (a == 0) return x; |
| 1240 |
if (mrb_integer_p(y)) { |
| 1241 |
b = mrb_integer(y); |
| 1242 |
if (b == 0) mrb_int_zerodiv(mrb); |
| 1243 |
if (a == MRB_INT_MIN && b == -1) return mrb_fixnum_value(0); |
| 1244 |
mrb_int mod = a % b; |
| 1245 |
if ((a < 0) != (b < 0) && mod != 0) { |
| 1246 |
mod += b; |
| 1247 |
} |
| 1248 |
return mrb_int_value(mrb, mod); |
| 1249 |
} |
| 1250 |
#ifdef MRB_NO_FLOAT |
| 1251 |
mrb_raise(mrb, E_TYPE_ERROR, "non integer modulo"); |
| 1252 |
#else |
| 1253 |
mrb_float mod; |
| 1254 |
|
| 1255 |
flodivmod(mrb, (mrb_float)a, mrb_as_float(mrb, y), NULL, &mod); |
| 1256 |
return mrb_float_value(mrb, mod); |
| 1257 |
#endif |
| 1258 |
} |
| 1259 |
|
| 1260 |
#ifndef MRB_NO_FLOAT |
| 1261 |
static mrb_value flo_divmod(mrb_state *mrb, mrb_value x); |
| 1262 |
#endif |
| 1263 |
|
| 1264 |
/* |
| 1265 |
* call-seq: |
| 1266 |
* int.divmod(numeric) -> array |
| 1267 |
* |
| 1268 |
* See <code>Numeric#divmod</code>. |
| 1269 |
*/ |
| 1270 |
static mrb_value |
| 1271 |
int_divmod(mrb_state *mrb, mrb_value x) |
| 1272 |
{ |
| 1273 |
mrb_value y = mrb_get_arg1(mrb); |
| 1274 |
|
| 1275 |
#ifdef MRB_USE_BIGINT |
| 1276 |
if (mrb_bigint_p(x)) { |
| 1277 |
#ifndef MRB_NO_FLOAT |
| 1278 |
if (mrb_float_p(y)) { |
| 1279 |
mrb_float f = mrb_bint_as_float(mrb, x); |
| 1280 |
return flo_divmod(mrb, mrb_float_value(mrb, f)); |
| 1281 |
} |
| 1282 |
#endif |
| 1283 |
return mrb_bint_divmod(mrb, x, y); |
| 1284 |
} |
| 1285 |
#endif |
| 1286 |
if (mrb_integer_p(y)) { |
| 1287 |
mrb_int div, mod; |
| 1288 |
|
| 1289 |
intdivmod(mrb, mrb_integer(x), mrb_integer(y), &div, &mod); |
| 1290 |
return mrb_assoc_new(mrb, mrb_int_value(mrb, div), mrb_int_value(mrb, mod)); |
| 1291 |
} |
| 1292 |
#ifdef MRB_NO_FLOAT |
| 1293 |
mrb_raise(mrb, E_TYPE_ERROR, "non integer divmod"); |
| 1294 |
#else |
| 1295 |
return flo_divmod(mrb, x); |
| 1296 |
#endif |
| 1297 |
} |
| 1298 |
|
| 1299 |
#ifndef MRB_NO_FLOAT |
| 1300 |
static mrb_value |
| 1301 |
flo_divmod(mrb_state *mrb, mrb_value x) |
| 1302 |
{ |
| 1303 |
mrb_value y = mrb_get_arg1(mrb); |
| 1304 |
mrb_float div, mod; |
| 1305 |
mrb_value a, b; |
| 1306 |
|
| 1307 |
flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), &div, &mod); |
| 1308 |
if (!FIXABLE_FLOAT(div)) |
| 1309 |
a = mrb_float_value(mrb, div); |
| 1310 |
else |
| 1311 |
a = mrb_int_value(mrb, (mrb_int)div); |
| 1312 |
b = mrb_float_value(mrb, mod); |
| 1313 |
return mrb_assoc_new(mrb, a, b); |
| 1314 |
} |
| 1315 |
#endif |
| 1316 |
|
| 1317 |
/* 15.2.8.3.2 */ |
| 1318 |
/* |
| 1319 |
* call-seq: |
| 1320 |
* int == other -> true or false |
| 1321 |
* |
| 1322 |
* Return <code>true</code> if <code>int</code> equals <code>other</code> |
| 1323 |
* numerically. |
| 1324 |
* |
| 1325 |
* 1 == 2 #=> false |
| 1326 |
* 1 == 1.0 #=> true |
| 1327 |
*/ |
| 1328 |
|
| 1329 |
static mrb_value |
| 1330 |
int_equal(mrb_state *mrb, mrb_value x) |
| 1331 |
{ |
| 1332 |
mrb_value y = mrb_get_arg1(mrb); |
| 1333 |
|
| 1334 |
switch (mrb_type(y)) { |
| 1335 |
case MRB_TT_INTEGER: |
| 1336 |
return mrb_bool_value(mrb_integer(x) == mrb_integer(y)); |
| 1337 |
#ifndef MRB_NO_FLOAT |
| 1338 |
case MRB_TT_FLOAT: |
| 1339 |
return mrb_bool_value((mrb_float)mrb_integer(x) == mrb_float(y)); |
| 1340 |
#endif |
| 1341 |
#ifdef MRB_USE_BIGINT |
| 1342 |
case MRB_TT_BIGINT: |
| 1343 |
return mrb_bool_value(mrb_bint_cmp(mrb, y, x) == 0); |
| 1344 |
#endif |
| 1345 |
#ifdef MRB_USE_RATIONAL |
| 1346 |
case MRB_TT_RATIONAL: |
| 1347 |
return mrb_bool_value(mrb_equal(mrb, y, x)); |
| 1348 |
#endif |
| 1349 |
#ifdef MRB_USE_COMPLEX |
| 1350 |
case MRB_TT_COMPLEX: |
| 1351 |
return mrb_bool_value(mrb_equal(mrb, y, x)); |
| 1352 |
#endif |
| 1353 |
default: |
| 1354 |
return mrb_false_value(); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* 15.2.8.3.8 */ |
| 1359 |
/* |
| 1360 |
* call-seq: |
| 1361 |
* ~int -> integer |
| 1362 |
* |
| 1363 |
* One's complement: returns a number where each bit is flipped. |
| 1364 |
* ex.0---00001 (1)-> 1---11110 (-2) |
| 1365 |
* ex.0---00010 (2)-> 1---11101 (-3) |
| 1366 |
* ex.0---00100 (4)-> 1---11011 (-5) |
| 1367 |
*/ |
| 1368 |
|
| 1369 |
static mrb_value |
| 1370 |
int_rev(mrb_state *mrb, mrb_value num) |
| 1371 |
{ |
| 1372 |
#ifdef MRB_USE_BIGINT |
| 1373 |
if (mrb_bigint_p(num)) { |
| 1374 |
return mrb_bint_rev(mrb, num); |
| 1375 |
} |
| 1376 |
#endif |
| 1377 |
mrb_int val = mrb_integer(num); |
| 1378 |
return mrb_int_value(mrb, ~val); |
| 1379 |
} |
| 1380 |
|
| 1381 |
#ifdef MRB_NO_FLOAT |
| 1382 |
#define bit_op(x,y,op1,op2) do {\ |
| 1383 |
return mrb_int_value(mrb, (mrb_integer(x) op2 mrb_integer(y)));\ |
| 1384 |
} while(0) |
| 1385 |
#else |
| 1386 |
static mrb_value flo_and(mrb_state *mrb, mrb_value x); |
| 1387 |
static mrb_value flo_or(mrb_state *mrb, mrb_value x); |
| 1388 |
static mrb_value flo_xor(mrb_state *mrb, mrb_value x); |
| 1389 |
#define bit_op(x,y,op1,op2) do {\ |
| 1390 |
if (mrb_integer_p(y)) return mrb_int_value(mrb, (mrb_integer(x) op2 mrb_integer(y))); \ |
| 1391 |
return flo_ ## op1(mrb, mrb_float_value(mrb, (mrb_float)mrb_integer(x)));\ |
| 1392 |
} while(0) |
| 1393 |
#endif |
| 1394 |
|
| 1395 |
/* 15.2.8.3.9 */ |
| 1396 |
/* |
| 1397 |
* call-seq: |
| 1398 |
* int & integer -> integer_result |
| 1399 |
* |
| 1400 |
* Bitwise AND. |
| 1401 |
*/ |
| 1402 |
|
| 1403 |
static mrb_value |
| 1404 |
int_and(mrb_state *mrb, mrb_value x) |
| 1405 |
{ |
| 1406 |
mrb_value y = mrb_get_arg1(mrb); |
| 1407 |
|
| 1408 |
#ifdef MRB_USE_BIGINT |
| 1409 |
if (mrb_bigint_p(x)) { |
| 1410 |
return mrb_bint_and(mrb, x, y); |
| 1411 |
} |
| 1412 |
if (mrb_bigint_p(y)) { |
| 1413 |
return mrb_bint_and(mrb, mrb_as_bint(mrb, x), y); |
| 1414 |
} |
| 1415 |
#endif |
| 1416 |
bit_op(x, y, and, &); |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* 15.2.8.3.10 */ |
| 1420 |
/* |
| 1421 |
* call-seq: |
| 1422 |
* int | integer -> integer_result |
| 1423 |
* |
| 1424 |
* Bitwise OR. |
| 1425 |
*/ |
| 1426 |
|
| 1427 |
static mrb_value |
| 1428 |
int_or(mrb_state *mrb, mrb_value x) |
| 1429 |
{ |
| 1430 |
mrb_value y = mrb_get_arg1(mrb); |
| 1431 |
|
| 1432 |
#ifdef MRB_USE_BIGINT |
| 1433 |
if (mrb_bigint_p(x)) { |
| 1434 |
return mrb_bint_or(mrb, x, y); |
| 1435 |
} |
| 1436 |
if (mrb_bigint_p(y)) { |
| 1437 |
return mrb_bint_or(mrb, mrb_as_bint(mrb, x), y); |
| 1438 |
} |
| 1439 |
#endif |
| 1440 |
bit_op(x, y, or, |); |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* 15.2.8.3.11 */ |
| 1444 |
/* |
| 1445 |
* call-seq: |
| 1446 |
* int ^ integer -> integer_result |
| 1447 |
* |
| 1448 |
* Bitwise EXCLUSIVE OR. |
| 1449 |
*/ |
| 1450 |
|
| 1451 |
static mrb_value |
| 1452 |
int_xor(mrb_state *mrb, mrb_value x) |
| 1453 |
{ |
| 1454 |
mrb_value y = mrb_get_arg1(mrb); |
| 1455 |
|
| 1456 |
#ifdef MRB_USE_BIGINT |
| 1457 |
if (mrb_bigint_p(x)) { |
| 1458 |
return mrb_bint_xor(mrb, x, y); |
| 1459 |
} |
| 1460 |
if (mrb_bigint_p(y)) { |
| 1461 |
return mrb_bint_xor(mrb, mrb_as_bint(mrb, x), y); |
| 1462 |
} |
| 1463 |
#endif |
| 1464 |
bit_op(x, y, or, ^); |
| 1465 |
} |
| 1466 |
|
| 1467 |
#define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1) |
| 1468 |
|
| 1469 |
mrb_bool |
| 1470 |
mrb_num_shift(mrb_state *mrb, mrb_int val, mrb_int width, mrb_int *num) |
| 1471 |
{ |
| 1472 |
if (width < 0) { /* rshift */ |
| 1473 |
if (width == MRB_INT_MIN || -width >= NUMERIC_SHIFT_WIDTH_MAX) { |
| 1474 |
if (val < 0) { |
| 1475 |
*num = -1; |
| 1476 |
} |
| 1477 |
else { |
| 1478 |
*num = 0; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
else { |
| 1482 |
*num = val >> -width; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
else if (val > 0) { |
| 1486 |
if ((width > NUMERIC_SHIFT_WIDTH_MAX) || |
| 1487 |
(val > (MRB_INT_MAX >> width))) { |
| 1488 |
return FALSE; |
| 1489 |
} |
| 1490 |
*num = val << width; |
| 1491 |
} |
| 1492 |
else { |
| 1493 |
if ((width > NUMERIC_SHIFT_WIDTH_MAX) || |
| 1494 |
(val < (MRB_INT_MIN >> width))) { |
| 1495 |
return FALSE; |
| 1496 |
} |
| 1497 |
if (width == NUMERIC_SHIFT_WIDTH_MAX) |
| 1498 |
*num = MRB_INT_MIN; |
| 1499 |
else |
| 1500 |
*num = val * ((mrb_int)1 << width); |
| 1501 |
} |
| 1502 |
return TRUE; |
| 1503 |
} |
| 1504 |
|
| 1505 |
/* 15.2.8.3.12 */ |
| 1506 |
/* |
| 1507 |
* call-seq: |
| 1508 |
* int << count -> integer or float |
| 1509 |
* |
| 1510 |
* Shifts _int_ left _count_ positions (right if _count_ is negative). |
| 1511 |
*/ |
| 1512 |
|
| 1513 |
static mrb_value |
| 1514 |
int_lshift(mrb_state *mrb, mrb_value x) |
| 1515 |
{ |
| 1516 |
mrb_int width, val; |
| 1517 |
|
| 1518 |
width = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 1519 |
if (width == 0) { |
| 1520 |
return x; |
| 1521 |
} |
| 1522 |
if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift"); |
| 1523 |
#ifdef MRB_USE_BIGINT |
| 1524 |
if (mrb_bigint_p(x)) { |
| 1525 |
return mrb_bint_lshift(mrb, x, width); |
| 1526 |
} |
| 1527 |
#endif |
| 1528 |
val = mrb_integer(x); |
| 1529 |
if (val == 0) return x; |
| 1530 |
if (!mrb_num_shift(mrb, val, width, &val)) { |
| 1531 |
#ifdef MRB_USE_BIGINT |
| 1532 |
return mrb_bint_lshift(mrb, mrb_bint_new_int(mrb, val), width); |
| 1533 |
#else |
| 1534 |
mrb_int_overflow(mrb, "bit shift"); |
| 1535 |
#endif |
| 1536 |
} |
| 1537 |
return mrb_int_value(mrb, val); |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* 15.2.8.3.13 */ |
| 1541 |
/* |
| 1542 |
* call-seq: |
| 1543 |
* int >> count -> integer or float |
| 1544 |
* |
| 1545 |
* Shifts _int_ right _count_ positions (left if _count_ is negative). |
| 1546 |
*/ |
| 1547 |
|
| 1548 |
static mrb_value |
| 1549 |
int_rshift(mrb_state *mrb, mrb_value x) |
| 1550 |
{ |
| 1551 |
mrb_int width, val; |
| 1552 |
|
| 1553 |
width = mrb_as_int(mrb, mrb_get_arg1(mrb)); |
| 1554 |
if (width == 0) { |
| 1555 |
return x; |
| 1556 |
} |
| 1557 |
if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift"); |
| 1558 |
#ifdef MRB_USE_BIGINT |
| 1559 |
if (mrb_bigint_p(x)) { |
| 1560 |
return mrb_bint_rshift(mrb, x, width); |
| 1561 |
} |
| 1562 |
#endif |
| 1563 |
val = mrb_integer(x); |
| 1564 |
if (val == 0) return x; |
| 1565 |
if (!mrb_num_shift(mrb, val, -width, &val)) { |
| 1566 |
#ifdef MRB_USE_BIGINT |
| 1567 |
return mrb_bint_rshift(mrb, mrb_bint_new_int(mrb, val), width); |
| 1568 |
#else |
| 1569 |
mrb_int_overflow(mrb, "bit shift"); |
| 1570 |
#endif |
| 1571 |
} |
| 1572 |
return mrb_int_value(mrb, val); |
| 1573 |
} |
| 1574 |
|
| 1575 |
static mrb_value |
| 1576 |
prepare_int_rounding(mrb_state *mrb, mrb_value x) |
| 1577 |
{ |
| 1578 |
mrb_int nd = 0; |
| 1579 |
size_t bytes; |
| 1580 |
|
| 1581 |
mrb_get_args(mrb, "|i", &nd); |
| 1582 |
if (nd >= 0) { |
| 1583 |
return mrb_nil_value(); |
| 1584 |
} |
| 1585 |
#ifdef MRB_USE_BIGINT |
| 1586 |
if (mrb_bigint_p(x)) { |
| 1587 |
bytes = mrb_bint_memsize(x); |
| 1588 |
} |
| 1589 |
else |
| 1590 |
#endif |
| 1591 |
bytes = sizeof(mrb_int); |
| 1592 |
if (-0.415241 * nd - 0.125 > bytes) { |
| 1593 |
return mrb_undef_value(); |
| 1594 |
} |
| 1595 |
return mrb_int_pow(mrb, mrb_fixnum_value(10), mrb_fixnum_value(-nd)); |
| 1596 |
} |
| 1597 |
|
| 1598 |
/* 15.2.8.3.14 Integer#ceil */ |
| 1599 |
/* |
| 1600 |
* call-seq: |
| 1601 |
* int.ceil -> int |
| 1602 |
* int.ceil(ndigits) -> int |
| 1603 |
* |
| 1604 |
* Returns self. |
| 1605 |
* |
| 1606 |
* When the precision (ndigits) is negative, the returned value is an integer |
| 1607 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 1608 |
*/ |
| 1609 |
static mrb_value |
| 1610 |
int_ceil(mrb_state *mrb, mrb_value x) |
| 1611 |
{ |
| 1612 |
mrb_value f = prepare_int_rounding(mrb, x); |
| 1613 |
if (mrb_undef_p(f)) return mrb_fixnum_value(0); |
| 1614 |
if (mrb_nil_p(f)) return x; |
| 1615 |
#ifdef MRB_USE_BIGINT |
| 1616 |
if (mrb_bigint_p(x)) { |
| 1617 |
x = mrb_bint_add_d(mrb, x, f); |
| 1618 |
return mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f)); |
| 1619 |
} |
| 1620 |
#endif |
| 1621 |
mrb_int a = mrb_integer(x); |
| 1622 |
mrb_int b = mrb_integer(f); |
| 1623 |
mrb_int c = a % b; |
| 1624 |
int neg = a < 0; |
| 1625 |
a -= c; |
| 1626 |
if (!neg) { |
| 1627 |
if (mrb_int_add_overflow(a, b, &c)) { |
| 1628 |
#ifdef MRB_USE_BIGINT |
| 1629 |
x = mrb_bint_new_int(mrb, a); |
| 1630 |
return mrb_bint_add(mrb, x, f); |
| 1631 |
#else |
| 1632 |
mrb_int_overflow(mrb, "ceil"); |
| 1633 |
#endif |
| 1634 |
} |
| 1635 |
a = c; |
| 1636 |
} |
| 1637 |
return mrb_int_value(mrb, a); |
| 1638 |
} |
| 1639 |
|
| 1640 |
/* 15.2.8.3.17 Integer#floor */ |
| 1641 |
/* |
| 1642 |
* call-seq: |
| 1643 |
* int.floor -> int |
| 1644 |
* int.floor(ndigits) -> int |
| 1645 |
* |
| 1646 |
* Returns self. |
| 1647 |
* |
| 1648 |
* When the precision (ndigits) is negative, the returned value is an integer |
| 1649 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 1650 |
*/ |
| 1651 |
static mrb_value |
| 1652 |
int_floor(mrb_state *mrb, mrb_value x) |
| 1653 |
{ |
| 1654 |
mrb_value f = prepare_int_rounding(mrb, x); |
| 1655 |
if (mrb_undef_p(f)) return mrb_fixnum_value(0); |
| 1656 |
if (mrb_nil_p(f)) return x; |
| 1657 |
#ifdef MRB_USE_BIGINT |
| 1658 |
if (mrb_bigint_p(x)) { |
| 1659 |
return mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f)); |
| 1660 |
} |
| 1661 |
#endif |
| 1662 |
mrb_int a = mrb_integer(x); |
| 1663 |
mrb_int b = mrb_integer(f); |
| 1664 |
mrb_int c = a % b; |
| 1665 |
int neg = a < 0; |
| 1666 |
a -= c; |
| 1667 |
if (neg) { |
| 1668 |
if (mrb_int_sub_overflow(a, b, &c)) { |
| 1669 |
#ifdef MRB_USE_BIGINT |
| 1670 |
x = mrb_bint_new_int(mrb, a); |
| 1671 |
return mrb_bint_sub(mrb, x, f); |
| 1672 |
#else |
| 1673 |
mrb_int_overflow(mrb, "floor"); |
| 1674 |
#endif |
| 1675 |
} |
| 1676 |
a = c; |
| 1677 |
} |
| 1678 |
return mrb_int_value(mrb, a); |
| 1679 |
} |
| 1680 |
|
| 1681 |
/* 15.2.8.3.20 Integer#round */ |
| 1682 |
/* |
| 1683 |
* call-seq: |
| 1684 |
* int.round -> int |
| 1685 |
* int.round(ndigits) -> int |
| 1686 |
* |
| 1687 |
* Returns self. |
| 1688 |
* |
| 1689 |
* When the precision (ndigits) is negative, the returned value is an integer |
| 1690 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 1691 |
*/ |
| 1692 |
static mrb_value |
| 1693 |
int_round(mrb_state *mrb, mrb_value x) |
| 1694 |
{ |
| 1695 |
mrb_value f = prepare_int_rounding(mrb, x); |
| 1696 |
if (mrb_undef_p(f)) return mrb_fixnum_value(0); |
| 1697 |
if (mrb_nil_p(f)) return x; |
| 1698 |
#ifdef MRB_USE_BIGINT |
| 1699 |
if (mrb_bigint_p(x)) { |
| 1700 |
mrb_value r = mrb_bint_mod(mrb, x, f); |
| 1701 |
mrb_value n = mrb_bint_sub(mrb, x, r); |
| 1702 |
mrb_value h = mrb_bigint_p(f) ? mrb_bint_rshift(mrb, f, 1) : mrb_int_value(mrb, mrb_integer(f)>>1); |
| 1703 |
mrb_int cmp = mrb_bigint_p(r) ? mrb_bint_cmp(mrb, r, h) : (mrb_bigint_p(h) ? -mrb_bint_cmp(mrb, h, r) : (mrb_integer(r)-mrb_integer(h))); |
| 1704 |
if ((cmp > 0) || (cmp == 0 && mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) > 0)) { |
| 1705 |
n = mrb_as_bint(mrb, n); |
| 1706 |
n = mrb_bint_add(mrb, n, f); |
| 1707 |
} |
| 1708 |
return n; |
| 1709 |
} |
| 1710 |
#endif |
| 1711 |
mrb_int a = mrb_integer(x); |
| 1712 |
mrb_int b = mrb_integer(f); |
| 1713 |
mrb_int c = a % b; |
| 1714 |
a -= c; |
| 1715 |
if (c < 0) { |
| 1716 |
c = -c; |
| 1717 |
if (b/2 < c) { |
| 1718 |
if (mrb_int_sub_overflow(a, b, &c)) { |
| 1719 |
#ifdef MRB_USE_BIGINT |
| 1720 |
x = mrb_bint_new_int(mrb, a); |
| 1721 |
return mrb_bint_sub(mrb, x, f); |
| 1722 |
#else |
| 1723 |
mrb_int_overflow(mrb, "round"); |
| 1724 |
#endif |
| 1725 |
} |
| 1726 |
} |
| 1727 |
a = c; |
| 1728 |
} |
| 1729 |
else { |
| 1730 |
if (b/2 < c) { |
| 1731 |
if (mrb_int_add_overflow(a, b, &c)) { |
| 1732 |
#ifdef MRB_USE_BIGINT |
| 1733 |
x = mrb_bint_new_int(mrb, a); |
| 1734 |
return mrb_bint_add(mrb, x, f); |
| 1735 |
#else |
| 1736 |
mrb_int_overflow(mrb, "round"); |
| 1737 |
#endif |
| 1738 |
} |
| 1739 |
} |
| 1740 |
a = c; |
| 1741 |
} |
| 1742 |
return mrb_int_value(mrb, a); |
| 1743 |
} |
| 1744 |
|
| 1745 |
/* 15.2.8.3.26 Integer#truncate */ |
| 1746 |
/* |
| 1747 |
* call-seq: |
| 1748 |
* int.truncate -> int |
| 1749 |
* int.truncate(ndigits) -> int |
| 1750 |
* |
| 1751 |
* Returns self. |
| 1752 |
* |
| 1753 |
* When the precision (ndigits) is negative, the returned value is an integer |
| 1754 |
* with at least <code>ndigits.abs</code> trailing zeros. |
| 1755 |
*/ |
| 1756 |
static mrb_value |
| 1757 |
int_truncate(mrb_state *mrb, mrb_value x) |
| 1758 |
{ |
| 1759 |
mrb_value f = prepare_int_rounding(mrb, x); |
| 1760 |
if (mrb_undef_p(f)) return mrb_fixnum_value(0); |
| 1761 |
if (mrb_nil_p(f)) return x; |
| 1762 |
#ifdef MRB_USE_BIGINT |
| 1763 |
if (mrb_bigint_p(x)) { |
| 1764 |
mrb_value m = mrb_bint_mod(mrb, x, f); |
| 1765 |
x = mrb_bint_sub_d(mrb, x, m); |
| 1766 |
if (mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) < 0) { |
| 1767 |
return mrb_bint_add(mrb, x, f); |
| 1768 |
} |
| 1769 |
return x; |
| 1770 |
} |
| 1771 |
#endif |
| 1772 |
mrb_int a = mrb_integer(x); |
| 1773 |
mrb_int b = mrb_integer(f); |
| 1774 |
return mrb_int_value(mrb, a - (a % b)); |
| 1775 |
} |
| 1776 |
|
| 1777 |
/* 15.2.8.3.23 */ |
| 1778 |
/* |
| 1779 |
* call-seq: |
| 1780 |
* int.to_f -> float |
| 1781 |
* |
| 1782 |
* Converts <i>int</i> to a <code>Float</code>. |
| 1783 |
* |
| 1784 |
*/ |
| 1785 |
|
| 1786 |
#ifndef MRB_NO_FLOAT |
| 1787 |
static mrb_value |
| 1788 |
int_to_f(mrb_state *mrb, mrb_value num) |
| 1789 |
{ |
| 1790 |
#ifdef MRB_USE_BIGINT |
| 1791 |
if (mrb_bigint_p(num)) { |
| 1792 |
return mrb_float_value(mrb, mrb_bint_as_float(mrb, num)); |
| 1793 |
} |
| 1794 |
#endif |
| 1795 |
return mrb_float_value(mrb, (mrb_float)mrb_integer(num)); |
| 1796 |
} |
| 1797 |
|
| 1798 |
MRB_API mrb_value |
| 1799 |
mrb_float_to_integer(mrb_state *mrb, mrb_value x) |
| 1800 |
{ |
| 1801 |
if (!mrb_float_p(x)) { |
| 1802 |
mrb_raise(mrb, E_TYPE_ERROR, "non float value"); |
| 1803 |
} |
| 1804 |
mrb_float f = mrb_float(x); |
| 1805 |
if (isinf(f) || isnan(f)) { |
| 1806 |
mrb_raisef(mrb, E_RANGE_ERROR, "float %f out of range", f); |
| 1807 |
} |
| 1808 |
return flo_to_i(mrb, x); |
| 1809 |
} |
| 1810 |
#endif |
| 1811 |
|
| 1812 |
mrb_value |
| 1813 |
mrb_int_add(mrb_state *mrb, mrb_value x, mrb_value y) |
| 1814 |
{ |
| 1815 |
mrb_int a; |
| 1816 |
|
| 1817 |
a = mrb_integer(x); |
| 1818 |
if (mrb_integer_p(y)) { |
| 1819 |
mrb_int b, c; |
| 1820 |
|
| 1821 |
if (a == 0) return y; |
| 1822 |
b = mrb_integer(y); |
| 1823 |
if (b == 0) return x; |
| 1824 |
if (mrb_int_add_overflow(a, b, &c)) { |
| 1825 |
#ifdef MRB_USE_BIGINT |
| 1826 |
x = mrb_bint_new_int(mrb, a); |
| 1827 |
return mrb_bint_add(mrb, x, y); |
| 1828 |
#else |
| 1829 |
mrb_int_overflow(mrb, "addition"); |
| 1830 |
#endif |
| 1831 |
} |
| 1832 |
return mrb_int_value(mrb, c); |
| 1833 |
} |
| 1834 |
switch (mrb_type(y)) { |
| 1835 |
#ifdef MRB_USE_BIGINT |
| 1836 |
case MRB_TT_BIGINT: |
| 1837 |
return mrb_bint_add(mrb, y, x); |
| 1838 |
#endif |
| 1839 |
#ifdef MRB_USE_RATIONAL |
| 1840 |
case MRB_TT_RATIONAL: |
| 1841 |
return mrb_rational_add(mrb, y, x); |
| 1842 |
#endif |
| 1843 |
#ifdef MRB_USE_COMPLEX |
| 1844 |
case MRB_TT_COMPLEX: |
| 1845 |
return mrb_complex_add(mrb, y, x); |
| 1846 |
#endif |
| 1847 |
default: |
| 1848 |
#ifdef MRB_NO_FLOAT |
| 1849 |
mrb_raise(mrb, E_TYPE_ERROR, "non integer addition"); |
| 1850 |
#else |
| 1851 |
return mrb_float_value(mrb, (mrb_float)a + mrb_as_float(mrb, y)); |
| 1852 |
#endif |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
/* 15.2.8.3.3 */ |
| 1857 |
/* |
| 1858 |
* call-seq: |
| 1859 |
* int + numeric -> numeric_result |
| 1860 |
* |
| 1861 |
* Performs addition: the class of the resulting object depends on |
| 1862 |
* the class of <code>numeric</code> and on the magnitude of the |
| 1863 |
* result. |
| 1864 |
*/ |
| 1865 |
static mrb_value |
| 1866 |
int_add(mrb_state *mrb, mrb_value self) |
| 1867 |
{ |
| 1868 |
mrb_value other = mrb_get_arg1(mrb); |
| 1869 |
|
| 1870 |
#ifdef MRB_USE_BIGINT |
| 1871 |
if (mrb_bigint_p(self)) { |
| 1872 |
return mrb_bint_add(mrb, self, other); |
| 1873 |
} |
| 1874 |
#endif |
| 1875 |
return mrb_int_add(mrb, self, other); |
| 1876 |
} |
| 1877 |
|
| 1878 |
mrb_value |
| 1879 |
mrb_int_sub(mrb_state *mrb, mrb_value x, mrb_value y) |
| 1880 |
{ |
| 1881 |
mrb_int a; |
| 1882 |
|
| 1883 |
a = mrb_integer(x); |
| 1884 |
if (mrb_integer_p(y)) { |
| 1885 |
mrb_int b, c; |
| 1886 |
|
| 1887 |
b = mrb_integer(y); |
| 1888 |
if (mrb_int_sub_overflow(a, b, &c)) { |
| 1889 |
#ifdef MRB_USE_BIGINT |
| 1890 |
x = mrb_bint_new_int(mrb, a); |
| 1891 |
return mrb_bint_sub(mrb, x, y); |
| 1892 |
#else |
| 1893 |
mrb_int_overflow(mrb, "subtraction"); |
| 1894 |
#endif |
| 1895 |
} |
| 1896 |
return mrb_int_value(mrb, c); |
| 1897 |
} |
| 1898 |
switch (mrb_type(y)) { |
| 1899 |
#ifdef MRB_USE_BIGINT |
| 1900 |
case MRB_TT_BIGINT: |
| 1901 |
return mrb_bint_sub(mrb, mrb_bint_new_int(mrb, a), y); |
| 1902 |
#endif |
| 1903 |
#ifdef MRB_USE_RATIONAL |
| 1904 |
case MRB_TT_RATIONAL: |
| 1905 |
return mrb_rational_sub(mrb, mrb_rational_new(mrb, a, 1), y); |
| 1906 |
#endif |
| 1907 |
#ifdef MRB_USE_COMPLEX |
| 1908 |
case MRB_TT_COMPLEX: |
| 1909 |
return mrb_complex_sub(mrb, mrb_complex_new(mrb, (mrb_float)a, 0), y); |
| 1910 |
#endif |
| 1911 |
default: |
| 1912 |
#ifdef MRB_NO_FLOAT |
| 1913 |
mrb_raise(mrb, E_TYPE_ERROR, "non integer subtraction"); |
| 1914 |
#else |
| 1915 |
return mrb_float_value(mrb, (mrb_float)a - mrb_as_float(mrb, y)); |
| 1916 |
#endif |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
/* 15.2.8.3.4 */ |
| 1921 |
/* |
| 1922 |
* call-seq: |
| 1923 |
* int - numeric -> numeric |
| 1924 |
* |
| 1925 |
* Performs subtraction: the class of the resulting object depends on |
| 1926 |
* the class of <code>numeric</code> and on the magnitude of the |
| 1927 |
* result. |
| 1928 |
*/ |
| 1929 |
static mrb_value |
| 1930 |
int_sub(mrb_state *mrb, mrb_value self) |
| 1931 |
{ |
| 1932 |
mrb_value other = mrb_get_arg1(mrb); |
| 1933 |
|
| 1934 |
#ifdef MRB_USE_BIGINT |
| 1935 |
if (mrb_bigint_p(self)) { |
| 1936 |
return mrb_bint_sub(mrb, self, other); |
| 1937 |
} |
| 1938 |
#endif |
| 1939 |
return mrb_int_sub(mrb, self, other); |
| 1940 |
} |
| 1941 |
|
| 1942 |
MRB_API char* |
| 1943 |
mrb_int_to_cstr(char *buf, size_t len, mrb_int n, mrb_int base) |
| 1944 |
{ |
| 1945 |
char *bufend = buf + len; |
| 1946 |
char *b = bufend-1; |
| 1947 |
|
| 1948 |
if (base < 2 || 36 < base) return NULL; |
| 1949 |
if (len < 2) return NULL; |
| 1950 |
|
| 1951 |
if (n == 0) { |
| 1952 |
buf[0] = '0'; |
| 1953 |
buf[1] = '\0'; |
| 1954 |
return buf; |
| 1955 |
} |
| 1956 |
|
| 1957 |
*b = '\0'; |
| 1958 |
if (n < 0) { |
| 1959 |
do { |
| 1960 |
if (b-- == buf) return NULL; |
| 1961 |
*b = mrb_digitmap[-(n % base)]; |
| 1962 |
} while (n /= base); |
| 1963 |
if (b-- == buf) return NULL; |
| 1964 |
*b = '-'; |
| 1965 |
} |
| 1966 |
else { |
| 1967 |
do { |
| 1968 |
if (b-- == buf) return NULL; |
| 1969 |
*b = mrb_digitmap[(int)(n % base)]; |
| 1970 |
} while (n /= base); |
| 1971 |
} |
| 1972 |
return b; |
| 1973 |
} |
| 1974 |
|
| 1975 |
MRB_API mrb_value |
| 1976 |
mrb_integer_to_str(mrb_state *mrb, mrb_value x, mrb_int base) |
| 1977 |
{ |
| 1978 |
char buf[MRB_INT_BIT+1]; |
| 1979 |
mrb_int val = mrb_integer(x); |
| 1980 |
|
| 1981 |
if (base < 2 || 36 < base) { |
| 1982 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %i", base); |
| 1983 |
} |
| 1984 |
#ifdef MRB_USE_BIGINT |
| 1985 |
if (mrb_bigint_p(x)) { |
| 1986 |
return mrb_bint_to_s(mrb, x, base); |
| 1987 |
} |
| 1988 |
#endif |
| 1989 |
const char *p = mrb_int_to_cstr(buf, sizeof(buf), val, base); |
| 1990 |
mrb_assert(p != NULL); |
| 1991 |
mrb_value str = mrb_str_new_cstr(mrb, p); |
| 1992 |
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str)); |
| 1993 |
return str; |
| 1994 |
} |
| 1995 |
|
| 1996 |
/* 15.2.8.3.25 */ |
| 1997 |
/* |
| 1998 |
* call-seq: |
| 1999 |
* int.to_s(base=10) -> string |
| 2000 |
* |
| 2001 |
* Returns a string containing the representation of <i>int</i> radix |
| 2002 |
* <i>base</i> (between 2 and 36). |
| 2003 |
* |
| 2004 |
* 12345.to_s #=> "12345" |
| 2005 |
* 12345.to_s(2) #=> "11000000111001" |
| 2006 |
* 12345.to_s(8) #=> "30071" |
| 2007 |
* 12345.to_s(10) #=> "12345" |
| 2008 |
* 12345.to_s(16) #=> "3039" |
| 2009 |
* 12345.to_s(36) #=> "9ix" |
| 2010 |
* |
| 2011 |
*/ |
| 2012 |
static mrb_value |
| 2013 |
int_to_s(mrb_state *mrb, mrb_value self) |
| 2014 |
{ |
| 2015 |
mrb_int base = 10; |
| 2016 |
|
| 2017 |
mrb_get_args(mrb, "|i", &base); |
| 2018 |
return mrb_integer_to_str(mrb, self, base); |
| 2019 |
} |
| 2020 |
|
| 2021 |
/* compare two numbers: (1:0:-1; -2 for error) */ |
| 2022 |
static mrb_int |
| 2023 |
cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2) |
| 2024 |
{ |
| 2025 |
#ifdef MRB_USE_BIGINT |
| 2026 |
if (mrb_bigint_p(v1)) { |
| 2027 |
return mrb_bint_cmp(mrb, v1, v2); |
| 2028 |
} |
| 2029 |
#endif |
| 2030 |
|
| 2031 |
#ifdef MRB_NO_FLOAT |
| 2032 |
mrb_int x, y; |
| 2033 |
#else |
| 2034 |
mrb_float x, y; |
| 2035 |
#endif |
| 2036 |
|
| 2037 |
#ifdef MRB_NO_FLOAT |
| 2038 |
x = mrb_integer(v1); |
| 2039 |
#else |
| 2040 |
x = mrb_as_float(mrb, v1); |
| 2041 |
#endif |
| 2042 |
switch (mrb_type(v2)) { |
| 2043 |
case MRB_TT_INTEGER: |
| 2044 |
#ifdef MRB_NO_FLOAT |
| 2045 |
y = mrb_integer(v2); |
| 2046 |
#else |
| 2047 |
y = (mrb_float)mrb_integer(v2); |
| 2048 |
#endif |
| 2049 |
break; |
| 2050 |
#ifndef MRB_NO_FLOAT |
| 2051 |
case MRB_TT_FLOAT: |
| 2052 |
y = mrb_float(v2); |
| 2053 |
break; |
| 2054 |
#ifdef MRB_USE_RATIONAL |
| 2055 |
case MRB_TT_RATIONAL: |
| 2056 |
y = mrb_as_float(mrb, v2); |
| 2057 |
break; |
| 2058 |
#endif |
| 2059 |
#endif |
| 2060 |
default: |
| 2061 |
return -2; |
| 2062 |
} |
| 2063 |
if (x > y) |
| 2064 |
return 1; |
| 2065 |
else { |
| 2066 |
if (x < y) |
| 2067 |
return -1; |
| 2068 |
return 0; |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
static mrb_value |
| 2073 |
int_hash(mrb_state *mrb, mrb_value self) |
| 2074 |
{ |
| 2075 |
#ifdef MRB_USE_BIGINT |
| 2076 |
if (mrb_bigint_p(self)) { |
| 2077 |
return mrb_bint_hash(mrb, self); |
| 2078 |
} |
| 2079 |
#endif |
| 2080 |
mrb_int n = mrb_integer(self); |
| 2081 |
return mrb_int_value(mrb, mrb_byte_hash((uint8_t*)&n, sizeof(n))); |
| 2082 |
} |
| 2083 |
|
| 2084 |
/* 15.2.8.3.1 */ |
| 2085 |
/* 15.2.9.3.1 */ |
| 2086 |
/* |
| 2087 |
* call-seq: |
| 2088 |
* self.f <=> other.f => -1, 0, +1, or nil |
| 2089 |
* < => -1 |
| 2090 |
* = => 0 |
| 2091 |
* > => +1 |
| 2092 |
* Comparison---Returns -1, 0, or +1 depending on whether <i>int</i> is |
| 2093 |
* less than, equal to, or greater than <i>numeric</i>. This is the |
| 2094 |
* basis for the tests in <code>Comparable</code>. When the operands are |
| 2095 |
* not comparable, it returns nil instead of raising an exception. |
| 2096 |
*/ |
| 2097 |
static mrb_value |
| 2098 |
num_cmp(mrb_state *mrb, mrb_value self) |
| 2099 |
{ |
| 2100 |
mrb_value other = mrb_get_arg1(mrb); |
| 2101 |
mrb_int n; |
| 2102 |
|
| 2103 |
n = cmpnum(mrb, self, other); |
| 2104 |
if (n == -2) return mrb_nil_value(); |
| 2105 |
return mrb_fixnum_value(n); |
| 2106 |
} |
| 2107 |
|
| 2108 |
static mrb_noreturn void |
| 2109 |
cmperr(mrb_state *mrb, mrb_value v1, mrb_value v2) |
| 2110 |
{ |
| 2111 |
mrb_raisef(mrb, E_ARGUMENT_ERROR, "comparison of %t with %t failed", v1, v2); |
| 2112 |
} |
| 2113 |
|
| 2114 |
static mrb_value |
| 2115 |
num_lt(mrb_state *mrb, mrb_value self) |
| 2116 |
{ |
| 2117 |
mrb_value other = mrb_get_arg1(mrb); |
| 2118 |
mrb_int n; |
| 2119 |
|
| 2120 |
n = cmpnum(mrb, self, other); |
| 2121 |
if (n == -2) cmperr(mrb, self, other); |
| 2122 |
if (n < 0) return mrb_true_value(); |
| 2123 |
return mrb_false_value(); |
| 2124 |
} |
| 2125 |
|
| 2126 |
static mrb_value |
| 2127 |
num_le(mrb_state *mrb, mrb_value self) |
| 2128 |
{ |
| 2129 |
mrb_value other = mrb_get_arg1(mrb); |
| 2130 |
mrb_int n; |
| 2131 |
|
| 2132 |
n = cmpnum(mrb, self, other); |
| 2133 |
if (n == -2) cmperr(mrb, self, other); |
| 2134 |
if (n <= 0) return mrb_true_value(); |
| 2135 |
return mrb_false_value(); |
| 2136 |
} |
| 2137 |
|
| 2138 |
static mrb_value |
| 2139 |
num_gt(mrb_state *mrb, mrb_value self) |
| 2140 |
{ |
| 2141 |
mrb_value other = mrb_get_arg1(mrb); |
| 2142 |
mrb_int n; |
| 2143 |
|
| 2144 |
n = cmpnum(mrb, self, other); |
| 2145 |
if (n == -2) cmperr(mrb, self, other); |
| 2146 |
if (n > 0) return mrb_true_value(); |
| 2147 |
return mrb_false_value(); |
| 2148 |
} |
| 2149 |
|
| 2150 |
static mrb_value |
| 2151 |
num_ge(mrb_state *mrb, mrb_value self) |
| 2152 |
{ |
| 2153 |
mrb_value other = mrb_get_arg1(mrb); |
| 2154 |
mrb_int n; |
| 2155 |
|
| 2156 |
n = cmpnum(mrb, self, other); |
| 2157 |
if (n == -2) cmperr(mrb, self, other); |
| 2158 |
if (n >= 0) return mrb_true_value(); |
| 2159 |
return mrb_false_value(); |
| 2160 |
} |
| 2161 |
|
| 2162 |
MRB_API mrb_int |
| 2163 |
mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2) |
| 2164 |
{ |
| 2165 |
mrb_value v; |
| 2166 |
|
| 2167 |
switch (mrb_type(obj1)) { |
| 2168 |
case MRB_TT_INTEGER: |
| 2169 |
case MRB_TT_FLOAT: |
| 2170 |
case MRB_TT_BIGINT: |
| 2171 |
return cmpnum(mrb, obj1, obj2); |
| 2172 |
case MRB_TT_STRING: |
| 2173 |
if (!mrb_string_p(obj2)) |
| 2174 |
return -2; |
| 2175 |
return mrb_str_cmp(mrb, obj1, obj2); |
| 2176 |
default: |
| 2177 |
if (!mrb_respond_to(mrb, obj1, MRB_OPSYM(cmp))) return -2; |
| 2178 |
v = mrb_funcall_argv(mrb, obj1, MRB_OPSYM(cmp), 1, &obj2); |
| 2179 |
if (mrb_nil_p(v) || !mrb_integer_p(v)) |
| 2180 |
return -2; |
| 2181 |
return mrb_integer(v); |
| 2182 |
} |
| 2183 |
} |
| 2184 |
|
| 2185 |
static mrb_value |
| 2186 |
num_finite_p(mrb_state *mrb, mrb_value self) |
| 2187 |
{ |
| 2188 |
return mrb_true_value(); |
| 2189 |
} |
| 2190 |
|
| 2191 |
static mrb_value |
| 2192 |
num_infinite_p(mrb_state *mrb, mrb_value self) |
| 2193 |
{ |
| 2194 |
return mrb_false_value(); |
| 2195 |
} |
| 2196 |
|
| 2197 |
#ifndef MRB_NO_FLOAT |
| 2198 |
static mrb_value |
| 2199 |
flo_hash(mrb_state *mrb, mrb_value flo) |
| 2200 |
{ |
| 2201 |
mrb_float f = mrb_float(flo); |
| 2202 |
/* normalize -0.0 to 0.0 */ |
| 2203 |
if (f == 0) f = 0.0; |
| 2204 |
return mrb_int_value(mrb, (mrb_int)mrb_byte_hash((uint8_t*)&f, sizeof(f))); |
| 2205 |
} |
| 2206 |
#endif |
| 2207 |
|
| 2208 |
/* ------------------------------------------------------------------------*/ |
| 2209 |
void |
| 2210 |
mrb_init_numeric(mrb_state *mrb) |
| 2211 |
{ |
| 2212 |
struct RClass *numeric, *integer; |
| 2213 |
#ifndef MRB_NO_FLOAT |
| 2214 |
struct RClass *fl; |
| 2215 |
#endif |
| 2216 |
|
| 2217 |
/* Numeric Class */ |
| 2218 |
numeric = mrb_define_class(mrb, "Numeric", mrb->object_class); /* 15.2.7 */ |
| 2219 |
mrb_define_method(mrb, numeric, "finite?", num_finite_p, MRB_ARGS_NONE()); |
| 2220 |
mrb_define_method(mrb, numeric, "infinite?",num_infinite_p, MRB_ARGS_NONE()); |
| 2221 |
mrb_define_method(mrb, numeric, "eql?", num_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */ |
| 2222 |
|
| 2223 |
/* Integer Class */ |
| 2224 |
mrb->integer_class = integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */ |
| 2225 |
MRB_SET_INSTANCE_TT(integer, MRB_TT_UNDEF); |
| 2226 |
mrb_undef_class_method(mrb, integer, "new"); |
| 2227 |
mrb_define_method(mrb, integer, "**", int_pow, MRB_ARGS_REQ(1)); |
| 2228 |
mrb_define_method(mrb, integer, "<=>", num_cmp, MRB_ARGS_REQ(1)); /* 15.2.8.3.1 */ |
| 2229 |
mrb_define_method(mrb, integer, "<", num_lt, MRB_ARGS_REQ(1)); |
| 2230 |
mrb_define_method(mrb, integer, "<=", num_le, MRB_ARGS_REQ(1)); |
| 2231 |
mrb_define_method(mrb, integer, ">", num_gt, MRB_ARGS_REQ(1)); |
| 2232 |
mrb_define_method(mrb, integer, ">=", num_ge, MRB_ARGS_REQ(1)); |
| 2233 |
|
| 2234 |
mrb_define_method(mrb, integer, "to_i", int_to_i, MRB_ARGS_NONE()); /* 15.2.8.3.24 */ |
| 2235 |
mrb_define_method(mrb, integer, "to_int", int_to_i, MRB_ARGS_NONE()); |
| 2236 |
|
| 2237 |
mrb_define_method(mrb, integer, "+", int_add, MRB_ARGS_REQ(1)); /* 15.2.8.3.1 */ |
| 2238 |
mrb_define_method(mrb, integer, "-", int_sub, MRB_ARGS_REQ(1)); /* 15.2.8.3.2 */ |
| 2239 |
mrb_define_method(mrb, integer, "*", int_mul, MRB_ARGS_REQ(1)); /* 15.2.8.3.3 */ |
| 2240 |
mrb_define_method(mrb, integer, "%", int_mod, MRB_ARGS_REQ(1)); /* 15.2.8.3.5 */ |
| 2241 |
mrb_define_method(mrb, integer, "/", int_div, MRB_ARGS_REQ(1)); /* 15.2.8.3.6 */ |
| 2242 |
mrb_define_method(mrb, integer, "quo", int_quo, MRB_ARGS_REQ(1)); /* 15.2.7.4.5(x) */ |
| 2243 |
mrb_define_method(mrb, integer, "div", int_idiv, MRB_ARGS_REQ(1)); |
| 2244 |
mrb_define_method(mrb, integer, "==", int_equal, MRB_ARGS_REQ(1)); /* 15.2.8.3.7 */ |
| 2245 |
mrb_define_method(mrb, integer, "~", int_rev, MRB_ARGS_NONE()); /* 15.2.8.3.8 */ |
| 2246 |
mrb_define_method(mrb, integer, "&", int_and, MRB_ARGS_REQ(1)); /* 15.2.8.3.9 */ |
| 2247 |
mrb_define_method(mrb, integer, "|", int_or, MRB_ARGS_REQ(1)); /* 15.2.8.3.10 */ |
| 2248 |
mrb_define_method(mrb, integer, "^", int_xor, MRB_ARGS_REQ(1)); /* 15.2.8.3.11 */ |
| 2249 |
mrb_define_method(mrb, integer, "<<", int_lshift, MRB_ARGS_REQ(1)); /* 15.2.8.3.12 */ |
| 2250 |
mrb_define_method(mrb, integer, ">>", int_rshift, MRB_ARGS_REQ(1)); /* 15.2.8.3.13 */ |
| 2251 |
mrb_define_method(mrb, integer, "ceil", int_ceil, MRB_ARGS_OPT(1)); /* 15.2.8.3.14 */ |
| 2252 |
mrb_define_method(mrb, integer, "floor", int_floor, MRB_ARGS_OPT(1)); /* 15.2.8.3.17 */ |
| 2253 |
mrb_define_method(mrb, integer, "round", int_round, MRB_ARGS_OPT(1)); /* 15.2.8.3.20 */ |
| 2254 |
mrb_define_method(mrb, integer, "truncate", int_truncate, MRB_ARGS_OPT(1)); /* 15.2.8.3.26 */ |
| 2255 |
mrb_define_method(mrb, integer, "hash", int_hash, MRB_ARGS_NONE()); /* 15.2.8.3.18 */ |
| 2256 |
#ifndef MRB_NO_FLOAT |
| 2257 |
mrb_define_method(mrb, integer, "to_f", int_to_f, MRB_ARGS_NONE()); /* 15.2.8.3.23 */ |
| 2258 |
#endif |
| 2259 |
mrb_define_method(mrb, integer, "to_s", int_to_s, MRB_ARGS_OPT(1)); /* 15.2.8.3.25 */ |
| 2260 |
mrb_define_method(mrb, integer, "inspect", int_to_s, MRB_ARGS_OPT(1)); |
| 2261 |
mrb_define_method(mrb, integer, "divmod", int_divmod, MRB_ARGS_REQ(1)); /* 15.2.8.3.30(x) */ |
| 2262 |
mrb_define_method(mrb, integer, "__coerce_step_counter", coerce_step_counter, MRB_ARGS_REQ(2)); |
| 2263 |
|
| 2264 |
/* Fixnum Class for compatibility */ |
| 2265 |
mrb_define_const(mrb, mrb->object_class, "Fixnum", mrb_obj_value(integer)); |
| 2266 |
|
| 2267 |
#ifndef MRB_NO_FLOAT |
| 2268 |
/* Float Class */ |
| 2269 |
mrb->float_class = fl = mrb_define_class(mrb, "Float", numeric); /* 15.2.9 */ |
| 2270 |
MRB_SET_INSTANCE_TT(fl, MRB_TT_UNDEF); |
| 2271 |
mrb_undef_class_method(mrb, fl, "new"); |
| 2272 |
mrb_define_method(mrb, fl, "**", flo_pow, MRB_ARGS_REQ(1)); |
| 2273 |
mrb_define_method(mrb, fl, "/", flo_div, MRB_ARGS_REQ(1)); /* 15.2.9.3.6 */ |
| 2274 |
mrb_define_method(mrb, fl, "quo", flo_div, MRB_ARGS_REQ(1)); /* 15.2.7.4.5(x) */ |
| 2275 |
mrb_define_method(mrb, fl, "div", flo_idiv, MRB_ARGS_REQ(1)); |
| 2276 |
mrb_define_method(mrb, fl, "+", flo_add, MRB_ARGS_REQ(1)); /* 15.2.9.3.3 */ |
| 2277 |
mrb_define_method(mrb, fl, "-", flo_sub, MRB_ARGS_REQ(1)); /* 15.2.9.3.4 */ |
| 2278 |
mrb_define_method(mrb, fl, "*", flo_mul, MRB_ARGS_REQ(1)); /* 15.2.9.3.5 */ |
| 2279 |
mrb_define_method(mrb, fl, "%", flo_mod, MRB_ARGS_REQ(1)); /* 15.2.9.3.7 */ |
| 2280 |
mrb_define_method(mrb, fl, "<=>", num_cmp, MRB_ARGS_REQ(1)); /* 15.2.9.3.1 */ |
| 2281 |
mrb_define_method(mrb, fl, "<", num_lt, MRB_ARGS_REQ(1)); |
| 2282 |
mrb_define_method(mrb, fl, "<=", num_le, MRB_ARGS_REQ(1)); |
| 2283 |
mrb_define_method(mrb, fl, ">", num_gt, MRB_ARGS_REQ(1)); |
| 2284 |
mrb_define_method(mrb, fl, ">=", num_ge, MRB_ARGS_REQ(1)); |
| 2285 |
mrb_define_method(mrb, fl, "==", flo_eq, MRB_ARGS_REQ(1)); /* 15.2.9.3.2 */ |
| 2286 |
mrb_define_method(mrb, fl, "~", flo_rev, MRB_ARGS_NONE()); |
| 2287 |
mrb_define_method(mrb, fl, "&", flo_and, MRB_ARGS_REQ(1)); |
| 2288 |
mrb_define_method(mrb, fl, "|", flo_or, MRB_ARGS_REQ(1)); |
| 2289 |
mrb_define_method(mrb, fl, "^", flo_xor, MRB_ARGS_REQ(1)); |
| 2290 |
mrb_define_method(mrb, fl, ">>", flo_rshift, MRB_ARGS_REQ(1)); |
| 2291 |
mrb_define_method(mrb, fl, "<<", flo_lshift, MRB_ARGS_REQ(1)); |
| 2292 |
mrb_define_method(mrb, fl, "ceil", flo_ceil, MRB_ARGS_OPT(1)); /* 15.2.9.3.8 */ |
| 2293 |
mrb_define_method(mrb, fl, "finite?", flo_finite_p, MRB_ARGS_NONE()); /* 15.2.9.3.9 */ |
| 2294 |
mrb_define_method(mrb, fl, "floor", flo_floor, MRB_ARGS_OPT(1)); /* 15.2.9.3.10 */ |
| 2295 |
mrb_define_method(mrb, fl, "infinite?", flo_infinite_p, MRB_ARGS_NONE()); /* 15.2.9.3.11 */ |
| 2296 |
mrb_define_method(mrb, fl, "round", flo_round, MRB_ARGS_OPT(1)); /* 15.2.9.3.12 */ |
| 2297 |
mrb_define_method(mrb, fl, "to_f", flo_to_f, MRB_ARGS_NONE()); /* 15.2.9.3.13 */ |
| 2298 |
mrb_define_method(mrb, fl, "to_i", flo_to_i, MRB_ARGS_NONE()); /* 15.2.9.3.14 */ |
| 2299 |
mrb_define_method(mrb, fl, "truncate", flo_truncate, MRB_ARGS_OPT(1)); /* 15.2.9.3.15 */ |
| 2300 |
mrb_define_method(mrb, fl, "divmod", flo_divmod, MRB_ARGS_REQ(1)); |
| 2301 |
|
| 2302 |
mrb_define_method(mrb, fl, "to_s", flo_to_s, MRB_ARGS_NONE()); /* 15.2.9.3.16(x) */ |
| 2303 |
mrb_define_method(mrb, fl, "inspect", flo_to_s, MRB_ARGS_NONE()); |
| 2304 |
mrb_define_method(mrb, fl, "nan?", flo_nan_p, MRB_ARGS_NONE()); |
| 2305 |
mrb_define_method(mrb, fl, "abs", flo_abs, MRB_ARGS_NONE()); /* 15.2.7.4.3 */ |
| 2306 |
mrb_define_method(mrb, fl, "hash", flo_hash, MRB_ARGS_NONE()); |
| 2307 |
|
| 2308 |
#ifdef INFINITY |
| 2309 |
mrb_define_const_id(mrb, fl, MRB_SYM(INFINITY), mrb_float_value(mrb, INFINITY)); |
| 2310 |
#endif |
| 2311 |
#ifdef NAN |
| 2312 |
mrb_define_const_id(mrb, fl, MRB_SYM(NAN), mrb_float_value(mrb, NAN)); |
| 2313 |
#endif |
| 2314 |
#endif |
| 2315 |
} |