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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#[cfg(feature = "full")]
use core::num::FpCategory;

use crate::{DomainError, NotImplementedError};

/// Computes the arccosine of the given value. Returns results in the range
/// `(0..=PI)`.
///
/// Domain: [-1, 1]
///
/// Codomain: [0, PI]
///
/// # Examples
///
/// ```
/// # use spinoso_math::PI;
/// use spinoso_math as math;
/// assert_eq!(math::acos(0.0), Ok(PI / 2.0));
/// assert!(math::acos(100.0).is_err());
///
/// assert!(matches!(math::acos(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the arccosine is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn acos(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.acos();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "acos""#,
        ))
    } else {
        Ok(result)
    }
}

/// Computes the inverse hyperbolic cosine of the given value.
///
/// Domain: [1, INFINITY)
///
/// Codomain: [0, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::acosh(1.0), Ok(0.0));
/// assert!(math::acosh(0.0).is_err());
///
/// assert!(matches!(math::acosh(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the inverse hyperbolic cosine is [`NAN`], a
/// domain error is returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn acosh(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.acosh();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "acosh""#,
        ))
    } else {
        Ok(result)
    }
}

/// Computes the arcsine of the given value. Returns results in the range
/// `(-PI/2..=PI/2)`.
///
/// Domain: [-1, -1]
///
/// Codomain: [-PI/2, PI/2]
///
/// # Examples
///
/// ```
/// # use spinoso_math::PI;
/// use spinoso_math as math;
/// assert_eq!(math::asin(1.0), Ok(PI / 2.0));
/// assert!(math::asin(100.0).is_err());
///
/// assert!(matches!(math::asin(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the arcsine is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn asin(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.asin();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "asin""#,
        ))
    } else {
        Ok(result)
    }
}

/// Computes the inverse hyperbolic sine of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert!((math::asinh(1.0) - 0.881373587019543).abs() < f64::EPSILON);
/// ```
#[inline]
#[must_use]
pub fn asinh(value: f64) -> f64 {
    value.asinh()
}

/// Computes the arctangent of the given value. Returns results in the range
/// `(-PI/2..=PI/2)`.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-PI/2, PI/2)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::atan(0.0), 0.0);
/// ```
#[inline]
#[must_use]
pub fn atan(value: f64) -> f64 {
    value.atan()
}

/// Computes the four quadrant arctangent of `value` (`y`) and `other` (`x`) in
/// radians.
///
/// Return value is a angle in radians between the positive x-axis of Cartesian
/// plane and the point given by the coordinates (`x`, `y`) on it.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: [-PI, PI]
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert!((math::atan2(-0.0, -1.0) - (-3.141592653589793)).abs() < f64::EPSILON);
/// assert!((math::atan2(-1.0, -1.0) - (-2.356194490192345)).abs() < f64::EPSILON);
/// assert!((math::atan2(-1.0, 0.0) - (-1.5707963267948966)).abs() < f64::EPSILON);
/// assert!((math::atan2(-1.0, 1.0) - (-0.7853981633974483)).abs() < f64::EPSILON);
/// assert!(math::atan2(-0.0, 1.0) == -0.0);
/// assert!(math::atan2(0.0, 1.0) == 0.0);
/// assert!((math::atan2(1.0, 1.0) - 0.7853981633974483).abs() < f64::EPSILON);
/// assert!((math::atan2(1.0, 0.0) - 1.5707963267948966).abs() < f64::EPSILON);
/// assert!((math::atan2(1.0, -1.0) - 2.356194490192345).abs() < f64::EPSILON);
/// assert!((math::atan2(0.0, -1.0) - 3.141592653589793).abs() < f64::EPSILON);
/// assert!((math::atan2(f64::INFINITY, f64::INFINITY) - 0.7853981633974483).abs() < f64::EPSILON);
/// assert!(
///     (math::atan2(f64::INFINITY, f64::NEG_INFINITY) - 2.356194490192345).abs() < f64::EPSILON
/// );
/// assert!(
///     (math::atan2(f64::NEG_INFINITY, f64::INFINITY) - (-0.7853981633974483)).abs()
///         < f64::EPSILON
/// );
/// assert!(
///     (math::atan2(f64::NEG_INFINITY, f64::NEG_INFINITY) - (-2.356194490192345)).abs()
///         < f64::EPSILON
/// );
/// ```
#[inline]
#[must_use]
pub fn atan2(value: f64, other: f64) -> f64 {
    value.atan2(other)
}

