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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::fp_decimal::{FPDecimal, U256};
use num::pow::Pow;
impl FPDecimal {
pub fn _pow(a: FPDecimal, b: FPDecimal) -> FPDecimal {
if a == FPDecimal::zero() {
return FPDecimal::zero();
}
FPDecimal::_exp(FPDecimal::_mul(FPDecimal::_ln(a), b))
}
pub fn _exp(a: FPDecimal) -> FPDecimal {
if a.sign == 0 && a.num >= FPDecimal::from(45i128).num {
return FPDecimal::zero();
}
let mut x = a.num;
let mut r = FPDecimal::ONE;
while x >= U256([10, 0, 0, 0]) * FPDecimal::ONE.num {
x = x - U256([10, 0, 0, 0]) * FPDecimal::ONE.num;
r = FPDecimal::_mul(r, FPDecimal::E_10);
}
if x == FPDecimal::ONE.num {
let val = FPDecimal::_mul(r, FPDecimal::E);
if a.sign == 0 {
return FPDecimal::reciprocal(val);
}
return val;
} else if x == FPDecimal::zero().num {
let val = r;
if a.sign == 0 {
return FPDecimal::reciprocal(val);
}
return val;
}
let mut tr = FPDecimal::ONE.num;
let mut d = tr;
for i in 1..((2 * FPDecimal::DIGITS + 1) as u64) {
d = (d * x) / (FPDecimal::ONE.num * U256([i, 0, 0, 0]));
tr = tr + d;
}
let val = FPDecimal::_mul(FPDecimal { num: tr, sign: 1 }, r);
if a.sign == 0 {
return FPDecimal::reciprocal(val);
}
val
}
}
impl Pow<FPDecimal> for FPDecimal {
type Output = Self;
fn pow(self, rhs: FPDecimal) -> Self::Output {
Self::_pow(self, rhs)
}
}
#[cfg(test)]
mod tests {
use crate::FPDecimal;
use bigint::U256;
use num::pow::Pow;
#[test]
fn test_exp() {
assert_eq!(FPDecimal::_exp(FPDecimal::ONE), FPDecimal::E);
}
#[test]
fn test_exp0() {
assert_eq!(FPDecimal::_exp(FPDecimal::zero()), FPDecimal::ONE);
}
#[test]
fn test_exp10() {
assert_eq!(
FPDecimal::_exp(FPDecimal {
num: U256([10, 0, 0, 0]) * FPDecimal::ONE.num,
sign: 1
}),
FPDecimal::E_10
);
}
#[test]
fn test_pow_zero() {
FPDecimal::pow(FPDecimal::zero(), FPDecimal::one().div(2i128));
assert_eq!(FPDecimal::ZERO.pow(FPDecimal::ONE), FPDecimal::ZERO);
}
#[test]
fn test_pow_exp() {
assert_eq!(FPDecimal::E.pow(FPDecimal::ONE), FPDecimal::E);
}
#[test]
fn test_pow_exp0() {
assert_eq!(FPDecimal::E.pow(FPDecimal::zero()), FPDecimal::ONE);
}
#[test]
fn test_pow_exp10() {
assert_eq!(
FPDecimal::E.pow(FPDecimal {
num: U256([10, 0, 0, 0]) * FPDecimal::ONE.num,
sign: 1
}),
FPDecimal::E_10
);
}
#[test]
fn test_pow_zero_2() {
FPDecimal::ZERO.pow(FPDecimal::one().div(2i128));
}
}