oxinum-float 0.1.0

Arbitrary-precision floats for OxiNum (FBig/DBig via dashu-float)
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! `atan` and `atan2` for native `BigFloat`.
//!
//! # Algorithm for `atan(x)`
//!
//! 1. **Special cases:** `atan(0) = 0`.
//!
//! 2. **Range reduction for `|x| > 1`:** `atan(x) = sign(x) · π/2 − atan(1/|x|)`.
//!    After this step, the input satisfies `|x| ≤ 1`.
//!
//! 3. **Half-angle acceleration:** apply
//!    `atan(x) = 2 · atan(x / (1 + √(1 + x²)))` repeatedly until `|x| < 2^{−32}`.
//!    Count the number of halvings `m`. This reduces to an exponentially small
//!    argument for which the Taylor series converges in O(prec / 32) terms.
//!
//! 4. **Taylor series for small `u`:**
//!    `atan(u) = u − u³/3 + u⁵/5 − …`  (convergent for `|u| < 1`).
//!    Term count: with `|u| < 2^{−32}`, the `(2k+1)`-th term is bounded by
//!    `|u|^(2k+1) / (2k+1) < 2^{−32(2k+1)} / (2k+1)`.  For `k = prec/64` this
//!    is already below `2^{−prec}`, so `n_terms = prec / 64 + 8` suffices.
//!
//! 5. **Undo half-angle:** multiply result by `2^m`.
//!
//! 6. **Undo range reduction:** if `|x| > 1`, return `sign · (π/2 − result)`.
//!
//! # Algorithm for `atan2(y, x)`
//!
//! Standard four-quadrant table:
//!
//! | y | x  | result            |
//! |---|----|-------------------|
//! | 0 | 0  | 0 (by convention) |
//! | * | >0 | atan(y/x)         |
//! | ≥0| <0 | π + atan(y/x)     |
//! | <0| <0 | −π + atan(y/x)    |
//! | ≥0| 0  | +π/2              |
//! | <0| 0  | −π/2              |

use oxinum_core::{OxiNumError, OxiNumResult, Sign};

use super::constants::pi;
use super::float::{BigFloat, RoundingMode};

// ---------------------------------------------------------------------------
// Half-angle reduction threshold
// ---------------------------------------------------------------------------

/// Apply `atan(x) = 2·atan(x / (1 + sqrt(1 + x²)))` until `|x| < 2^{-32}`.
/// Returns `(reduced_x, m)` where `m` is the number of halvings applied.
fn half_angle_reduce(x: BigFloat, prec: u32, mode: RoundingMode) -> OxiNumResult<(BigFloat, u32)> {
    let one = BigFloat::from_i64(1, prec, mode);
    let mut u = x;
    let mut m = 0u32;

    // Threshold: |u| < 2^{-32} means exponent(mantissa) < -32 relative to 1.
    // We check via to_f64: safe as long as |u| ≤ 1, which it is after the
    // range-reduction step.
    loop {
        // f64 comparison is safe here since |u| ≤ 1.
        let abs_f64 = u.abs().to_f64();
        if abs_f64 < f64::from_bits(0x3e00_0000_0000_0000u64) {
            // 2^{-31} — conservative threshold
            break;
        }
        if m >= 300 {
            // Safety cap — should never be reached for any sane precision.
            break;
        }
        // u = u / (1 + sqrt(1 + u²))
        let u_sq = u.mul_ref_with_mode(&u, mode).with_precision(prec, mode);
        let one_plus_u_sq = one.add_ref_with_mode(&u_sq, mode);
        let sqrt_val = one_plus_u_sq.sqrt(prec, mode)?;
        let denom = one.add_ref_with_mode(&sqrt_val, mode);
        u = u.div_ref_with_mode(&denom, mode)?;
        u = u.with_precision(prec, mode);
        m += 1;
    }

    Ok((u, m))
}

// ---------------------------------------------------------------------------
// Taylor series for atan on small arguments
// ---------------------------------------------------------------------------

