Method: BigDecimal#power

Defined in:
bigdecimal.c

#power(*args) ⇒ Object

power(n) power(n, prec)

Returns the value raised to the power of n.

Note that n must be an Integer.

Also available as the operator **.



2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
# File 'bigdecimal.c', line 2430

static VALUE
BigDecimal_power(int argc, VALUE*argv, VALUE self)
{
    ENTER(5);
    VALUE vexp, prec;
    Real* exp = NULL;
    Real *x, *y;
    ssize_t mp, ma, n;
    SIGNED_VALUE int_exp;
    double d;

    rb_scan_args(argc, argv, "11", &vexp, &prec);

    GUARD_OBJ(x, GetVpValue(self, 1));
    n = NIL_P(prec) ? (ssize_t)(x->Prec*VpBaseFig()) : NUM2SSIZET(prec);

    if (VpIsNaN(x)) {
	y = VpCreateRbObject(n, "0");
	RB_GC_GUARD(y->obj);
	VpSetNaN(y);
	return ToValue(y);
    }

  retry:
    switch (TYPE(vexp)) {
      case T_FIXNUM:
	break;

      case T_BIGNUM:
	break;

      case T_FLOAT:
	d = RFLOAT_VALUE(vexp);
	if (d == round(d)) {
	    if (FIXABLE(d)) {
		vexp = LONG2FIX((long)d);
	    }
	    else {
		vexp = rb_dbl2big(d);
	    }
	    goto retry;
	}
        if (NIL_P(prec)) {
            n += DBLE_FIG;
        }
        exp = GetVpValueWithPrec(vexp, DBLE_FIG, 1);
	break;

      case T_RATIONAL:
	if (is_zero(rb_rational_num(vexp))) {
	    if (is_positive(vexp)) {
		vexp = INT2FIX(0);
		goto retry;
	    }
	}
	else if (is_one(rb_rational_den(vexp))) {
	    vexp = rb_rational_num(vexp);
	    goto retry;
	}
	exp = GetVpValueWithPrec(vexp, n, 1);
        if (NIL_P(prec)) {
            n += n;
        }
	break;

      case T_DATA:
	if (is_kind_of_BigDecimal(vexp)) {
	    VALUE zero = INT2FIX(0);
	    VALUE rounded = BigDecimal_round(1, &zero, vexp);
	    if (RTEST(BigDecimal_eq(vexp, rounded))) {
		vexp = BigDecimal_to_i(vexp);
		goto retry;
	    }
            if (NIL_P(prec)) {
                GUARD_OBJ(y, GetVpValue(vexp, 1));
                n += y->Prec*VpBaseFig();
            }
	    exp = DATA_PTR(vexp);
	    break;
	}
	/* fall through */
      default:
	rb_raise(rb_eTypeError,
		 "wrong argument type %"PRIsVALUE" (expected scalar Numeric)",
		 RB_OBJ_CLASSNAME(vexp));
    }

    if (VpIsZero(x)) {
	if (is_negative(vexp)) {
	    y = VpCreateRbObject(n, "#0");
	    RB_GC_GUARD(y->obj);
	    if (BIGDECIMAL_NEGATIVE_P(x)) {
		if (is_integer(vexp)) {
		    if (is_even(vexp)) {
			/* (-0) ** (-even_integer)  -> Infinity */
			VpSetPosInf(y);
		    }
		    else {
			/* (-0) ** (-odd_integer)  -> -Infinity */
			VpSetNegInf(y);
		    }
		}
		else {
		    /* (-0) ** (-non_integer)  -> Infinity */
		    VpSetPosInf(y);
		}
	    }
	    else {
		/* (+0) ** (-num)  -> Infinity */
		VpSetPosInf(y);
	    }
	    return ToValue(y);
	}
	else if (is_zero(vexp)) {
	    return ToValue(VpCreateRbObject(n, "1"));
	}
	else {
	    return ToValue(VpCreateRbObject(n, "0"));
	}
    }

    if (is_zero(vexp)) {
	return ToValue(VpCreateRbObject(n, "1"));
    }
    else if (is_one(vexp)) {
	return self;
    }

    if (VpIsInf(x)) {
	if (is_negative(vexp)) {
	    if (BIGDECIMAL_NEGATIVE_P(x)) {
		if (is_integer(vexp)) {
		    if (is_even(vexp)) {
			/* (-Infinity) ** (-even_integer) -> +0 */
			return ToValue(VpCreateRbObject(n, "0"));
		    }
		    else {
			/* (-Infinity) ** (-odd_integer) -> -0 */
			return ToValue(VpCreateRbObject(n, "-0"));
		    }
		}
		else {
		    /* (-Infinity) ** (-non_integer) -> -0 */
		    return ToValue(VpCreateRbObject(n, "-0"));
		}
	    }
	    else {
		return ToValue(VpCreateRbObject(n, "0"));
	    }
	}
	else {
	    y = VpCreateRbObject(n, "0");
	    if (BIGDECIMAL_NEGATIVE_P(x)) {
		if (is_integer(vexp)) {
		    if (is_even(vexp)) {
			VpSetPosInf(y);
		    }
		    else {
			VpSetNegInf(y);
		    }
		}
		else {
		    /* TODO: support complex */
		    rb_raise(rb_eMathDomainError,
			     "a non-integral exponent for a negative base");
		}
	    }
	    else {
		VpSetPosInf(y);
	    }
	    return ToValue(y);
	}
    }

    if (exp != NULL) {
	return rmpd_power_by_big_decimal(x, exp, n);
    }
    else if (RB_TYPE_P(vexp, T_BIGNUM)) {
	VALUE abs_value = BigDecimal_abs(self);
	if (is_one(abs_value)) {
	    return ToValue(VpCreateRbObject(n, "1"));
	}
	else if (RTEST(rb_funcall(abs_value, '<', 1, INT2FIX(1)))) {
	    if (is_negative(vexp)) {
		y = VpCreateRbObject(n, "0");
		if (is_even(vexp)) {
		    VpSetInf(y, VpGetSign(x));
		}
		else {
		    VpSetInf(y, -VpGetSign(x));
		}
		return ToValue(y);
	    }
	    else if (BIGDECIMAL_NEGATIVE_P(x) && is_even(vexp)) {
		return ToValue(VpCreateRbObject(n, "-0"));
	    }
	    else {
		return ToValue(VpCreateRbObject(n, "0"));
	    }
	}
	else {
	    if (is_positive(vexp)) {
		y = VpCreateRbObject(n, "0");
		if (is_even(vexp)) {
		    VpSetInf(y, VpGetSign(x));
		}
		else {
		    VpSetInf(y, -VpGetSign(x));
		}
		return ToValue(y);
	    }
	    else if (BIGDECIMAL_NEGATIVE_P(x) && is_even(vexp)) {
		return ToValue(VpCreateRbObject(n, "-0"));
	    }
	    else {
		return ToValue(VpCreateRbObject(n, "0"));
	    }
	}
    }

    int_exp = FIX2LONG(vexp);
    ma = int_exp;
    if (ma <  0) ma = -ma;
    if (ma == 0) ma = 1;

    if (VpIsDef(x)) {
	mp = x->Prec * (VpBaseFig() + 1);
	GUARD_OBJ(y, VpCreateRbObject(mp * (ma + 1), "0"));
    }
    else {
	GUARD_OBJ(y, VpCreateRbObject(1, "0"));
    }
    VpPower(y, x, int_exp);
    if (!NIL_P(prec) && VpIsDef(y)) {
	VpMidRound(y, VpGetRoundMode(), n);
    }
    return ToValue(y);
}