Class: Decimal

Inherits:
Numeric
  • Object
show all
Defined in:
decimal.c

Defined Under Namespace

Classes: ArithmeticError, DomainError

Constant Summary collapse

INFINITY =

represents +Infinity.

VALUE_PINF
NAN =

represents NaN. (Not a Number)

VALUE_NaN
ROUND_CEILING =
ROUND_CEILING
ROUND_DOWN =
ROUND_DOWN
ROUND_FLOOR =
ROUND_FLOOR
ROUND_HALF_DOWN =
ROUND_HALF_DOWN
ROUND_HALF_EVEN =
ROUND_HALF_EVEN
ROUND_HALF_UP =
ROUND_HALF_UP
ROUND_UP =
ROUND_UP
ROUND_UNNECESSARY =
ROUND_UNNECESSARY

Instance Method Summary collapse

Constructor Details

#new(arg) ⇒ Object

Returns a new decimal made from arg. The arg must be an Integer or a String. An acceptable format of String is equal to Kernel.Float()‘s one. In a Regexp, it should be:

digits  = /(\d+_)*\d+/
number  = /(\+-)?#{digits}/
body    = /#{number}(\.#{digits})?([eE]#{number})?/
decimal = /\A\s*#{body}\s*\z/

And its samples are:

Decimal(1)                  #=> Decimal(1)
Decimal(2**64)              #=> Decimal(18446744073709551616)
Decimal("1")                #=> Decimal(1)
Decimal("1.1")              #=> Decimal(1.1)
Decimal("1e10")             #=> Decimal(10000000000)
Decimal("299_792_458")      #=> Decimal(299792458)
Decimal("2.99_792_458e8")   #=> Decimal(299792458)

Notice that a Float is not acceptable for arg to keep exactness.

Decimal.new(1.1)            #=> (ArgumentError)


296
297
298
299
300
301
302
303
304
# File 'decimal.c', line 296

static VALUE
dec_initialize(VALUE self, VALUE arg)
{
    if (DECIMAL_P(arg)) {
	return arg;
    }
    DATA_PTR(self) = create_dec(arg);
    return self;
}

Instance Method Details

#%(other) ⇒ Object #modulo(other) ⇒ Object

Return the modulo after division of dec by other.

Decimal("6543.21").modulo(137)                 #=> Decimal(104.21)
Decimal("6543.21").modulo(Decimal("137.24"))   #=> Decimal(92.9299999999996)


1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'decimal.c', line 1105

static VALUE
dec_mod(VALUE x, VALUE y)
{
    Decimal *a, *b;
    VALUE mod;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, '%');
    }
    GetDecimal(x, a);
    divmod(a, b, NULL, &mod);
    return mod;
}

#*(other) ⇒ Object

Returns a new decimal which is the product of dec and other.



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'decimal.c', line 718

static VALUE
dec_mul(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
        /* TODO: can be optimized if y = 0, 1 or -1 */
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
        break;
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, '*');
    }
    GetDecimal(x, a);

    if (DEC_ISINF(a)) {
	if (DEC_ISINF(b)) return x == y ? VALUE_PINF : VALUE_NINF;
	if (DEC_ZERO_P(b)) return VALUE_NaN;
	if (!INUM_NEGATIVE_P(b->inum)) return x;
	return dec_uminus(x);
    }
    if (DEC_ZERO_P(a)) {
	if (DEC_ISINF(b)) return VALUE_NaN;
	if (DEC_ZERO_P(b))  {
	    return a->inum == DEC_PZERO ? y : dec_uminus(y);
	}
	if (INUM_NEGATIVE_P(b->inum)) return dec_uminus(x);
	return x;
    }
    if (DEC_IMMEDIATE_P(b) || DEC_ZERO_P(b)) {
        if (INUM_NEGATIVE_P(a->inum)) return dec_uminus(y);
        return y;
    }
    return WrapDecimal(normal_mul(a, b));
}

#**(fix) ⇒ Object