/// Computes the inverse hyperbolic tangent of the given value.
///
/// Domain: (-1, 1)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::atanh(1.0), Ok(f64::INFINITY));
/// assert!(math::atanh(100.0).is_err());
///
/// assert!(matches!(math::atanh(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the inverse hyperbolic tangent is [`NAN`]
/// a domain error is returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn atanh(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.atanh();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "atanh""#,
        ))
    } else {
        Ok(result)
    }
}

/// Returns the cube root of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert!((math::cbrt(-9.0) - (-2.080083823051904)).abs() < f64::EPSILON);
/// assert!((math::cbrt(9.0) - 2.080083823051904).abs() < f64::EPSILON);
/// ```
#[inline]
#[must_use]
pub fn cbrt(value: f64) -> f64 {
    value.cbrt()
}

/// Computes the cosine of the given value (expressed in radians). Returns
/// values in the range `-1.0..=1.0`.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: [-1, 1]
///
/// # Examples
///
/// ```
/// # use spinoso_math::PI;
/// use spinoso_math as math;
/// assert_eq!(math::cos(PI), -1.0);
/// ```
#[inline]
#[must_use]
pub fn cos(value: f64) -> f64 {
    value.cos()
}

/// Computes the hyperbolic cosine of the given value (expressed in radians).
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: [1, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::cosh(0.0), 1.0);
/// ```
#[inline]
#[must_use]
pub fn cosh(value: f64) -> f64 {
    value.cosh()
}

/// Calculates the error function of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-1, 1)
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[inline]
#[cfg(not(feature = "full"))]
pub fn erf(value: f64) -> Result<f64, NotImplementedError> {
    let _ = value;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::erf support",
    ))
}

/// Calculates the error function of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-1, 1)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::erf(0.0), Ok(0.0));
/// ```
///
/// # Errors
///
/// Because `spinoso-math` was built with the `full` feature, this function will
/// always succeed and return the error function of the given value.
#[inline]
#[cfg(feature = "full")]
pub fn erf(value: f64) -> Result<f64, NotImplementedError> {
    let result = libm::erf(value);
    Ok(result)
}

/// Calculates the complementary error function of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (0, 2)
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[inline]
#[cfg(not(feature = "full"))]
pub fn erfc(value: f64) -> Result<f64, NotImplementedError> {
    let _ = value;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::erfc support",
    ))
}

/// Calculates the complementary error function of the given value.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (0, 2)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::erfc(0.0), Ok(1.0));
/// ```
///
/// # Errors
///
/// Because `spinoso-math` was built with the `full` feature, this function will
/// always succeed and return the complementary error function of the given
/// value.
#[inline]
#[cfg(feature = "full")]
pub fn erfc(value: f64) -> Result<f64, NotImplementedError> {
    let result = libm::erfc(value);
    Ok(result)
}

/// Returns `e**x`.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (0, INFINITY)
///
/// # Examples
///
/// ```
/// # use spinoso_math::E;
/// use spinoso_math as math;
/// assert_eq!(math::exp(0.0), 1.0);
/// assert_eq!(math::exp(1.0), E);
/// assert!((math::exp(1.5) - 4.4816890703380645).abs() < f64::EPSILON);
/// ```
#[inline]
#[must_use]
pub fn exp(value: f64) -> f64 {
    value.exp()
}

/// Returns a tuple array containing the normalized fraction (a Float) and
/// exponent (an Integer) of the given value.
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[inline]
#[cfg(not(feature = "full"))]
pub const fn frexp(value: f64) -> Result<(f64, i32), NotImplementedError> {
    let _ = value;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::frexp support",
    ))
}

