1/********************************************************************** 2 3 numeric.c - 4 5 $Author: nagachika $ 6 created at: Fri Aug 13 18:33:09 JST 1993 7 8 Copyright (C) 1993-2007 Yukihiro Matsumoto 9 10**********************************************************************/ 11 12#include "ruby/ruby.h" 13#include "ruby/encoding.h" 14#include "ruby/util.h" 15#include "internal.h" 16#include "id.h" 17#include <ctype.h> 18#include <math.h> 19#include <stdio.h> 20 21#if defined(__FreeBSD__) && __FreeBSD__ < 4 22#include <floatingpoint.h> 23#endif 24 25#ifdef HAVE_FLOAT_H 26#include <float.h> 27#endif 28 29#ifdef HAVE_IEEEFP_H 30#include <ieeefp.h> 31#endif 32 33/* use IEEE 64bit values if not defined */ 34#ifndef FLT_RADIX 35#define FLT_RADIX 2 36#endif 37#ifndef FLT_ROUNDS 38#define FLT_ROUNDS 1 39#endif 40#ifndef DBL_MIN 41#define DBL_MIN 2.2250738585072014e-308 42#endif 43#ifndef DBL_MAX 44#define DBL_MAX 1.7976931348623157e+308 45#endif 46#ifndef DBL_MIN_EXP 47#define DBL_MIN_EXP (-1021) 48#endif 49#ifndef DBL_MAX_EXP 50#define DBL_MAX_EXP 1024 51#endif 52#ifndef DBL_MIN_10_EXP 53#define DBL_MIN_10_EXP (-307) 54#endif 55#ifndef DBL_MAX_10_EXP 56#define DBL_MAX_10_EXP 308 57#endif 58#ifndef DBL_DIG 59#define DBL_DIG 15 60#endif 61#ifndef DBL_MANT_DIG 62#define DBL_MANT_DIG 53 63#endif 64#ifndef DBL_EPSILON 65#define DBL_EPSILON 2.2204460492503131e-16 66#endif 67 68#ifdef HAVE_INFINITY 69#elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */ 70const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}}; 71#else 72const union bytesequence4_or_float rb_infinity = {{0x7f, 0x80, 0x00, 0x00}}; 73#endif 74 75#ifdef HAVE_NAN 76#elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */ 77const union bytesequence4_or_float rb_nan = {{0x00, 0x00, 0xc0, 0x7f}}; 78#else 79const union bytesequence4_or_float rb_nan = {{0x7f, 0xc0, 0x00, 0x00}}; 80#endif 81 82#ifndef HAVE_ROUND 83double 84round(double x) 85{ 86 double f; 87 88 if (x > 0.0) { 89 f = floor(x); 90 x = f + (x - f >= 0.5); 91 } 92 else if (x < 0.0) { 93 f = ceil(x); 94 x = f - (f - x >= 0.5); 95 } 96 return x; 97} 98#endif 99 100static VALUE fix_uminus(VALUE num); 101static VALUE fix_mul(VALUE x, VALUE y); 102static VALUE int_pow(long x, unsigned long y); 103 104static ID id_coerce, id_to_i, id_eq, id_div; 105 106VALUE rb_cNumeric; 107VALUE rb_cFloat; 108VALUE rb_cInteger; 109VALUE rb_cFixnum; 110 111VALUE rb_eZeroDivError; 112VALUE rb_eFloatDomainError; 113 114void 115rb_num_zerodiv(void) 116{ 117 rb_raise(rb_eZeroDivError, "divided by 0"); 118} 119 120/* experimental API */ 121int 122rb_num_to_uint(VALUE val, unsigned int *ret) 123{ 124#define NUMERR_TYPE 1 125#define NUMERR_NEGATIVE 2 126#define NUMERR_TOOLARGE 3 127 if (FIXNUM_P(val)) { 128 long v = FIX2LONG(val); 129#if SIZEOF_INT < SIZEOF_LONG 130 if (v > (long)UINT_MAX) return NUMERR_TOOLARGE; 131#endif 132 if (v < 0) return NUMERR_NEGATIVE; 133 *ret = (unsigned int)v; 134 return 0; 135 } 136 137 switch (TYPE(val)) { 138 case T_BIGNUM: 139 if (RBIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE; 140#if SIZEOF_INT < SIZEOF_LONG 141 /* long is 64bit */ 142 return NUMERR_TOOLARGE; 143#else 144 /* long is 32bit */ 145#define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS) 146 if (RBIGNUM_LEN(val) > DIGSPERLONG) return NUMERR_TOOLARGE; 147 *ret = (unsigned int)rb_big2ulong((VALUE)val); 148 return 0; 149#endif 150 } 151 return NUMERR_TYPE; 152} 153 154#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid) 155 156static inline int 157positive_int_p(VALUE num) 158{ 159 const ID mid = '>'; 160 161 if (FIXNUM_P(num)) { 162 if (method_basic_p(rb_cFixnum)) 163 return (SIGNED_VALUE)num > 0; 164 } 165 else if (RB_TYPE_P(num, T_BIGNUM)) { 166 if (method_basic_p(rb_cBignum)) 167 return RBIGNUM_POSITIVE_P(num); 168 } 169 return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); 170} 171 172static inline int 173negative_int_p(VALUE num) 174{ 175 const ID mid = '<'; 176 177 if (FIXNUM_P(num)) { 178 if (method_basic_p(rb_cFixnum)) 179 return (SIGNED_VALUE)num < 0; 180 } 181 else if (RB_TYPE_P(num, T_BIGNUM)) { 182 if (method_basic_p(rb_cBignum)) 183 return RBIGNUM_NEGATIVE_P(num); 184 } 185 return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); 186} 187 188int 189rb_num_negative_p(VALUE num) 190{ 191 return negative_int_p(num); 192} 193 194/* 195 * call-seq: 196 * num.coerce(numeric) -> array 197 * 198 * If <i>aNumeric</i> is the same type as <i>num</i>, returns an array 199 * containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an 200 * array with both <i>aNumeric</i> and <i>num</i> represented as 201 * <code>Float</code> objects. This coercion mechanism is used by 202 * Ruby to handle mixed-type numeric operations: it is intended to 203 * find a compatible common type between the two operands of the operator. 204 * 205 * 1.coerce(2.5) #=> [2.5, 1.0] 206 * 1.2.coerce(3) #=> [3.0, 1.2] 207 * 1.coerce(2) #=> [2, 1] 208 */ 209 210static VALUE 211num_coerce(VALUE x, VALUE y) 212{ 213 if (CLASS_OF(x) == CLASS_OF(y)) 214 return rb_assoc_new(y, x); 215 x = rb_Float(x); 216 y = rb_Float(y); 217 return rb_assoc_new(y, x); 218} 219 220static VALUE 221coerce_body(VALUE *x) 222{ 223 return rb_funcall(x[1], id_coerce, 1, x[0]); 224} 225 226NORETURN(static void coerce_failed(VALUE x, VALUE y)); 227static void 228coerce_failed(VALUE x, VALUE y) 229{ 230 rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE, 231 (rb_special_const_p(y)? rb_inspect(y) : rb_obj_class(y)), 232 rb_obj_class(x)); 233} 234 235static VALUE 236coerce_rescue(VALUE *x) 237{ 238 coerce_failed(x[0], x[1]); 239 return Qnil; /* dummy */ 240} 241 242static int 243do_coerce(VALUE *x, VALUE *y, int err) 244{ 245 VALUE ary; 246 VALUE a[2]; 247 248 a[0] = *x; a[1] = *y; 249 250 if (!rb_respond_to(*y, id_coerce)) { 251 if (err) { 252 coerce_rescue(a); 253 } 254 return FALSE; 255 } 256 257 ary = rb_rescue(coerce_body, (VALUE)a, err ? coerce_rescue : 0, (VALUE)a); 258 if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) { 259 if (err) { 260 rb_raise(rb_eTypeError, "coerce must return [x, y]"); 261 } 262 return FALSE; 263 } 264 265 *x = RARRAY_PTR(ary)[0]; 266 *y = RARRAY_PTR(ary)[1]; 267 return TRUE; 268} 269 270VALUE 271rb_num_coerce_bin(VALUE x, VALUE y, ID func) 272{ 273 do_coerce(&x, &y, TRUE); 274 return rb_funcall(x, func, 1, y); 275} 276 277VALUE 278rb_num_coerce_cmp(VALUE x, VALUE y, ID func) 279{ 280 if (do_coerce(&x, &y, FALSE)) 281 return rb_funcall(x, func, 1, y); 282 return Qnil; 283} 284 285VALUE 286rb_num_coerce_relop(VALUE x, VALUE y, ID func) 287{ 288 VALUE c, x0 = x, y0 = y; 289 290 if (!do_coerce(&x, &y, FALSE) || 291 NIL_P(c = rb_funcall(x, func, 1, y))) { 292 rb_cmperr(x0, y0); 293 return Qnil; /* not reached */ 294 } 295 return c; 296} 297 298/* 299 * Trap attempts to add methods to <code>Numeric</code> objects. Always 300 * raises a <code>TypeError</code> 301 */ 302 303static VALUE 304num_sadded(VALUE x, VALUE name) 305{ 306 ID mid = rb_to_id(name); 307 /* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */ 308 /* Numerics should be values; singleton_methods should not be added to them */ 309 rb_remove_method_id(rb_singleton_class(x), mid); 310 rb_raise(rb_eTypeError, 311 "can't define singleton method \"%"PRIsVALUE"\" for %"PRIsVALUE, 312 rb_id2str(mid), 313 rb_obj_class(x)); 314 315 UNREACHABLE; 316} 317 318/* :nodoc: */ 319static VALUE 320num_init_copy(VALUE x, VALUE y) 321{ 322 /* Numerics are immutable values, which should not be copied */ 323 rb_raise(rb_eTypeError, "can't copy %"PRIsVALUE, rb_obj_class(x)); 324 325 UNREACHABLE; 326} 327 328/* 329 * call-seq: 330 * +num -> num 331 * 332 * Unary Plus---Returns the receiver's value. 333 */ 334 335static VALUE 336num_uplus(VALUE num) 337{ 338 return num; 339} 340 341/* 342 * call-seq: 343 * num.i -> Complex(0,num) 344 * 345 * Returns the corresponding imaginary number. 346 * Not available for complex numbers. 347 */ 348 349static VALUE 350num_imaginary(VALUE num) 351{ 352 return rb_complex_new(INT2FIX(0), num); 353} 354 355 356/* 357 * call-seq: 358 * -num -> numeric 359 * 360 * Unary Minus---Returns the receiver's value, negated. 361 */ 362 363static VALUE 364num_uminus(VALUE num) 365{ 366 VALUE zero; 367 368 zero = INT2FIX(0); 369 do_coerce(&zero, &num, TRUE); 370 371 return rb_funcall(zero, '-', 1, num); 372} 373 374/* 375 * call-seq: 376 * num.quo(numeric) -> real 377 * 378 * Returns most exact division (rational for integers, float for floats). 379 */ 380 381static VALUE 382num_quo(VALUE x, VALUE y) 383{ 384 return rb_funcall(rb_rational_raw1(x), '/', 1, y); 385} 386 387 388/* 389 * call-seq: 390 * num.fdiv(numeric) -> float 391 * 392 * Returns float division. 393 */ 394 395static VALUE 396num_fdiv(VALUE x, VALUE y) 397{ 398 return rb_funcall(rb_Float(x), '/', 1, y); 399} 400 401 402/* 403 * call-seq: 404 * num.div(numeric) -> integer 405 * 406 * Uses <code>/</code> to perform division, then converts the result to 407 * an integer. <code>numeric</code> does not define the <code>/</code> 408 * operator; this is left to subclasses. 409 * 410 * Equivalent to 411 * <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[0]</code>. 412 * 413 * See <code>Numeric#divmod</code>. 414 */ 415 416static VALUE 417num_div(VALUE x, VALUE y) 418{ 419 if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv(); 420 return rb_funcall(rb_funcall(x, '/', 1, y), rb_intern("floor"), 0); 421} 422 423 424/* 425 * call-seq: 426 * num.modulo(numeric) -> real 427 * 428 * x.modulo(y) means x-y*(x/y).floor 429 * 430 * Equivalent to 431 * <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[1]</code>. 432 * 433 * See <code>Numeric#divmod</code>. 434 */ 435 436static VALUE 437num_modulo(VALUE x, VALUE y) 438{ 439 return rb_funcall(x, '-', 1, 440 rb_funcall(y, '*', 1, 441 rb_funcall(x, rb_intern("div"), 1, y))); 442} 443 444/* 445 * call-seq: 446 * num.remainder(numeric) -> real 447 * 448 * x.remainder(y) means x-y*(x/y).truncate 449 * 450 * See <code>Numeric#divmod</code>. 451 */ 452 453static VALUE 454num_remainder(VALUE x, VALUE y) 455{ 456 VALUE z = rb_funcall(x, '%', 1, y); 457 458 if ((!rb_equal(z, INT2FIX(0))) && 459 ((negative_int_p(x) && 460 positive_int_p(y)) || 461 (positive_int_p(x) && 462 negative_int_p(y)))) { 463 return rb_funcall(z, '-', 1, y); 464 } 465 return z; 466} 467 468/* 469 * call-seq: 470 * num.divmod(numeric) -> array 471 * 472 * Returns an array containing the quotient and modulus obtained by 473 * dividing <i>num</i> by <i>numeric</i>. If <code>q, r = 474 * x.divmod(y)</code>, then 475 * 476 * q = floor(x/y) 477 * x = q*y+r 478 * 479 * The quotient is rounded toward -infinity, as shown in the following table: 480 * 481 * a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b) 482 * ------+-----+---------------+---------+-------------+--------------- 483 * 13 | 4 | 3, 1 | 3 | 1 | 1 484 * ------+-----+---------------+---------+-------------+--------------- 485 * 13 | -4 | -4, -3 | -4 | -3 | 1 486 * ------+-----+---------------+---------+-------------+--------------- 487 * -13 | 4 | -4, 3 | -4 | 3 | -1 488 * ------+-----+---------------+---------+-------------+--------------- 489 * -13 | -4 | 3, -1 | 3 | -1 | -1 490 * ------+-----+---------------+---------+-------------+--------------- 491 * 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5 492 * ------+-----+---------------+---------+-------------+--------------- 493 * 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5 494 * ------+-----+---------------+---------+-------------+--------------- 495 * -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5 496 * ------+-----+---------------+---------+-------------+--------------- 497 * -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5 498 * 499 * 500 * Examples 501 * 502 * 11.divmod(3) #=> [3, 2] 503 * 11.divmod(-3) #=> [-4, -1] 504 * 11.divmod(3.5) #=> [3, 0.5] 505 * (-11).divmod(3.5) #=> [-4, 3.0] 506 * (11.5).divmod(3.5) #=> [3, 1.0] 507 */ 508 509static VALUE 510num_divmod(VALUE x, VALUE y) 511{ 512 return rb_assoc_new(num_div(x, y), num_modulo(x, y)); 513} 514 515/* 516 * call-seq: 517 * num.real? -> true or false 518 * 519 * Returns <code>true</code> if <i>num</i> is a <code>Real</code> 520 * (i.e. non <code>Complex</code>). 521 */ 522 523static VALUE 524num_real_p(VALUE num) 525{ 526 return Qtrue; 527} 528 529/* 530 * call-seq: 531 * num.integer? -> true or false 532 * 533 * Returns +true+ if +num+ is an Integer (including Fixnum and Bignum). 534 * 535 * (1.0).integer? #=> false 536 * (1).integer? #=> true 537 */ 538 539static VALUE 540num_int_p(VALUE num) 541{ 542 return Qfalse; 543} 544 545/* 546 * call-seq: 547 * num.abs -> numeric 548 * num.magnitude -> numeric 549 * 550 * Returns the absolute value of <i>num</i>. 551 * 552 * 12.abs #=> 12 553 * (-34.56).abs #=> 34.56 554 * -34.56.abs #=> 34.56 555 */ 556 557static VALUE 558num_abs(VALUE num) 559{ 560 if (negative_int_p(num)) { 561 return rb_funcall(num, rb_intern("-@"), 0); 562 } 563 return num; 564} 565 566 567/* 568 * call-seq: 569 * num.zero? -> true or false 570 * 571 * Returns <code>true</code> if <i>num</i> has a zero value. 572 */ 573 574static VALUE 575num_zero_p(VALUE num) 576{ 577 if (rb_equal(num, INT2FIX(0))) { 578 return Qtrue; 579 } 580 return Qfalse; 581} 582 583 584/* 585 * call-seq: 586 * num.nonzero? -> self or nil 587 * 588 * Returns +self+ if <i>num</i> is not zero, <code>nil</code> 589 * otherwise. This behavior is useful when chaining comparisons: 590 * 591 * a = %w( z Bb bB bb BB a aA Aa AA A ) 592 * b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b } 593 * b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"] 594 */ 595 596static VALUE 597num_nonzero_p(VALUE num) 598{ 599 if (RTEST(rb_funcall(num, rb_intern("zero?"), 0, 0))) { 600 return Qnil; 601 } 602 return num; 603} 604 605/* 606 * call-seq: 607 * num.to_int -> integer 608 * 609 * Invokes the child class's +to_i+ method to convert +num+ to an integer. 610 * 611 * 1.0.class => Float 612 * 1.0.to_int.class => Fixnum 613 * 1.0.to_i.class => Fixnum 614 */ 615 616static VALUE 617num_to_int(VALUE num) 618{ 619 return rb_funcall(num, id_to_i, 0, 0); 620} 621 622 623/******************************************************************** 624 * 625 * Document-class: Float 626 * 627 * <code>Float</code> objects represent inexact real numbers using 628 * the native architecture's double-precision floating point 629 * representation. 630 * 631 * Floating point has a different arithmetic and is a inexact number. 632 * So you should know its esoteric system. see following: 633 * 634 * - http://docs.sun.com/source/806-3568/ncg_goldberg.html 635 * - http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#wiki-floats_imprecise 636 * - http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems 637 */ 638 639VALUE 640rb_float_new_in_heap(double d) 641{ 642 NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT); 643 644 flt->float_value = d; 645 OBJ_FREEZE(flt); 646 return (VALUE)flt; 647} 648 649/* 650 * call-seq: 651 * flt.to_s -> string 652 * 653 * Returns a string containing a representation of self. As well as a 654 * fixed or exponential form of the number, the call may return 655 * ``<code>NaN</code>'', ``<code>Infinity</code>'', and 656 * ``<code>-Infinity</code>''. 657 */ 658 659static VALUE 660flo_to_s(VALUE flt) 661{ 662 char *ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve); 663 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG}; 664 enum {float_dig = DBL_DIG+1}; 665 char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10]; 666 double value = RFLOAT_VALUE(flt); 667 VALUE s; 668 char *p, *e; 669 int sign, decpt, digs; 670 671 if (isinf(value)) 672 return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity"); 673 else if (isnan(value)) 674 return rb_usascii_str_new2("NaN"); 675 676 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e); 677 s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0); 678 if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1; 679 memcpy(buf, p, digs); 680 xfree(p); 681 if (decpt > 0) { 682 if (decpt < digs) { 683 memmove(buf + decpt + 1, buf + decpt, digs - decpt); 684 buf[decpt] = '.'; 685 rb_str_cat(s, buf, digs + 1); 686 } 687 else if (decpt <= DBL_DIG) { 688 long len; 689 char *ptr; 690 rb_str_cat(s, buf, digs); 691 rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2); 692 ptr = RSTRING_PTR(s) + len; 693 if (decpt > digs) { 694 memset(ptr, '0', decpt - digs); 695 ptr += decpt - digs; 696 } 697 memcpy(ptr, ".0", 2); 698 } 699 else { 700 goto exp; 701 } 702 } 703 else if (decpt > -4) { 704 long len; 705 char *ptr; 706 rb_str_cat(s, "0.", 2); 707 rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs); 708 ptr = RSTRING_PTR(s); 709 memset(ptr += len, '0', -decpt); 710 memcpy(ptr -= decpt, buf, digs); 711 } 712 else { 713 exp: 714 if (digs > 1) { 715 memmove(buf + 2, buf + 1, digs - 1); 716 } 717 else { 718 buf[2] = '0'; 719 digs++; 720 } 721 buf[1] = '.'; 722 rb_str_cat(s, buf, digs + 1); 723 rb_str_catf(s, "e%+03d", decpt - 1); 724 } 725 return s; 726} 727 728/* 729 * call-seq: 730 * flt.coerce(numeric) -> array 731 * 732 * Returns an array with both <i>aNumeric</i> and <i>flt</i> represented 733 * as <code>Float</code> objects. 734 * This is achieved by converting <i>aNumeric</i> to a <code>Float</code>. 735 * 736 * 1.2.coerce(3) #=> [3.0, 1.2] 737 * 2.5.coerce(1.1) #=> [1.1, 2.5] 738 */ 739 740static VALUE 741flo_coerce(VALUE x, VALUE y) 742{ 743 return rb_assoc_new(rb_Float(y), x); 744} 745 746/* 747 * call-seq: 748 * -float -> float 749 * 750 * Returns float, negated. 751 */ 752 753static VALUE 754flo_uminus(VALUE flt) 755{ 756 return DBL2NUM(-RFLOAT_VALUE(flt)); 757} 758 759/* 760 * call-seq: 761 * float + other -> float 762 * 763 * Returns a new float which is the sum of <code>float</code> 764 * and <code>other</code>. 765 */ 766 767static VALUE 768flo_plus(VALUE x, VALUE y) 769{ 770 switch (TYPE(y)) { 771 case T_FIXNUM: 772 return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y)); 773 case T_BIGNUM: 774 return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y)); 775 case T_FLOAT: 776 return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y)); 777 default: 778 return rb_num_coerce_bin(x, y, '+'); 779 } 780} 781 782/* 783 * call-seq: 784 * float - other -> float 785 * 786 * Returns a new float which is the difference of <code>float</code> 787 * and <code>other</code>. 788 */ 789 790static VALUE 791flo_minus(VALUE x, VALUE y) 792{ 793 switch (TYPE(y)) { 794 case T_FIXNUM: 795 return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y)); 796 case T_BIGNUM: 797 return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y)); 798 case T_FLOAT: 799 return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y)); 800 default: 801 return rb_num_coerce_bin(x, y, '-'); 802 } 803} 804 805/* 806 * call-seq: 807 * float * other -> float 808 * 809 * Returns a new float which is the product of <code>float</code> 810 * and <code>other</code>. 811 */ 812 813static VALUE 814flo_mul(VALUE x, VALUE y) 815{ 816 switch (TYPE(y)) { 817 case T_FIXNUM: 818 return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y)); 819 case T_BIGNUM: 820 return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y)); 821 case T_FLOAT: 822 return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y)); 823 default: 824 return rb_num_coerce_bin(x, y, '*'); 825 } 826} 827 828/* 829 * call-seq: 830 * float / other -> float 831 * 832 * Returns a new float which is the result of dividing 833 * <code>float</code> by <code>other</code>. 834 */ 835 836static VALUE 837flo_div(VALUE x, VALUE y) 838{ 839 long f_y; 840 double d; 841 842 switch (TYPE(y)) { 843 case T_FIXNUM: 844 f_y = FIX2LONG(y); 845 return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y); 846 case T_BIGNUM: 847 d = rb_big2dbl(y); 848 return DBL2NUM(RFLOAT_VALUE(x) / d); 849 case T_FLOAT: 850 return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y)); 851 default: 852 return rb_num_coerce_bin(x, y, '/'); 853 } 854} 855 856/* 857 * call-seq: 858 * float.quo(numeric) -> float 859 * 860 * Returns float / numeric. 861 */ 862 863static VALUE 864flo_quo(VALUE x, VALUE y) 865{ 866 return rb_funcall(x, '/', 1, y); 867} 868 869static void 870flodivmod(double x, double y, double *divp, double *modp) 871{ 872 double div, mod; 873 874 if (y == 0.0) rb_num_zerodiv(); 875 if ((x == 0.0) || (isinf(y) && !isinf(x))) 876 mod = x; 877 else { 878#ifdef HAVE_FMOD 879 mod = fmod(x, y); 880#else 881 double z; 882 883 modf(x/y, &z); 884 mod = x - z * y; 885#endif 886 } 887 if (isinf(x) && !isinf(y) && !isnan(y)) 888 div = x; 889 else 890 div = (x - mod) / y; 891 if (y*mod < 0) { 892 mod += y; 893 div -= 1.0; 894 } 895 if (modp) *modp = mod; 896 if (divp) *divp = div; 897} 898 899/* 900 * Returns the modulo of division of x by y. 901 * An error will be raised if y == 0. 902 */ 903 904double 905ruby_float_mod(double x, double y) 906{ 907 double mod; 908 flodivmod(x, y, 0, &mod); 909 return mod; 910} 911 912 913/* 914 * call-seq: 915 * float % other -> float 916 * float.modulo(other) -> float 917 * 918 * Return the modulo after division of +float+ by +other+. 919 * 920 * 6543.21.modulo(137) #=> 104.21 921 * 6543.21.modulo(137.24) #=> 92.9299999999996 922 */ 923 924static VALUE 925flo_mod(VALUE x, VALUE y) 926{ 927 double fy; 928 929 switch (TYPE(y)) { 930 case T_FIXNUM: 931 fy = (double)FIX2LONG(y); 932 break; 933 case T_BIGNUM: 934 fy = rb_big2dbl(y); 935 break; 936 case T_FLOAT: 937 fy = RFLOAT_VALUE(y); 938 break; 939 default: 940 return rb_num_coerce_bin(x, y, '%'); 941 } 942 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy)); 943} 944 945static VALUE 946dbl2ival(double d) 947{ 948 d = round(d); 949 if (FIXABLE(d)) { 950 return LONG2FIX((long)d); 951 } 952 return rb_dbl2big(d); 953} 954 955/* 956 * call-seq: 957 * float.divmod(numeric) -> array 958 * 959 * See Numeric#divmod. 960 * 961 * 42.0.divmod 6 #=> [7, 0.0] 962 * 42.0.divmod 5 #=> [8, 2.0] 963 */ 964 965static VALUE 966flo_divmod(VALUE x, VALUE y) 967{ 968 double fy, div, mod; 969 volatile VALUE a, b; 970 971 switch (TYPE(y)) { 972 case T_FIXNUM: 973 fy = (double)FIX2LONG(y); 974 break; 975 case T_BIGNUM: 976 fy = rb_big2dbl(y); 977 break; 978 case T_FLOAT: 979 fy = RFLOAT_VALUE(y); 980 break; 981 default: 982 return rb_num_coerce_bin(x, y, rb_intern("divmod")); 983 } 984 flodivmod(RFLOAT_VALUE(x), fy, &div, &mod); 985 a = dbl2ival(div); 986 b = DBL2NUM(mod); 987 return rb_assoc_new(a, b); 988} 989 990/* 991 * call-seq: 992 * 993 * flt ** other -> float 994 * 995 * Raises <code>float</code> the <code>other</code> power. 996 * 997 * 2.0**3 #=> 8.0 998 */ 999 1000static VALUE 1001flo_pow(VALUE x, VALUE y) 1002{ 1003 switch (TYPE(y)) { 1004 case T_FIXNUM: 1005 return DBL2NUM(pow(RFLOAT_VALUE(x), (double)FIX2LONG(y))); 1006 case T_BIGNUM: 1007 return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y))); 1008 case T_FLOAT: 1009 { 1010 double dx = RFLOAT_VALUE(x); 1011 double dy = RFLOAT_VALUE(y); 1012 if (dx < 0 && dy != round(dy)) 1013 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y); 1014 return DBL2NUM(pow(dx, dy)); 1015 } 1016 default: 1017 return rb_num_coerce_bin(x, y, rb_intern("**")); 1018 } 1019} 1020 1021/* 1022 * call-seq: 1023 * num.eql?(numeric) -> true or false 1024 * 1025 * Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the 1026 * same type and have equal values. 1027 * 1028 * 1 == 1.0 #=> true 1029 * 1.eql?(1.0) #=> false 1030 * (1.0).eql?(1.0) #=> true 1031 */ 1032 1033static VALUE 1034num_eql(VALUE x, VALUE y) 1035{ 1036 if (TYPE(x) != TYPE(y)) return Qfalse; 1037 1038 return rb_equal(x, y); 1039} 1040 1041/* 1042 * call-seq: 1043 * number <=> other -> 0 or nil 1044 * 1045 * Returns zero if +number+ equals +other+, otherwise +nil+ is returned if the 1046 * two values are incomparable. 1047 */ 1048 1049static VALUE 1050num_cmp(VALUE x, VALUE y) 1051{ 1052 if (x == y) return INT2FIX(0); 1053 return Qnil; 1054} 1055 1056static VALUE 1057num_equal(VALUE x, VALUE y) 1058{ 1059 if (x == y) return Qtrue; 1060 return rb_funcall(y, id_eq, 1, x); 1061} 1062 1063/* 1064 * call-seq: 1065 * flt == obj -> true or false 1066 * 1067 * Returns <code>true</code> only if <i>obj</i> has the same value 1068 * as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which 1069 * requires <i>obj</i> to be a <code>Float</code>. 1070 * The result of <code>NaN == NaN</code> is undefined, so the 1071 * implementation-dependent value is returned. 1072 * 1073 * 1.0 == 1 #=> true 1074 * 1075 */ 1076 1077static VALUE 1078flo_eq(VALUE x, VALUE y) 1079{ 1080 volatile double a, b; 1081 1082 switch (TYPE(y)) { 1083 case T_FIXNUM: 1084 case T_BIGNUM: 1085 return rb_integer_float_eq(y, x); 1086 case T_FLOAT: 1087 b = RFLOAT_VALUE(y); 1088#if defined(_MSC_VER) && _MSC_VER < 1300 1089 if (isnan(b)) return Qfalse; 1090#endif 1091 break; 1092 default: 1093 return num_equal(x, y); 1094 } 1095 a = RFLOAT_VALUE(x); 1096#if defined(_MSC_VER) && _MSC_VER < 1300 1097 if (isnan(a)) return Qfalse; 1098#endif 1099 return (a == b)?Qtrue:Qfalse; 1100} 1101 1102/* 1103 * call-seq: 1104 * flt.hash -> integer 1105 * 1106 * Returns a hash code for this float. 1107 */ 1108 1109static VALUE 1110flo_hash(VALUE num) 1111{ 1112 double d; 1113 st_index_t hash; 1114 1115 d = RFLOAT_VALUE(num); 1116 /* normalize -0.0 to 0.0 */ 1117 if (d == 0.0) d = 0.0; 1118 hash = rb_memhash(&d, sizeof(d)); 1119 return LONG2FIX(hash); 1120} 1121 1122VALUE 1123rb_dbl_cmp(double a, double b) 1124{ 1125 if (isnan(a) || isnan(b)) return Qnil; 1126 if (a == b) return INT2FIX(0); 1127 if (a > b) return INT2FIX(1); 1128 if (a < b) return INT2FIX(-1); 1129 return Qnil; 1130} 1131 1132/* 1133 * call-seq: 1134 * float <=> real -> -1, 0, +1 or nil 1135 * 1136 * Returns -1, 0, +1 or nil depending on whether +float+ is less than, equal 1137 * to, or greater than +real+. This is the basis for the tests in Comparable. 1138 * 1139 * The result of <code>NaN <=> NaN</code> is undefined, so the 1140 * implementation-dependent value is returned. 1141 * 1142 * +nil+ is returned if the two values are incomparable. 1143 */ 1144 1145static VALUE 1146flo_cmp(VALUE x, VALUE y) 1147{ 1148 double a, b; 1149 VALUE i; 1150 1151 a = RFLOAT_VALUE(x); 1152 if (isnan(a)) return Qnil; 1153 switch (TYPE(y)) { 1154 case T_FIXNUM: 1155 case T_BIGNUM: 1156 { 1157 VALUE rel = rb_integer_float_cmp(y, x); 1158 if (FIXNUM_P(rel)) 1159 return INT2FIX(-FIX2INT(rel)); 1160 return rel; 1161 } 1162 1163 case T_FLOAT: 1164 b = RFLOAT_VALUE(y); 1165 break; 1166 1167 default: 1168 if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) { 1169 if (RTEST(i)) { 1170 int j = rb_cmpint(i, x, y); 1171 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1); 1172 return INT2FIX(j); 1173 } 1174 if (a > 0.0) return INT2FIX(1); 1175 return INT2FIX(-1); 1176 } 1177 return rb_num_coerce_cmp(x, y, rb_intern("<=>")); 1178 } 1179 return rb_dbl_cmp(a, b); 1180} 1181 1182/* 1183 * call-seq: 1184 * flt > real -> true or false 1185 * 1186 * <code>true</code> if <code>flt</code> is greater than <code>real</code>. 1187 * The result of <code>NaN > NaN</code> is undefined, so the 1188 * implementation-dependent value is returned. 1189 */ 1190 1191static VALUE 1192flo_gt(VALUE x, VALUE y) 1193{ 1194 double a, b; 1195 1196 a = RFLOAT_VALUE(x); 1197 switch (TYPE(y)) { 1198 case T_FIXNUM: 1199 case T_BIGNUM: 1200 { 1201 VALUE rel = rb_integer_float_cmp(y, x); 1202 if (FIXNUM_P(rel)) 1203 return -FIX2INT(rel) > 0 ? Qtrue : Qfalse; 1204 return Qfalse; 1205 } 1206 1207 case T_FLOAT: 1208 b = RFLOAT_VALUE(y); 1209#if defined(_MSC_VER) && _MSC_VER < 1300 1210 if (isnan(b)) return Qfalse; 1211#endif 1212 break; 1213 1214 default: 1215 return rb_num_coerce_relop(x, y, '>'); 1216 } 1217#if defined(_MSC_VER) && _MSC_VER < 1300 1218 if (isnan(a)) return Qfalse; 1219#endif 1220 return (a > b)?Qtrue:Qfalse; 1221} 1222 1223/* 1224 * call-seq: 1225 * flt >= real -> true or false 1226 * 1227 * <code>true</code> if <code>flt</code> is greater than 1228 * or equal to <code>real</code>. 1229 * The result of <code>NaN >= NaN</code> is undefined, so the 1230 * implementation-dependent value is returned. 1231 */ 1232 1233static VALUE 1234flo_ge(VALUE x, VALUE y) 1235{ 1236 double a, b; 1237 1238 a = RFLOAT_VALUE(x); 1239 switch (TYPE(y)) { 1240 case T_FIXNUM: 1241 case T_BIGNUM: 1242 { 1243 VALUE rel = rb_integer_float_cmp(y, x); 1244 if (FIXNUM_P(rel)) 1245 return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse; 1246 return Qfalse; 1247 } 1248 1249 case T_FLOAT: 1250 b = RFLOAT_VALUE(y); 1251#if defined(_MSC_VER) && _MSC_VER < 1300 1252 if (isnan(b)) return Qfalse; 1253#endif 1254 break; 1255 1256 default: 1257 return rb_num_coerce_relop(x, y, rb_intern(">=")); 1258 } 1259#if defined(_MSC_VER) && _MSC_VER < 1300 1260 if (isnan(a)) return Qfalse; 1261#endif 1262 return (a >= b)?Qtrue:Qfalse; 1263} 1264 1265/* 1266 * call-seq: 1267 * flt < real -> true or false 1268 * 1269 * <code>true</code> if <code>flt</code> is less than <code>real</code>. 1270 * The result of <code>NaN < NaN</code> is undefined, so the 1271 * implementation-dependent value is returned. 1272 */ 1273 1274static VALUE 1275flo_lt(VALUE x, VALUE y) 1276{ 1277 double a, b; 1278 1279 a = RFLOAT_VALUE(x); 1280 switch (TYPE(y)) { 1281 case T_FIXNUM: 1282 case T_BIGNUM: 1283 { 1284 VALUE rel = rb_integer_float_cmp(y, x); 1285 if (FIXNUM_P(rel)) 1286 return -FIX2INT(rel) < 0 ? Qtrue : Qfalse; 1287 return Qfalse; 1288 } 1289 1290 case T_FLOAT: 1291 b = RFLOAT_VALUE(y); 1292#if defined(_MSC_VER) && _MSC_VER < 1300 1293 if (isnan(b)) return Qfalse; 1294#endif 1295 break; 1296 1297 default: 1298 return rb_num_coerce_relop(x, y, '<'); 1299 } 1300#if defined(_MSC_VER) && _MSC_VER < 1300 1301 if (isnan(a)) return Qfalse; 1302#endif 1303 return (a < b)?Qtrue:Qfalse; 1304} 1305 1306/* 1307 * call-seq: 1308 * flt <= real -> true or false 1309 * 1310 * <code>true</code> if <code>flt</code> is less than 1311 * or equal to <code>real</code>. 1312 * The result of <code>NaN <= NaN</code> is undefined, so the 1313 * implementation-dependent value is returned. 1314 */ 1315 1316static VALUE 1317flo_le(VALUE x, VALUE y) 1318{ 1319 double a, b; 1320 1321 a = RFLOAT_VALUE(x); 1322 switch (TYPE(y)) { 1323 case T_FIXNUM: 1324 case T_BIGNUM: 1325 { 1326 VALUE rel = rb_integer_float_cmp(y, x); 1327 if (FIXNUM_P(rel)) 1328 return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse; 1329 return Qfalse; 1330 } 1331 1332 case T_FLOAT: 1333 b = RFLOAT_VALUE(y); 1334#if defined(_MSC_VER) && _MSC_VER < 1300 1335 if (isnan(b)) return Qfalse; 1336#endif 1337 break; 1338 1339 default: 1340 return rb_num_coerce_relop(x, y, rb_intern("<=")); 1341 } 1342#if defined(_MSC_VER) && _MSC_VER < 1300 1343 if (isnan(a)) return Qfalse; 1344#endif 1345 return (a <= b)?Qtrue:Qfalse; 1346} 1347 1348/* 1349 * call-seq: 1350 * flt.eql?(obj) -> true or false 1351 * 1352 * Returns <code>true</code> only if <i>obj</i> is a 1353 * <code>Float</code> with the same value as <i>flt</i>. Contrast this 1354 * with <code>Float#==</code>, which performs type conversions. 1355 * The result of <code>NaN.eql?(NaN)</code> is undefined, so the 1356 * implementation-dependent value is returned. 1357 * 1358 * 1.0.eql?(1) #=> false 1359 */ 1360 1361static VALUE 1362flo_eql(VALUE x, VALUE y) 1363{ 1364 if (RB_TYPE_P(y, T_FLOAT)) { 1365 double a = RFLOAT_VALUE(x); 1366 double b = RFLOAT_VALUE(y); 1367#if defined(_MSC_VER) && _MSC_VER < 1300 1368 if (isnan(a) || isnan(b)) return Qfalse; 1369#endif 1370 if (a == b) 1371 return Qtrue; 1372 } 1373 return Qfalse; 1374} 1375 1376/* 1377 * call-seq: 1378 * flt.to_f -> self 1379 * 1380 * As <code>flt</code> is already a float, returns +self+. 1381 */ 1382 1383static VALUE 1384flo_to_f(VALUE num) 1385{ 1386 return num; 1387} 1388 1389/* 1390 * call-seq: 1391 * flt.abs -> float 1392 * flt.magnitude -> float 1393 * 1394 * Returns the absolute value of <i>flt</i>. 1395 * 1396 * (-34.56).abs #=> 34.56 1397 * -34.56.abs #=> 34.56 1398 * 1399 */ 1400 1401static VALUE 1402flo_abs(VALUE flt) 1403{ 1404 double val = fabs(RFLOAT_VALUE(flt)); 1405 return DBL2NUM(val); 1406} 1407 1408/* 1409 * call-seq: 1410 * flt.zero? -> true or false 1411 * 1412 * Returns <code>true</code> if <i>flt</i> is 0.0. 1413 * 1414 */ 1415 1416static VALUE 1417flo_zero_p(VALUE num) 1418{ 1419 if (RFLOAT_VALUE(num) == 0.0) { 1420 return Qtrue; 1421 } 1422 return Qfalse; 1423} 1424 1425/* 1426 * call-seq: 1427 * flt.nan? -> true or false 1428 * 1429 * Returns <code>true</code> if <i>flt</i> is an invalid IEEE floating 1430 * point number. 1431 * 1432 * a = -1.0 #=> -1.0 1433 * a.nan? #=> false 1434 * a = 0.0/0.0 #=> NaN 1435 * a.nan? #=> true 1436 */ 1437 1438static VALUE 1439flo_is_nan_p(VALUE num) 1440{ 1441 double value = RFLOAT_VALUE(num); 1442 1443 return isnan(value) ? Qtrue : Qfalse; 1444} 1445 1446/* 1447 * call-seq: 1448 * flt.infinite? -> nil, -1, +1 1449 * 1450 * Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i> 1451 * is finite, -infinity, or +infinity. 1452 * 1453 * (0.0).infinite? #=> nil 1454 * (-1.0/0.0).infinite? #=> -1 1455 * (+1.0/0.0).infinite? #=> 1 1456 */ 1457 1458static VALUE 1459flo_is_infinite_p(VALUE num) 1460{ 1461 double value = RFLOAT_VALUE(num); 1462 1463 if (isinf(value)) { 1464 return INT2FIX( value < 0 ? -1 : 1 ); 1465 } 1466 1467 return Qnil; 1468} 1469 1470/* 1471 * call-seq: 1472 * flt.finite? -> true or false 1473 * 1474 * Returns <code>true</code> if <i>flt</i> is a valid IEEE floating 1475 * point number (it is not infinite, and <code>nan?</code> is 1476 * <code>false</code>). 1477 * 1478 */ 1479 1480static VALUE 1481flo_is_finite_p(VALUE num) 1482{ 1483 double value = RFLOAT_VALUE(num); 1484 1485#if HAVE_FINITE 1486 if (!finite(value)) 1487 return Qfalse; 1488#else 1489 if (isinf(value) || isnan(value)) 1490 return Qfalse; 1491#endif 1492 1493 return Qtrue; 1494} 1495 1496/* 1497 * call-seq: 1498 * flt.floor -> integer 1499 * 1500 * Returns the largest integer less than or equal to <i>flt</i>. 1501 * 1502 * 1.2.floor #=> 1 1503 * 2.0.floor #=> 2 1504 * (-1.2).floor #=> -2 1505 * (-2.0).floor #=> -2 1506 */ 1507 1508static VALUE 1509flo_floor(VALUE num) 1510{ 1511 double f = floor(RFLOAT_VALUE(num)); 1512 long val; 1513 1514 if (!FIXABLE(f)) { 1515 return rb_dbl2big(f); 1516 } 1517 val = (long)f; 1518 return LONG2FIX(val); 1519} 1520 1521/* 1522 * call-seq: 1523 * flt.ceil -> integer 1524 * 1525 * Returns the smallest <code>Integer</code> greater than or equal to 1526 * <i>flt</i>. 1527 * 1528 * 1.2.ceil #=> 2 1529 * 2.0.ceil #=> 2 1530 * (-1.2).ceil #=> -1 1531 * (-2.0).ceil #=> -2 1532 */ 1533 1534static VALUE 1535flo_ceil(VALUE num) 1536{ 1537 double f = ceil(RFLOAT_VALUE(num)); 1538 long val; 1539 1540 if (!FIXABLE(f)) { 1541 return rb_dbl2big(f); 1542 } 1543 val = (long)f; 1544 return LONG2FIX(val); 1545} 1546 1547/* 1548 * Assumes num is an Integer, ndigits <= 0 1549 */ 1550static VALUE 1551int_round_0(VALUE num, int ndigits) 1552{ 1553 VALUE n, f, h, r; 1554 long bytes; 1555 ID op; 1556 /* If 10**N / 2 > num, then return 0 */ 1557 /* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */ 1558 bytes = FIXNUM_P(num) ? sizeof(long) : rb_funcall(num, idSize, 0); 1559 if (-0.415241 * ndigits - 0.125 > bytes ) { 1560 return INT2FIX(0); 1561 } 1562 1563 f = int_pow(10, -ndigits); 1564 if (FIXNUM_P(num) && FIXNUM_P(f)) { 1565 SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f); 1566 int neg = x < 0; 1567 if (neg) x = -x; 1568 x = (x + y / 2) / y * y; 1569 if (neg) x = -x; 1570 return LONG2NUM(x); 1571 } 1572 if (RB_TYPE_P(f, T_FLOAT)) { 1573 /* then int_pow overflow */ 1574 return INT2FIX(0); 1575 } 1576 h = rb_funcall(f, '/', 1, INT2FIX(2)); 1577 r = rb_funcall(num, '%', 1, f); 1578 n = rb_funcall(num, '-', 1, r); 1579 op = negative_int_p(num) ? rb_intern("<=") : '<'; 1580 if (!RTEST(rb_funcall(r, op, 1, h))) { 1581 n = rb_funcall(n, '+', 1, f); 1582 } 1583 return n; 1584} 1585 1586static VALUE 1587flo_truncate(VALUE num); 1588 1589/* 1590 * call-seq: 1591 * flt.round([ndigits]) -> integer or float 1592 * 1593 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits). 1594 * Precision may be negative. Returns a floating point number when ndigits 1595 * is more than zero. 1596 * 1597 * 1.4.round #=> 1 1598 * 1.5.round #=> 2 1599 * 1.6.round #=> 2 1600 * (-1.5).round #=> -2 1601 * 1602 * 1.234567.round(2) #=> 1.23 1603 * 1.234567.round(3) #=> 1.235 1604 * 1.234567.round(4) #=> 1.2346 1605 * 1.234567.round(5) #=> 1.23457 1606 * 1607 * 34567.89.round(-5) #=> 0 1608 * 34567.89.round(-4) #=> 30000 1609 * 34567.89.round(-3) #=> 35000 1610 * 34567.89.round(-2) #=> 34600 1611 * 34567.89.round(-1) #=> 34570 1612 * 34567.89.round(0) #=> 34568 1613 * 34567.89.round(1) #=> 34567.9 1614 * 34567.89.round(2) #=> 34567.89 1615 * 34567.89.round(3) #=> 34567.89 1616 * 1617 */ 1618 1619static VALUE 1620flo_round(int argc, VALUE *argv, VALUE num) 1621{ 1622 VALUE nd; 1623 double number, f; 1624 int ndigits = 0; 1625 int binexp; 1626 enum {float_dig = DBL_DIG+2}; 1627 1628 if (argc > 0 && rb_scan_args(argc, argv, "01", &nd) == 1) { 1629 ndigits = NUM2INT(nd); 1630 } 1631 if (ndigits < 0) { 1632 return int_round_0(flo_truncate(num), ndigits); 1633 } 1634 number = RFLOAT_VALUE(num); 1635 if (ndigits == 0) { 1636 return dbl2ival(number); 1637 } 1638 frexp(number, &binexp); 1639 1640/* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}", 1641 i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp 1642 Recall that up to float_dig digits can be needed to represent a double, 1643 so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits) 1644 will be an integer and thus the result is the original number. 1645 If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so 1646 if ndigits + exp < 0, the result is 0. 1647 We have: 1648 2 ** (binexp-1) <= |number| < 2 ** binexp 1649 10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10)) 1650 If binexp >= 0, and since log_2(10) = 3.322259: 1651 10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3) 1652 floor(binexp/4) <= exp <= ceil(binexp/3) 1653 If binexp <= 0, swap the /4 and the /3 1654 So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number 1655 If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0 1656*/ 1657 if (isinf(number) || isnan(number) || 1658 (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) { 1659 return num; 1660 } 1661 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) { 1662 return DBL2NUM(0); 1663 } 1664 f = pow(10, ndigits); 1665 return DBL2NUM(round(number * f) / f); 1666} 1667 1668/* 1669 * call-seq: 1670 * flt.to_i -> integer 1671 * flt.to_int -> integer 1672 * flt.truncate -> integer 1673 * 1674 * Returns <i>flt</i> truncated to an <code>Integer</code>. 1675 */ 1676 1677static VALUE 1678flo_truncate(VALUE num) 1679{ 1680 double f = RFLOAT_VALUE(num); 1681 long val; 1682 1683 if (f > 0.0) f = floor(f); 1684 if (f < 0.0) f = ceil(f); 1685 1686 if (!FIXABLE(f)) { 1687 return rb_dbl2big(f); 1688 } 1689 val = (long)f; 1690 return LONG2FIX(val); 1691} 1692 1693/* 1694 * call-seq: 1695 * num.floor -> integer 1696 * 1697 * Returns the largest integer less than or equal to <i>num</i>. 1698 * <code>Numeric</code> implements this by converting <i>anInteger</i> 1699 * to a <code>Float</code> and invoking <code>Float#floor</code>. 1700 * 1701 * 1.floor #=> 1 1702 * (-1).floor #=> -1 1703 */ 1704 1705static VALUE 1706num_floor(VALUE num) 1707{ 1708 return flo_floor(rb_Float(num)); 1709} 1710 1711 1712/* 1713 * call-seq: 1714 * num.ceil -> integer 1715 * 1716 * Returns the smallest <code>Integer</code> greater than or equal to 1717 * <i>num</i>. Class <code>Numeric</code> achieves this by converting 1718 * itself to a <code>Float</code> then invoking 1719 * <code>Float#ceil</code>. 1720 * 1721 * 1.ceil #=> 1 1722 * 1.2.ceil #=> 2 1723 * (-1.2).ceil #=> -1 1724 * (-1.0).ceil #=> -1 1725 */ 1726 1727static VALUE 1728num_ceil(VALUE num) 1729{ 1730 return flo_ceil(rb_Float(num)); 1731} 1732 1733/* 1734 * call-seq: 1735 * num.round([ndigits]) -> integer or float 1736 * 1737 * Rounds <i>num</i> to a given precision in decimal digits (default 0 digits). 1738 * Precision may be negative. Returns a floating point number when <i>ndigits</i> 1739 * is more than zero. <code>Numeric</code> implements this by converting itself 1740 * to a <code>Float</code> and invoking <code>Float#round</code>. 1741 */ 1742 1743static VALUE 1744num_round(int argc, VALUE* argv, VALUE num) 1745{ 1746 return flo_round(argc, argv, rb_Float(num)); 1747} 1748 1749/* 1750 * call-seq: 1751 * num.truncate -> integer 1752 * 1753 * Returns <i>num</i> truncated to an integer. <code>Numeric</code> 1754 * implements this by converting its value to a float and invoking 1755 * <code>Float#truncate</code>. 1756 */ 1757 1758static VALUE 1759num_truncate(VALUE num) 1760{ 1761 return flo_truncate(rb_Float(num)); 1762} 1763 1764static double 1765ruby_float_step_size(double beg, double end, double unit, int excl) 1766{ 1767 const double epsilon = DBL_EPSILON; 1768 double n = (end - beg)/unit; 1769 double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon; 1770 1771 if (isinf(unit)) { 1772 return unit > 0 ? beg <= end : beg >= end; 1773 } 1774 if (err>0.5) err=0.5; 1775 if (excl) { 1776 if (n<=0) return 0; 1777 if (n<1) 1778 n = 0; 1779 else 1780 n = floor(n - err); 1781 } 1782 else { 1783 if (n<0) return 0; 1784 n = floor(n + err); 1785 } 1786 return n+1; 1787} 1788 1789int 1790ruby_float_step(VALUE from, VALUE to, VALUE step, int excl) 1791{ 1792 if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) { 1793 double beg = NUM2DBL(from); 1794 double end = NUM2DBL(to); 1795 double unit = NUM2DBL(step); 1796 double n = ruby_float_step_size(beg, end, unit, excl); 1797 long i; 1798 1799 if (isinf(unit)) { 1800 /* if unit is infinity, i*unit+beg is NaN */ 1801 if (n) rb_yield(DBL2NUM(beg)); 1802 } 1803 else { 1804 for (i=0; i<n; i++) { 1805 double d = i*unit+beg; 1806 if (unit >= 0 ? end < d : d < end) d = end; 1807 rb_yield(DBL2NUM(d)); 1808 } 1809 } 1810 return TRUE; 1811 } 1812 return FALSE; 1813} 1814 1815VALUE 1816num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl) 1817{ 1818 if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) { 1819 long delta, diff; 1820 1821 diff = FIX2LONG(step); 1822 if (!diff) rb_num_zerodiv(); 1823 delta = FIX2LONG(to) - FIX2LONG(from); 1824 if (diff < 0) { 1825 diff = -diff; 1826 delta = -delta; 1827 } 1828 if (excl) { 1829 delta--; 1830 } 1831 if (delta < 0) { 1832 return INT2FIX(0); 1833 } 1834 return ULONG2NUM(delta / diff + 1UL); 1835 } 1836 else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) { 1837 double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl); 1838 1839 if (isinf(n)) return DBL2NUM(n); 1840 if (POSFIXABLE(n)) return LONG2FIX(n); 1841 return rb_dbl2big(n); 1842 } 1843 else { 1844 VALUE result; 1845 ID cmp = RTEST(rb_funcall(step, '>', 1, INT2FIX(0))) ? '>' : '<'; 1846 if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0); 1847 result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step); 1848 if (!excl || RTEST(rb_funcall(rb_funcall(from, '+', 1, rb_funcall(result, '*', 1, step)), cmp, 1, to))) { 1849 result = rb_funcall(result, '+', 1, INT2FIX(1)); 1850 } 1851 return result; 1852 } 1853} 1854 1855static VALUE 1856num_step_size(VALUE from, VALUE args) 1857{ 1858 VALUE to = RARRAY_PTR(args)[0]; 1859 VALUE step = (RARRAY_LEN(args) > 1) ? RARRAY_PTR(args)[1] : INT2FIX(1); 1860 return num_interval_step_size(from, to, step, FALSE); 1861} 1862/* 1863 * call-seq: 1864 * num.step(limit[, step]) {|i| block } -> self 1865 * num.step(limit[, step]) -> an_enumerator 1866 * 1867 * Invokes <em>block</em> with the sequence of numbers starting at 1868 * <i>num</i>, incremented by <i>step</i> (default 1) on each 1869 * call. The loop finishes when the value to be passed to the block 1870 * is greater than <i>limit</i> (if <i>step</i> is positive) or less 1871 * than <i>limit</i> (if <i>step</i> is negative). If all the 1872 * arguments are integers, the loop operates using an integer 1873 * counter. If any of the arguments are floating point numbers, all 1874 * are converted to floats, and the loop is executed <i>floor(n + 1875 * n*epsilon)+ 1</i> times, where <i>n = (limit - 1876 * num)/step</i>. Otherwise, the loop starts at <i>num</i>, uses 1877 * either the <code><</code> or <code>></code> operator to compare 1878 * the counter against <i>limit</i>, and increments itself using the 1879 * <code>+</code> operator. 1880 * 1881 * If no block is given, an enumerator is returned instead. 1882 * 1883 * 1.step(10, 2) { |i| print i, " " } 1884 * Math::E.step(Math::PI, 0.2) { |f| print f, " " } 1885 * 1886 * <em>produces:</em> 1887 * 1888 * 1 3 5 7 9 1889 * 2.71828182845905 2.91828182845905 3.11828182845905 1890 */ 1891 1892static VALUE 1893num_step(int argc, VALUE *argv, VALUE from) 1894{ 1895 VALUE to, step; 1896 1897 RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size); 1898 if (argc == 1) { 1899 to = argv[0]; 1900 step = INT2FIX(1); 1901 } 1902 else { 1903 rb_check_arity(argc, 1, 2); 1904 to = argv[0]; 1905 step = argv[1]; 1906 if (rb_equal(step, INT2FIX(0))) { 1907 rb_raise(rb_eArgError, "step can't be 0"); 1908 } 1909 } 1910 1911 if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) { 1912 long i, end, diff; 1913 1914 i = FIX2LONG(from); 1915 end = FIX2LONG(to); 1916 diff = FIX2LONG(step); 1917 1918 if (diff > 0) { 1919 while (i <= end) { 1920 rb_yield(LONG2FIX(i)); 1921 i += diff; 1922 } 1923 } 1924 else { 1925 while (i >= end) { 1926 rb_yield(LONG2FIX(i)); 1927 i += diff; 1928 } 1929 } 1930 } 1931 else if (!ruby_float_step(from, to, step, FALSE)) { 1932 VALUE i = from; 1933 ID cmp; 1934 1935 if (positive_int_p(step)) { 1936 cmp = '>'; 1937 } 1938 else { 1939 cmp = '<'; 1940 } 1941 for (;;) { 1942 if (RTEST(rb_funcall(i, cmp, 1, to))) break; 1943 rb_yield(i); 1944 i = rb_funcall(i, '+', 1, step); 1945 } 1946 } 1947 return from; 1948} 1949 1950#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1) 1951#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1)) 1952#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1)) 1953 1954SIGNED_VALUE 1955rb_num2long(VALUE val) 1956{ 1957 again: 1958 if (NIL_P(val)) { 1959 rb_raise(rb_eTypeError, "no implicit conversion from nil to integer"); 1960 } 1961 1962 if (FIXNUM_P(val)) return FIX2LONG(val); 1963 1964 switch (TYPE(val)) { 1965 case T_FLOAT: 1966 if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE 1967 && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) { 1968 return (SIGNED_VALUE)(RFLOAT_VALUE(val)); 1969 } 1970 else { 1971 char buf[24]; 1972 char *s; 1973 1974 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); 1975 if ((s = strchr(buf, ' ')) != 0) *s = '\0'; 1976 rb_raise(rb_eRangeError, "float %s out of range of integer", buf); 1977 } 1978 1979 case T_BIGNUM: 1980 return rb_big2long(val); 1981 1982 default: 1983 val = rb_to_int(val); 1984 goto again; 1985 } 1986} 1987 1988VALUE 1989rb_num2ulong(VALUE val) 1990{ 1991 again: 1992 if (NIL_P(val)) { 1993 rb_raise(rb_eTypeError, "no implicit conversion from nil to integer"); 1994 } 1995 1996 if (FIXNUM_P(val)) return FIX2LONG(val); /* this is FIX2LONG, inteneded */ 1997 1998 switch (TYPE(val)) { 1999 case T_FLOAT: 2000 if (RFLOAT_VALUE(val) < ULONG_MAX_PLUS_ONE 2001 && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) { 2002 return (VALUE)RFLOAT_VALUE(val); 2003 } 2004 else { 2005 char buf[24]; 2006 char *s; 2007 2008 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); 2009 if ((s = strchr(buf, ' ')) != 0) *s = '\0'; 2010 rb_raise(rb_eRangeError, "float %s out of range of integer", buf); 2011 } 2012 2013 case T_BIGNUM: 2014 return rb_big2ulong(val); 2015 2016 default: 2017 val = rb_to_int(val); 2018 goto again; 2019 } 2020} 2021 2022#if SIZEOF_INT < SIZEOF_VALUE 2023void 2024rb_out_of_int(SIGNED_VALUE num) 2025{ 2026 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `int'", 2027 num, num < 0 ? "small" : "big"); 2028} 2029 2030static void 2031check_int(SIGNED_VALUE num) 2032{ 2033 if ((SIGNED_VALUE)(int)num != num) { 2034 rb_out_of_int(num); 2035 } 2036} 2037 2038static void 2039check_uint(VALUE num, int sign) 2040{ 2041 static const VALUE mask = ~(VALUE)UINT_MAX; 2042 2043 if (sign) { 2044 /* minus */ 2045 if ((num & mask) != mask || (num & ~mask) <= INT_MAX) 2046#define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1)) 2047 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned int'", num|VALUE_MSBMASK); 2048 } 2049 else { 2050 /* plus */ 2051 if ((num & mask) != 0) 2052 rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned int'", num); 2053 } 2054} 2055 2056long 2057rb_num2int(VALUE val) 2058{ 2059 long num = rb_num2long(val); 2060 2061 check_int(num); 2062 return num; 2063} 2064 2065long 2066rb_fix2int(VALUE val) 2067{ 2068 long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val); 2069 2070 check_int(num); 2071 return num; 2072} 2073 2074unsigned long 2075rb_num2uint(VALUE val) 2076{ 2077 VALUE num = rb_num2ulong(val); 2078 2079 check_uint(num, negative_int_p(val)); 2080 return (unsigned long)num; 2081} 2082 2083unsigned long 2084rb_fix2uint(VALUE val) 2085{ 2086 unsigned long num; 2087 2088 if (!FIXNUM_P(val)) { 2089 return rb_num2uint(val); 2090 } 2091 num = FIX2ULONG(val); 2092 2093 check_uint(num, negative_int_p(val)); 2094 return num; 2095} 2096#else 2097long 2098rb_num2int(VALUE val) 2099{ 2100 return rb_num2long(val); 2101} 2102 2103long 2104rb_fix2int(VALUE val) 2105{ 2106 return FIX2INT(val); 2107} 2108#endif 2109 2110void 2111rb_out_of_short(SIGNED_VALUE num) 2112{ 2113 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'", 2114 num, num < 0 ? "small" : "big"); 2115} 2116 2117static void 2118check_short(SIGNED_VALUE num) 2119{ 2120 if ((SIGNED_VALUE)(short)num != num) { 2121 rb_out_of_short(num); 2122 } 2123} 2124 2125static void 2126check_ushort(VALUE num, int sign) 2127{ 2128 static const VALUE mask = ~(VALUE)USHRT_MAX; 2129 2130 if (sign) { 2131 /* minus */ 2132 if ((num & mask) != mask || (num & ~mask) <= SHRT_MAX) 2133#define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1)) 2134 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned short'", num|VALUE_MSBMASK); 2135 } 2136 else { 2137 /* plus */ 2138 if ((num & mask) != 0) 2139 rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned short'", num); 2140 } 2141} 2142 2143short 2144rb_num2short(VALUE val) 2145{ 2146 long num = rb_num2long(val); 2147 2148 check_short(num); 2149 return num; 2150} 2151 2152short 2153rb_fix2short(VALUE val) 2154{ 2155 long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val); 2156 2157 check_short(num); 2158 return num; 2159} 2160 2161unsigned short 2162rb_num2ushort(VALUE val) 2163{ 2164 VALUE num = rb_num2ulong(val); 2165 2166 check_ushort(num, negative_int_p(val)); 2167 return (unsigned long)num; 2168} 2169 2170unsigned short 2171rb_fix2ushort(VALUE val) 2172{ 2173 unsigned long num; 2174 2175 if (!FIXNUM_P(val)) { 2176 return rb_num2ushort(val); 2177 } 2178 num = FIX2ULONG(val); 2179 2180 check_ushort(num, negative_int_p(val)); 2181 return num; 2182} 2183 2184VALUE 2185rb_num2fix(VALUE val) 2186{ 2187 SIGNED_VALUE v; 2188 2189 if (FIXNUM_P(val)) return val; 2190 2191 v = rb_num2long(val); 2192 if (!FIXABLE(v)) 2193 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " out of range of fixnum", v); 2194 return LONG2FIX(v); 2195} 2196 2197#if HAVE_LONG_LONG 2198 2199#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1) 2200#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1)) 2201#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1)) 2202#ifndef ULLONG_MAX 2203#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1) 2204#endif 2205 2206LONG_LONG 2207rb_num2ll(VALUE val) 2208{ 2209 if (NIL_P(val)) { 2210 rb_raise(rb_eTypeError, "no implicit conversion from nil"); 2211 } 2212 2213 if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val); 2214 2215 switch (TYPE(val)) { 2216 case T_FLOAT: 2217 if (RFLOAT_VALUE(val) < LLONG_MAX_PLUS_ONE 2218 && RFLOAT_VALUE(val) > LLONG_MIN_MINUS_ONE) { 2219 return (LONG_LONG)(RFLOAT_VALUE(val)); 2220 } 2221 else { 2222 char buf[24]; 2223 char *s; 2224 2225 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); 2226 if ((s = strchr(buf, ' ')) != 0) *s = '\0'; 2227 rb_raise(rb_eRangeError, "float %s out of range of long long", buf); 2228 } 2229 2230 case T_BIGNUM: 2231 return rb_big2ll(val); 2232 2233 case T_STRING: 2234 rb_raise(rb_eTypeError, "no implicit conversion from string"); 2235 break; 2236 2237 case T_TRUE: 2238 case T_FALSE: 2239 rb_raise(rb_eTypeError, "no implicit conversion from boolean"); 2240 break; 2241 2242 default: 2243 break; 2244 } 2245 2246 val = rb_to_int(val); 2247 return NUM2LL(val); 2248} 2249 2250unsigned LONG_LONG 2251rb_num2ull(VALUE val) 2252{ 2253 switch (TYPE(val)) { 2254 case T_NIL: 2255 rb_raise(rb_eTypeError, "no implicit conversion from nil"); 2256 2257 case T_FIXNUM: 2258 return (LONG_LONG)FIX2LONG(val); /* this is FIX2LONG, inteneded */ 2259 2260 case T_FLOAT: 2261 if (RFLOAT_VALUE(val) < ULLONG_MAX_PLUS_ONE 2262 && RFLOAT_VALUE(val) > 0) { 2263 return (unsigned LONG_LONG)(RFLOAT_VALUE(val)); 2264 } 2265 else { 2266 char buf[24]; 2267 char *s; 2268 2269 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); 2270 if ((s = strchr(buf, ' ')) != 0) *s = '\0'; 2271 rb_raise(rb_eRangeError, "float %s out of range of unsgined long long", buf); 2272 } 2273 2274 case T_BIGNUM: 2275 return rb_big2ull(val); 2276 2277 case T_STRING: 2278 rb_raise(rb_eTypeError, "no implicit conversion from string"); 2279 break; 2280 2281 case T_TRUE: 2282 case T_FALSE: 2283 rb_raise(rb_eTypeError, "no implicit conversion from boolean"); 2284 break; 2285 2286 default: 2287 break; 2288 } 2289 2290 val = rb_to_int(val); 2291 return NUM2ULL(val); 2292} 2293 2294#endif /* HAVE_LONG_LONG */ 2295 2296/* 2297 * Document-class: Integer 2298 * 2299 * <code>Integer</code> is the basis for the two concrete classes that 2300 * hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>. 2301 * 2302 */ 2303 2304/* 2305 * call-seq: 2306 * int.to_i -> integer 2307 * int.to_int -> integer 2308 * int.floor -> integer 2309 * int.ceil -> integer 2310 * int.truncate -> integer 2311 * 2312 * As <i>int</i> is already an <code>Integer</code>, all these 2313 * methods simply return the receiver. 2314 */ 2315 2316static VALUE 2317int_to_i(VALUE num) 2318{ 2319 return num; 2320} 2321 2322/* 2323 * call-seq: 2324 * int.integer? -> true 2325 * 2326 * Always returns <code>true</code>. 2327 */ 2328 2329static VALUE 2330int_int_p(VALUE num) 2331{ 2332 return Qtrue; 2333} 2334 2335/* 2336 * call-seq: 2337 * int.odd? -> true or false 2338 * 2339 * Returns <code>true</code> if <i>int</i> is an odd number. 2340 */ 2341 2342static VALUE 2343int_odd_p(VALUE num) 2344{ 2345 if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) { 2346 return Qtrue; 2347 } 2348 return Qfalse; 2349} 2350 2351/* 2352 * call-seq: 2353 * int.even? -> true or false 2354 * 2355 * Returns <code>true</code> if <i>int</i> is an even number. 2356 */ 2357 2358static VALUE 2359int_even_p(VALUE num) 2360{ 2361 if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) { 2362 return Qtrue; 2363 } 2364 return Qfalse; 2365} 2366 2367/* 2368 * call-seq: 2369 * fixnum.next -> integer 2370 * fixnum.succ -> integer 2371 * 2372 * Returns the <code>Integer</code> equal to <i>int</i> + 1. 2373 * 2374 * 1.next #=> 2 2375 * (-1).next #=> 0 2376 */ 2377 2378static VALUE 2379fix_succ(VALUE num) 2380{ 2381 long i = FIX2LONG(num) + 1; 2382 return LONG2NUM(i); 2383} 2384 2385/* 2386 * call-seq: 2387 * int.next -> integer 2388 * int.succ -> integer 2389 * 2390 * Returns the <code>Integer</code> equal to <i>int</i> + 1. 2391 * 2392 * 1.next #=> 2 2393 * (-1).next #=> 0 2394 */ 2395 2396VALUE 2397rb_int_succ(VALUE num) 2398{ 2399 if (FIXNUM_P(num)) { 2400 long i = FIX2LONG(num) + 1; 2401 return LONG2NUM(i); 2402 } 2403 return rb_funcall(num, '+', 1, INT2FIX(1)); 2404} 2405 2406#define int_succ rb_int_succ 2407 2408/* 2409 * call-seq: 2410 * int.pred -> integer 2411 * 2412 * Returns the <code>Integer</code> equal to <i>int</i> - 1. 2413 * 2414 * 1.pred #=> 0 2415 * (-1).pred #=> -2 2416 */ 2417 2418VALUE 2419rb_int_pred(VALUE num) 2420{ 2421 if (FIXNUM_P(num)) { 2422 long i = FIX2LONG(num) - 1; 2423 return LONG2NUM(i); 2424 } 2425 return rb_funcall(num, '-', 1, INT2FIX(1)); 2426} 2427 2428#define int_pred rb_int_pred 2429 2430VALUE 2431rb_enc_uint_chr(unsigned int code, rb_encoding *enc) 2432{ 2433 int n; 2434 VALUE str; 2435 switch (n = rb_enc_codelen(code, enc)) { 2436 case ONIGERR_INVALID_CODE_POINT_VALUE: 2437 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc)); 2438 break; 2439 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE: 2440 case 0: 2441 rb_raise(rb_eRangeError, "%u out of char range", code); 2442 break; 2443 } 2444 str = rb_enc_str_new(0, n, enc); 2445 rb_enc_mbcput(code, RSTRING_PTR(str), enc); 2446 if (rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_END(str), enc) != n) { 2447 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc)); 2448 } 2449 return str; 2450} 2451 2452/* 2453 * call-seq: 2454 * int.chr([encoding]) -> string 2455 * 2456 * Returns a string containing the character represented by the 2457 * receiver's value according to +encoding+. 2458 * 2459 * 65.chr #=> "A" 2460 * 230.chr #=> "\346" 2461 * 255.chr(Encoding::UTF_8) #=> "\303\277" 2462 */ 2463 2464static VALUE 2465int_chr(int argc, VALUE *argv, VALUE num) 2466{ 2467 char c; 2468 unsigned int i; 2469 rb_encoding *enc; 2470 2471 if (rb_num_to_uint(num, &i) == 0) { 2472 } 2473 else if (FIXNUM_P(num)) { 2474 rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num)); 2475 } 2476 else { 2477 rb_raise(rb_eRangeError, "bignum out of char range"); 2478 } 2479 2480 switch (argc) { 2481 case 0: 2482 if (0xff < i) { 2483 enc = rb_default_internal_encoding(); 2484 if (!enc) { 2485 rb_raise(rb_eRangeError, "%d out of char range", i); 2486 } 2487 goto decode; 2488 } 2489 c = (char)i; 2490 if (i < 0x80) { 2491 return rb_usascii_str_new(&c, 1); 2492 } 2493 else { 2494 return rb_str_new(&c, 1); 2495 } 2496 case 1: 2497 break; 2498 default: 2499 rb_check_arity(argc, 0, 1); 2500 break; 2501 } 2502 enc = rb_to_encoding(argv[0]); 2503 if (!enc) enc = rb_ascii8bit_encoding(); 2504 decode: 2505 return rb_enc_uint_chr(i, enc); 2506} 2507 2508/* 2509 * call-seq: 2510 * int.ord -> self 2511 * 2512 * Returns the int itself. 2513 * 2514 * ?a.ord #=> 97 2515 * 2516 * This method is intended for compatibility to 2517 * character constant in Ruby 1.9. 2518 * For example, ?a.ord returns 97 both in 1.8 and 1.9. 2519 */ 2520 2521static VALUE 2522int_ord(VALUE num) 2523{ 2524 return num; 2525} 2526 2527/******************************************************************** 2528 * 2529 * Document-class: Fixnum 2530 * 2531 * A <code>Fixnum</code> holds <code>Integer</code> values that can be 2532 * represented in a native machine word (minus 1 bit). If any operation 2533 * on a <code>Fixnum</code> exceeds this range, the value is 2534 * automatically converted to a <code>Bignum</code>. 2535 * 2536 * <code>Fixnum</code> objects have immediate value. This means that 2537 * when they are assigned or passed as parameters, the actual object is 2538 * passed, rather than a reference to that object. Assignment does not 2539 * alias <code>Fixnum</code> objects. There is effectively only one 2540 * <code>Fixnum</code> object instance for any given integer value, so, 2541 * for example, you cannot add a singleton method to a 2542 * <code>Fixnum</code>. 2543 */ 2544 2545 2546/* 2547 * call-seq: 2548 * -fix -> integer 2549 * 2550 * Negates <code>fix</code> (which might return a Bignum). 2551 */ 2552 2553static VALUE 2554fix_uminus(VALUE num) 2555{ 2556 return LONG2NUM(-FIX2LONG(num)); 2557} 2558 2559VALUE 2560rb_fix2str(VALUE x, int base) 2561{ 2562 extern const char ruby_digitmap[]; 2563 char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf; 2564 long val = FIX2LONG(x); 2565 int neg = 0; 2566 2567 if (base < 2 || 36 < base) { 2568 rb_raise(rb_eArgError, "invalid radix %d", base); 2569 } 2570 if (val == 0) { 2571 return rb_usascii_str_new2("0"); 2572 } 2573 if (val < 0) { 2574 val = -val; 2575 neg = 1; 2576 } 2577 *--b = '\0'; 2578 do { 2579 *--b = ruby_digitmap[(int)(val % base)]; 2580 } while (val /= base); 2581 if (neg) { 2582 *--b = '-'; 2583 } 2584 2585 return rb_usascii_str_new2(b); 2586} 2587 2588/* 2589 * call-seq: 2590 * fix.to_s(base=10) -> string 2591 * 2592 * Returns a string containing the representation of <i>fix</i> radix 2593 * <i>base</i> (between 2 and 36). 2594 * 2595 * 12345.to_s #=> "12345" 2596 * 12345.to_s(2) #=> "11000000111001" 2597 * 12345.to_s(8) #=> "30071" 2598 * 12345.to_s(10) #=> "12345" 2599 * 12345.to_s(16) #=> "3039" 2600 * 12345.to_s(36) #=> "9ix" 2601 * 2602 */ 2603static VALUE 2604fix_to_s(int argc, VALUE *argv, VALUE x) 2605{ 2606 int base; 2607 2608 if (argc == 0) base = 10; 2609 else { 2610 VALUE b; 2611 2612 rb_scan_args(argc, argv, "01", &b); 2613 base = NUM2INT(b); 2614 } 2615 2616 return rb_fix2str(x, base); 2617} 2618 2619/* 2620 * call-seq: 2621 * fix + numeric -> numeric_result 2622 * 2623 * Performs addition: the class of the resulting object depends on 2624 * the class of <code>numeric</code> and on the magnitude of the 2625 * result. 2626 */ 2627 2628static VALUE 2629fix_plus(VALUE x, VALUE y) 2630{ 2631 if (FIXNUM_P(y)) { 2632 long a, b, c; 2633 VALUE r; 2634 2635 a = FIX2LONG(x); 2636 b = FIX2LONG(y); 2637 c = a + b; 2638 r = LONG2NUM(c); 2639 2640 return r; 2641 } 2642 switch (TYPE(y)) { 2643 case T_BIGNUM: 2644 return rb_big_plus(y, x); 2645 case T_FLOAT: 2646 return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y)); 2647 default: 2648 return rb_num_coerce_bin(x, y, '+'); 2649 } 2650} 2651 2652/* 2653 * call-seq: 2654 * fix - numeric -> numeric_result 2655 * 2656 * Performs subtraction: the class of the resulting object depends on 2657 * the class of <code>numeric</code> and on the magnitude of the 2658 * result. 2659 */ 2660 2661static VALUE 2662fix_minus(VALUE x, VALUE y) 2663{ 2664 if (FIXNUM_P(y)) { 2665 long a, b, c; 2666 VALUE r; 2667 2668 a = FIX2LONG(x); 2669 b = FIX2LONG(y); 2670 c = a - b; 2671 r = LONG2NUM(c); 2672 2673 return r; 2674 } 2675 switch (TYPE(y)) { 2676 case T_BIGNUM: 2677 x = rb_int2big(FIX2LONG(x)); 2678 return rb_big_minus(x, y); 2679 case T_FLOAT: 2680 return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y)); 2681 default: 2682 return rb_num_coerce_bin(x, y, '-'); 2683 } 2684} 2685 2686#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2)) 2687/*tests if N*N would overflow*/ 2688#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX)) 2689 2690/* 2691 * call-seq: 2692 * fix * numeric -> numeric_result 2693 * 2694 * Performs multiplication: the class of the resulting object depends on 2695 * the class of <code>numeric</code> and on the magnitude of the 2696 * result. 2697 */ 2698 2699static VALUE 2700fix_mul(VALUE x, VALUE y) 2701{ 2702 if (FIXNUM_P(y)) { 2703#ifdef __HP_cc 2704/* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */ 2705 volatile 2706#endif 2707 long a, b; 2708#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG 2709 LONG_LONG d; 2710#else 2711 VALUE r; 2712#endif 2713 2714 a = FIX2LONG(x); 2715 b = FIX2LONG(y); 2716 2717#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG 2718 d = (LONG_LONG)a * b; 2719 if (FIXABLE(d)) return LONG2FIX(d); 2720 return rb_ll2inum(d); 2721#else 2722 if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b)) 2723 return LONG2FIX(a*b); 2724 if (a == 0) return x; 2725 if (MUL_OVERFLOW_FIXNUM_P(a, b)) 2726 r = rb_big_mul(rb_int2big(a), rb_int2big(b)); 2727 else 2728 r = LONG2FIX(a * b); 2729 return r; 2730#endif 2731 } 2732 switch (TYPE(y)) { 2733 case T_BIGNUM: 2734 return rb_big_mul(y, x); 2735 case T_FLOAT: 2736 return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y)); 2737 default: 2738 return rb_num_coerce_bin(x, y, '*'); 2739 } 2740} 2741 2742static void 2743fixdivmod(long x, long y, long *divp, long *modp) 2744{ 2745 long div, mod; 2746 2747 if (y == 0) rb_num_zerodiv(); 2748 if (y < 0) { 2749 if (x < 0) 2750 div = -x / -y; 2751 else 2752 div = - (x / -y); 2753 } 2754 else { 2755 if (x < 0) 2756 div = - (-x / y); 2757 else 2758 div = x / y; 2759 } 2760 mod = x - div*y; 2761 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) { 2762 mod += y; 2763 div -= 1; 2764 } 2765 if (divp) *divp = div; 2766 if (modp) *modp = mod; 2767} 2768 2769/* 2770 * call-seq: 2771 * fix.fdiv(numeric) -> float 2772 * 2773 * Returns the floating point result of dividing <i>fix</i> by 2774 * <i>numeric</i>. 2775 * 2776 * 654321.fdiv(13731) #=> 47.6528293642124 2777 * 654321.fdiv(13731.24) #=> 47.6519964693647 2778 * 2779 */ 2780 2781static VALUE 2782fix_fdiv(VALUE x, VALUE y) 2783{ 2784 if (FIXNUM_P(y)) { 2785 return DBL2NUM((double)FIX2LONG(x) / (double)FIX2LONG(y)); 2786 } 2787 switch (TYPE(y)) { 2788 case T_BIGNUM: 2789 return rb_big_fdiv(rb_int2big(FIX2LONG(x)), y); 2790 case T_FLOAT: 2791 return DBL2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y)); 2792 default: 2793 return rb_num_coerce_bin(x, y, rb_intern("fdiv")); 2794 } 2795} 2796 2797static VALUE 2798fix_divide(VALUE x, VALUE y, ID op) 2799{ 2800 if (FIXNUM_P(y)) { 2801 long div; 2802 2803 fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0); 2804 return LONG2NUM(div); 2805 } 2806 switch (TYPE(y)) { 2807 case T_BIGNUM: 2808 x = rb_int2big(FIX2LONG(x)); 2809 return rb_big_div(x, y); 2810 case T_FLOAT: 2811 { 2812 double div; 2813 2814 if (op == '/') { 2815 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y); 2816 return DBL2NUM(div); 2817 } 2818 else { 2819 if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv(); 2820 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y); 2821 return rb_dbl2big(floor(div)); 2822 } 2823 } 2824 case T_RATIONAL: 2825 if (op == '/' && FIX2LONG(x) == 1) 2826 return rb_rational_reciprocal(y); 2827 /* fall through */ 2828 default: 2829 return rb_num_coerce_bin(x, y, op); 2830 } 2831} 2832 2833/* 2834 * call-seq: 2835 * fix / numeric -> numeric_result 2836 * 2837 * Performs division: the class of the resulting object depends on 2838 * the class of <code>numeric</code> and on the magnitude of the 2839 * result. 2840 */ 2841 2842static VALUE 2843fix_div(VALUE x, VALUE y) 2844{ 2845 return fix_divide(x, y, '/'); 2846} 2847 2848/* 2849 * call-seq: 2850 * fix.div(numeric) -> integer 2851 * 2852 * Performs integer division: returns integer value. 2853 */ 2854 2855static VALUE 2856fix_idiv(VALUE x, VALUE y) 2857{ 2858 return fix_divide(x, y, rb_intern("div")); 2859} 2860 2861/* 2862 * call-seq: 2863 * fix % other -> real 2864 * fix.modulo(other) -> real 2865 * 2866 * Returns <code>fix</code> modulo <code>other</code>. 2867 * See <code>numeric.divmod</code> for more information. 2868 */ 2869 2870static VALUE 2871fix_mod(VALUE x, VALUE y) 2872{ 2873 if (FIXNUM_P(y)) { 2874 long mod; 2875 2876 fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod); 2877 return LONG2NUM(mod); 2878 } 2879 switch (TYPE(y)) { 2880 case T_BIGNUM: 2881 x = rb_int2big(FIX2LONG(x)); 2882 return rb_big_modulo(x, y); 2883 case T_FLOAT: 2884 return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y))); 2885 default: 2886 return rb_num_coerce_bin(x, y, '%'); 2887 } 2888} 2889 2890/* 2891 * call-seq: 2892 * fix.divmod(numeric) -> array 2893 * 2894 * See <code>Numeric#divmod</code>. 2895 */ 2896static VALUE 2897fix_divmod(VALUE x, VALUE y) 2898{ 2899 if (FIXNUM_P(y)) { 2900 long div, mod; 2901 2902 fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod); 2903 2904 return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod)); 2905 } 2906 switch (TYPE(y)) { 2907 case T_BIGNUM: 2908 x = rb_int2big(FIX2LONG(x)); 2909 return rb_big_divmod(x, y); 2910 case T_FLOAT: 2911 { 2912 double div, mod; 2913 volatile VALUE a, b; 2914 2915 flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod); 2916 a = dbl2ival(div); 2917 b = DBL2NUM(mod); 2918 return rb_assoc_new(a, b); 2919 } 2920 default: 2921 return rb_num_coerce_bin(x, y, rb_intern("divmod")); 2922 } 2923} 2924 2925static VALUE 2926int_pow(long x, unsigned long y) 2927{ 2928 int neg = x < 0; 2929 long z = 1; 2930 2931 if (neg) x = -x; 2932 if (y & 1) 2933 z = x; 2934 else 2935 neg = 0; 2936 y &= ~1; 2937 do { 2938 while (y % 2 == 0) { 2939 if (!FIT_SQRT_LONG(x)) { 2940 VALUE v; 2941 bignum: 2942 v = rb_big_pow(rb_int2big(x), LONG2NUM(y)); 2943 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v); 2944 return v; 2945 } 2946 x = x * x; 2947 y >>= 1; 2948 } 2949 { 2950 if (MUL_OVERFLOW_FIXNUM_P(x, z)) { 2951 goto bignum; 2952 } 2953 z = x * z; 2954 } 2955 } while (--y); 2956 if (neg) z = -z; 2957 return LONG2NUM(z); 2958} 2959 2960/* 2961 * call-seq: 2962 * fix ** numeric -> numeric_result 2963 * 2964 * Raises <code>fix</code> to the <code>numeric</code> power, which may 2965 * be negative or fractional. 2966 * 2967 * 2 ** 3 #=> 8 2968 * 2 ** -1 #=> (1/2) 2969 * 2 ** 0.5 #=> 1.4142135623731 2970 */ 2971 2972static VALUE 2973fix_pow(VALUE x, VALUE y) 2974{ 2975 long a = FIX2LONG(x); 2976 2977 if (FIXNUM_P(y)) { 2978 long b = FIX2LONG(y); 2979 2980 if (a == 1) return INT2FIX(1); 2981 if (a == -1) { 2982 if (b % 2 == 0) 2983 return INT2FIX(1); 2984 else 2985 return INT2FIX(-1); 2986 } 2987 if (b < 0) 2988 return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); 2989 2990 if (b == 0) return INT2FIX(1); 2991 if (b == 1) return x; 2992 if (a == 0) { 2993 if (b > 0) return INT2FIX(0); 2994 return DBL2NUM(INFINITY); 2995 } 2996 return int_pow(a, b); 2997 } 2998 switch (TYPE(y)) { 2999 case T_BIGNUM: 3000 if (a == 1) return INT2FIX(1); 3001 if (a == -1) { 3002 if (int_even_p(y)) return INT2FIX(1); 3003 else return INT2FIX(-1); 3004 } 3005 if (negative_int_p(y)) 3006 return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); 3007 if (a == 0) return INT2FIX(0); 3008 x = rb_int2big(FIX2LONG(x)); 3009 return rb_big_pow(x, y); 3010 case T_FLOAT: 3011 if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0); 3012 if (a == 0) { 3013 return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0); 3014 } 3015 if (a == 1) return DBL2NUM(1.0); 3016 { 3017 double dy = RFLOAT_VALUE(y); 3018 if (a < 0 && dy != round(dy)) 3019 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y); 3020 return DBL2NUM(pow((double)a, dy)); 3021 } 3022 default: 3023 return rb_num_coerce_bin(x, y, rb_intern("**")); 3024 } 3025} 3026 3027/* 3028 * call-seq: 3029 * fix == other -> true or false 3030 * 3031 * Return <code>true</code> if <code>fix</code> equals <code>other</code> 3032 * numerically. 3033 * 3034 * 1 == 2 #=> false 3035 * 1 == 1.0 #=> true 3036 */ 3037 3038static VALUE 3039fix_equal(VALUE x, VALUE y) 3040{ 3041 if (x == y) return Qtrue; 3042 if (FIXNUM_P(y)) return Qfalse; 3043 switch (TYPE(y)) { 3044 case T_BIGNUM: 3045 return rb_big_eq(y, x); 3046 case T_FLOAT: 3047 return rb_integer_float_eq(x, y); 3048 default: 3049 return num_equal(x, y); 3050 } 3051} 3052 3053/* 3054 * call-seq: 3055 * fix <=> numeric -> -1, 0, +1 or nil 3056 * 3057 * Comparison---Returns -1, 0, +1 or nil depending on whether +fix+ is less 3058 * than, equal to, or greater than +numeric+. This is the basis for the tests 3059 * in Comparable. 3060 * 3061 * +nil+ is returned if the two values are incomparable. 3062 */ 3063 3064static VALUE 3065fix_cmp(VALUE x, VALUE y) 3066{ 3067 if (x == y) return INT2FIX(0); 3068 if (FIXNUM_P(y)) { 3069 if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1); 3070 return INT2FIX(-1); 3071 } 3072 switch (TYPE(y)) { 3073 case T_BIGNUM: 3074 return rb_big_cmp(rb_int2big(FIX2LONG(x)), y); 3075 case T_FLOAT: 3076 return rb_integer_float_cmp(x, y); 3077 default: 3078 return rb_num_coerce_cmp(x, y, rb_intern("<=>")); 3079 } 3080} 3081 3082/* 3083 * call-seq: 3084 * fix > real -> true or false 3085 * 3086 * Returns <code>true</code> if the value of <code>fix</code> is 3087 * greater than that of <code>real</code>. 3088 */ 3089 3090static VALUE 3091fix_gt(VALUE x, VALUE y) 3092{ 3093 if (FIXNUM_P(y)) { 3094 if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue; 3095 return Qfalse; 3096 } 3097 switch (TYPE(y)) { 3098 case T_BIGNUM: 3099 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse; 3100 case T_FLOAT: 3101 return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse; 3102 default: 3103 return rb_num_coerce_relop(x, y, '>'); 3104 } 3105} 3106 3107/* 3108 * call-seq: 3109 * fix >= real -> true or false 3110 * 3111 * Returns <code>true</code> if the value of <code>fix</code> is 3112 * greater than or equal to that of <code>real</code>. 3113 */ 3114 3115static VALUE 3116fix_ge(VALUE x, VALUE y) 3117{ 3118 if (FIXNUM_P(y)) { 3119 if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue; 3120 return Qfalse; 3121 } 3122 switch (TYPE(y)) { 3123 case T_BIGNUM: 3124 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse; 3125 case T_FLOAT: 3126 { 3127 VALUE rel = rb_integer_float_cmp(x, y); 3128 return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse; 3129 } 3130 default: 3131 return rb_num_coerce_relop(x, y, rb_intern(">=")); 3132 } 3133} 3134 3135/* 3136 * call-seq: 3137 * fix < real -> true or false 3138 * 3139 * Returns <code>true</code> if the value of <code>fix</code> is 3140 * less than that of <code>real</code>. 3141 */ 3142 3143static VALUE 3144fix_lt(VALUE x, VALUE y) 3145{ 3146 if (FIXNUM_P(y)) { 3147 if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue; 3148 return Qfalse; 3149 } 3150 switch (TYPE(y)) { 3151 case T_BIGNUM: 3152 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse; 3153 case T_FLOAT: 3154 return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse; 3155 default: 3156 return rb_num_coerce_relop(x, y, '<'); 3157 } 3158} 3159 3160/* 3161 * call-seq: 3162 * fix <= real -> true or false 3163 * 3164 * Returns <code>true</code> if the value of <code>fix</code> is 3165 * less than or equal to that of <code>real</code>. 3166 */ 3167 3168static VALUE 3169fix_le(VALUE x, VALUE y) 3170{ 3171 if (FIXNUM_P(y)) { 3172 if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue; 3173 return Qfalse; 3174 } 3175 switch (TYPE(y)) { 3176 case T_BIGNUM: 3177 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse; 3178 case T_FLOAT: 3179 { 3180 VALUE rel = rb_integer_float_cmp(x, y); 3181 return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse; 3182 } 3183 default: 3184 return rb_num_coerce_relop(x, y, rb_intern("<=")); 3185 } 3186} 3187 3188/* 3189 * call-seq: 3190 * ~fix -> integer 3191 * 3192 * One's complement: returns a number where each bit is flipped. 3193 */ 3194 3195static VALUE 3196fix_rev(VALUE num) 3197{ 3198 return ~num | FIXNUM_FLAG; 3199} 3200 3201static int 3202bit_coerce(VALUE *x, VALUE *y, int err) 3203{ 3204 if (!FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) { 3205 do_coerce(x, y, err); 3206 if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM) 3207 && !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) { 3208 if (!err) return FALSE; 3209 coerce_failed(*x, *y); 3210 } 3211 } 3212 return TRUE; 3213} 3214 3215VALUE 3216rb_num_coerce_bit(VALUE x, VALUE y, ID func) 3217{ 3218 bit_coerce(&x, &y, TRUE); 3219 return rb_funcall(x, func, 1, y); 3220} 3221 3222/* 3223 * call-seq: 3224 * fix & integer -> integer_result 3225 * 3226 * Bitwise AND. 3227 */ 3228 3229static VALUE 3230fix_and(VALUE x, VALUE y) 3231{ 3232 if (FIXNUM_P(y)) { 3233 long val = FIX2LONG(x) & FIX2LONG(y); 3234 return LONG2NUM(val); 3235 } 3236 3237 if (RB_TYPE_P(y, T_BIGNUM)) { 3238 return rb_big_and(y, x); 3239 } 3240 3241 bit_coerce(&x, &y, TRUE); 3242 return rb_funcall(x, rb_intern("&"), 1, y); 3243} 3244 3245/* 3246 * call-seq: 3247 * fix | integer -> integer_result 3248 * 3249 * Bitwise OR. 3250 */ 3251 3252static VALUE 3253fix_or(VALUE x, VALUE y) 3254{ 3255 if (FIXNUM_P(y)) { 3256 long val = FIX2LONG(x) | FIX2LONG(y); 3257 return LONG2NUM(val); 3258 } 3259 3260 if (RB_TYPE_P(y, T_BIGNUM)) { 3261 return rb_big_or(y, x); 3262 } 3263 3264 bit_coerce(&x, &y, TRUE); 3265 return rb_funcall(x, rb_intern("|"), 1, y); 3266} 3267 3268/* 3269 * call-seq: 3270 * fix ^ integer -> integer_result 3271 * 3272 * Bitwise EXCLUSIVE OR. 3273 */ 3274 3275static VALUE 3276fix_xor(VALUE x, VALUE y) 3277{ 3278 if (FIXNUM_P(y)) { 3279 long val = FIX2LONG(x) ^ FIX2LONG(y); 3280 return LONG2NUM(val); 3281 } 3282 3283 if (RB_TYPE_P(y, T_BIGNUM)) { 3284 return rb_big_xor(y, x); 3285 } 3286 3287 bit_coerce(&x, &y, TRUE); 3288 return rb_funcall(x, rb_intern("^"), 1, y); 3289} 3290 3291static VALUE fix_lshift(long, unsigned long); 3292static VALUE fix_rshift(long, unsigned long); 3293 3294/* 3295 * call-seq: 3296 * fix << count -> integer 3297 * 3298 * Shifts _fix_ left _count_ positions (right if _count_ is negative). 3299 */ 3300 3301static VALUE 3302rb_fix_lshift(VALUE x, VALUE y) 3303{ 3304 long val, width; 3305 3306 val = NUM2LONG(x); 3307 if (!FIXNUM_P(y)) 3308 return rb_big_lshift(rb_int2big(val), y); 3309 width = FIX2LONG(y); 3310 if (width < 0) 3311 return fix_rshift(val, (unsigned long)-width); 3312 return fix_lshift(val, width); 3313} 3314 3315static VALUE 3316fix_lshift(long val, unsigned long width) 3317{ 3318 if (width > (SIZEOF_LONG*CHAR_BIT-1) 3319 || ((unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) { 3320 return rb_big_lshift(rb_int2big(val), ULONG2NUM(width)); 3321 } 3322 val = val << width; 3323 return LONG2NUM(val); 3324} 3325 3326/* 3327 * call-seq: 3328 * fix >> count -> integer 3329 * 3330 * Shifts _fix_ right _count_ positions (left if _count_ is negative). 3331 */ 3332 3333static VALUE 3334rb_fix_rshift(VALUE x, VALUE y) 3335{ 3336 long i, val; 3337 3338 val = FIX2LONG(x); 3339 if (!FIXNUM_P(y)) 3340 return rb_big_rshift(rb_int2big(val), y); 3341 i = FIX2LONG(y); 3342 if (i == 0) return x; 3343 if (i < 0) 3344 return fix_lshift(val, (unsigned long)-i); 3345 return fix_rshift(val, i); 3346} 3347 3348static VALUE 3349fix_rshift(long val, unsigned long i) 3350{ 3351 if (i >= sizeof(long)*CHAR_BIT-1) { 3352 if (val < 0) return INT2FIX(-1); 3353 return INT2FIX(0); 3354 } 3355 val = RSHIFT(val, i); 3356 return LONG2FIX(val); 3357} 3358 3359/* 3360 * call-seq: 3361 * fix[n] -> 0, 1 3362 * 3363 * Bit Reference---Returns the <em>n</em>th bit in the binary 3364 * representation of <i>fix</i>, where <i>fix</i>[0] is the least 3365 * significant bit. 3366 * 3367 * a = 0b11001100101010 3368 * 30.downto(0) do |n| print a[n] end 3369 * 3370 * <em>produces:</em> 3371 * 3372 * 0000000000000000011001100101010 3373 */ 3374 3375static VALUE 3376fix_aref(VALUE fix, VALUE idx) 3377{ 3378 long val = FIX2LONG(fix); 3379 long i; 3380 3381 idx = rb_to_int(idx); 3382 if (!FIXNUM_P(idx)) { 3383 idx = rb_big_norm(idx); 3384 if (!FIXNUM_P(idx)) { 3385 if (!RBIGNUM_SIGN(idx) || val >= 0) 3386 return INT2FIX(0); 3387 return INT2FIX(1); 3388 } 3389 } 3390 i = FIX2LONG(idx); 3391 3392 if (i < 0) return INT2FIX(0); 3393 if (SIZEOF_LONG*CHAR_BIT-1 < i) { 3394 if (val < 0) return INT2FIX(1); 3395 return INT2FIX(0); 3396 } 3397 if (val & (1L<<i)) 3398 return INT2FIX(1); 3399 return INT2FIX(0); 3400} 3401 3402/* 3403 * call-seq: 3404 * fix.to_f -> float 3405 * 3406 * Converts <i>fix</i> to a <code>Float</code>. 3407 * 3408 */ 3409 3410static VALUE 3411fix_to_f(VALUE num) 3412{ 3413 double val; 3414 3415 val = (double)FIX2LONG(num); 3416 3417 return DBL2NUM(val); 3418} 3419 3420/* 3421 * call-seq: 3422 * fix.abs -> integer 3423 * fix.magnitude -> integer 3424 * 3425 * Returns the absolute value of <i>fix</i>. 3426 * 3427 * -12345.abs #=> 12345 3428 * 12345.abs #=> 12345 3429 * 3430 */ 3431 3432static VALUE 3433fix_abs(VALUE fix) 3434{ 3435 long i = FIX2LONG(fix); 3436 3437 if (i < 0) i = -i; 3438 3439 return LONG2NUM(i); 3440} 3441 3442 3443 3444/* 3445 * call-seq: 3446 * fix.size -> fixnum 3447 * 3448 * Returns the number of <em>bytes</em> in the machine representation 3449 * of a <code>Fixnum</code>. 3450 * 3451 * 1.size #=> 4 3452 * -1.size #=> 4 3453 * 2147483647.size #=> 4 3454 */ 3455 3456static VALUE 3457fix_size(VALUE fix) 3458{ 3459 return INT2FIX(sizeof(long)); 3460} 3461 3462static VALUE 3463int_upto_size(VALUE from, VALUE args) 3464{ 3465 return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(1), FALSE); 3466} 3467 3468/* 3469 * call-seq: 3470 * int.upto(limit) {|i| block } -> self 3471 * int.upto(limit) -> an_enumerator 3472 * 3473 * Iterates <em>block</em>, passing in integer values from <i>int</i> 3474 * up to and including <i>limit</i>. 3475 * 3476 * If no block is given, an enumerator is returned instead. 3477 * 3478 * 5.upto(10) { |i| print i, " " } 3479 * 3480 * <em>produces:</em> 3481 * 3482 * 5 6 7 8 9 10 3483 */ 3484 3485static VALUE 3486int_upto(VALUE from, VALUE to) 3487{ 3488 RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size); 3489 if (FIXNUM_P(from) && FIXNUM_P(to)) { 3490 long i, end; 3491 3492 end = FIX2LONG(to); 3493 for (i = FIX2LONG(from); i <= end; i++) { 3494 rb_yield(LONG2FIX(i)); 3495 } 3496 } 3497 else { 3498 VALUE i = from, c; 3499 3500 while (!(c = rb_funcall(i, '>', 1, to))) { 3501 rb_yield(i); 3502 i = rb_funcall(i, '+', 1, INT2FIX(1)); 3503 } 3504 if (NIL_P(c)) rb_cmperr(i, to); 3505 } 3506 return from; 3507} 3508 3509static VALUE 3510int_downto_size(VALUE from, VALUE args) 3511{ 3512 return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(-1), FALSE); 3513} 3514 3515/* 3516 * call-seq: 3517 * int.downto(limit) {|i| block } -> self 3518 * int.downto(limit) -> an_enumerator 3519 * 3520 * Iterates <em>block</em>, passing decreasing values from <i>int</i> 3521 * down to and including <i>limit</i>. 3522 * 3523 * If no block is given, an enumerator is returned instead. 3524 * 3525 * 5.downto(1) { |n| print n, ".. " } 3526 * print " Liftoff!\n" 3527 * 3528 * <em>produces:</em> 3529 * 3530 * 5.. 4.. 3.. 2.. 1.. Liftoff! 3531 */ 3532 3533static VALUE 3534int_downto(VALUE from, VALUE to) 3535{ 3536 RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size); 3537 if (FIXNUM_P(from) && FIXNUM_P(to)) { 3538 long i, end; 3539 3540 end = FIX2LONG(to); 3541 for (i=FIX2LONG(from); i >= end; i--) { 3542 rb_yield(LONG2FIX(i)); 3543 } 3544 } 3545 else { 3546 VALUE i = from, c; 3547 3548 while (!(c = rb_funcall(i, '<', 1, to))) { 3549 rb_yield(i); 3550 i = rb_funcall(i, '-', 1, INT2FIX(1)); 3551 } 3552 if (NIL_P(c)) rb_cmperr(i, to); 3553 } 3554 return from; 3555} 3556 3557static VALUE 3558int_dotimes_size(VALUE num) 3559{ 3560 if (FIXNUM_P(num)) { 3561 if (NUM2LONG(num) <= 0) return INT2FIX(0); 3562 } 3563 else { 3564 if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0); 3565 } 3566 return num; 3567} 3568 3569/* 3570 * call-seq: 3571 * int.times {|i| block } -> self 3572 * int.times -> an_enumerator 3573 * 3574 * Iterates block <i>int</i> times, passing in values from zero to 3575 * <i>int</i> - 1. 3576 * 3577 * If no block is given, an enumerator is returned instead. 3578 * 3579 * 5.times do |i| 3580 * print i, " " 3581 * end 3582 * 3583 * <em>produces:</em> 3584 * 3585 * 0 1 2 3 4 3586 */ 3587 3588static VALUE 3589int_dotimes(VALUE num) 3590{ 3591 RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size); 3592 3593 if (FIXNUM_P(num)) { 3594 long i, end; 3595 3596 end = FIX2LONG(num); 3597 for (i=0; i<end; i++) { 3598 rb_yield(LONG2FIX(i)); 3599 } 3600 } 3601 else { 3602 VALUE i = INT2FIX(0); 3603 3604 for (;;) { 3605 if (!RTEST(rb_funcall(i, '<', 1, num))) break; 3606 rb_yield(i); 3607 i = rb_funcall(i, '+', 1, INT2FIX(1)); 3608 } 3609 } 3610 return num; 3611} 3612 3613/* 3614 * call-seq: 3615 * int.round([ndigits]) -> integer or float 3616 * 3617 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits). 3618 * Precision may be negative. Returns a floating point number when +ndigits+ 3619 * is positive, +self+ for zero, and round down for negative. 3620 * 3621 * 1.round #=> 1 3622 * 1.round(2) #=> 1.0 3623 * 15.round(-1) #=> 20 3624 */ 3625 3626static VALUE 3627int_round(int argc, VALUE* argv, VALUE num) 3628{ 3629 VALUE n; 3630 int ndigits; 3631 3632 if (argc == 0) return num; 3633 rb_scan_args(argc, argv, "1", &n); 3634 ndigits = NUM2INT(n); 3635 if (ndigits > 0) { 3636 return rb_Float(num); 3637 } 3638 if (ndigits == 0) { 3639 return num; 3640 } 3641 return int_round_0(num, ndigits); 3642} 3643 3644/* 3645 * call-seq: 3646 * fix.zero? -> true or false 3647 * 3648 * Returns <code>true</code> if <i>fix</i> is zero. 3649 * 3650 */ 3651 3652static VALUE 3653fix_zero_p(VALUE num) 3654{ 3655 if (FIX2LONG(num) == 0) { 3656 return Qtrue; 3657 } 3658 return Qfalse; 3659} 3660 3661/* 3662 * call-seq: 3663 * fix.odd? -> true or false 3664 * 3665 * Returns <code>true</code> if <i>fix</i> is an odd number. 3666 */ 3667 3668static VALUE 3669fix_odd_p(VALUE num) 3670{ 3671 if (num & 2) { 3672 return Qtrue; 3673 } 3674 return Qfalse; 3675} 3676 3677/* 3678 * call-seq: 3679 * fix.even? -> true or false 3680 * 3681 * Returns <code>true</code> if <i>fix</i> is an even number. 3682 */ 3683 3684static VALUE 3685fix_even_p(VALUE num) 3686{ 3687 if (num & 2) { 3688 return Qfalse; 3689 } 3690 return Qtrue; 3691} 3692 3693/* 3694 * Document-class: ZeroDivisionError 3695 * 3696 * Raised when attempting to divide an integer by 0. 3697 * 3698 * 42 / 0 3699 * 3700 * <em>raises the exception:</em> 3701 * 3702 * ZeroDivisionError: divided by 0 3703 * 3704 * Note that only division by an exact 0 will raise that exception: 3705 * 3706 * 42 / 0.0 #=> Float::INFINITY 3707 * 42 / -0.0 #=> -Float::INFINITY 3708 * 0 / 0.0 #=> NaN 3709 */ 3710 3711/* 3712 * Document-class: FloatDomainError 3713 * 3714 * Raised when attempting to convert special float values 3715 * (in particular infinite or NaN) 3716 * to numerical classes which don't support them. 3717 * 3718 * Float::INFINITY.to_r 3719 * 3720 * <em>raises the exception:</em> 3721 * 3722 * FloatDomainError: Infinity 3723 */ 3724 3725void 3726Init_Numeric(void) 3727{ 3728#undef rb_intern 3729#define rb_intern(str) rb_intern_const(str) 3730 3731#if defined(__FreeBSD__) && __FreeBSD__ < 4 3732 /* allow divide by zero -- Inf */ 3733 fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL)); 3734#elif defined(_UNICOSMP) 3735 /* Turn off floating point exceptions for divide by zero, etc. */ 3736 _set_Creg(0, 0); 3737#elif defined(__BORLANDC__) 3738 /* Turn off floating point exceptions for overflow, etc. */ 3739 _control87(MCW_EM, MCW_EM); 3740 _control87(_control87(0,0),0x1FFF); 3741#endif 3742 id_coerce = rb_intern("coerce"); 3743 id_to_i = rb_intern("to_i"); 3744 id_eq = rb_intern("=="); 3745 id_div = rb_intern("div"); 3746 3747 rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError); 3748 rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError); 3749 rb_cNumeric = rb_define_class("Numeric", rb_cObject); 3750 3751 rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1); 3752 rb_include_module(rb_cNumeric, rb_mComparable); 3753 rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1); 3754 rb_define_method(rb_cNumeric, "coerce", num_coerce, 1); 3755 3756 rb_define_method(rb_cNumeric, "i", num_imaginary, 0); 3757 rb_define_method(rb_cNumeric, "+@", num_uplus, 0); 3758 rb_define_method(rb_cNumeric, "-@", num_uminus, 0); 3759 rb_define_method(rb_cNumeric, "<=>", num_cmp, 1); 3760 rb_define_method(rb_cNumeric, "eql?", num_eql, 1); 3761 rb_define_method(rb_cNumeric, "quo", num_quo, 1); 3762 rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1); 3763 rb_define_method(rb_cNumeric, "div", num_div, 1); 3764 rb_define_method(rb_cNumeric, "divmod", num_divmod, 1); 3765 rb_define_method(rb_cNumeric, "%", num_modulo, 1); 3766 rb_define_method(rb_cNumeric, "modulo", num_modulo, 1); 3767 rb_define_method(rb_cNumeric, "remainder", num_remainder, 1); 3768 rb_define_method(rb_cNumeric, "abs", num_abs, 0); 3769 rb_define_method(rb_cNumeric, "magnitude", num_abs, 0); 3770 rb_define_method(rb_cNumeric, "to_int", num_to_int, 0); 3771 3772 rb_define_method(rb_cNumeric, "real?", num_real_p, 0); 3773 rb_define_method(rb_cNumeric, "integer?", num_int_p, 0); 3774 rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0); 3775 rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0); 3776 3777 rb_define_method(rb_cNumeric, "floor", num_floor, 0); 3778 rb_define_method(rb_cNumeric, "ceil", num_ceil, 0); 3779 rb_define_method(rb_cNumeric, "round", num_round, -1); 3780 rb_define_method(rb_cNumeric, "truncate", num_truncate, 0); 3781 rb_define_method(rb_cNumeric, "step", num_step, -1); 3782 3783 rb_cInteger = rb_define_class("Integer", rb_cNumeric); 3784 rb_undef_alloc_func(rb_cInteger); 3785 rb_undef_method(CLASS_OF(rb_cInteger), "new"); 3786 3787 rb_define_method(rb_cInteger, "integer?", int_int_p, 0); 3788 rb_define_method(rb_cInteger, "odd?", int_odd_p, 0); 3789 rb_define_method(rb_cInteger, "even?", int_even_p, 0); 3790 rb_define_method(rb_cInteger, "upto", int_upto, 1); 3791 rb_define_method(rb_cInteger, "downto", int_downto, 1); 3792 rb_define_method(rb_cInteger, "times", int_dotimes, 0); 3793 rb_define_method(rb_cInteger, "succ", int_succ, 0); 3794 rb_define_method(rb_cInteger, "next", int_succ, 0); 3795 rb_define_method(rb_cInteger, "pred", int_pred, 0); 3796 rb_define_method(rb_cInteger, "chr", int_chr, -1); 3797 rb_define_method(rb_cInteger, "ord", int_ord, 0); 3798 rb_define_method(rb_cInteger, "to_i", int_to_i, 0); 3799 rb_define_method(rb_cInteger, "to_int", int_to_i, 0); 3800 rb_define_method(rb_cInteger, "floor", int_to_i, 0); 3801 rb_define_method(rb_cInteger, "ceil", int_to_i, 0); 3802 rb_define_method(rb_cInteger, "truncate", int_to_i, 0); 3803 rb_define_method(rb_cInteger, "round", int_round, -1); 3804 3805 rb_cFixnum = rb_define_class("Fixnum", rb_cInteger); 3806 3807 rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1); 3808 rb_define_alias(rb_cFixnum, "inspect", "to_s"); 3809 3810 rb_define_method(rb_cFixnum, "-@", fix_uminus, 0); 3811 rb_define_method(rb_cFixnum, "+", fix_plus, 1); 3812 rb_define_method(rb_cFixnum, "-", fix_minus, 1); 3813 rb_define_method(rb_cFixnum, "*", fix_mul, 1); 3814 rb_define_method(rb_cFixnum, "/", fix_div, 1); 3815 rb_define_method(rb_cFixnum, "div", fix_idiv, 1); 3816 rb_define_method(rb_cFixnum, "%", fix_mod, 1); 3817 rb_define_method(rb_cFixnum, "modulo", fix_mod, 1); 3818 rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1); 3819 rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1); 3820 rb_define_method(rb_cFixnum, "**", fix_pow, 1); 3821 3822 rb_define_method(rb_cFixnum, "abs", fix_abs, 0); 3823 rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0); 3824 3825 rb_define_method(rb_cFixnum, "==", fix_equal, 1); 3826 rb_define_method(rb_cFixnum, "===", fix_equal, 1); 3827 rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1); 3828 rb_define_method(rb_cFixnum, ">", fix_gt, 1); 3829 rb_define_method(rb_cFixnum, ">=", fix_ge, 1); 3830 rb_define_method(rb_cFixnum, "<", fix_lt, 1); 3831 rb_define_method(rb_cFixnum, "<=", fix_le, 1); 3832 3833 rb_define_method(rb_cFixnum, "~", fix_rev, 0); 3834 rb_define_method(rb_cFixnum, "&", fix_and, 1); 3835 rb_define_method(rb_cFixnum, "|", fix_or, 1); 3836 rb_define_method(rb_cFixnum, "^", fix_xor, 1); 3837 rb_define_method(rb_cFixnum, "[]", fix_aref, 1); 3838 3839 rb_define_method(rb_cFixnum, "<<", rb_fix_lshift, 1); 3840 rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1); 3841 3842 rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0); 3843 rb_define_method(rb_cFixnum, "size", fix_size, 0); 3844 rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0); 3845 rb_define_method(rb_cFixnum, "odd?", fix_odd_p, 0); 3846 rb_define_method(rb_cFixnum, "even?", fix_even_p, 0); 3847 rb_define_method(rb_cFixnum, "succ", fix_succ, 0); 3848 3849 rb_cFloat = rb_define_class("Float", rb_cNumeric); 3850 3851 rb_undef_alloc_func(rb_cFloat); 3852 rb_undef_method(CLASS_OF(rb_cFloat), "new"); 3853 3854 /* 3855 * Represents the rounding mode for floating point addition. 3856 * 3857 * Usually defaults to 1, rounding to the nearest number. 3858 * 3859 * Other modes include: 3860 * 3861 * -1:: Indeterminable 3862 * 0:: Rounding towards zero 3863 * 1:: Rounding to the nearest number 3864 * 2:: Rounding towards positive infinity 3865 * 3:: Rounding towards negative infinity 3866 */ 3867 rb_define_const(rb_cFloat, "ROUNDS", INT2FIX(FLT_ROUNDS)); 3868 /* 3869 * The base of the floating point, or number of unique digits used to 3870 * represent the number. 3871 * 3872 * Usually defaults to 2 on most systems, which would represent a base-10 decimal. 3873 */ 3874 rb_define_const(rb_cFloat, "RADIX", INT2FIX(FLT_RADIX)); 3875 /* 3876 * The number of base digits for the +double+ data type. 3877 * 3878 * Usually defaults to 53. 3879 */ 3880 rb_define_const(rb_cFloat, "MANT_DIG", INT2FIX(DBL_MANT_DIG)); 3881 /* 3882 * The number of decimal digits in a double-precision floating point. 3883 * 3884 * Usually defaults to 15. 3885 */ 3886 rb_define_const(rb_cFloat, "DIG", INT2FIX(DBL_DIG)); 3887 /* 3888 * The smallest posable exponent value in a double-precision floating 3889 * point. 3890 * 3891 * Usually defaults to -1021. 3892 */ 3893 rb_define_const(rb_cFloat, "MIN_EXP", INT2FIX(DBL_MIN_EXP)); 3894 /* 3895 * The largest possible exponent value in a double-precision floating 3896 * point. 3897 * 3898 * Usually defaults to 1024. 3899 */ 3900 rb_define_const(rb_cFloat, "MAX_EXP", INT2FIX(DBL_MAX_EXP)); 3901 /* 3902 * The smallest negative exponent in a double-precision floating point 3903 * where 10 raised to this power minus 1. 3904 * 3905 * Usually defaults to -307. 3906 */ 3907 rb_define_const(rb_cFloat, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP)); 3908 /* 3909 * The largest positive exponent in a double-precision floating point where 3910 * 10 raised to this power minus 1. 3911 * 3912 * Usually defaults to 308. 3913 */ 3914 rb_define_const(rb_cFloat, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP)); 3915 /* 3916 * The smallest positive integer in a double-precision floating point. 3917 * 3918 * Usually defaults to 2.2250738585072014e-308. 3919 */ 3920 rb_define_const(rb_cFloat, "MIN", DBL2NUM(DBL_MIN)); 3921 /* 3922 * The largest possible integer in a double-precision floating point number. 3923 * 3924 * Usually defaults to 1.7976931348623157e+308. 3925 */ 3926 rb_define_const(rb_cFloat, "MAX", DBL2NUM(DBL_MAX)); 3927 /* 3928 * The difference between 1 and the smallest double-precision floating 3929 * point number. 3930 * 3931 * Usually defaults to 2.2204460492503131e-16. 3932 */ 3933 rb_define_const(rb_cFloat, "EPSILON", DBL2NUM(DBL_EPSILON)); 3934 /* 3935 * An expression representing positive infinity. 3936 */ 3937 rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY)); 3938 /* 3939 * An expression representing a value which is "not a number". 3940 */ 3941 rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN)); 3942 3943 rb_define_method(rb_cFloat, "to_s", flo_to_s, 0); 3944 rb_define_alias(rb_cFloat, "inspect", "to_s"); 3945 rb_define_method(rb_cFloat, "coerce", flo_coerce, 1); 3946 rb_define_method(rb_cFloat, "-@", flo_uminus, 0); 3947 rb_define_method(rb_cFloat, "+", flo_plus, 1); 3948 rb_define_method(rb_cFloat, "-", flo_minus, 1); 3949 rb_define_method(rb_cFloat, "*", flo_mul, 1); 3950 rb_define_method(rb_cFloat, "/", flo_div, 1); 3951 rb_define_method(rb_cFloat, "quo", flo_quo, 1); 3952 rb_define_method(rb_cFloat, "fdiv", flo_quo, 1); 3953 rb_define_method(rb_cFloat, "%", flo_mod, 1); 3954 rb_define_method(rb_cFloat, "modulo", flo_mod, 1); 3955 rb_define_method(rb_cFloat, "divmod", flo_divmod, 1); 3956 rb_define_method(rb_cFloat, "**", flo_pow, 1); 3957 rb_define_method(rb_cFloat, "==", flo_eq, 1); 3958 rb_define_method(rb_cFloat, "===", flo_eq, 1); 3959 rb_define_method(rb_cFloat, "<=>", flo_cmp, 1); 3960 rb_define_method(rb_cFloat, ">", flo_gt, 1); 3961 rb_define_method(rb_cFloat, ">=", flo_ge, 1); 3962 rb_define_method(rb_cFloat, "<", flo_lt, 1); 3963 rb_define_method(rb_cFloat, "<=", flo_le, 1); 3964 rb_define_method(rb_cFloat, "eql?", flo_eql, 1); 3965 rb_define_method(rb_cFloat, "hash", flo_hash, 0); 3966 rb_define_method(rb_cFloat, "to_f", flo_to_f, 0); 3967 rb_define_method(rb_cFloat, "abs", flo_abs, 0); 3968 rb_define_method(rb_cFloat, "magnitude", flo_abs, 0); 3969 rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0); 3970 3971 rb_define_method(rb_cFloat, "to_i", flo_truncate, 0); 3972 rb_define_method(rb_cFloat, "to_int", flo_truncate, 0); 3973 rb_define_method(rb_cFloat, "floor", flo_floor, 0); 3974 rb_define_method(rb_cFloat, "ceil", flo_ceil, 0); 3975 rb_define_method(rb_cFloat, "round", flo_round, -1); 3976 rb_define_method(rb_cFloat, "truncate", flo_truncate, 0); 3977 3978 rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0); 3979 rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0); 3980 rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0); 3981} 3982