WARNING: The behavior of this method may change.

Raises dec the fix power.



1352
1353
1354
1355
1356
# File 'decimal.c', line 1352

static VALUE
dec_pow(VALUE x, VALUE y)
{
    return dec_power(1, &y, x);
}

#+(other) ⇒ Object

Returns a new decimal which is the sum of dec and other.



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'decimal.c', line 599

static VALUE
dec_plus(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
	break;
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, '+');
    }

    if (DEC_VALUE_ISINF(x)) {
	if (DEC_VALUE_ISINF(y) && x != y) return VALUE_NaN;
	return x;
    }
    if (DEC_VALUE_ISINF(y)) return y;
    /* now, x and y are not NaN nor +-INFINITY */
    GetDecimal(x, a);
    if (DEC_ZERO_P(a)) {
	VALUE inum;

        if (DEC_ZERO_P(b)) {
	    const long scale = MAX(a->scale, b->scale);

            if (a->inum == DEC_NZERO && b->inum == DEC_NZERO)
		return dec_nzero(scale);
            return dec_pzero(scale);
        }
	if (a->scale <= b->scale)
	    return y;
	inum = inum_lshift(b->inum, a->scale - b->scale);
	return WrapDecimal(dec_raw_new(inum, a->scale));
    }
    if (DEC_ZERO_P(b)) {
	VALUE inum;
	
	if (a->scale >= b->scale)
	    return x;
	inum = inum_lshift(a->inum, b->scale - a->scale);
	return WrapDecimal(dec_raw_new(inum, b->scale));
    }
    /* "true" means addition */
    return WrapDecimal(normal_plus(a, b, Qtrue));
}

#-(other) ⇒ Object

Returns a new float which is the difference of dec and other.



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'decimal.c', line 664

static VALUE
dec_minus(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
	break;
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, '-');
    }
    if (DEC_VALUE_ISINF(x)) {
	if (x == y) return VALUE_NaN;
	return x;
    }
    if (DEC_VALUE_ISINF(y)) return NEGATE_INF(y);

    GetDecimal(x, a);
    if (DEC_ZERO_P(a)) { /* FIXME: needs refactoring */
	if (!DEC_ISINF(b) && DEC_ZERO_P(b) && a->inum == b->inum) {
	     /* FIXME: UNDER CONSTRUCTION for scaling */
	    return dec_pzero(MAX(a->scale, b->scale));
	}
	return dec_uminus(y);
    }
    if (DEC_ZERO_P(b)) return x;
    /* "false" means subtraction */
    return WrapDecimal(normal_plus(a, b, Qfalse));
}

#-Object

Returns a negated value of dec.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'decimal.c', line 533

static VALUE
dec_uminus(VALUE num)
{
    VALUE inum;
    Decimal *d;

    CHECK_NAN(num);
    if (num == VALUE_PINF) return VALUE_NINF;
    if (num == VALUE_NINF) return VALUE_PINF;

    GetDecimal(num, d);
    if (d->inum == DEC_PZERO)
	inum = DEC_NZERO;
    else if (d->inum == DEC_NZERO)
	inum = DEC_PZERO;
    else
	inum = INUM_UMINUS(d->inum);
    return WrapDecimal(dec_raw_new(inum, d->scale));
}

#/(y) ⇒ Object

:nodoc:



1002
1003
1004
1005
1006
# File 'decimal.c', line 1002

static VALUE
dec_div(VALUE x, VALUE y)
{
    return dec_divide(1, &y, x);
}

#<(other) ⇒ Boolean

Returns true if dec is less than other.

Returns:

  • (Boolean)


1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
# File 'decimal.c', line 1540

static VALUE
dec_lt(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_cmperr(x, y);
        return Qnil; /* not reached */
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_relop(x, y, '<');
    }
    return cmp(a, b) < 0 ? Qtrue : Qfalse;
}

#<=(other) ⇒ Boolean

Returns true if dec is less than or equal to other.

Returns:

  • (Boolean)