/// Compute `atan(u)` via Taylor series for `|u| < 2^{-31}`.
///
/// `atan(u) = u − u³/3 + u⁵/5 − …`
///
/// Term count: `prec / 64 + 8` suffices because each halving reduces by
/// 32 bits, so `|u|^{2k+1}/(2k+1) < 2^{-32(2k+1)} / (2k+1) < 2^{-prec}`
/// for `k ≥ prec/64`.
fn atan_taylor(u: &BigFloat, prec: u32, mode: RoundingMode) -> OxiNumResult<BigFloat> {
    if u.is_zero() {
        return Ok(BigFloat::zero(prec));
    }

    let u_sq = u.mul_ref_with_mode(u, mode).with_precision(prec, mode);
    // term = u; result = u
    let mut term = u.clone().with_precision(prec, mode);
    let mut result = term.clone();

    let n_terms: u64 = (prec as u64) / 64 + 8;

    for k in 1..=n_terms {
        // term *= -u²;  then divide by (2k+1)
        let numer = term
            .mul_ref_with_mode(&u_sq, mode)
            .with_precision(prec, mode);
        term = numer.neg().with_precision(prec, mode);
        let denom_val = 2 * k + 1;
        let denom_i64 = denom_val.min(i64::MAX as u64) as i64;
        let denom_f = BigFloat::from_i64(denom_i64, prec, mode);
        let scaled = term
            .div_ref_with_mode(&denom_f, mode)
            .map_err(|e| OxiNumError::Precision(format!("atan_taylor denom zero: {e}").into()))?;
        result = result
            .add_ref_with_mode(&scaled, mode)
            .with_precision(prec, mode);
    }

    Ok(result.with_precision(prec, mode))
}

// ---------------------------------------------------------------------------
// Public BigFloat methods
// ---------------------------------------------------------------------------

impl BigFloat {
    /// Return `atan(self)` at `prec` bits using the given rounding mode.
    ///
    /// The result lies in `(−π/2, π/2)`.
    ///
    /// # Errors
    ///
    /// Propagates sqrt or division errors (only if internal invariants break,
    /// which should not happen for well-formed inputs).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_float::native::{BigFloat, RoundingMode};
    /// let one = BigFloat::from_i64(1, 64, RoundingMode::HalfEven);
    /// let a = one.atan(64, RoundingMode::HalfEven).expect("atan(1)");
    /// // atan(1) = π/4 ≈ 0.7853981633974483
    /// assert!((a.to_f64() - std::f64::consts::FRAC_PI_4).abs() < 1e-14);
    /// ```
    pub fn atan(&self, prec: u32, mode: RoundingMode) -> OxiNumResult<BigFloat> {
        // IEEE-754 non-finite guards
        if self.is_nan() {
            return Ok(BigFloat::nan(prec));
        }
        if self.is_infinite() {
            // atan(+Inf) = +π/2, atan(-Inf) = -π/2
            let pi_val = pi(prec.saturating_add(16))?;
            let two = BigFloat::from_i64(2, prec.saturating_add(16), mode);
            let half_pi = pi_val.div_ref_with_mode(&two, mode)?;
            return if self.sign == Sign::Negative {
                Ok(half_pi.neg().with_precision(prec, mode))
            } else {
                Ok(half_pi.with_precision(prec, mode))
            };
        }

        if self.is_zero() {
            return Ok(BigFloat::zero(prec));
        }

        // Work at higher precision to absorb rounding errors.
        let work_prec = prec.saturating_add(64);

        // We work with |x| and track the sign separately.
        let orig_sign = self.signum();
        let abs_x = self.abs().with_precision(work_prec, mode);

        let one = BigFloat::from_i64(1, work_prec, mode);

        // Step 1: Range reduction — if |x| > 1, use atan(x) = π/2 − atan(1/x).
        let (working_x, use_complement): (BigFloat, bool) = if abs_x > one.clone() {
            let inv = one.div_ref_with_mode(&abs_x, mode)?;
            (inv.with_precision(work_prec, mode), true)
        } else {
            (abs_x, false)
        };

        // Step 2: Half-angle acceleration.
        let (reduced, m) = half_angle_reduce(working_x, work_prec, mode)?;

        // Step 3: Taylor series for the small argument.
        let mut result = atan_taylor(&reduced, work_prec, mode)?;

        // Step 4: Undo half-angle by multiplying by 2^m.
        let two = BigFloat::from_i64(2, work_prec, mode);
        for _ in 0..m {
            result = result
                .mul_ref_with_mode(&two, mode)
                .with_precision(work_prec, mode);
        }

        // Step 5: Undo range reduction if |x| > 1.
        if use_complement {
            let pi_val = pi(work_prec)?;
            let two_wp = BigFloat::from_i64(2, work_prec, mode);
            let pi_over_2 = pi_val.div_ref_with_mode(&two_wp, mode)?;
            result = pi_over_2
                .sub_ref_with_mode(&result, mode)
                .with_precision(work_prec, mode);
        }

        // Step 6: Apply sign.
        if orig_sign < 0 {
            result = result.neg();
        }

        Ok(result.with_precision(prec, mode))
    }