/// Returns a tuple array containing the normalized fraction (a Float) and
/// exponent (an Integer) of the given value.
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// # fn example() -> Result<(), math::NotImplementedError> {
/// let (fraction, exponent) = math::frexp(1234.0)?;
/// let float = math::ldexp(fraction, exponent)?;
/// assert_eq!(float, 1234.0);
/// # Ok(())
/// # }
/// # example().unwrap();
/// ```
///
/// # Errors
///
/// Because `spinoso-math` was built with the `full` feature, this function will
/// always succeed and return the normalized fraction and exponent of the given
/// value.
#[inline]
#[cfg(feature = "full")]
pub fn frexp(value: f64) -> Result<(f64, i32), NotImplementedError> {
    let result = libm::frexp(value);
    Ok(result)
}

/// Calculates the gamma function of the given value.
///
/// Note that `gamma(n)` is same as `fact(n-1)` for integer `n > 0`. However
/// `gamma(n)` returns float and can be an approximation.
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[cfg(not(feature = "full"))]
pub const fn gamma(value: f64) -> Result<f64, NotImplementedError> {
    let _ = value;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::gamma support",
    ))
}

/// Calculates the gamma function of the given value.
///
/// Note that `gamma(n)` is same as `fact(n-1)` for integer `n > 0`. However
/// `gamma(n)` returns float and can be an approximation.
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::gamma(1.0), Ok(1.0));
/// assert_eq!(math::gamma(2.0), Ok(1.0));
/// assert_eq!(math::gamma(3.0), Ok(2.0));
/// assert_eq!(math::gamma(4.0), Ok(6.0));
/// assert_eq!(math::gamma(5.0), Ok(24.0));
/// assert_eq!(math::gamma(20.0), Ok(1.21645100408832e+17));
///
/// assert!(math::gamma(-15.0).is_err());
/// assert!(matches!(math::gamma(-15.1), Ok(result) if (result - 5.9086389724319095e-12).abs() < f64::EPSILON));
///
/// assert!(math::gamma(f64::NEG_INFINITY).is_err());
/// assert_eq!(math::gamma(f64::INFINITY), Ok(f64::INFINITY));
/// ```
///
/// # Errors
///
/// If the given value is negative, a domain error is returned.
#[inline]
#[cfg(feature = "full")]
pub fn gamma(value: f64) -> Result<f64, DomainError> {
    // `gamma(n)` is the same as `n!` for integer `n > 0`. `gamma` returns float
    // and might be an approximation so include a lookup table for as many `n`
    // as can fit in the float mantissa.
    const FACTORIAL_TABLE: [f64; 23] = [
        1.0_f64,                         // fact(0)
        1.0,                             // fact(1)
        2.0,                             // fact(2)
        6.0,                             // fact(3)
        24.0,                            // fact(4)
        120.0,                           // fact(5)
        720.0,                           // fact(6)
        5_040.0,                         // fact(7)
        40_320.0,                        // fact(8)
        362_880.0,                       // fact(9)
        3_628_800.0,                     // fact(10)
        39_916_800.0,                    // fact(11)
        479_001_600.0,                   // fact(12)
        6_227_020_800.0,                 // fact(13)
        87_178_291_200.0,                // fact(14)
        1_307_674_368_000.0,             // fact(15)
        20_922_789_888_000.0,            // fact(16)
        355_687_428_096_000.0,           // fact(17)
        6_402_373_705_728_000.0,         // fact(18)
        121_645_100_408_832_000.0,       // fact(19)
        2_432_902_008_176_640_000.0,     // fact(20)
        51_090_942_171_709_440_000.0,    // fact(21)
        1_124_000_727_777_607_680_000.0, // fact(22)
    ];
    match value {
        value if value.is_infinite() && value.is_sign_negative() => Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "gamma""#,
        )),
        value if value.is_infinite() => Ok(f64::INFINITY),
        value if matches!(value.classify(), FpCategory::Zero) && value.is_sign_negative() => Ok(f64::NEG_INFINITY),
        value if matches!(value.classify(), FpCategory::Zero) => Ok(f64::INFINITY),
        value if (value - value.floor()).abs() < f64::EPSILON && value.is_sign_negative() => Err(
            DomainError::with_message(r#"Numerical argument is out of domain - "gamma""#),
        ),
        value if (value - value.floor()).abs() < f64::EPSILON => {
            #[allow(clippy::cast_possible_truncation)]
            let idx = (value as i64).checked_sub(1).map(usize::try_from);
            if let Some(Ok(idx)) = idx {
                if let Some(&result) = FACTORIAL_TABLE.get(idx) {
                    return Ok(result);
                }
            }
            let result = libm::tgamma(value);
            Ok(result)
        }
        value => {
            let result = libm::tgamma(value);
            Ok(result)
        }
    }
}