1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
# File 'decimal.c', line 1573

static VALUE
dec_le(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_cmperr(x, y);
        return Qnil; /* not reached */
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_relop(x, y, rb_intern("<="));
    }
    return cmp(a, b) <= 0 ? Qtrue : Qfalse;
}

#<=>(other) ⇒ -1, ...

Returns -1, 0, or +1 depending on whether dec is less than, equal to, or greater than other. This is the basis for the tests in Comparable.

Returns:

  • (-1, 0, +1)


1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
# File 'decimal.c', line 1442

static VALUE
dec_cmp(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qnil);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	return Qnil;
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
    }
    return INT2FIX(cmp(a, b));
}

#==(other) ⇒ Boolean

Returns true only if other has the same value as dec. Contrast this with eql?, which requires other to be the same class, a Decimal.

Decimal(1) == 1                #=> true
Decimal(1) == Decimal("1.0")   #=> true
Decimal(1) == 1.0              #=> false

Returns:

  • (Boolean)


1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# File 'decimal.c', line 1408

static VALUE
dec_eq(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	return Qfalse;
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return RTEST(rb_num_coerce_cmp(x, y, rb_intern("==")));
    }
    return cmp(a, b) == 0 ? Qtrue : Qfalse;
}

#>(other) ⇒ Boolean

Returns true if dec is greater than other.

Returns:

  • (Boolean)


1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'decimal.c', line 1474

static VALUE
dec_gt(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_cmperr(x, y);
        return Qnil; /* not reached */
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_relop(x, y, '>');
    }
    return cmp(a, b) > 0 ? Qtrue : Qfalse;
}

#>=(other) ⇒ Boolean

Returns true if dec is greater than or equal to other.

Returns:

  • (Boolean)


1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'decimal.c', line 1507

static VALUE
dec_ge(VALUE x, VALUE y)
{
    Decimal *a, *b;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    GetDecimal(x, a);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_cmperr(x, y);
        return Qnil; /* not reached */
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_relop(x, y, rb_intern(">="));
    }
    return cmp(a, b) >= 0 ? Qtrue : Qfalse;
}

#absObject

Returns the absolute value of dec.

Decimal("34.56").abs    #=> Decimal(34.56)
Decimal("-34.56").abs   #=> Decimal(34.56)


1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
# File 'decimal.c', line 1777

static VALUE
dec_abs(VALUE num)
{
    Decimal *d;
    VALUE inum;

    CHECK_NAN(num);
    if (DEC_VALUE_ISINF(num))
	return VALUE_PINF;
    GetDecimal(num, d);
    if (d->inum == DEC_PZERO ||
	(d->inum != DEC_NZERO && !INUM_NEGATIVE_P(d->inum))) {
	return num;
    }
    inum = (d->inum == DEC_NZERO) ? DEC_PZERO : INUM_UMINUS(d->inum);
    return WrapDecimal(dec_raw_new(inum, d->scale));
}

#ceil(n = 0) ⇒ Integer

Returns the smallest Integer greater than or equal to dec.

Decimal("1.2").ceil    #=> 2
Decimal("2.0").ceil    #=> 2
Decimal("-1.2").ceil   #=> -1
Decimal("-2.0").ceil   #=> -2

This is identical to dec.round(n, Decimal::ROUND_CEILING). See Decimal#round for more details.

Returns:

  • (Integer)


1896
1897
1898
1899
1900
# File 'decimal.c', line 1896

static VALUE
dec_ceil(int argc, VALUE *argv, VALUE x)
{
    return rounding_method(argc, argv, x, ROUND_CEILING);
}

#coerce(other) ⇒ Array

Returns array [Decimal(other), dec] if other has a compatible type, Integer or Decimal. Otherwise raises a TypeError.

Decimal(1).coerce(2)            #=> [Decimal(2), Decimal(1)]
Decimal(1).coerce(Decimal(2))   #=> [Decimal(2), Decimal(1)]
Decimal(1).coerce(2.5)          #=> (TypeError)

