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)


299
300
301
302
303
304
305
306
307
# File 'decimal.c', line 299

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)


1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'decimal.c', line 1109

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.



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
764
765
766
# File 'decimal.c', line 721

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.



1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# File 'decimal.c', line 1201

static VALUE
dec_pow(VALUE x, VALUE y)
{
    Decimal *a;
    long l;

    CHECK_NAN(x);
    Check_Type(y, T_FIXNUM);
    l = FIX2LONG(y);
    if (l < 0) rb_raise(rb_eArgError, "in a**b, b should be positive integer");
    if (l == 0) return WrapDecimal(dec_raw_new(INT2FIX(1), 0));
    if (l == 1) return x;

    if (x == VALUE_PINF) return x;
    if (x == VALUE_NINF) {
  return l % 2 == 0 ? VALUE_PINF : VALUE_NINF;
    }
    GetDecimal(x, a);
    if (a->inum == DEC_PZERO) return x;
    if (a->inum == DEC_NZERO) {
  return l % 2 == 0 ? dec_uminus(x) : x;
    }
    return power_with_fixnum(a, y);
}

#+(other) ⇒ Object

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



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
656
657
658
# File 'decimal.c', line 602

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.



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
705
706
707
# File 'decimal.c', line 667

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.



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

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:



1005
1006
1007
1008
1009
# File 'decimal.c', line 1005

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)


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
1433
# File 'decimal.c', line 1408

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)


1441
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 1441

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)


1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'decimal.c', line 1310

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)


1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'decimal.c', line 1276

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)


1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
# File 'decimal.c', line 1342

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)


1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
# File 'decimal.c', line 1375

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)


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

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)


1764
1765
1766
1767
1768
# File 'decimal.c', line 1764

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)


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

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:



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'decimal.c', line 1071

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



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
999
1000
1001
# File 'decimal.c', line 917

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)


1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'decimal.c', line 1152

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)


1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'decimal.c', line 1480

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)


1844
1845
1846
1847
1848
1849
1850
1851
# File 'decimal.c', line 1844

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)


1744
1745
1746
1747
1748
# File 'decimal.c', line 1744

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)


1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'decimal.c', line 1511

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)


1864
1865
1866
1867
1868
1869
1870
# File 'decimal.c', line 1864

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)


477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'decimal.c', line 477

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)


1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'decimal.c', line 1109

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)


1827
1828
1829
1830
1831
# File 'decimal.c', line 1827

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

#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)


1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
# File 'decimal.c', line 1783

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:



329
330
331
332
333
334
335
336
337
# File 'decimal.c', line 329

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

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

#stripObject

:nodoc:



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

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:



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

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)


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

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)


1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'decimal.c', line 1687

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)


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
463
464
465
# File 'decimal.c', line 438

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)


1724
1725
1726
1727
1728
# File 'decimal.c', line 1724

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

#unscaled_valueObject

:nodoc:



340
341
342
343
344
345
346
347
348
# File 'decimal.c', line 340

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)


1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
# File 'decimal.c', line 1669

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;
}