    /// Return `atan2(self, x)` at `prec` bits using the given rounding mode.
    ///
    /// Conventionally `atan2(y, x)` — `self` is `y` and `x` is the argument.
    /// The result lies in `(−π, π]`.
    ///
    /// # Special cases
    ///
    /// - `atan2(0, 0) = 0` (by convention, not mathematically defined).
    /// - `atan2(y, 0) = ±π/2` depending on sign of `y`.
    ///
    /// # Errors
    ///
    /// Propagates errors from [`BigFloat::atan`].
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_float::native::{BigFloat, RoundingMode};
    /// let y = BigFloat::from_i64(1, 64, RoundingMode::HalfEven);
    /// let x = BigFloat::from_i64(1, 64, RoundingMode::HalfEven);
    /// let a = y.atan2(&x, 64, RoundingMode::HalfEven).expect("atan2(1,1)");
    /// // atan2(1,1) = π/4 ≈ 0.7853981633974483
    /// assert!((a.to_f64() - std::f64::consts::FRAC_PI_4).abs() < 1e-14);
    /// ```
    pub fn atan2(&self, x: &BigFloat, prec: u32, mode: RoundingMode) -> OxiNumResult<BigFloat> {
        let y = self;

        // IEEE-754 non-finite guards
        // NaN propagates immediately.
        if y.is_nan() || x.is_nan() {
            return Ok(BigFloat::nan(prec));
        }
        // Both infinite: atan2(±Inf, ±Inf) — four-quadrant results per IEEE.
        if y.is_infinite() && x.is_infinite() {
            // atan2(+Inf,+Inf)=+π/4, atan2(+Inf,-Inf)=+3π/4
            // atan2(-Inf,+Inf)=-π/4, atan2(-Inf,-Inf)=-3π/4
            let pi_val = pi(prec.saturating_add(16))?;
            let four = BigFloat::from_i64(4, prec.saturating_add(16), mode);
            let pi_over_4 = pi_val
                .div_ref_with_mode(&four, mode)?
                .with_precision(prec, mode);
            let three_pi_over_4 = {
                let three = BigFloat::from_i64(3, prec, mode);
                three
                    .mul_ref_with_mode(&pi_over_4, mode)
                    .with_precision(prec, mode)
            };
            let (mag, apply_neg) = if x.sign == Sign::Negative {
                (three_pi_over_4, y.sign == Sign::Negative)
            } else {
                (pi_over_4, y.sign == Sign::Negative)
            };
            return Ok(if apply_neg { mag.neg() } else { mag });
        }
        // y is Inf, x is finite: atan2(+Inf, x) = +π/2, atan2(-Inf, x) = -π/2
        if y.is_infinite() {
            let pi_val = pi(prec.saturating_add(16))?;
            let two = BigFloat::from_i64(2, prec.saturating_add(16), mode);
            let half_pi = pi_val
                .div_ref_with_mode(&two, mode)?
                .with_precision(prec, mode);
            return if y.sign == Sign::Negative {
                Ok(half_pi.neg())
            } else {
                Ok(half_pi)
            };
        }
        // x is Inf, y is finite: atan2(y, +Inf) = +0, atan2(y, -Inf) = ±π
        if x.is_infinite() {
            if x.sign == Sign::Positive {
                return Ok(BigFloat::zero(prec));
            } else {
                // atan2(y, -Inf) = +π if y ≥ 0, -π if y < 0
                let pi_val = pi(prec.saturating_add(16))?.with_precision(prec, mode);
                return if y.sign == Sign::Negative {
                    Ok(pi_val.neg())
                } else {
                    Ok(pi_val)
                };
            }
        }

        let y_sign = y.signum();
        let x_sign = x.signum();

        // Both zero: return 0 by convention.
        if y.is_zero() && x.is_zero() {
            return Ok(BigFloat::zero(prec));
        }

        // x = 0: result is ±π/2.
        if x.is_zero() {
            let pi_val = pi(prec.saturating_add(16))?;
            let two = BigFloat::from_i64(2, prec.saturating_add(16), mode);
            let pi_over_2 = pi_val.div_ref_with_mode(&two, mode)?;
            return if y_sign >= 0 {
                Ok(pi_over_2.with_precision(prec, mode))
            } else {
                Ok(pi_over_2.neg().with_precision(prec, mode))
            };
        }

        // x > 0: atan(y/x).
        if x_sign > 0 {
            let work_prec = prec.saturating_add(16);
            let ratio = y
                .clone()
                .with_precision(work_prec, mode)
                .div_ref_with_mode(&x.clone().with_precision(work_prec, mode), mode)?;
            return ratio.atan(prec, mode);
        }

        // x < 0: atan(y/x) ± π.
        let work_prec = prec.saturating_add(32);
        let ratio = y
            .clone()
            .with_precision(work_prec, mode)
            .div_ref_with_mode(&x.clone().with_precision(work_prec, mode), mode)?;
        let atan_val = ratio.atan(work_prec, mode)?;
        let pi_val = pi(work_prec)?;

        if y_sign >= 0 {
            // y ≥ 0, x < 0: result in (π/2, π]
            let result = pi_val.add_ref_with_mode(&atan_val, mode);
            Ok(result.with_precision(prec, mode))
        } else {
            // y < 0, x < 0: result in (−π, −π/2)
            let result = atan_val.sub_ref_with_mode(&pi_val, mode);
            Ok(result.with_precision(prec, mode))
        }
    }
}