Returns:

  • (Array)


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'decimal.c', line 502

static VALUE
dec_coerce(VALUE x, VALUE y)
{
    VALUE yy;

    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	yy = WrapDecimal(inum_to_dec(y));
	return rb_assoc_new(yy, x);
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't coerce Float to Decimal; "
		 "use Decimal#to_f explicitly if needed");
	break;
      case T_DATA:
	if (DECIMAL_P(y)) return rb_assoc_new(y, x);
	/* fall through */
      default:
	rb_raise(rb_eTypeError, "can't coerce %s to Decimal",
		 rb_obj_classname(y));
	break;
    }
    return Qnil; /* not reached */
}

#div(y) ⇒ Object

:nodoc:



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'decimal.c', line 1067

static VALUE
dec_idiv(VALUE x, VALUE y)
{
    Decimal *a, *b;
    VALUE div;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, rb_intern("div"));
    }
    GetDecimal(x, a);
    divmod(a, b, &div, NULL);
    return div;
}

#divide(other, scale = 0, mode = Decimal::ROUND_UNNECESSARY) ⇒ Object

WARNING: The behavior of this method may change.

Returns a new decimal which is the result of dividing dec by other.

FIXME: write details



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
# File 'decimal.c', line 914

static VALUE
dec_divide(int argc, VALUE *argv, VALUE x)
{
    VALUE y;
    Decimal *a, *b;
    VALUE mode = ROUND_UNNECESSARY;
    long l, scale = 0; /* FIXME: dummy 0 */
    VALUE vscale, vmode;

    CHECK_NAN(x);
    GetDecimal(x, a);

    rb_scan_args(argc, argv, "12", &y, &vscale, &vmode);
    switch (argc) {
      case 3:
	Check_Type(vmode, T_SYMBOL);
	if (!valid_rounding_mode_p(vmode)) {
	    rb_raise(rb_eArgError, "invalid rounding mode %s",
                     RSTRING_PTR(rb_inspect(vmode)));
	}
	mode = vmode;
	/* fall through */
      case 2:
	scale = NUM2LONG(vscale);
	break;
      case 1:
	if (mode != ROUND_UNNECESSARY) {
	    rb_raise(rb_eArgError, "scale number argument needed");
	}
    }
    CHECK_NAN(y);

    switch (TYPE(y)) {
      case T_FIXNUM:
        l = FIX2LONG(y);
        if (l == 0) {
            if (DEC_ISINF(a)) return x;
            if (DEC_ZERO_P(a)) return VALUE_NaN;
            return INUM_NEGATIVE_P(a->inum) ? VALUE_NINF : VALUE_PINF;
        }
        else if (l == 1) return x;
        else if (l == -1) return dec_uminus(x);
	/* fall through */
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
        return Qnil; /* not reached */
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
        return rb_num_coerce_bin(x, y, rb_intern("divide"));
    }

    if (DEC_ISINF(a)) {
	if (DEC_ISINF(b)) return VALUE_NaN;
	if (b->inum == DEC_PZERO) return x;
	if (b->inum == DEC_NZERO) return NEGATE_INF(x);
	return INUM_NEGATIVE_P(b->inum) ? NEGATE_INF(x) : x;
    }
    if (DEC_ZERO_P(a)) {
	if (b == DEC_PINF) return x;
	if (b == DEC_NINF) return dec_uminus(x);
	if (INUM_SPZERO_P(b->inum)) return VALUE_NaN;
	return INUM_NEGATIVE_P(b->inum) ? dec_uminus(x) : x;
    }
    if (DEC_ISINF(b)) {
	if (INUM_NEGATIVE_P(a->inum) == (b == DEC_NINF)) {
	    return dec_pzero(0); /* FIXME for scaling */
	}
	return dec_nzero(0); /* FIXME for scaling */
    }
    if (DEC_ZERO_P(b)) {
	if (INUM_NEGATIVE_P(a->inum) == (b->inum == DEC_NZERO)) {
	    return VALUE_PINF;
	}
	return VALUE_NINF;
    }
    return WrapDecimal(normal_divide(a, b, scale, mode));
}