/// Returns `sqrt(x**2 + y**2)`, the hypotenuse of a right-angled triangle with
/// sides x and y.
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::hypot(3.0, 4.0), 5.0);
/// ```
#[inline]
#[must_use]
pub fn hypot(x: f64, y: f64) -> f64 {
    x.hypot(y)
}

/// Returns the value of `fraction * (2**exponent)`.
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[cfg(not(feature = "full"))]
pub fn ldexp(fraction: f64, exponent: i32) -> Result<f64, NotImplementedError> {
    let _ = fraction;
    let _ = exponent;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::ldexp support",
    ))
}

/// Returns the value of `fraction * (2**exponent)`.
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// # fn example() -> Result<(), math::NotImplementedError> {
/// let (fraction, exponent) = math::frexp(1234.0)?;
/// let float = math::ldexp(fraction, exponent)?;
/// assert_eq!(float, 1234.0);
/// # Ok(())
/// # }
/// # example().unwrap();
/// ```
///
/// # Errors
///
/// Because `spinoso-math` was built with the `full` feature, this function will
/// always succeed and return the float determined by the given fraction and
/// exponent.
#[inline]
#[cfg(feature = "full")]
pub fn ldexp(fraction: f64, exponent: i32) -> Result<f64, NotImplementedError> {
    let result = libm::ldexp(fraction, exponent);
    Ok(result)
}

/// Calculates the logarithmic gamma of value and the sign of gamma of value.
///
/// `lgamma` is same as:
///
/// ```ruby
/// [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
/// ```
///
/// but avoids overflow of `gamma` for large values.
///
/// # Errors
///
/// Because `spinoso-math` was built without the `full` feature, this function
/// will always return a not implemented error.
#[inline]
#[cfg(not(feature = "full"))]
pub fn lgamma(value: f64) -> Result<(f64, i32), NotImplementedError> {
    let _ = value;
    Err(NotImplementedError::with_message(
        "Artichoke was not built with Math::lgamma support",
    ))
}

/// Calculates the logarithmic gamma of value and the sign of gamma of value.
///
/// `lgamma` is same as:
///
/// ```ruby
/// [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
/// ```
///
/// but avoids overflow of `gamma` for large values.
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::lgamma(0.0), Ok((f64::INFINITY, 1)));
///
/// assert!(math::lgamma(f64::NEG_INFINITY).is_err());
/// ```
///
/// # Errors
///
/// If the given value is [negative infinity], an error is returned.
///
/// [negative infinity]: f64::NEG_INFINITY
#[inline]
#[cfg(feature = "full")]
pub fn lgamma(value: f64) -> Result<(f64, i32), DomainError> {
    if value.is_infinite() && value.is_sign_negative() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "lgamma""#,
        ))
    } else {
        let (result, sign) = libm::lgamma_r(value);
        Ok((result, sign))
    }
}

/// Returns the logarithm of the number with respect to an arbitrary base.
///
/// Domain: (0, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// # use spinoso_math::E;
/// use spinoso_math as math;
/// assert_eq!(math::log(1.0, None), Ok(0.0));
/// assert_eq!(math::log(E, None), Ok(1.0));
/// assert_eq!(math::log(64.0, Some(4.0)), Ok(3.0));
///
/// assert_eq!(math::log(0.0, None), Ok(f64::NEG_INFINITY));
/// assert!(math::log(-0.1, None).is_err());
///
/// assert!(matches!(math::log(f64::NAN, None), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the given arbitrary base is [`NAN`], a domain error is returned.
///
/// If the result of computing the logarithm is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn log(value: f64, base: Option<f64>) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = match base {
        Some(base) if base.is_nan() => return Ok(f64::NAN),
        Some(base) => value.log(base),
        None => value.ln(),
    };
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "log""#,
        ))
    } else {
        Ok(result)
    }
}

