-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibfns.cpp
More file actions
50 lines (45 loc) · 1.35 KB
/
libfns.cpp
File metadata and controls
50 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// $Id: libfns.cpp,v 1.4 2015-07-03 14:46:41-07 - - $
#include <stdexcept>
#include "libfns.h"
#include "ubigint.h"
//
// This algorithm would be more efficient with operators
// *=, /=2, and is_odd. But we leave it here.
//
bigint pow (const bigint& base_arg, const bigint& exponent_arg) {
bigint base (base_arg);
bigint exponent (exponent_arg);
const string longMax = "2147483647";
const string longMin = "_2147483648";
bigint b_longMax (longMax);
bigint b_longMin (longMin);
if (b_longMax < exponent || exponent < b_longMin)
{
throw domain_error ("pow: exponent is too big (absolute value) to handle!");
}
//ubigint temp = exponent.uvalue;
ubigint u_ZERO('0');
ubigint u_ONE('1');
ubigint u_TWO('2');
static const bigint ZERO (u_ZERO, false);
static const bigint ONE (u_ONE, false);
static const bigint TWO (u_TWO, false);
DEBUGF ('^', "base = " << base << ", exponent = " << exponent);
if (base == ZERO) return ZERO;
bigint result = ONE;
if (exponent < ZERO) {
base = ONE / base;
exponent = - exponent;
}
while (exponent > ZERO) {
if (exponent % TWO == ONE) {
result = result * base;
exponent = exponent - ONE;
}else {
base = base * base;
exponent = exponent / TWO;
}
}
DEBUGF ('^', "result = " << result);
return result;
}