#divmod(other) ⇒ Array

Returns an array containing the quotient and modulus obtained by dividing dec by other.

Decimal(11).divmod(3)                    #=> [3, Decimal(2)]
Decimal(11).divmod(-3)	                #=> [-4, Decimal(-1)]
Decimal(11).divmod(Decimal("3.5"))       #=> [3, Decimal(0.5)]
Decimal(-11).divmod(Decimal("3.5"))      #=> [-4, Decimal(3.0)]
Decimal("11.5").divmod(Decimal("3.5"))   #=> [3, Decimal(1.0)]

See Numeric#divmod for more details.

Returns:

  • (Array)


1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
# File 'decimal.c', line 1148

static VALUE
dec_divmod(VALUE x, VALUE y)
{
    Decimal *a, *b;
    VALUE div, mod;

    CHECK_NAN2_WITH_VAL(x, y, rb_assoc_new(VALUE_NaN, VALUE_NaN));
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, rb_intern("divmod"));
    }
    GetDecimal(x, a);
    divmod(a, b, &div, &mod);
    return rb_assoc_new(div, mod);
}

#eql?(other) ⇒ Boolean

Returns true if other is a Decimal and is equal to dec including their values of scale.

Decimal(1) == 1                    #=> true
Decimal(1).eql?(1)                 #=> false
Decimal(1).eql?(Decimal(1))        #=> true
Decimal(1).eql?(Decimal("1.0")))   #=> false

Returns:

  • (Boolean)


1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
# File 'decimal.c', line 1612

static VALUE
dec_eql(VALUE x, VALUE y)
{
    Decimal *a, *b;

    if (TYPE(y) != T_DATA || !DECIMAL_P(y))
	return Qfalse;

    CHECK_NAN2_WITH_VAL(x, y, Qfalse);
    if (DEC_VALUE_ISINF(x) || DEC_VALUE_ISINF(y))
	return x == y ? Qtrue : Qfalse;

    GetDecimal(x, a);
    GetDecimal(y, b);
    if (a->scale != b->scale)
	return Qfalse;
    if (a->inum == b->inum)
	return Qtrue;
    if (INUM_SPZERO_P(a->inum) || INUM_SPZERO_P(b->inum))
	return Qfalse;
    if (INUM_EQ(a->inum, b->inum))
	return Qtrue;
    return Qfalse;
}

#finite?Boolean

Returns true if dec is a finite number (it is not infinite nor NaN).

Decimal(0).finite?             #=> true
Decimal(1).divide(0).finite?   #=> false
Decimal(0).divide(0).finite?   #=> false

Returns:

  • (Boolean)


1976
1977
1978
1979
1980
1981
1982
1983
# File 'decimal.c', line 1976

static VALUE
dec_finite_p(VALUE num)
{
    if (!DEC_VALUE_ISINF(num) && num != VALUE_NaN) {
	return Qtrue;
    }
    return Qfalse;
}

#floor(n = 0) ⇒ Integer

Returns the largest integer less than or equal to dec.

Decimal("1.2").floor    #=> 1
Decimal("2.0").floor    #=> 2
Decimal("-1.2").floor   #=> -2
Decimal("-2.0").floor   #=> -2

This is identical to dec.round(n, Decimal::ROUND_FLOOR). See Decimal#round for more details.

Returns:

  • (Integer)


1876
1877
1878
1879
1880
# File 'decimal.c', line 1876

static VALUE
dec_floor(int argc, VALUE *argv, VALUE x)
{
    return rounding_method(argc, argv, x, ROUND_FLOOR);
}

#hashInteger

Returns a hash code for dec.

Returns:

  • (Integer)


1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
# File 'decimal.c', line 1643

