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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use dashu_base::{Approximation::*, EstimatedLog2, Sign};
use dashu_int::IBig;
use crate::{
error::{assert_finite, assert_limited_precision},
fbig::FBig,
repr::{Context, Repr, Word},
round::{Round, Rounded},
};
impl<const B: Word> EstimatedLog2 for Repr<B> {
fn log2_bounds(&self) -> (f32, f32) {
let (logs_lb, logs_ub) = self.significand.log2_bounds();
let (logb_lb, logb_ub) = if B.is_power_of_two() {
let log = B.trailing_zeros() as f32;
(log, log)
} else {
B.log2_bounds()
};
let e = self.exponent as f32;
if self.exponent >= 0 {
(logs_lb + e * logb_lb, logs_ub + e * logb_ub)
} else {
(logs_lb + e * logb_ub, logs_ub + e * logb_lb)
}
}
fn log2_est(&self) -> f32 {
let logs = self.significand.log2_est();
let logb = if B.is_power_of_two() {
B.trailing_zeros() as f32
} else {
B.log2_est()
};
logs + self.exponent as f32 * logb
}
}
impl<R: Round, const B: Word> EstimatedLog2 for FBig<R, B> {
#[inline]
fn log2_bounds(&self) -> (f32, f32) {
self.repr.log2_bounds()
}
#[inline]
fn log2_est(&self) -> f32 {
self.repr.log2_est()
}
}
impl<R: Round, const B: Word> FBig<R, B> {
#[inline]
pub fn ln(&self) -> Self {
self.context.ln(&self.repr).value()
}
#[inline]
pub fn ln_1p(&self) -> Self {
self.context.ln_1p(&self.repr).value()
}
}
impl<R: Round> Context<R> {
#[inline]
fn ln2<const B: Word>(&self) -> FBig<R, B> {
4 * self.iacoth(6.into()) + 2 * self.iacoth(99.into())
}
#[inline]
fn ln10<const B: Word>(&self) -> FBig<R, B> {
3 * self.ln2() + 2 * self.iacoth(9.into())
}
#[inline]
pub(crate) fn ln_base<const B: Word>(&self) -> FBig<R, B> {
match B {
2 => self.ln2(),
10 => self.ln10(),
i if i.is_power_of_two() => self.ln2() * i.trailing_zeros(),
_ => self.ln(&Repr::new(Repr::<B>::BASE, 0)).value(),
}
}
fn iacoth<const B: Word>(&self, n: IBig) -> FBig<R, B> {
let guard_digits = (self.precision.log2_est() / B.log2_est()) as usize;
let work_context = Self::new(self.precision + guard_digits + 2);
let n = work_context.convert_int(n).value();
let inv = FBig::ONE / n;
let inv2 = inv.square();
let mut sum = inv.clone();
let mut pow = inv;
let mut k: usize = 3;
loop {
pow *= &inv2;
let next = &sum + &pow / work_context.convert_int::<B>(k.into()).value();
if next == sum {
return sum;
}
sum = next;
k += 2;
}
}
#[inline]
pub fn ln<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>> {
self.ln_internal(x, false)
}
#[inline]
pub fn ln_1p<const B: Word>(&self, x: &Repr<B>) -> Rounded<FBig<R, B>> {
self.ln_internal(x, true)
}
fn ln_internal<const B: Word>(&self, x: &Repr<B>, one_plus: bool) -> Rounded<FBig<R, B>> {
assert_finite(x);
assert_limited_precision(self.precision);
if (one_plus && x.is_zero()) || (!one_plus && x.is_one()) {
return Exact(FBig::ZERO);
}
let guard_digits = (self.precision.log2_est() / B.log2_est()) as usize + 2;
let mut work_precision = self.precision + guard_digits + one_plus as usize;
let context = Context::<R>::new(work_precision);
let x = FBig::new(context.repr_round_ref(x).value(), context);
let no_scaling = one_plus && x.log2_est() < -B.log2_est();
let (s, mut x_scaled) = if no_scaling {
(0, x)
} else {
let x = if one_plus { x + FBig::ONE } else { x };
let log2 = x.log2_bounds().0;
let s = log2 as isize - (log2 < 0.) as isize; let x_scaled = if B == 2 {
x >> s
} else if s > 0 {
x / (IBig::ONE << s as usize)
} else {
x * (IBig::ONE << (-s) as usize)
};
debug_assert!(x_scaled >= FBig::<R, B>::ONE);
(s, x_scaled)
};
if s < 0 || x_scaled.repr.sign() == Sign::Negative {
work_precision += self.precision;
x_scaled.context.precision = work_precision;
};
let work_context = Context::new(work_precision);
let z = if no_scaling {
let d = &x_scaled + (FBig::ONE + FBig::ONE);
x_scaled / d
} else {
(&x_scaled - FBig::ONE) / (x_scaled + FBig::ONE)
};
let z2 = z.square();
let mut pow = z.clone();
let mut sum = z;
let mut k: usize = 3;
loop {
pow *= &z2;
let next = &sum + &pow / work_context.convert_int::<B>(k.into()).value();
if next == sum {
break;
}
sum = next;
k += 2;
}
let result: FBig<R, B> = if no_scaling {
2 * sum
} else {
2 * sum + s * work_context.ln2()
};
result.with_precision(self.precision)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::round::mode;
#[test]
fn test_iacoth() {
let context = Context::<mode::Zero>::new(10);
let binary_6 = context.iacoth::<2>(6.into()).with_precision(10).value();
assert_eq!(binary_6.repr.significand, IBig::from(689));
let decimal_6 = context.iacoth::<10>(6.into()).with_precision(10).value();
assert_eq!(decimal_6.repr.significand, IBig::from(1682361183));
let context = Context::<mode::Zero>::new(40);
let decimal_6 = context.iacoth::<10>(6.into()).with_precision(40).value();
assert_eq!(
decimal_6.repr.significand,
IBig::from_str_radix("1682361183106064652522967051084960450557", 10).unwrap()
);
let context = Context::<mode::Zero>::new(201);
let binary_6 = context.iacoth::<2>(6.into()).with_precision(201).value();
assert_eq!(
binary_6.repr.significand,
IBig::from_str_radix(
"2162760151454160450909229890833066944953539957685348083415205",
10
)
.unwrap()
);
}
#[test]
fn test_ln2_ln10() {
let context = Context::<mode::Zero>::new(45);
let decimal_ln2 = context.ln2::<10>().with_precision(45).value();
assert_eq!(
decimal_ln2.repr.significand,
IBig::from_str_radix("693147180559945309417232121458176568075500134", 10).unwrap()
);
let decimal_ln10 = context.ln10::<10>().with_precision(45).value();
assert_eq!(
decimal_ln10.repr.significand,
IBig::from_str_radix("230258509299404568401799145468436420760110148", 10).unwrap()
);
let context = Context::<mode::Zero>::new(180);
let binary_ln2 = context.ln2::<2>().with_precision(180).value();
assert_eq!(
binary_ln2.repr.significand,
IBig::from_str_radix("1062244963371879310175186301324412638028404515790072203", 10)
.unwrap()
);
let binary_ln10 = context.ln10::<2>().with_precision(180).value();
assert_eq!(
binary_ln10.repr.significand,
IBig::from_str_radix("882175346869410758689845931257775553286341791676474847", 10)
.unwrap()
);
}
}