/// Returns the base 10 logarithm of the number.
///
/// Domain: (0, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::log10(1.0), Ok(0.0));
/// assert_eq!(math::log10(10.0), Ok(1.0));
/// assert_eq!(math::log10(1e100), Ok(100.0));
///
/// assert_eq!(math::log10(0.0), Ok(f64::NEG_INFINITY));
/// assert!(math::log10(-0.1).is_err());
///
/// assert!(matches!(math::log10(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the logarithm is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn log10(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.log10();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "log10""#,
        ))
    } else {
        Ok(result)
    }
}

/// Returns the base 2 logarithm of the number.
///
/// Domain: (0, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::log2(1.0), Ok(0.0));
/// assert_eq!(math::log2(2.0), Ok(1.0));
/// assert_eq!(math::log2(32768.0), Ok(15.0));
/// assert_eq!(math::log2(65536.0), Ok(16.0));
///
/// assert_eq!(math::log2(0.0), Ok(f64::NEG_INFINITY));
/// assert!(math::log2(-0.1).is_err());
///
/// assert!(matches!(math::log2(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the logarithm is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn log2(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.log2();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "log2""#,
        ))
    } else {
        Ok(result)
    }
}

/// Computes the sine of the given value (expressed in radians). Returns a Float
/// in the range `-1.0..=1.0`.
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: [-1, 1]
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::sin(math::PI / 2.0), 1.0);
/// ```
#[inline]
#[must_use]
pub fn sin(value: f64) -> f64 {
    value.sin()
}

/// Computes the hyperbolic sine of the given value (expressed in radians).
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::sinh(0.0), 0.0);
/// ```
#[inline]
#[must_use]
pub fn sinh(value: f64) -> f64 {
    value.sinh()
}

/// Returns the non-negative square root of the given value.
///
/// Domain: [0, INFINITY)
///
/// Codomain: [0, INFINITY)
///
/// # Examples
///
/// ```
/// # use spinoso_math::DomainError;
/// use spinoso_math as math;
/// assert_eq!(math::sqrt(0.0), Ok(0.0));
/// assert_eq!(math::sqrt(1.0), Ok(1.0));
/// assert_eq!(math::sqrt(9.0), Ok(3.0));
///
/// assert!(math::sqrt(-9.0).is_err());
///
/// assert!(matches!(math::sqrt(f64::NAN), Ok(result) if result.is_nan()));
/// ```
///
/// # Errors
///
/// If the result of computing the square root is [`NAN`], a domain error is
/// returned.
///
/// [`NAN`]: f64::NAN
#[inline]
pub fn sqrt(value: f64) -> Result<f64, DomainError> {
    if value.is_nan() {
        return Ok(f64::NAN);
    }
    let result = value.sqrt();
    if result.is_nan() {
        Err(DomainError::with_message(
            r#"Numerical argument is out of domain - "sqrt""#,
        ))
    } else {
        Ok(result)
    }
}

/// Computes the tangent of the given value (expressed in radians).
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-INFINITY, INFINITY)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::tan(0.0), 0.0);
/// ```
#[inline]
#[must_use]
pub fn tan(value: f64) -> f64 {
    value.tan()
}

/// Computes the hyperbolic tangent of the given value (expressed in radians).
///
/// Domain: (-INFINITY, INFINITY)
///
/// Codomain: (-1, 1)
///
/// # Examples
///
/// ```
/// use spinoso_math as math;
/// assert_eq!(math::tanh(0.0), 0.0);
/// ```
#[inline]
#[must_use]
pub fn tanh(value: f64) -> f64 {
    value.tanh()
}