// ---------------------------------------------------------------------------
// Internal tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn mk(n: i64, prec: u32) -> BigFloat {
        BigFloat::from_i64(n, prec, RoundingMode::HalfEven)
    }

    #[test]
    fn atan_zero() {
        let z = mk(0, 64);
        let r = z.atan(64, RoundingMode::HalfEven).expect("atan(0)");
        assert!(r.is_zero());
    }

    #[test]
    fn atan_one_is_pi_over_4() {
        let prec = 64u32;
        let one = mk(1, prec);
        let a = one.atan(prec, RoundingMode::HalfEven).expect("atan(1)");
        assert!(
            (a.to_f64() - std::f64::consts::FRAC_PI_4).abs() < 1e-14,
            "got {}",
            a.to_f64()
        );
    }

    #[test]
    fn atan_minus_one() {
        let prec = 64u32;
        let minus_one = mk(-1, prec);
        let a = minus_one
            .atan(prec, RoundingMode::HalfEven)
            .expect("atan(-1)");
        assert!((a.to_f64() + std::f64::consts::FRAC_PI_4).abs() < 1e-14);
    }

    #[test]
    fn atan2_quadrant_i() {
        let prec = 64u32;
        let mode = RoundingMode::HalfEven;
        let one = mk(1, prec);
        let a = one.atan2(&one, prec, mode).expect("atan2(1,1)");
        assert!((a.to_f64() - std::f64::consts::FRAC_PI_4).abs() < 1e-14);
    }

    #[test]
    fn atan2_negative_x() {
        let prec = 64u32;
        let mode = RoundingMode::HalfEven;
        let one = mk(1, prec);
        let neg_one = mk(-1, prec);
        // atan2(1, -1) = 3π/4
        let a = one.atan2(&neg_one, prec, mode).expect("atan2(1,-1)");
        let expected = 3.0 * std::f64::consts::FRAC_PI_4;
        assert!(
            (a.to_f64() - expected).abs() < 1e-13,
            "got {}, expected {}",
            a.to_f64(),
            expected
        );
    }

    #[test]
    fn atan2_zero_x_positive_y() {
        let prec = 64u32;
        let mode = RoundingMode::HalfEven;
        let one = mk(1, prec);
        let zero = mk(0, prec);
        let a = one.atan2(&zero, prec, mode).expect("atan2(1,0)");
        assert!((a.to_f64() - std::f64::consts::FRAC_PI_2).abs() < 1e-14);
    }

    #[test]
    fn atan2_zero_x_negative_y() {
        let prec = 64u32;
        let mode = RoundingMode::HalfEven;
        let neg_one = mk(-1, prec);
        let zero = mk(0, prec);
        let a = neg_one.atan2(&zero, prec, mode).expect("atan2(-1,0)");
        assert!((a.to_f64() + std::f64::consts::FRAC_PI_2).abs() < 1e-14);
    }
}