static VALUE
dec_hash(VALUE x)
{
    Decimal *d;
    long hash;

    GetDecimal(x, d);
    if (!DEC_IMMEDIATE_P(d)) {
        VALUE inum = d->inum; 

        if (INUM_SPZERO_P(inum)) inum = INT2FIX(0);
        hash = NUM2LONG(INUM_HASH(inum));
	hash ^= d->scale;
    }
    else hash = (long)d;
    return LONG2NUM(hash);
}

#infinite?nil, ...

Returns nil, -1, or 1 depending on whether dec is finite, -infinity, or infinity.

Decimal(0).infinite?              #=> nil
Decimal(-1).divide(0).infinite?   #=> -1
Decimal(+1).divide(0).infinite?   #=> 1

Returns:

  • (nil, -1, +1)


1996
1997
1998
1999
2000
2001
2002
# File 'decimal.c', line 1996

static VALUE
dec_infinite_p(VALUE num)
{
    if (num == VALUE_PINF) return INT2FIX(1);
    if (num == VALUE_NINF) return INT2FIX(-1);
    return Qnil;
}

#inspectString

Returns a easy-to-distinguish string: "Decimal(#{dec})".

Decimal(1).inspect             #=> "Decimal(1)"
Decimal("1.1").inspect         #=> "Decimal(1.1)"
Decimal::INFINITY.inspect   #=> "Decimal(Infinity)"

Returns:

  • (String)


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'decimal.c', line 474

static VALUE
dec_inspect(VALUE self)
{
    char *s;
    VALUE str, newstr;
    long len;

    str = dec_to_s(self);
    len = 9 + RSTRING_LEN(str); /* 9 == strlen("Decimal()") */
    s = ALLOC_N(char, len + 1); /* +1 for NUL */
    sprintf(s, "Decimal(%s)", RSTRING_PTR(str));
    newstr = rb_usascii_str_new(s, len);
    xfree(s);
    return newstr;
}

#%(other) ⇒ Object #modulo(other) ⇒ Object

Return the modulo after division of dec by other.

Decimal("6543.21").modulo(137)                 #=> Decimal(104.21)
Decimal("6543.21").modulo(Decimal("137.24"))   #=> Decimal(92.9299999999996)


1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'decimal.c', line 1105

static VALUE
dec_mod(VALUE x, VALUE y)
{
    Decimal *a, *b;
    VALUE mod;

    CHECK_NAN2(x, y);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
	b = inum_to_dec(y);
	break;
      case T_FLOAT:
	rb_raise(rb_eTypeError, "can't operate with Float");
      case T_DATA:
	if (DECIMAL_P(y)) {
	    GetDecimal(y, b);
	    break;
	}
	/* fall through */
      default:
	return rb_num_coerce_bin(x, y, '%');
    }
    GetDecimal(x, a);
    divmod(a, b, NULL, &mod);
    return mod;
}

#nan?Boolean

Returns true if dec is an invalid point number, NaN.

Decimal(-1).nan?            #=> false
Decimal(1).divide(0).nan?   #=> false
Decimal(0).divide(0).nan?   #=> true

Returns:

  • (Boolean)


1959
1960
1961
1962
1963
# File 'decimal.c', line 1959

static VALUE
dec_nan_p(VALUE num)
{
    return num == VALUE_NaN;
}

#power(*args) ⇒ Object

:nodoc:



1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
# File 'decimal.c', line 1312

static VALUE
dec_power(int argc, VALUE *argv, VALUE x)
{
    VALUE mode = ROUND_UNNECESSARY;
    VALUE y, scale, vmode;

    rb_scan_args(argc, argv, "12", &y, &scale, &vmode);
    switch (argc) {
      case 3:
        Check_Type(vmode, T_SYMBOL);
	if (!valid_rounding_mode_p(vmode)) {
	    rb_raise(rb_eArgError, "invalid rounding mode %s",
                     RSTRING_PTR(rb_inspect(vmode)));
	}
	mode = vmode;
	/* fall through */
    case 2:
        Check_Type(scale, T_FIXNUM);
        break;
    case 1:
	if (mode != ROUND_UNNECESSARY) {
	    rb_raise(rb_eArgError, "scale number argument needed");
	}
    }

    if (!FIXNUM_P(y) && !DECIMAL_P(y)) {
        rb_raise(rb_eTypeError, "2nd argument %s must be Fixnum or Decimal",
                 RSTRING_PTR(rb_inspect(y)));
    }
    return power_body(x, y, scale, mode);
}

