IntPow
Description
Generic exponentiation convenience macro.
Parameters
| Name | Direction | Description |
|---|---|---|
result |
out | Destination for the result |
base |
in | Base value |
exponent |
in | Exponent selected through generic dispatch |
Usage example (from documentation)
IntPow(&power, &base, 20u);Success
Returns true; *result holds base ** exponent computed via repeated-squaring.
Failure
Returns false if an intermediate allocation fails or if exponent is negative; *result is unchanged.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Float.c:80:
Int pow5 = IntInit(alloc);
Int sig = IntInit(alloc);
if (!int_try_from_u64(&five, 5u, alloc) || !IntPow(&pow5, &five, n) ||
!int_mul(&sig, &out->significand, &pow5)) {
IntDeinit(&five);- In
Math.c:320:
bool test_int_pow_generic(void) {
WriteFmt("Testing IntPow generic dispatch\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Math.c:329:
Str text = StrInit(&alloc.base);
IntPow(&result_value, &base, 20u);
text = IntToStr(&result_value);
bool result = ZstrCompare(StrBegin(&text), "79792266297612001") == 0;- In
Math.c:334:
StrDeinit(&text);
IntPow(&result_value, &base, &exponent);
text = IntToStr(&result_value);
result = result && (ZstrCompare(StrBegin(&text), "79792266297612001") == 0);- In
Math.c:1608:
// exponent 0 -> 1 (loop body never runs)
IntPow(&result, &base, 0u);
bool ok = IntToU64(&result) == 1;- In
Math.c:1612:
// exponent 1 -> base (one odd bit, no squaring)
IntPow(&result, &base, 1u);
ok = ok && (IntToU64(&result) == 7);- In
Math.c:1616:
// exponent 2 -> 49 (squaring path)
IntPow(&result, &base, 2u);
ok = ok && (IntToU64(&result) == 49);- In
Math.c:1620:
// exponent 5 -> 16807 (mixed odd/even bits)
IntPow(&result, &base, 5u);
ok = ok && (IntToU64(&result) == 16807);- In
Math.c:2096:
// exponent the real code must take the int_pow_u64 path and return true.
bool test_m22_pow_int_exponent_value(void) {
WriteFmt("Testing IntPow with an Int exponent (small, exact value)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Math.c:2105:
// 7**5 == 16807.
bool ok = IntPow(&power, &base, &exp);
bool result = ok;
result = result && (IntToU64(&power) == 16807u);- In
Math.c:2122:
// exponent through IntToU64) and not return the clean false.
bool test_m22_pow_int_exponent_too_large(void) {
WriteFmt("Testing IntPow rejects an exponent that overflows u64\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Math.c:2132:
Int sentinel = IntFrom(123, &alloc.base);
bool rejected = !IntPow(&power, &base, &exp);
// Result must be left untouched on rejection.
bool result = rejected && (IntCompare(&power, &sentinel) == 0);- In
Math.c:2351:
Int result_value = IntFrom(99, &alloc.base);
bool ok = IntPow(&result_value, &base, (i64)4);
bool result = ok && (IntToU64(&result_value) == 81);- In
Math.c:2375:
Int result_value = IntFrom(99, &alloc.base);
bool ok = IntPow(&result_value, &base, (i64)0);
bool result = ok && (IntToU64(&result_value) == 1);- In
Math.c:3975:
Int r = IntFrom(7u, a);
bool ok = IntPow(&r, &base, 13u);
ok = ok && IntToU64(&r) == 1594323u; // 3^13
Last updated on