#round(n = 0, mode = Decimal::ROUND_HALF_UP) ⇒ Integer

FIXME: more examples

Rounds dec to a given precision n in decimal digits (default 0 digits) with rounding mode mode. Precision may be negative. Returns a Decimal when n is greater than 0, Integer otherwise.

Decimal("1.5").round    #=> 2
Decimal("-1.5").round   #=> -2

Returns:

  • (Integer)


1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
# File 'decimal.c', line 1915

static VALUE
dec_round(int argc, VALUE *argv, VALUE x)
{
    Decimal *d;
    VALUE vscale, mode;
    long scale = 0;

    rb_scan_args(argc, argv, "02", &vscale, &mode);
    switch (argc) {
      case 2:
	Check_Type(mode, T_SYMBOL);
	if (!valid_rounding_mode_p(mode)) {
	    rb_raise(rb_eArgError, "invalid rounding mode %s",
                     RSTRING_PTR(rb_inspect(mode)));
	}
	/* fall through */
      case 1:
	scale = NUM2LONG(vscale);
	/* fall through */
      default:
        if (NIL_P(mode)) mode = ROUND_HALF_UP;
	break;
    }
    GetDecimal(x, d);
    if (scale <= 0) {
	VALUE inum;

	do_round(d, scale, mode, &inum);
	return inum;
    }
    return WrapDecimal(do_round(d, scale, mode, NULL));
}

#scaleObject

:nodoc:



326
327
328
329
330
331
332
333
334
# File 'decimal.c', line 326

static VALUE
dec_scale(VALUE self)
{
    Decimal *d;

    GetDecimal(self, d);
    if (DEC_IMMEDIATE_P(d)) return Qnil;
    return LONG2NUM(d->scale);
}

#stripObject

:nodoc:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'decimal.c', line 348

static VALUE
dec_strip_trailing_zeros(VALUE self)
{
    Decimal *d, *d2;

    GetDecimal(self, d);
    if (DEC_IMMEDIATE_P(d))
	return self;
    if (DEC_ZERO_P(d)) { /* XXX: negative scale? */
        if (d->scale <= 0) return self;
	d2 = finite_dup(d);
	d2->scale = 0;
	return WrapDecimal(d2);
    }
    d2 = finite_dup(d);
    /* TODO: can be optimized with dividing each part
     * for Bignums and Fixnums */
    while (INUM_BOTTOMDIG(d2->inum) == 0) {
	d2->inum = INUM_DIV(d2->inum, INT2FIX(10));
	d2->scale--;
    }
    return WrapDecimal(d2);
}

#strip_trailing_zerosObject

:nodoc:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'decimal.c', line 348

static VALUE
dec_strip_trailing_zeros(VALUE self)
{
    Decimal *d, *d2;

    GetDecimal(self, d);
    if (DEC_IMMEDIATE_P(d))
	return self;
    if (DEC_ZERO_P(d)) { /* XXX: negative scale? */
        if (d->scale <= 0) return self;
	d2 = finite_dup(d);
	d2->scale = 0;
	return WrapDecimal(d2);
    }
    d2 = finite_dup(d);
    /* TODO: can be optimized with dividing each part
     * for Bignums and Fixnums */
    while (INUM_BOTTOMDIG(d2->inum) == 0) {
	d2->inum = INUM_DIV(d2->inum, INT2FIX(10));
	d2->scale--;
    }
    return WrapDecimal(d2);
}

#to_fFloat

Converts dec to a Float. Note that this may lose some precision and/or exactness. If you want to operate Decimal with Float, use this method explicitly.

Returns:

  • (Float)


1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
# File 'decimal.c', line 1743

static VALUE
dec_to_f(VALUE num)
{
    Decimal *d;
    double f;

    CHECK_NAN_WITH_VAL(num, rb_float_new(NAN));
    if (num == VALUE_PINF)
	return rb_float_new(INFINITY);
    if (num == VALUE_NINF)
	return rb_float_new(-INFINITY);

    GetDecimal(num, d);
    if (d->inum == DEC_PZERO)
	f = 0.0;
    else if (d->inum == DEC_NZERO)
	f = -0.0;
    else if (out_of_double_range_p(d, &f))
	rb_warning("Decimal out of Float range");
    else
	f = normal_to_f(d);

    return rb_float_new(f);
}

#to_iInteger

Returns dec truncated to an Integer.

Returns:

  • (Integer)


1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
# File 'decimal.c', line 1819

static VALUE
dec_to_i(VALUE num)
{
   Decimal *d;
   VALUE inum;

   GetDecimal(num, d);
   do_round(d, 0, ROUND_DOWN, &inum); /* equal to "d.round(0, :down)" */
   return inum;
}

#to_sString

WARNING: The behavior of this method may change.

Returns a string containing a simple representation of self.

Decimal(1).to_s             #=> "1"
Decimal("1.1").to_s         #=> "1.1"
Decimal::INFINITY.to_s   #=> "Infinity"

Returns:

  • (String)


435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'decimal.c', line 435

static VALUE
dec_to_s(VALUE self)
{
    Decimal *d;

    CHECK_NAN_WITH_VAL(self, rb_usascii_str_new_cstr("NaN"));
    if (self == VALUE_PINF) return rb_usascii_str_new_cstr("Infinity");
    if (self == VALUE_NINF) return rb_usascii_str_new_cstr("-Infinity");
    GetDecimal(self, d);
    if (DEC_ZERO_P(d)) {
	const size_t HEAD_LEN = d->inum == DEC_PZERO ? 2U : 3U; /* "-0.".length */
	long len = HEAD_LEN + d->scale;
	char *buf;

	/* FIXME: use "0eN" style when the scale is negative? */
	if (d->scale <= 0) /* ignore the case of negative scale */
	    return d->inum == DEC_PZERO ?
	      rb_usascii_str_new_cstr("0") : rb_usascii_str_new_cstr("-0");
	buf = xmalloc(len);
	if (d->inum == DEC_PZERO)
	    memcpy(buf, "0.", HEAD_LEN);
	else
	    memcpy(buf, "-0.", HEAD_LEN);
	memset(buf + HEAD_LEN, '0', d->scale);
	return rb_usascii_str_new(buf, len);
    }
    return finite_to_s(d);
}

#truncate(n = 0) ⇒ Integer

Returns dec truncated to an Integer.

This is identical to dec.round(n, Decimal::ROUND_DOWN). See Decimal#round for more details.

Returns:

  • (Integer)


1856
1857
1858
1859
1860
# File 'decimal.c', line 1856

static VALUE
dec_truncate(int argc, VALUE *argv, VALUE x)
{
    return rounding_method(argc, argv, x, ROUND_DOWN);
}

#unscaled_valueObject

:nodoc:



337
338
339
340
341
342
343
344
345
# File 'decimal.c', line 337

static VALUE
dec_unscaled_value(VALUE self)
{
    Decimal *d;

    GetDecimal(self, d);
    if (DEC_IMMEDIATE_P(d)) return Qnil;
    return DEC_ZERO_P(d) ? INT2FIX(0) : d->inum;
}

#zero?Boolean

Returns true if dec is zero.

Returns:

  • (Boolean)


1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
# File 'decimal.c', line 1801

static VALUE
dec_zero_p(VALUE num)
{
    Decimal *d;

    GetDecimal(num, d);
    if (!DEC_IMMEDIATE_P(d) && DEC_ZERO_P(d)) {
        return Qtrue;
    }
    return Qfalse;
}