Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 24c932d

Browse files
committedApr 29, 2025·
Move applicable float tests from coretests back to std
The previous commit moved all test files from `std` to `core` so git understands the move. Not all functionality is actually testable in `core`, however, so perform move the relevant portions back. Changes from inherent to module methods is also done since this is the form of math operations available in `core` (as `core_float_math`).
1 parent 510bc1f commit 24c932d

File tree

12 files changed

+1346
-1201
lines changed

12 files changed

+1346
-1201
lines changed
 

‎compiler/rustc_codegen_cranelift/patches/0027-sysroot_tests-128bit-atomic-operations.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ index 1e336bf..35e6f54 100644
1616
+++ b/coretests/tests/lib.rs
1717
@@ -2,5 +2,4 @@
1818
// tidy-alphabetical-start
19+
#![cfg_attr(not(bootstrap), feature(cfg_target_has_reliable_f16_f128))]
1920
-#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
2021
#![cfg_attr(test, feature(cfg_match))]
2122
#![feature(alloc_layout_extra)]
22-
#![feature(array_chunks)]
2323
diff --git a/coretests/tests/atomic.rs b/coretests/tests/atomic.rs
2424
index b735957..ea728b6 100644
2525
--- a/coretests/tests/atomic.rs

‎library/coretests/tests/floats/f128.rs

Lines changed: 3 additions & 298 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@ use std::ops::{Add, Div, Mul, Sub};
1212

1313
// Note these tolerances make sense around zero, but not for more extreme exponents.
1414

15-
/// For operations that are near exact, usually not involving math of different
16-
/// signs.
17-
const TOL_PRECISE: f128 = 1e-28;
18-
1915
/// Default tolerances. Works for values that should be near precise but not exact. Roughly
2016
/// the precision carried by `100 * 100`.
2117
const TOL: f128 = 1e-12;
2218

23-
/// Tolerances for math that is allowed to be imprecise, usually due to multiple chained
24-
/// operations.
25-
#[cfg(not(miri))]
26-
#[cfg(not(bootstrap))]
27-
#[cfg(target_has_reliable_f128_math)]
28-
const TOL_IMPR: f128 = 1e-10;
19+
/// For operations that are near exact, usually not involving math of different
20+
/// signs.
21+
const TOL_PRECISE: f128 = 1e-28;
2922

3023
/// Smallest number
3124
const TINY_BITS: u128 = 0x1;
@@ -517,8 +510,6 @@ fn test_recip() {
517510
assert_eq!(neg_inf.recip(), 0.0);
518511
}
519512

520-
// Many math functions allow for less accurate results, so the next tolerance up is used
521-
522513
#[test]
523514
#[cfg(not(miri))]
524515
#[cfg(not(bootstrap))]
@@ -536,25 +527,6 @@ fn test_powi() {
536527
assert_eq!(neg_inf.powi(2), inf);
537528
}
538529

539-
#[test]
540-
#[cfg(not(miri))]
541-
#[cfg(not(bootstrap))]
542-
#[cfg(target_has_reliable_f128_math)]
543-
fn test_powf() {
544-
let nan: f128 = f128::NAN;
545-
let inf: f128 = f128::INFINITY;
546-
let neg_inf: f128 = f128::NEG_INFINITY;
547-
assert_eq!(1.0f128.powf(1.0), 1.0);
548-
assert_approx_eq!(3.4f128.powf(4.5), 246.40818323761892815995637964326426756, TOL_IMPR);
549-
assert_approx_eq!(2.7f128.powf(-3.2), 0.041652009108526178281070304373500889273, TOL_IMPR);
550-
assert_approx_eq!((-3.1f128).powf(2.0), 9.6100000000000005506706202140776519387, TOL_IMPR);
551-
assert_approx_eq!(5.9f128.powf(-2.0), 0.028727377190462507313100483690639638451, TOL_IMPR);
552-
assert_eq!(8.3f128.powf(0.0), 1.0);
553-
assert!(nan.powf(2.0).is_nan());
554-
assert_eq!(inf.powf(2.0), inf);
555-
assert_eq!(neg_inf.powf(3.0), neg_inf);
556-
}
557-
558530
#[test]
559531
#[cfg(not(miri))]
560532
#[cfg(not(bootstrap))]
@@ -569,117 +541,6 @@ fn test_sqrt_domain() {
569541
assert_eq!(f128::INFINITY.sqrt(), f128::INFINITY);
570542
}
571543

572-
#[test]
573-
#[cfg(not(miri))]
574-
#[cfg(not(bootstrap))]
575-
#[cfg(target_has_reliable_f128_math)]
576-
fn test_exp() {
577-
assert_eq!(1.0, 0.0f128.exp());
578-
assert_approx_eq!(consts::E, 1.0f128.exp(), TOL);
579-
assert_approx_eq!(148.41315910257660342111558004055227962348775, 5.0f128.exp(), TOL);
580-
581-
let inf: f128 = f128::INFINITY;
582-
let neg_inf: f128 = f128::NEG_INFINITY;
583-
let nan: f128 = f128::NAN;
584-
assert_eq!(inf, inf.exp());
585-
assert_eq!(0.0, neg_inf.exp());
586-
assert!(nan.exp().is_nan());
587-
}
588-
589-
#[test]
590-
#[cfg(not(miri))]
591-
#[cfg(not(bootstrap))]
592-
#[cfg(target_has_reliable_f128_math)]
593-
fn test_exp2() {
594-
assert_eq!(32.0, 5.0f128.exp2());
595-
assert_eq!(1.0, 0.0f128.exp2());
596-
597-
let inf: f128 = f128::INFINITY;
598-
let neg_inf: f128 = f128::NEG_INFINITY;
599-
let nan: f128 = f128::NAN;
600-
assert_eq!(inf, inf.exp2());
601-
assert_eq!(0.0, neg_inf.exp2());
602-
assert!(nan.exp2().is_nan());
603-
}
604-
605-
#[test]
606-
#[cfg(not(miri))]
607-
#[cfg(not(bootstrap))]
608-
#[cfg(target_has_reliable_f128_math)]
609-
fn test_ln() {
610-
let nan: f128 = f128::NAN;
611-
let inf: f128 = f128::INFINITY;
612-
let neg_inf: f128 = f128::NEG_INFINITY;
613-
assert_approx_eq!(1.0f128.exp().ln(), 1.0, TOL);
614-
assert!(nan.ln().is_nan());
615-
assert_eq!(inf.ln(), inf);
616-
assert!(neg_inf.ln().is_nan());
617-
assert!((-2.3f128).ln().is_nan());
618-
assert_eq!((-0.0f128).ln(), neg_inf);
619-
assert_eq!(0.0f128.ln(), neg_inf);
620-
assert_approx_eq!(4.0f128.ln(), 1.3862943611198906188344642429163531366, TOL);
621-
}
622-
623-
#[test]
624-
#[cfg(not(miri))]
625-
#[cfg(not(bootstrap))]
626-
#[cfg(target_has_reliable_f128_math)]
627-
fn test_log() {
628-
let nan: f128 = f128::NAN;
629-
let inf: f128 = f128::INFINITY;
630-
let neg_inf: f128 = f128::NEG_INFINITY;
631-
assert_eq!(10.0f128.log(10.0), 1.0);
632-
assert_approx_eq!(2.3f128.log(3.5), 0.66485771361478710036766645911922010272, TOL);
633-
assert_eq!(1.0f128.exp().log(1.0f128.exp()), 1.0);
634-
assert!(1.0f128.log(1.0).is_nan());
635-
assert!(1.0f128.log(-13.9).is_nan());
636-
assert!(nan.log(2.3).is_nan());
637-
assert_eq!(inf.log(10.0), inf);
638-
assert!(neg_inf.log(8.8).is_nan());
639-
assert!((-2.3f128).log(0.1).is_nan());
640-
assert_eq!((-0.0f128).log(2.0), neg_inf);
641-
assert_eq!(0.0f128.log(7.0), neg_inf);
642-
}
643-
644-
#[test]
645-
#[cfg(not(miri))]
646-
#[cfg(not(bootstrap))]
647-
#[cfg(target_has_reliable_f128_math)]
648-
fn test_log2() {
649-
let nan: f128 = f128::NAN;
650-
let inf: f128 = f128::INFINITY;
651-
let neg_inf: f128 = f128::NEG_INFINITY;
652-
assert_approx_eq!(10.0f128.log2(), 3.32192809488736234787031942948939017, TOL);
653-
assert_approx_eq!(2.3f128.log2(), 1.2016338611696504130002982471978765921, TOL);
654-
assert_approx_eq!(1.0f128.exp().log2(), 1.4426950408889634073599246810018921381, TOL);
655-
assert!(nan.log2().is_nan());
656-
assert_eq!(inf.log2(), inf);
657-
assert!(neg_inf.log2().is_nan());
658-
assert!((-2.3f128).log2().is_nan());
659-
assert_eq!((-0.0f128).log2(), neg_inf);
660-
assert_eq!(0.0f128.log2(), neg_inf);
661-
}
662-
663-
#[test]
664-
#[cfg(not(miri))]
665-
#[cfg(not(bootstrap))]
666-
#[cfg(target_has_reliable_f128_math)]
667-
fn test_log10() {
668-
let nan: f128 = f128::NAN;
669-
let inf: f128 = f128::INFINITY;
670-
let neg_inf: f128 = f128::NEG_INFINITY;
671-
assert_eq!(10.0f128.log10(), 1.0);
672-
assert_approx_eq!(2.3f128.log10(), 0.36172783601759284532595218865859309898, TOL);
673-
assert_approx_eq!(1.0f128.exp().log10(), 0.43429448190325182765112891891660508222, TOL);
674-
assert_eq!(1.0f128.log10(), 0.0);
675-
assert!(nan.log10().is_nan());
676-
assert_eq!(inf.log10(), inf);
677-
assert!(neg_inf.log10().is_nan());
678-
assert!((-2.3f128).log10().is_nan());
679-
assert_eq!((-0.0f128).log10(), neg_inf);
680-
assert_eq!(0.0f128.log10(), neg_inf);
681-
}
682-
683544
#[test]
684545
fn test_to_degrees() {
685546
let pi: f128 = consts::PI;
@@ -712,162 +573,6 @@ fn test_to_radians() {
712573
assert_eq!(neg_inf.to_radians(), neg_inf);
713574
}
714575

715-
#[test]
716-
#[cfg(not(miri))]
717-
#[cfg(not(bootstrap))]
718-
#[cfg(target_has_reliable_f128_math)]
719-
fn test_asinh() {
720-
// Lower accuracy results are allowed, use increased tolerances
721-
assert_eq!(0.0f128.asinh(), 0.0f128);
722-
assert_eq!((-0.0f128).asinh(), -0.0f128);
723-
724-
let inf: f128 = f128::INFINITY;
725-
let neg_inf: f128 = f128::NEG_INFINITY;
726-
let nan: f128 = f128::NAN;
727-
assert_eq!(inf.asinh(), inf);
728-
assert_eq!(neg_inf.asinh(), neg_inf);
729-
assert!(nan.asinh().is_nan());
730-
assert!((-0.0f128).asinh().is_sign_negative());
731-
732-
// issue 63271
733-
assert_approx_eq!(2.0f128.asinh(), 1.443635475178810342493276740273105f128, TOL_IMPR);
734-
assert_approx_eq!((-2.0f128).asinh(), -1.443635475178810342493276740273105f128, TOL_IMPR);
735-
// regression test for the catastrophic cancellation fixed in 72486
736-
assert_approx_eq!(
737-
(-67452098.07139316f128).asinh(),
738-
-18.720075426274544393985484294000831757220,
739-
TOL_IMPR
740-
);
741-
742-
// test for low accuracy from issue 104548
743-
assert_approx_eq!(60.0f128, 60.0f128.sinh().asinh(), TOL_IMPR);
744-
// mul needed for approximate comparison to be meaningful
745-
assert_approx_eq!(1.0f128, 1e-15f128.sinh().asinh() * 1e15f128, TOL_IMPR);
746-
}
747-
748-
#[test]
749-
#[cfg(not(miri))]
750-
#[cfg(not(bootstrap))]
751-
#[cfg(target_has_reliable_f128_math)]
752-
fn test_acosh() {
753-
assert_eq!(1.0f128.acosh(), 0.0f128);
754-
assert!(0.999f128.acosh().is_nan());
755-
756-
let inf: f128 = f128::INFINITY;
757-
let neg_inf: f128 = f128::NEG_INFINITY;
758-
let nan: f128 = f128::NAN;
759-
assert_eq!(inf.acosh(), inf);
760-
assert!(neg_inf.acosh().is_nan());
761-
assert!(nan.acosh().is_nan());
762-
assert_approx_eq!(2.0f128.acosh(), 1.31695789692481670862504634730796844f128, TOL_IMPR);
763-
assert_approx_eq!(3.0f128.acosh(), 1.76274717403908605046521864995958461f128, TOL_IMPR);
764-
765-
// test for low accuracy from issue 104548
766-
assert_approx_eq!(60.0f128, 60.0f128.cosh().acosh(), TOL_IMPR);
767-
}
768-
769-
#[test]
770-
#[cfg(not(miri))]
771-
#[cfg(not(bootstrap))]
772-
#[cfg(target_has_reliable_f128_math)]
773-
fn test_atanh() {
774-
assert_eq!(0.0f128.atanh(), 0.0f128);
775-
assert_eq!((-0.0f128).atanh(), -0.0f128);
776-
777-
let inf: f128 = f128::INFINITY;
778-
let neg_inf: f128 = f128::NEG_INFINITY;
779-
let nan: f128 = f128::NAN;
780-
assert_eq!(1.0f128.atanh(), inf);
781-
assert_eq!((-1.0f128).atanh(), neg_inf);
782-
assert!(2f128.atanh().atanh().is_nan());
783-
assert!((-2f128).atanh().atanh().is_nan());
784-
assert!(inf.atanh().is_nan());
785-
assert!(neg_inf.atanh().is_nan());
786-
assert!(nan.atanh().is_nan());
787-
assert_approx_eq!(0.5f128.atanh(), 0.54930614433405484569762261846126285f128, TOL_IMPR);
788-
assert_approx_eq!((-0.5f128).atanh(), -0.54930614433405484569762261846126285f128, TOL_IMPR);
789-
}
790-
791-
#[test]
792-
#[cfg(not(miri))]
793-
#[cfg(not(bootstrap))]
794-
#[cfg(target_has_reliable_f128_math)]
795-
fn test_gamma() {
796-
// precision can differ among platforms
797-
assert_approx_eq!(1.0f128.gamma(), 1.0f128, TOL_IMPR);
798-
assert_approx_eq!(2.0f128.gamma(), 1.0f128, TOL_IMPR);
799-
assert_approx_eq!(3.0f128.gamma(), 2.0f128, TOL_IMPR);
800-
assert_approx_eq!(4.0f128.gamma(), 6.0f128, TOL_IMPR);
801-
assert_approx_eq!(5.0f128.gamma(), 24.0f128, TOL_IMPR);
802-
assert_approx_eq!(0.5f128.gamma(), consts::PI.sqrt(), TOL_IMPR);
803-
assert_approx_eq!((-0.5f128).gamma(), -2.0 * consts::PI.sqrt(), TOL_IMPR);
804-
assert_eq!(0.0f128.gamma(), f128::INFINITY);
805-
assert_eq!((-0.0f128).gamma(), f128::NEG_INFINITY);
806-
assert!((-1.0f128).gamma().is_nan());
807-
assert!((-2.0f128).gamma().is_nan());
808-
assert!(f128::NAN.gamma().is_nan());
809-
assert!(f128::NEG_INFINITY.gamma().is_nan());
810-
assert_eq!(f128::INFINITY.gamma(), f128::INFINITY);
811-
assert_eq!(1760.9f128.gamma(), f128::INFINITY);
812-
}
813-
814-
#[test]
815-
#[cfg(not(miri))]
816-
#[cfg(not(bootstrap))]
817-
#[cfg(target_has_reliable_f128_math)]
818-
fn test_ln_gamma() {
819-
assert_approx_eq!(1.0f128.ln_gamma().0, 0.0f128, TOL_IMPR);
820-
assert_eq!(1.0f128.ln_gamma().1, 1);
821-
assert_approx_eq!(2.0f128.ln_gamma().0, 0.0f128, TOL_IMPR);
822-
assert_eq!(2.0f128.ln_gamma().1, 1);
823-
assert_approx_eq!(3.0f128.ln_gamma().0, 2.0f128.ln(), TOL_IMPR);
824-
assert_eq!(3.0f128.ln_gamma().1, 1);
825-
assert_approx_eq!((-0.5f128).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_IMPR);
826-
assert_eq!((-0.5f128).ln_gamma().1, -1);
827-
}
828-
829-
#[test]
830-
fn test_real_consts() {
831-
let pi: f128 = consts::PI;
832-
let frac_pi_2: f128 = consts::FRAC_PI_2;
833-
let frac_pi_3: f128 = consts::FRAC_PI_3;
834-
let frac_pi_4: f128 = consts::FRAC_PI_4;
835-
let frac_pi_6: f128 = consts::FRAC_PI_6;
836-
let frac_pi_8: f128 = consts::FRAC_PI_8;
837-
let frac_1_pi: f128 = consts::FRAC_1_PI;
838-
let frac_2_pi: f128 = consts::FRAC_2_PI;
839-
840-
assert_approx_eq!(frac_pi_2, pi / 2f128, TOL_PRECISE);
841-
assert_approx_eq!(frac_pi_3, pi / 3f128, TOL_PRECISE);
842-
assert_approx_eq!(frac_pi_4, pi / 4f128, TOL_PRECISE);
843-
assert_approx_eq!(frac_pi_6, pi / 6f128, TOL_PRECISE);
844-
assert_approx_eq!(frac_pi_8, pi / 8f128, TOL_PRECISE);
845-
assert_approx_eq!(frac_1_pi, 1f128 / pi, TOL_PRECISE);
846-
assert_approx_eq!(frac_2_pi, 2f128 / pi, TOL_PRECISE);
847-
848-
#[cfg(not(miri))]
849-
#[cfg(not(bootstrap))]
850-
#[cfg(target_has_reliable_f128_math)]
851-
{
852-
let frac_2_sqrtpi: f128 = consts::FRAC_2_SQRT_PI;
853-
let sqrt2: f128 = consts::SQRT_2;
854-
let frac_1_sqrt2: f128 = consts::FRAC_1_SQRT_2;
855-
let e: f128 = consts::E;
856-
let log2_e: f128 = consts::LOG2_E;
857-
let log10_e: f128 = consts::LOG10_E;
858-
let ln_2: f128 = consts::LN_2;
859-
let ln_10: f128 = consts::LN_10;
860-
861-
assert_approx_eq!(frac_2_sqrtpi, 2f128 / pi.sqrt(), TOL_PRECISE);
862-
assert_approx_eq!(sqrt2, 2f128.sqrt(), TOL_PRECISE);
863-
assert_approx_eq!(frac_1_sqrt2, 1f128 / 2f128.sqrt(), TOL_PRECISE);
864-
assert_approx_eq!(log2_e, e.log2(), TOL_PRECISE);
865-
assert_approx_eq!(log10_e, e.log10(), TOL_PRECISE);
866-
assert_approx_eq!(ln_2, 2f128.ln(), TOL_PRECISE);
867-
assert_approx_eq!(ln_10, 10f128.ln(), TOL_PRECISE);
868-
}
869-
}
870-
871576
#[test]
872577
fn test_float_bits_conv() {
873578
assert_eq!((1f128).to_bits(), 0x3fff0000000000000000000000000000);

‎library/coretests/tests/floats/f16.rs

Lines changed: 1 addition & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ macro_rules! assert_f16_biteq {
5555

5656
#[test]
5757
fn test_num_f16() {
58-
crate::test_num(10f16, 2f16);
58+
super::test_num(10f16, 2f16);
5959
}
6060

6161
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
@@ -507,25 +507,6 @@ fn test_powi() {
507507
assert_eq!(neg_inf.powi(2), inf);
508508
}
509509

510-
#[test]
511-
#[cfg(not(miri))]
512-
#[cfg(not(bootstrap))]
513-
#[cfg(target_has_reliable_f16_math)]
514-
fn test_powf() {
515-
let nan: f16 = f16::NAN;
516-
let inf: f16 = f16::INFINITY;
517-
let neg_inf: f16 = f16::NEG_INFINITY;
518-
assert_eq!(1.0f16.powf(1.0), 1.0);
519-
assert_approx_eq!(3.4f16.powf(4.5), 246.408183, TOL_P2);
520-
assert_approx_eq!(2.7f16.powf(-3.2), 0.041652, TOL_N2);
521-
assert_approx_eq!((-3.1f16).powf(2.0), 9.61, TOL_P2);
522-
assert_approx_eq!(5.9f16.powf(-2.0), 0.028727, TOL_N2);
523-
assert_eq!(8.3f16.powf(0.0), 1.0);
524-
assert!(nan.powf(2.0).is_nan());
525-
assert_eq!(inf.powf(2.0), inf);
526-
assert_eq!(neg_inf.powf(3.0), neg_inf);
527-
}
528-
529510
#[test]
530511
#[cfg(not(miri))]
531512
#[cfg(not(bootstrap))]
@@ -540,117 +521,6 @@ fn test_sqrt_domain() {
540521
assert_eq!(f16::INFINITY.sqrt(), f16::INFINITY);
541522
}
542523

543-
#[test]
544-
#[cfg(not(miri))]
545-
#[cfg(not(bootstrap))]
546-
#[cfg(target_has_reliable_f16_math)]
547-
fn test_exp() {
548-
assert_eq!(1.0, 0.0f16.exp());
549-
assert_approx_eq!(2.718282, 1.0f16.exp(), TOL_0);
550-
assert_approx_eq!(148.413159, 5.0f16.exp(), TOL_0);
551-
552-
let inf: f16 = f16::INFINITY;
553-
let neg_inf: f16 = f16::NEG_INFINITY;
554-
let nan: f16 = f16::NAN;
555-
assert_eq!(inf, inf.exp());
556-
assert_eq!(0.0, neg_inf.exp());
557-
assert!(nan.exp().is_nan());
558-
}
559-
560-
#[test]
561-
#[cfg(not(miri))]
562-
#[cfg(not(bootstrap))]
563-
#[cfg(target_has_reliable_f16_math)]
564-
fn test_exp2() {
565-
assert_eq!(32.0, 5.0f16.exp2());
566-
assert_eq!(1.0, 0.0f16.exp2());
567-
568-
let inf: f16 = f16::INFINITY;
569-
let neg_inf: f16 = f16::NEG_INFINITY;
570-
let nan: f16 = f16::NAN;
571-
assert_eq!(inf, inf.exp2());
572-
assert_eq!(0.0, neg_inf.exp2());
573-
assert!(nan.exp2().is_nan());
574-
}
575-
576-
#[test]
577-
#[cfg(not(miri))]
578-
#[cfg(not(bootstrap))]
579-
#[cfg(target_has_reliable_f16_math)]
580-
fn test_ln() {
581-
let nan: f16 = f16::NAN;
582-
let inf: f16 = f16::INFINITY;
583-
let neg_inf: f16 = f16::NEG_INFINITY;
584-
assert_approx_eq!(1.0f16.exp().ln(), 1.0, TOL_0);
585-
assert!(nan.ln().is_nan());
586-
assert_eq!(inf.ln(), inf);
587-
assert!(neg_inf.ln().is_nan());
588-
assert!((-2.3f16).ln().is_nan());
589-
assert_eq!((-0.0f16).ln(), neg_inf);
590-
assert_eq!(0.0f16.ln(), neg_inf);
591-
assert_approx_eq!(4.0f16.ln(), 1.386294, TOL_0);
592-
}
593-
594-
#[test]
595-
#[cfg(not(miri))]
596-
#[cfg(not(bootstrap))]
597-
#[cfg(target_has_reliable_f16_math)]
598-
fn test_log() {
599-
let nan: f16 = f16::NAN;
600-
let inf: f16 = f16::INFINITY;
601-
let neg_inf: f16 = f16::NEG_INFINITY;
602-
assert_eq!(10.0f16.log(10.0), 1.0);
603-
assert_approx_eq!(2.3f16.log(3.5), 0.664858, TOL_0);
604-
assert_eq!(1.0f16.exp().log(1.0f16.exp()), 1.0);
605-
assert!(1.0f16.log(1.0).is_nan());
606-
assert!(1.0f16.log(-13.9).is_nan());
607-
assert!(nan.log(2.3).is_nan());
608-
assert_eq!(inf.log(10.0), inf);
609-
assert!(neg_inf.log(8.8).is_nan());
610-
assert!((-2.3f16).log(0.1).is_nan());
611-
assert_eq!((-0.0f16).log(2.0), neg_inf);
612-
assert_eq!(0.0f16.log(7.0), neg_inf);
613-
}
614-
615-
#[test]
616-
#[cfg(not(miri))]
617-
#[cfg(not(bootstrap))]
618-
#[cfg(target_has_reliable_f16_math)]
619-
fn test_log2() {
620-
let nan: f16 = f16::NAN;
621-
let inf: f16 = f16::INFINITY;
622-
let neg_inf: f16 = f16::NEG_INFINITY;
623-
assert_approx_eq!(10.0f16.log2(), 3.321928, TOL_0);
624-
assert_approx_eq!(2.3f16.log2(), 1.201634, TOL_0);
625-
assert_approx_eq!(1.0f16.exp().log2(), 1.442695, TOL_0);
626-
assert!(nan.log2().is_nan());
627-
assert_eq!(inf.log2(), inf);
628-
assert!(neg_inf.log2().is_nan());
629-
assert!((-2.3f16).log2().is_nan());
630-
assert_eq!((-0.0f16).log2(), neg_inf);
631-
assert_eq!(0.0f16.log2(), neg_inf);
632-
}
633-
634-
#[test]
635-
#[cfg(not(miri))]
636-
#[cfg(not(bootstrap))]
637-
#[cfg(target_has_reliable_f16_math)]
638-
fn test_log10() {
639-
let nan: f16 = f16::NAN;
640-
let inf: f16 = f16::INFINITY;
641-
let neg_inf: f16 = f16::NEG_INFINITY;
642-
assert_eq!(10.0f16.log10(), 1.0);
643-
assert_approx_eq!(2.3f16.log10(), 0.361728, TOL_0);
644-
assert_approx_eq!(1.0f16.exp().log10(), 0.434294, TOL_0);
645-
assert_eq!(1.0f16.log10(), 0.0);
646-
assert!(nan.log10().is_nan());
647-
assert_eq!(inf.log10(), inf);
648-
assert!(neg_inf.log10().is_nan());
649-
assert!((-2.3f16).log10().is_nan());
650-
assert_eq!((-0.0f16).log10(), neg_inf);
651-
assert_eq!(0.0f16.log10(), neg_inf);
652-
}
653-
654524
#[test]
655525
fn test_to_degrees() {
656526
let pi: f16 = consts::PI;
@@ -681,158 +551,6 @@ fn test_to_radians() {
681551
assert_eq!(neg_inf.to_radians(), neg_inf);
682552
}
683553

684-
#[test]
685-
#[cfg(not(miri))]
686-
#[cfg(not(bootstrap))]
687-
#[cfg(target_has_reliable_f16_math)]
688-
fn test_asinh() {
689-
assert_eq!(0.0f16.asinh(), 0.0f16);
690-
assert_eq!((-0.0f16).asinh(), -0.0f16);
691-
692-
let inf: f16 = f16::INFINITY;
693-
let neg_inf: f16 = f16::NEG_INFINITY;
694-
let nan: f16 = f16::NAN;
695-
assert_eq!(inf.asinh(), inf);
696-
assert_eq!(neg_inf.asinh(), neg_inf);
697-
assert!(nan.asinh().is_nan());
698-
assert!((-0.0f16).asinh().is_sign_negative());
699-
// issue 63271
700-
assert_approx_eq!(2.0f16.asinh(), 1.443635475178810342493276740273105f16, TOL_0);
701-
assert_approx_eq!((-2.0f16).asinh(), -1.443635475178810342493276740273105f16, TOL_0);
702-
// regression test for the catastrophic cancellation fixed in 72486
703-
assert_approx_eq!((-200.0f16).asinh(), -5.991470797049389, TOL_0);
704-
705-
// test for low accuracy from issue 104548
706-
assert_approx_eq!(10.0f16, 10.0f16.sinh().asinh(), TOL_0);
707-
// mul needed for approximate comparison to be meaningful
708-
assert_approx_eq!(1.0f16, 1e-3f16.sinh().asinh() * 1e3f16, TOL_0);
709-
}
710-
711-
#[test]
712-
#[cfg(not(miri))]
713-
#[cfg(not(bootstrap))]
714-
#[cfg(target_has_reliable_f16_math)]
715-
fn test_acosh() {
716-
assert_eq!(1.0f16.acosh(), 0.0f16);
717-
assert!(0.999f16.acosh().is_nan());
718-
719-
let inf: f16 = f16::INFINITY;
720-
let neg_inf: f16 = f16::NEG_INFINITY;
721-
let nan: f16 = f16::NAN;
722-
assert_eq!(inf.acosh(), inf);
723-
assert!(neg_inf.acosh().is_nan());
724-
assert!(nan.acosh().is_nan());
725-
assert_approx_eq!(2.0f16.acosh(), 1.31695789692481670862504634730796844f16, TOL_0);
726-
assert_approx_eq!(3.0f16.acosh(), 1.76274717403908605046521864995958461f16, TOL_0);
727-
728-
// test for low accuracy from issue 104548
729-
assert_approx_eq!(10.0f16, 10.0f16.cosh().acosh(), TOL_P2);
730-
}
731-
732-
#[test]
733-
#[cfg(not(miri))]
734-
#[cfg(not(bootstrap))]
735-
#[cfg(target_has_reliable_f16_math)]
736-
fn test_atanh() {
737-
assert_eq!(0.0f16.atanh(), 0.0f16);
738-
assert_eq!((-0.0f16).atanh(), -0.0f16);
739-
740-
let inf: f16 = f16::INFINITY;
741-
let neg_inf: f16 = f16::NEG_INFINITY;
742-
let nan: f16 = f16::NAN;
743-
assert_eq!(1.0f16.atanh(), inf);
744-
assert_eq!((-1.0f16).atanh(), neg_inf);
745-
assert!(2f16.atanh().atanh().is_nan());
746-
assert!((-2f16).atanh().atanh().is_nan());
747-
assert!(inf.atanh().is_nan());
748-
assert!(neg_inf.atanh().is_nan());
749-
assert!(nan.atanh().is_nan());
750-
assert_approx_eq!(0.5f16.atanh(), 0.54930614433405484569762261846126285f16, TOL_0);
751-
assert_approx_eq!((-0.5f16).atanh(), -0.54930614433405484569762261846126285f16, TOL_0);
752-
}
753-
754-
#[test]
755-
#[cfg(not(miri))]
756-
#[cfg(not(bootstrap))]
757-
#[cfg(target_has_reliable_f16_math)]
758-
fn test_gamma() {
759-
// precision can differ among platforms
760-
assert_approx_eq!(1.0f16.gamma(), 1.0f16, TOL_0);
761-
assert_approx_eq!(2.0f16.gamma(), 1.0f16, TOL_0);
762-
assert_approx_eq!(3.0f16.gamma(), 2.0f16, TOL_0);
763-
assert_approx_eq!(4.0f16.gamma(), 6.0f16, TOL_0);
764-
assert_approx_eq!(5.0f16.gamma(), 24.0f16, TOL_0);
765-
assert_approx_eq!(0.5f16.gamma(), consts::PI.sqrt(), TOL_0);
766-
assert_approx_eq!((-0.5f16).gamma(), -2.0 * consts::PI.sqrt(), TOL_0);
767-
assert_eq!(0.0f16.gamma(), f16::INFINITY);
768-
assert_eq!((-0.0f16).gamma(), f16::NEG_INFINITY);
769-
assert!((-1.0f16).gamma().is_nan());
770-
assert!((-2.0f16).gamma().is_nan());
771-
assert!(f16::NAN.gamma().is_nan());
772-
assert!(f16::NEG_INFINITY.gamma().is_nan());
773-
assert_eq!(f16::INFINITY.gamma(), f16::INFINITY);
774-
assert_eq!(171.71f16.gamma(), f16::INFINITY);
775-
}
776-
777-
#[test]
778-
#[cfg(not(miri))]
779-
#[cfg(not(bootstrap))]
780-
#[cfg(target_has_reliable_f16_math)]
781-
fn test_ln_gamma() {
782-
assert_approx_eq!(1.0f16.ln_gamma().0, 0.0f16, TOL_0);
783-
assert_eq!(1.0f16.ln_gamma().1, 1);
784-
assert_approx_eq!(2.0f16.ln_gamma().0, 0.0f16, TOL_0);
785-
assert_eq!(2.0f16.ln_gamma().1, 1);
786-
assert_approx_eq!(3.0f16.ln_gamma().0, 2.0f16.ln(), TOL_0);
787-
assert_eq!(3.0f16.ln_gamma().1, 1);
788-
assert_approx_eq!((-0.5f16).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_0);
789-
assert_eq!((-0.5f16).ln_gamma().1, -1);
790-
}
791-
792-
#[test]
793-
fn test_real_consts() {
794-
// FIXME(f16_f128): add math tests when available
795-
796-
let pi: f16 = consts::PI;
797-
let frac_pi_2: f16 = consts::FRAC_PI_2;
798-
let frac_pi_3: f16 = consts::FRAC_PI_3;
799-
let frac_pi_4: f16 = consts::FRAC_PI_4;
800-
let frac_pi_6: f16 = consts::FRAC_PI_6;
801-
let frac_pi_8: f16 = consts::FRAC_PI_8;
802-
let frac_1_pi: f16 = consts::FRAC_1_PI;
803-
let frac_2_pi: f16 = consts::FRAC_2_PI;
804-
805-
assert_approx_eq!(frac_pi_2, pi / 2f16, TOL_0);
806-
assert_approx_eq!(frac_pi_3, pi / 3f16, TOL_0);
807-
assert_approx_eq!(frac_pi_4, pi / 4f16, TOL_0);
808-
assert_approx_eq!(frac_pi_6, pi / 6f16, TOL_0);
809-
assert_approx_eq!(frac_pi_8, pi / 8f16, TOL_0);
810-
assert_approx_eq!(frac_1_pi, 1f16 / pi, TOL_0);
811-
assert_approx_eq!(frac_2_pi, 2f16 / pi, TOL_0);
812-
813-
#[cfg(not(miri))]
814-
#[cfg(not(bootstrap))]
815-
#[cfg(target_has_reliable_f16_math)]
816-
{
817-
let frac_2_sqrtpi: f16 = consts::FRAC_2_SQRT_PI;
818-
let sqrt2: f16 = consts::SQRT_2;
819-
let frac_1_sqrt2: f16 = consts::FRAC_1_SQRT_2;
820-
let e: f16 = consts::E;
821-
let log2_e: f16 = consts::LOG2_E;
822-
let log10_e: f16 = consts::LOG10_E;
823-
let ln_2: f16 = consts::LN_2;
824-
let ln_10: f16 = consts::LN_10;
825-
826-
assert_approx_eq!(frac_2_sqrtpi, 2f16 / pi.sqrt(), TOL_0);
827-
assert_approx_eq!(sqrt2, 2f16.sqrt(), TOL_0);
828-
assert_approx_eq!(frac_1_sqrt2, 1f16 / 2f16.sqrt(), TOL_0);
829-
assert_approx_eq!(log2_e, e.log2(), TOL_0);
830-
assert_approx_eq!(log10_e, e.log10(), TOL_0);
831-
assert_approx_eq!(ln_2, 2f16.ln(), TOL_0);
832-
assert_approx_eq!(ln_10, 10f16.ln(), TOL_0);
833-
}
834-
}
835-
836554
#[test]
837555
fn test_float_bits_conv() {
838556
assert_eq!((1f16).to_bits(), 0x3c00);

‎library/coretests/tests/floats/f32.rs

Lines changed: 75 additions & 315 deletions
Large diffs are not rendered by default.

‎library/coretests/tests/floats/f64.rs

Lines changed: 63 additions & 300 deletions
Large diffs are not rendered by default.

‎library/coretests/tests/floats/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![feature(f16, f128, float_algebraic, float_gamma, float_minimum_maximum)]
2-
#![cfg_attr(not(bootstrap), feature(cfg_target_has_reliable_f16_f128))]
3-
#![cfg_attr(not(bootstrap), expect(internal_features))] // for reliable_f16_f128
4-
51
use std::fmt;
62
use std::ops::{Add, Div, Mul, Rem, Sub};
73

‎library/coretests/tests/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// tidy-alphabetical-start
2+
#![cfg_attr(not(bootstrap), feature(cfg_target_has_reliable_f16_f128))]
23
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
34
#![cfg_attr(test, feature(cfg_match))]
45
#![feature(alloc_layout_extra)]
@@ -17,6 +18,7 @@
1718
#![feature(const_eval_select)]
1819
#![feature(const_swap_nonoverlapping)]
1920
#![feature(const_trait_impl)]
21+
#![feature(core_float_math)]
2022
#![feature(core_intrinsics)]
2123
#![feature(core_intrinsics_fallbacks)]
2224
#![feature(core_io_borrowed_buf)]
@@ -29,6 +31,10 @@
2931
#![feature(exact_size_is_empty)]
3032
#![feature(extend_one)]
3133
#![feature(extern_types)]
34+
#![feature(f128)]
35+
#![feature(f16)]
36+
#![feature(float_algebraic)]
37+
#![feature(float_gamma)]
3238
#![feature(float_minimum_maximum)]
3339
#![feature(flt2dec)]
3440
#![feature(fmt_internals)]

‎library/std/tests/floats/f128.rs

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
2+
#![cfg(not(bootstrap))]
3+
#![cfg(target_has_reliable_f128)]
4+
5+
use std::f128::consts;
6+
use std::ops::{Add, Div, Mul, Sub};
7+
8+
// Note these tolerances make sense around zero, but not for more extreme exponents.
9+
10+
/// Default tolerances. Works for values that should be near precise but not exact. Roughly
11+
/// the precision carried by `100 * 100`.
12+
#[cfg(not(miri))]
13+
#[cfg(not(bootstrap))]
14+
#[cfg(target_has_reliable_f128_math)]
15+
const TOL: f128 = 1e-12;
16+
17+
/// For operations that are near exact, usually not involving math of different
18+
/// signs.
19+
const TOL_PRECISE: f128 = 1e-28;
20+
21+
/// Tolerances for math that is allowed to be imprecise, usually due to multiple chained
22+
/// operations.
23+
#[cfg(not(miri))]
24+
#[cfg(not(bootstrap))]
25+
#[cfg(target_has_reliable_f128_math)]
26+
const TOL_IMPR: f128 = 1e-10;
27+
28+
/// Compare by representation
29+
#[allow(unused_macros)]
30+
macro_rules! assert_f128_biteq {
31+
($a:expr, $b:expr) => {
32+
let (l, r): (&f128, &f128) = (&$a, &$b);
33+
let lb = l.to_bits();
34+
let rb = r.to_bits();
35+
assert_eq!(lb, rb, "float {l:?} is not bitequal to {r:?}.\na: {lb:#034x}\nb: {rb:#034x}");
36+
};
37+
}
38+
39+
#[test]
40+
fn test_num_f128() {
41+
// FIXME(f16_f128): replace with a `test_num` call once the required `fmodl`/`fmodf128`
42+
// function is available on all platforms.
43+
let ten = 10f128;
44+
let two = 2f128;
45+
assert_eq!(ten.add(two), ten + two);
46+
assert_eq!(ten.sub(two), ten - two);
47+
assert_eq!(ten.mul(two), ten * two);
48+
assert_eq!(ten.div(two), ten / two);
49+
}
50+
51+
// Many math functions allow for less accurate results, so the next tolerance up is used
52+
53+
#[test]
54+
#[cfg(not(miri))]
55+
#[cfg(not(bootstrap))]
56+
#[cfg(target_has_reliable_f128_math)]
57+
fn test_powf() {
58+
let nan: f128 = f128::NAN;
59+
let inf: f128 = f128::INFINITY;
60+
let neg_inf: f128 = f128::NEG_INFINITY;
61+
assert_eq!(1.0f128.powf(1.0), 1.0);
62+
assert_approx_eq!(3.4f128.powf(4.5), 246.40818323761892815995637964326426756, TOL_IMPR);
63+
assert_approx_eq!(2.7f128.powf(-3.2), 0.041652009108526178281070304373500889273, TOL_IMPR);
64+
assert_approx_eq!((-3.1f128).powf(2.0), 9.6100000000000005506706202140776519387, TOL_IMPR);
65+
assert_approx_eq!(5.9f128.powf(-2.0), 0.028727377190462507313100483690639638451, TOL_IMPR);
66+
assert_eq!(8.3f128.powf(0.0), 1.0);
67+
assert!(nan.powf(2.0).is_nan());
68+
assert_eq!(inf.powf(2.0), inf);
69+
assert_eq!(neg_inf.powf(3.0), neg_inf);
70+
}
71+
72+
#[test]
73+
#[cfg(not(miri))]
74+
#[cfg(not(bootstrap))]
75+
#[cfg(target_has_reliable_f128_math)]
76+
fn test_exp() {
77+
assert_eq!(1.0, 0.0f128.exp());
78+
assert_approx_eq!(consts::E, 1.0f128.exp(), TOL);
79+
assert_approx_eq!(148.41315910257660342111558004055227962348775, 5.0f128.exp(), TOL);
80+
81+
let inf: f128 = f128::INFINITY;
82+
let neg_inf: f128 = f128::NEG_INFINITY;
83+
let nan: f128 = f128::NAN;
84+
assert_eq!(inf, inf.exp());
85+
assert_eq!(0.0, neg_inf.exp());
86+
assert!(nan.exp().is_nan());
87+
}
88+
89+
#[test]
90+
#[cfg(not(miri))]
91+
#[cfg(not(bootstrap))]
92+
#[cfg(target_has_reliable_f128_math)]
93+
fn test_exp2() {
94+
assert_eq!(32.0, 5.0f128.exp2());
95+
assert_eq!(1.0, 0.0f128.exp2());
96+
97+
let inf: f128 = f128::INFINITY;
98+
let neg_inf: f128 = f128::NEG_INFINITY;
99+
let nan: f128 = f128::NAN;
100+
assert_eq!(inf, inf.exp2());
101+
assert_eq!(0.0, neg_inf.exp2());
102+
assert!(nan.exp2().is_nan());
103+
}
104+
105+
#[test]
106+
#[cfg(not(miri))]
107+
#[cfg(not(bootstrap))]
108+
#[cfg(target_has_reliable_f128_math)]
109+
fn test_ln() {
110+
let nan: f128 = f128::NAN;
111+
let inf: f128 = f128::INFINITY;
112+
let neg_inf: f128 = f128::NEG_INFINITY;
113+
assert_approx_eq!(1.0f128.exp().ln(), 1.0, TOL);
114+
assert!(nan.ln().is_nan());
115+
assert_eq!(inf.ln(), inf);
116+
assert!(neg_inf.ln().is_nan());
117+
assert!((-2.3f128).ln().is_nan());
118+
assert_eq!((-0.0f128).ln(), neg_inf);
119+
assert_eq!(0.0f128.ln(), neg_inf);
120+
assert_approx_eq!(4.0f128.ln(), 1.3862943611198906188344642429163531366, TOL);
121+
}
122+
123+
#[test]
124+
#[cfg(not(miri))]
125+
#[cfg(not(bootstrap))]
126+
#[cfg(target_has_reliable_f128_math)]
127+
fn test_log() {
128+
let nan: f128 = f128::NAN;
129+
let inf: f128 = f128::INFINITY;
130+
let neg_inf: f128 = f128::NEG_INFINITY;
131+
assert_eq!(10.0f128.log(10.0), 1.0);
132+
assert_approx_eq!(2.3f128.log(3.5), 0.66485771361478710036766645911922010272, TOL);
133+
assert_eq!(1.0f128.exp().log(1.0f128.exp()), 1.0);
134+
assert!(1.0f128.log(1.0).is_nan());
135+
assert!(1.0f128.log(-13.9).is_nan());
136+
assert!(nan.log(2.3).is_nan());
137+
assert_eq!(inf.log(10.0), inf);
138+
assert!(neg_inf.log(8.8).is_nan());
139+
assert!((-2.3f128).log(0.1).is_nan());
140+
assert_eq!((-0.0f128).log(2.0), neg_inf);
141+
assert_eq!(0.0f128.log(7.0), neg_inf);
142+
}
143+
144+
#[test]
145+
#[cfg(not(miri))]
146+
#[cfg(not(bootstrap))]
147+
#[cfg(target_has_reliable_f128_math)]
148+
fn test_log2() {
149+
let nan: f128 = f128::NAN;
150+
let inf: f128 = f128::INFINITY;
151+
let neg_inf: f128 = f128::NEG_INFINITY;
152+
assert_approx_eq!(10.0f128.log2(), 3.32192809488736234787031942948939017, TOL);
153+
assert_approx_eq!(2.3f128.log2(), 1.2016338611696504130002982471978765921, TOL);
154+
assert_approx_eq!(1.0f128.exp().log2(), 1.4426950408889634073599246810018921381, TOL);
155+
assert!(nan.log2().is_nan());
156+
assert_eq!(inf.log2(), inf);
157+
assert!(neg_inf.log2().is_nan());
158+
assert!((-2.3f128).log2().is_nan());
159+
assert_eq!((-0.0f128).log2(), neg_inf);
160+
assert_eq!(0.0f128.log2(), neg_inf);
161+
}
162+
163+
#[test]
164+
#[cfg(not(miri))]
165+
#[cfg(not(bootstrap))]
166+
#[cfg(target_has_reliable_f128_math)]
167+
fn test_log10() {
168+
let nan: f128 = f128::NAN;
169+
let inf: f128 = f128::INFINITY;
170+
let neg_inf: f128 = f128::NEG_INFINITY;
171+
assert_eq!(10.0f128.log10(), 1.0);
172+
assert_approx_eq!(2.3f128.log10(), 0.36172783601759284532595218865859309898, TOL);
173+
assert_approx_eq!(1.0f128.exp().log10(), 0.43429448190325182765112891891660508222, TOL);
174+
assert_eq!(1.0f128.log10(), 0.0);
175+
assert!(nan.log10().is_nan());
176+
assert_eq!(inf.log10(), inf);
177+
assert!(neg_inf.log10().is_nan());
178+
assert!((-2.3f128).log10().is_nan());
179+
assert_eq!((-0.0f128).log10(), neg_inf);
180+
assert_eq!(0.0f128.log10(), neg_inf);
181+
}
182+
183+
#[test]
184+
#[cfg(not(miri))]
185+
#[cfg(not(bootstrap))]
186+
#[cfg(target_has_reliable_f128_math)]
187+
fn test_asinh() {
188+
// Lower accuracy results are allowed, use increased tolerances
189+
assert_eq!(0.0f128.asinh(), 0.0f128);
190+
assert_eq!((-0.0f128).asinh(), -0.0f128);
191+
192+
let inf: f128 = f128::INFINITY;
193+
let neg_inf: f128 = f128::NEG_INFINITY;
194+
let nan: f128 = f128::NAN;
195+
assert_eq!(inf.asinh(), inf);
196+
assert_eq!(neg_inf.asinh(), neg_inf);
197+
assert!(nan.asinh().is_nan());
198+
assert!((-0.0f128).asinh().is_sign_negative());
199+
200+
// issue 63271
201+
assert_approx_eq!(2.0f128.asinh(), 1.443635475178810342493276740273105f128, TOL_IMPR);
202+
assert_approx_eq!((-2.0f128).asinh(), -1.443635475178810342493276740273105f128, TOL_IMPR);
203+
// regression test for the catastrophic cancellation fixed in 72486
204+
assert_approx_eq!(
205+
(-67452098.07139316f128).asinh(),
206+
-18.720075426274544393985484294000831757220,
207+
TOL_IMPR
208+
);
209+
210+
// test for low accuracy from issue 104548
211+
assert_approx_eq!(60.0f128, 60.0f128.sinh().asinh(), TOL_IMPR);
212+
// mul needed for approximate comparison to be meaningful
213+
assert_approx_eq!(1.0f128, 1e-15f128.sinh().asinh() * 1e15f128, TOL_IMPR);
214+
}
215+
216+
#[test]
217+
#[cfg(not(miri))]
218+
#[cfg(not(bootstrap))]
219+
#[cfg(target_has_reliable_f128_math)]
220+
fn test_acosh() {
221+
assert_eq!(1.0f128.acosh(), 0.0f128);
222+
assert!(0.999f128.acosh().is_nan());
223+
224+
let inf: f128 = f128::INFINITY;
225+
let neg_inf: f128 = f128::NEG_INFINITY;
226+
let nan: f128 = f128::NAN;
227+
assert_eq!(inf.acosh(), inf);
228+
assert!(neg_inf.acosh().is_nan());
229+
assert!(nan.acosh().is_nan());
230+
assert_approx_eq!(2.0f128.acosh(), 1.31695789692481670862504634730796844f128, TOL_IMPR);
231+
assert_approx_eq!(3.0f128.acosh(), 1.76274717403908605046521864995958461f128, TOL_IMPR);
232+
233+
// test for low accuracy from issue 104548
234+
assert_approx_eq!(60.0f128, 60.0f128.cosh().acosh(), TOL_IMPR);
235+
}
236+
237+
#[test]
238+
#[cfg(not(miri))]
239+
#[cfg(not(bootstrap))]
240+
#[cfg(target_has_reliable_f128_math)]
241+
fn test_atanh() {
242+
assert_eq!(0.0f128.atanh(), 0.0f128);
243+
assert_eq!((-0.0f128).atanh(), -0.0f128);
244+
245+
let inf: f128 = f128::INFINITY;
246+
let neg_inf: f128 = f128::NEG_INFINITY;
247+
let nan: f128 = f128::NAN;
248+
assert_eq!(1.0f128.atanh(), inf);
249+
assert_eq!((-1.0f128).atanh(), neg_inf);
250+
assert!(2f128.atanh().atanh().is_nan());
251+
assert!((-2f128).atanh().atanh().is_nan());
252+
assert!(inf.atanh().is_nan());
253+
assert!(neg_inf.atanh().is_nan());
254+
assert!(nan.atanh().is_nan());
255+
assert_approx_eq!(0.5f128.atanh(), 0.54930614433405484569762261846126285f128, TOL_IMPR);
256+
assert_approx_eq!((-0.5f128).atanh(), -0.54930614433405484569762261846126285f128, TOL_IMPR);
257+
}
258+
259+
#[test]
260+
#[cfg(not(miri))]
261+
#[cfg(not(bootstrap))]
262+
#[cfg(target_has_reliable_f128_math)]
263+
fn test_gamma() {
264+
// precision can differ among platforms
265+
assert_approx_eq!(1.0f128.gamma(), 1.0f128, TOL_IMPR);
266+
assert_approx_eq!(2.0f128.gamma(), 1.0f128, TOL_IMPR);
267+
assert_approx_eq!(3.0f128.gamma(), 2.0f128, TOL_IMPR);
268+
assert_approx_eq!(4.0f128.gamma(), 6.0f128, TOL_IMPR);
269+
assert_approx_eq!(5.0f128.gamma(), 24.0f128, TOL_IMPR);
270+
assert_approx_eq!(0.5f128.gamma(), consts::PI.sqrt(), TOL_IMPR);
271+
assert_approx_eq!((-0.5f128).gamma(), -2.0 * consts::PI.sqrt(), TOL_IMPR);
272+
assert_eq!(0.0f128.gamma(), f128::INFINITY);
273+
assert_eq!((-0.0f128).gamma(), f128::NEG_INFINITY);
274+
assert!((-1.0f128).gamma().is_nan());
275+
assert!((-2.0f128).gamma().is_nan());
276+
assert!(f128::NAN.gamma().is_nan());
277+
assert!(f128::NEG_INFINITY.gamma().is_nan());
278+
assert_eq!(f128::INFINITY.gamma(), f128::INFINITY);
279+
assert_eq!(1760.9f128.gamma(), f128::INFINITY);
280+
}
281+
282+
#[test]
283+
#[cfg(not(miri))]
284+
#[cfg(not(bootstrap))]
285+
#[cfg(target_has_reliable_f128_math)]
286+
fn test_ln_gamma() {
287+
assert_approx_eq!(1.0f128.ln_gamma().0, 0.0f128, TOL_IMPR);
288+
assert_eq!(1.0f128.ln_gamma().1, 1);
289+
assert_approx_eq!(2.0f128.ln_gamma().0, 0.0f128, TOL_IMPR);
290+
assert_eq!(2.0f128.ln_gamma().1, 1);
291+
assert_approx_eq!(3.0f128.ln_gamma().0, 2.0f128.ln(), TOL_IMPR);
292+
assert_eq!(3.0f128.ln_gamma().1, 1);
293+
assert_approx_eq!((-0.5f128).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_IMPR);
294+
assert_eq!((-0.5f128).ln_gamma().1, -1);
295+
}
296+
297+
#[test]
298+
fn test_real_consts() {
299+
let pi: f128 = consts::PI;
300+
let frac_pi_2: f128 = consts::FRAC_PI_2;
301+
let frac_pi_3: f128 = consts::FRAC_PI_3;
302+
let frac_pi_4: f128 = consts::FRAC_PI_4;
303+
let frac_pi_6: f128 = consts::FRAC_PI_6;
304+
let frac_pi_8: f128 = consts::FRAC_PI_8;
305+
let frac_1_pi: f128 = consts::FRAC_1_PI;
306+
let frac_2_pi: f128 = consts::FRAC_2_PI;
307+
308+
assert_approx_eq!(frac_pi_2, pi / 2f128, TOL_PRECISE);
309+
assert_approx_eq!(frac_pi_3, pi / 3f128, TOL_PRECISE);
310+
assert_approx_eq!(frac_pi_4, pi / 4f128, TOL_PRECISE);
311+
assert_approx_eq!(frac_pi_6, pi / 6f128, TOL_PRECISE);
312+
assert_approx_eq!(frac_pi_8, pi / 8f128, TOL_PRECISE);
313+
assert_approx_eq!(frac_1_pi, 1f128 / pi, TOL_PRECISE);
314+
assert_approx_eq!(frac_2_pi, 2f128 / pi, TOL_PRECISE);
315+
316+
#[cfg(not(miri))]
317+
#[cfg(not(bootstrap))]
318+
#[cfg(target_has_reliable_f128_math)]
319+
{
320+
let frac_2_sqrtpi: f128 = consts::FRAC_2_SQRT_PI;
321+
let sqrt2: f128 = consts::SQRT_2;
322+
let frac_1_sqrt2: f128 = consts::FRAC_1_SQRT_2;
323+
let e: f128 = consts::E;
324+
let log2_e: f128 = consts::LOG2_E;
325+
let log10_e: f128 = consts::LOG10_E;
326+
let ln_2: f128 = consts::LN_2;
327+
let ln_10: f128 = consts::LN_10;
328+
329+
assert_approx_eq!(frac_2_sqrtpi, 2f128 / pi.sqrt(), TOL_PRECISE);
330+
assert_approx_eq!(sqrt2, 2f128.sqrt(), TOL_PRECISE);
331+
assert_approx_eq!(frac_1_sqrt2, 1f128 / 2f128.sqrt(), TOL_PRECISE);
332+
assert_approx_eq!(log2_e, e.log2(), TOL_PRECISE);
333+
assert_approx_eq!(log10_e, e.log10(), TOL_PRECISE);
334+
assert_approx_eq!(ln_2, 2f128.ln(), TOL_PRECISE);
335+
assert_approx_eq!(ln_10, 10f128.ln(), TOL_PRECISE);
336+
}
337+
}

‎library/std/tests/floats/f16.rs

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
2+
#![cfg(not(bootstrap))]
3+
#![cfg(target_has_reliable_f16)]
4+
5+
use std::f16::consts;
6+
7+
/// Tolerance for results on the order of 10.0e-2
8+
#[allow(unused)]
9+
const TOL_N2: f16 = 0.0001;
10+
11+
/// Tolerance for results on the order of 10.0e+0
12+
#[allow(unused)]
13+
const TOL_0: f16 = 0.01;
14+
15+
/// Tolerance for results on the order of 10.0e+2
16+
#[allow(unused)]
17+
const TOL_P2: f16 = 0.5;
18+
19+
/// Tolerance for results on the order of 10.0e+4
20+
#[allow(unused)]
21+
const TOL_P4: f16 = 10.0;
22+
23+
/// Compare by representation
24+
#[allow(unused_macros)]
25+
macro_rules! assert_f16_biteq {
26+
($a:expr, $b:expr) => {
27+
let (l, r): (&f16, &f16) = (&$a, &$b);
28+
let lb = l.to_bits();
29+
let rb = r.to_bits();
30+
assert_eq!(lb, rb, "float {l:?} ({lb:#04x}) is not bitequal to {r:?} ({rb:#04x})");
31+
};
32+
}
33+
34+
#[test]
35+
#[cfg(not(miri))]
36+
#[cfg(not(bootstrap))]
37+
#[cfg(target_has_reliable_f16_math)]
38+
fn test_powf() {
39+
let nan: f16 = f16::NAN;
40+
let inf: f16 = f16::INFINITY;
41+
let neg_inf: f16 = f16::NEG_INFINITY;
42+
assert_eq!(1.0f16.powf(1.0), 1.0);
43+
assert_approx_eq!(3.4f16.powf(4.5), 246.408183, TOL_P2);
44+
assert_approx_eq!(2.7f16.powf(-3.2), 0.041652, TOL_N2);
45+
assert_approx_eq!((-3.1f16).powf(2.0), 9.61, TOL_P2);
46+
assert_approx_eq!(5.9f16.powf(-2.0), 0.028727, TOL_N2);
47+
assert_eq!(8.3f16.powf(0.0), 1.0);
48+
assert!(nan.powf(2.0).is_nan());
49+
assert_eq!(inf.powf(2.0), inf);
50+
assert_eq!(neg_inf.powf(3.0), neg_inf);
51+
}
52+
53+
#[test]
54+
#[cfg(not(miri))]
55+
#[cfg(not(bootstrap))]
56+
#[cfg(target_has_reliable_f16_math)]
57+
fn test_exp() {
58+
assert_eq!(1.0, 0.0f16.exp());
59+
assert_approx_eq!(2.718282, 1.0f16.exp(), TOL_0);
60+
assert_approx_eq!(148.413159, 5.0f16.exp(), TOL_0);
61+
62+
let inf: f16 = f16::INFINITY;
63+
let neg_inf: f16 = f16::NEG_INFINITY;
64+
let nan: f16 = f16::NAN;
65+
assert_eq!(inf, inf.exp());
66+
assert_eq!(0.0, neg_inf.exp());
67+
assert!(nan.exp().is_nan());
68+
}
69+
70+
#[test]
71+
#[cfg(not(miri))]
72+
#[cfg(not(bootstrap))]
73+
#[cfg(target_has_reliable_f16_math)]
74+
fn test_exp2() {
75+
assert_eq!(32.0, 5.0f16.exp2());
76+
assert_eq!(1.0, 0.0f16.exp2());
77+
78+
let inf: f16 = f16::INFINITY;
79+
let neg_inf: f16 = f16::NEG_INFINITY;
80+
let nan: f16 = f16::NAN;
81+
assert_eq!(inf, inf.exp2());
82+
assert_eq!(0.0, neg_inf.exp2());
83+
assert!(nan.exp2().is_nan());
84+
}
85+
86+
#[test]
87+
#[cfg(not(miri))]
88+
#[cfg(not(bootstrap))]
89+
#[cfg(target_has_reliable_f16_math)]
90+
fn test_ln() {
91+
let nan: f16 = f16::NAN;
92+
let inf: f16 = f16::INFINITY;
93+
let neg_inf: f16 = f16::NEG_INFINITY;
94+
assert_approx_eq!(1.0f16.exp().ln(), 1.0, TOL_0);
95+
assert!(nan.ln().is_nan());
96+
assert_eq!(inf.ln(), inf);
97+
assert!(neg_inf.ln().is_nan());
98+
assert!((-2.3f16).ln().is_nan());
99+
assert_eq!((-0.0f16).ln(), neg_inf);
100+
assert_eq!(0.0f16.ln(), neg_inf);
101+
assert_approx_eq!(4.0f16.ln(), 1.386294, TOL_0);
102+
}
103+
104+
#[test]
105+
#[cfg(not(miri))]
106+
#[cfg(not(bootstrap))]
107+
#[cfg(target_has_reliable_f16_math)]
108+
fn test_log() {
109+
let nan: f16 = f16::NAN;
110+
let inf: f16 = f16::INFINITY;
111+
let neg_inf: f16 = f16::NEG_INFINITY;
112+
assert_eq!(10.0f16.log(10.0), 1.0);
113+
assert_approx_eq!(2.3f16.log(3.5), 0.664858, TOL_0);
114+
assert_eq!(1.0f16.exp().log(1.0f16.exp()), 1.0);
115+
assert!(1.0f16.log(1.0).is_nan());
116+
assert!(1.0f16.log(-13.9).is_nan());
117+
assert!(nan.log(2.3).is_nan());
118+
assert_eq!(inf.log(10.0), inf);
119+
assert!(neg_inf.log(8.8).is_nan());
120+
assert!((-2.3f16).log(0.1).is_nan());
121+
assert_eq!((-0.0f16).log(2.0), neg_inf);
122+
assert_eq!(0.0f16.log(7.0), neg_inf);
123+
}
124+
125+
#[test]
126+
#[cfg(not(miri))]
127+
#[cfg(not(bootstrap))]
128+
#[cfg(target_has_reliable_f16_math)]
129+
fn test_log2() {
130+
let nan: f16 = f16::NAN;
131+
let inf: f16 = f16::INFINITY;
132+
let neg_inf: f16 = f16::NEG_INFINITY;
133+
assert_approx_eq!(10.0f16.log2(), 3.321928, TOL_0);
134+
assert_approx_eq!(2.3f16.log2(), 1.201634, TOL_0);
135+
assert_approx_eq!(1.0f16.exp().log2(), 1.442695, TOL_0);
136+
assert!(nan.log2().is_nan());
137+
assert_eq!(inf.log2(), inf);
138+
assert!(neg_inf.log2().is_nan());
139+
assert!((-2.3f16).log2().is_nan());
140+
assert_eq!((-0.0f16).log2(), neg_inf);
141+
assert_eq!(0.0f16.log2(), neg_inf);
142+
}
143+
144+
#[test]
145+
#[cfg(not(miri))]
146+
#[cfg(not(bootstrap))]
147+
#[cfg(target_has_reliable_f16_math)]
148+
fn test_log10() {
149+
let nan: f16 = f16::NAN;
150+
let inf: f16 = f16::INFINITY;
151+
let neg_inf: f16 = f16::NEG_INFINITY;
152+
assert_eq!(10.0f16.log10(), 1.0);
153+
assert_approx_eq!(2.3f16.log10(), 0.361728, TOL_0);
154+
assert_approx_eq!(1.0f16.exp().log10(), 0.434294, TOL_0);
155+
assert_eq!(1.0f16.log10(), 0.0);
156+
assert!(nan.log10().is_nan());
157+
assert_eq!(inf.log10(), inf);
158+
assert!(neg_inf.log10().is_nan());
159+
assert!((-2.3f16).log10().is_nan());
160+
assert_eq!((-0.0f16).log10(), neg_inf);
161+
assert_eq!(0.0f16.log10(), neg_inf);
162+
}
163+
164+
#[test]
165+
#[cfg(not(miri))]
166+
#[cfg(not(bootstrap))]
167+
#[cfg(target_has_reliable_f16_math)]
168+
fn test_asinh() {
169+
assert_eq!(0.0f16.asinh(), 0.0f16);
170+
assert_eq!((-0.0f16).asinh(), -0.0f16);
171+
172+
let inf: f16 = f16::INFINITY;
173+
let neg_inf: f16 = f16::NEG_INFINITY;
174+
let nan: f16 = f16::NAN;
175+
assert_eq!(inf.asinh(), inf);
176+
assert_eq!(neg_inf.asinh(), neg_inf);
177+
assert!(nan.asinh().is_nan());
178+
assert!((-0.0f16).asinh().is_sign_negative());
179+
// issue 63271
180+
assert_approx_eq!(2.0f16.asinh(), 1.443635475178810342493276740273105f16, TOL_0);
181+
assert_approx_eq!((-2.0f16).asinh(), -1.443635475178810342493276740273105f16, TOL_0);
182+
// regression test for the catastrophic cancellation fixed in 72486
183+
assert_approx_eq!((-200.0f16).asinh(), -5.991470797049389, TOL_0);
184+
185+
// test for low accuracy from issue 104548
186+
assert_approx_eq!(10.0f16, 10.0f16.sinh().asinh(), TOL_0);
187+
// mul needed for approximate comparison to be meaningful
188+
assert_approx_eq!(1.0f16, 1e-3f16.sinh().asinh() * 1e3f16, TOL_0);
189+
}
190+
191+
#[test]
192+
#[cfg(not(miri))]
193+
#[cfg(not(bootstrap))]
194+
#[cfg(target_has_reliable_f16_math)]
195+
fn test_acosh() {
196+
assert_eq!(1.0f16.acosh(), 0.0f16);
197+
assert!(0.999f16.acosh().is_nan());
198+
199+
let inf: f16 = f16::INFINITY;
200+
let neg_inf: f16 = f16::NEG_INFINITY;
201+
let nan: f16 = f16::NAN;
202+
assert_eq!(inf.acosh(), inf);
203+
assert!(neg_inf.acosh().is_nan());
204+
assert!(nan.acosh().is_nan());
205+
assert_approx_eq!(2.0f16.acosh(), 1.31695789692481670862504634730796844f16, TOL_0);
206+
assert_approx_eq!(3.0f16.acosh(), 1.76274717403908605046521864995958461f16, TOL_0);
207+
208+
// test for low accuracy from issue 104548
209+
assert_approx_eq!(10.0f16, 10.0f16.cosh().acosh(), TOL_P2);
210+
}
211+
212+
#[test]
213+
#[cfg(not(miri))]
214+
#[cfg(not(bootstrap))]
215+
#[cfg(target_has_reliable_f16_math)]
216+
fn test_atanh() {
217+
assert_eq!(0.0f16.atanh(), 0.0f16);
218+
assert_eq!((-0.0f16).atanh(), -0.0f16);
219+
220+
let inf: f16 = f16::INFINITY;
221+
let neg_inf: f16 = f16::NEG_INFINITY;
222+
let nan: f16 = f16::NAN;
223+
assert_eq!(1.0f16.atanh(), inf);
224+
assert_eq!((-1.0f16).atanh(), neg_inf);
225+
assert!(2f16.atanh().atanh().is_nan());
226+
assert!((-2f16).atanh().atanh().is_nan());
227+
assert!(inf.atanh().is_nan());
228+
assert!(neg_inf.atanh().is_nan());
229+
assert!(nan.atanh().is_nan());
230+
assert_approx_eq!(0.5f16.atanh(), 0.54930614433405484569762261846126285f16, TOL_0);
231+
assert_approx_eq!((-0.5f16).atanh(), -0.54930614433405484569762261846126285f16, TOL_0);
232+
}
233+
234+
#[test]
235+
#[cfg(not(miri))]
236+
#[cfg(not(bootstrap))]
237+
#[cfg(target_has_reliable_f16_math)]
238+
fn test_gamma() {
239+
// precision can differ among platforms
240+
assert_approx_eq!(1.0f16.gamma(), 1.0f16, TOL_0);
241+
assert_approx_eq!(2.0f16.gamma(), 1.0f16, TOL_0);
242+
assert_approx_eq!(3.0f16.gamma(), 2.0f16, TOL_0);
243+
assert_approx_eq!(4.0f16.gamma(), 6.0f16, TOL_0);
244+
assert_approx_eq!(5.0f16.gamma(), 24.0f16, TOL_0);
245+
assert_approx_eq!(0.5f16.gamma(), consts::PI.sqrt(), TOL_0);
246+
assert_approx_eq!((-0.5f16).gamma(), -2.0 * consts::PI.sqrt(), TOL_0);
247+
assert_eq!(0.0f16.gamma(), f16::INFINITY);
248+
assert_eq!((-0.0f16).gamma(), f16::NEG_INFINITY);
249+
assert!((-1.0f16).gamma().is_nan());
250+
assert!((-2.0f16).gamma().is_nan());
251+
assert!(f16::NAN.gamma().is_nan());
252+
assert!(f16::NEG_INFINITY.gamma().is_nan());
253+
assert_eq!(f16::INFINITY.gamma(), f16::INFINITY);
254+
assert_eq!(171.71f16.gamma(), f16::INFINITY);
255+
}
256+
257+
#[test]
258+
#[cfg(not(miri))]
259+
#[cfg(not(bootstrap))]
260+
#[cfg(target_has_reliable_f16_math)]
261+
fn test_ln_gamma() {
262+
assert_approx_eq!(1.0f16.ln_gamma().0, 0.0f16, TOL_0);
263+
assert_eq!(1.0f16.ln_gamma().1, 1);
264+
assert_approx_eq!(2.0f16.ln_gamma().0, 0.0f16, TOL_0);
265+
assert_eq!(2.0f16.ln_gamma().1, 1);
266+
assert_approx_eq!(3.0f16.ln_gamma().0, 2.0f16.ln(), TOL_0);
267+
assert_eq!(3.0f16.ln_gamma().1, 1);
268+
assert_approx_eq!((-0.5f16).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_0);
269+
assert_eq!((-0.5f16).ln_gamma().1, -1);
270+
}
271+
272+
#[test]
273+
fn test_real_consts() {
274+
// FIXME(f16_f128): add math tests when available
275+
276+
let pi: f16 = consts::PI;
277+
let frac_pi_2: f16 = consts::FRAC_PI_2;
278+
let frac_pi_3: f16 = consts::FRAC_PI_3;
279+
let frac_pi_4: f16 = consts::FRAC_PI_4;
280+
let frac_pi_6: f16 = consts::FRAC_PI_6;
281+
let frac_pi_8: f16 = consts::FRAC_PI_8;
282+
let frac_1_pi: f16 = consts::FRAC_1_PI;
283+
let frac_2_pi: f16 = consts::FRAC_2_PI;
284+
285+
assert_approx_eq!(frac_pi_2, pi / 2f16, TOL_0);
286+
assert_approx_eq!(frac_pi_3, pi / 3f16, TOL_0);
287+
assert_approx_eq!(frac_pi_4, pi / 4f16, TOL_0);
288+
assert_approx_eq!(frac_pi_6, pi / 6f16, TOL_0);
289+
assert_approx_eq!(frac_pi_8, pi / 8f16, TOL_0);
290+
assert_approx_eq!(frac_1_pi, 1f16 / pi, TOL_0);
291+
assert_approx_eq!(frac_2_pi, 2f16 / pi, TOL_0);
292+
293+
#[cfg(not(miri))]
294+
#[cfg(not(bootstrap))]
295+
#[cfg(target_has_reliable_f16_math)]
296+
{
297+
let frac_2_sqrtpi: f16 = consts::FRAC_2_SQRT_PI;
298+
let sqrt2: f16 = consts::SQRT_2;
299+
let frac_1_sqrt2: f16 = consts::FRAC_1_SQRT_2;
300+
let e: f16 = consts::E;
301+
let log2_e: f16 = consts::LOG2_E;
302+
let log10_e: f16 = consts::LOG10_E;
303+
let ln_2: f16 = consts::LN_2;
304+
let ln_10: f16 = consts::LN_10;
305+
306+
assert_approx_eq!(frac_2_sqrtpi, 2f16 / pi.sqrt(), TOL_0);
307+
assert_approx_eq!(sqrt2, 2f16.sqrt(), TOL_0);
308+
assert_approx_eq!(frac_1_sqrt2, 1f16 / 2f16.sqrt(), TOL_0);
309+
assert_approx_eq!(log2_e, e.log2(), TOL_0);
310+
assert_approx_eq!(log10_e, e.log10(), TOL_0);
311+
assert_approx_eq!(ln_2, 2f16.ln(), TOL_0);
312+
assert_approx_eq!(ln_10, 10f16.ln(), TOL_0);
313+
}
314+
}

‎library/std/tests/floats/f32.rs

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
use std::f32::consts;
2+
3+
#[allow(unused_macros)]
4+
macro_rules! assert_f32_biteq {
5+
($left : expr, $right : expr) => {
6+
let l: &f32 = &$left;
7+
let r: &f32 = &$right;
8+
let lb = l.to_bits();
9+
let rb = r.to_bits();
10+
assert_eq!(lb, rb, "float {l} ({lb:#010x}) is not bitequal to {r} ({rb:#010x})");
11+
};
12+
}
13+
14+
#[test]
15+
fn test_powf() {
16+
let nan: f32 = f32::NAN;
17+
let inf: f32 = f32::INFINITY;
18+
let neg_inf: f32 = f32::NEG_INFINITY;
19+
assert_eq!(1.0f32.powf(1.0), 1.0);
20+
assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
21+
assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
22+
assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
23+
assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
24+
assert_eq!(8.3f32.powf(0.0), 1.0);
25+
assert!(nan.powf(2.0).is_nan());
26+
assert_eq!(inf.powf(2.0), inf);
27+
assert_eq!(neg_inf.powf(3.0), neg_inf);
28+
}
29+
30+
#[test]
31+
fn test_exp() {
32+
assert_eq!(1.0, 0.0f32.exp());
33+
assert_approx_eq!(2.718282, 1.0f32.exp());
34+
assert_approx_eq!(148.413162, 5.0f32.exp());
35+
36+
let inf: f32 = f32::INFINITY;
37+
let neg_inf: f32 = f32::NEG_INFINITY;
38+
let nan: f32 = f32::NAN;
39+
assert_eq!(inf, inf.exp());
40+
assert_eq!(0.0, neg_inf.exp());
41+
assert!(nan.exp().is_nan());
42+
}
43+
44+
#[test]
45+
fn test_exp2() {
46+
assert_eq!(32.0, 5.0f32.exp2());
47+
assert_eq!(1.0, 0.0f32.exp2());
48+
49+
let inf: f32 = f32::INFINITY;
50+
let neg_inf: f32 = f32::NEG_INFINITY;
51+
let nan: f32 = f32::NAN;
52+
assert_eq!(inf, inf.exp2());
53+
assert_eq!(0.0, neg_inf.exp2());
54+
assert!(nan.exp2().is_nan());
55+
}
56+
57+
#[test]
58+
fn test_ln() {
59+
let nan: f32 = f32::NAN;
60+
let inf: f32 = f32::INFINITY;
61+
let neg_inf: f32 = f32::NEG_INFINITY;
62+
assert_approx_eq!(1.0f32.exp().ln(), 1.0);
63+
assert!(nan.ln().is_nan());
64+
assert_eq!(inf.ln(), inf);
65+
assert!(neg_inf.ln().is_nan());
66+
assert!((-2.3f32).ln().is_nan());
67+
assert_eq!((-0.0f32).ln(), neg_inf);
68+
assert_eq!(0.0f32.ln(), neg_inf);
69+
assert_approx_eq!(4.0f32.ln(), 1.386294);
70+
}
71+
72+
#[test]
73+
fn test_log() {
74+
let nan: f32 = f32::NAN;
75+
let inf: f32 = f32::INFINITY;
76+
let neg_inf: f32 = f32::NEG_INFINITY;
77+
assert_eq!(10.0f32.log(10.0), 1.0);
78+
assert_approx_eq!(2.3f32.log(3.5), 0.664858);
79+
assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
80+
assert!(1.0f32.log(1.0).is_nan());
81+
assert!(1.0f32.log(-13.9).is_nan());
82+
assert!(nan.log(2.3).is_nan());
83+
assert_eq!(inf.log(10.0), inf);
84+
assert!(neg_inf.log(8.8).is_nan());
85+
assert!((-2.3f32).log(0.1).is_nan());
86+
assert_eq!((-0.0f32).log(2.0), neg_inf);
87+
assert_eq!(0.0f32.log(7.0), neg_inf);
88+
}
89+
90+
#[test]
91+
fn test_log2() {
92+
let nan: f32 = f32::NAN;
93+
let inf: f32 = f32::INFINITY;
94+
let neg_inf: f32 = f32::NEG_INFINITY;
95+
assert_approx_eq!(10.0f32.log2(), 3.321928);
96+
assert_approx_eq!(2.3f32.log2(), 1.201634);
97+
assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
98+
assert!(nan.log2().is_nan());
99+
assert_eq!(inf.log2(), inf);
100+
assert!(neg_inf.log2().is_nan());
101+
assert!((-2.3f32).log2().is_nan());
102+
assert_eq!((-0.0f32).log2(), neg_inf);
103+
assert_eq!(0.0f32.log2(), neg_inf);
104+
}
105+
106+
#[test]
107+
fn test_log10() {
108+
let nan: f32 = f32::NAN;
109+
let inf: f32 = f32::INFINITY;
110+
let neg_inf: f32 = f32::NEG_INFINITY;
111+
assert_eq!(10.0f32.log10(), 1.0);
112+
assert_approx_eq!(2.3f32.log10(), 0.361728);
113+
assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
114+
assert_eq!(1.0f32.log10(), 0.0);
115+
assert!(nan.log10().is_nan());
116+
assert_eq!(inf.log10(), inf);
117+
assert!(neg_inf.log10().is_nan());
118+
assert!((-2.3f32).log10().is_nan());
119+
assert_eq!((-0.0f32).log10(), neg_inf);
120+
assert_eq!(0.0f32.log10(), neg_inf);
121+
}
122+
123+
#[test]
124+
fn test_asinh() {
125+
assert_eq!(0.0f32.asinh(), 0.0f32);
126+
assert_eq!((-0.0f32).asinh(), -0.0f32);
127+
128+
let inf: f32 = f32::INFINITY;
129+
let neg_inf: f32 = f32::NEG_INFINITY;
130+
let nan: f32 = f32::NAN;
131+
assert_eq!(inf.asinh(), inf);
132+
assert_eq!(neg_inf.asinh(), neg_inf);
133+
assert!(nan.asinh().is_nan());
134+
assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
135+
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
136+
assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
137+
// regression test for the catastrophic cancellation fixed in 72486
138+
assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32);
139+
140+
// test for low accuracy from issue 104548
141+
assert_approx_eq!(60.0f32, 60.0f32.sinh().asinh());
142+
// mul needed for approximate comparison to be meaningful
143+
assert_approx_eq!(1.0f32, 1e-15f32.sinh().asinh() * 1e15f32);
144+
}
145+
146+
#[test]
147+
fn test_acosh() {
148+
assert_eq!(1.0f32.acosh(), 0.0f32);
149+
assert!(0.999f32.acosh().is_nan());
150+
151+
let inf: f32 = f32::INFINITY;
152+
let neg_inf: f32 = f32::NEG_INFINITY;
153+
let nan: f32 = f32::NAN;
154+
assert_eq!(inf.acosh(), inf);
155+
assert!(neg_inf.acosh().is_nan());
156+
assert!(nan.acosh().is_nan());
157+
assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
158+
assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
159+
160+
// test for low accuracy from issue 104548
161+
assert_approx_eq!(60.0f32, 60.0f32.cosh().acosh());
162+
}
163+
164+
#[test]
165+
fn test_atanh() {
166+
assert_eq!(0.0f32.atanh(), 0.0f32);
167+
assert_eq!((-0.0f32).atanh(), -0.0f32);
168+
169+
let inf32: f32 = f32::INFINITY;
170+
let neg_inf32: f32 = f32::NEG_INFINITY;
171+
assert_eq!(1.0f32.atanh(), inf32);
172+
assert_eq!((-1.0f32).atanh(), neg_inf32);
173+
174+
assert!(2f64.atanh().atanh().is_nan());
175+
assert!((-2f64).atanh().atanh().is_nan());
176+
177+
let inf64: f32 = f32::INFINITY;
178+
let neg_inf64: f32 = f32::NEG_INFINITY;
179+
let nan32: f32 = f32::NAN;
180+
assert!(inf64.atanh().is_nan());
181+
assert!(neg_inf64.atanh().is_nan());
182+
assert!(nan32.atanh().is_nan());
183+
184+
assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
185+
assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
186+
}
187+
188+
#[test]
189+
fn test_gamma() {
190+
// precision can differ between platforms
191+
assert_approx_eq!(1.0f32.gamma(), 1.0f32);
192+
assert_approx_eq!(2.0f32.gamma(), 1.0f32);
193+
assert_approx_eq!(3.0f32.gamma(), 2.0f32);
194+
assert_approx_eq!(4.0f32.gamma(), 6.0f32);
195+
assert_approx_eq!(5.0f32.gamma(), 24.0f32);
196+
assert_approx_eq!(0.5f32.gamma(), consts::PI.sqrt());
197+
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt());
198+
assert_eq!(0.0f32.gamma(), f32::INFINITY);
199+
assert_eq!((-0.0f32).gamma(), f32::NEG_INFINITY);
200+
assert!((-1.0f32).gamma().is_nan());
201+
assert!((-2.0f32).gamma().is_nan());
202+
assert!(f32::NAN.gamma().is_nan());
203+
assert!(f32::NEG_INFINITY.gamma().is_nan());
204+
assert_eq!(f32::INFINITY.gamma(), f32::INFINITY);
205+
assert_eq!(171.71f32.gamma(), f32::INFINITY);
206+
}
207+
208+
#[test]
209+
fn test_ln_gamma() {
210+
assert_approx_eq!(1.0f32.ln_gamma().0, 0.0f32);
211+
assert_eq!(1.0f32.ln_gamma().1, 1);
212+
assert_approx_eq!(2.0f32.ln_gamma().0, 0.0f32);
213+
assert_eq!(2.0f32.ln_gamma().1, 1);
214+
assert_approx_eq!(3.0f32.ln_gamma().0, 2.0f32.ln());
215+
assert_eq!(3.0f32.ln_gamma().1, 1);
216+
assert_approx_eq!((-0.5f32).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln());
217+
assert_eq!((-0.5f32).ln_gamma().1, -1);
218+
}
219+
220+
#[test]
221+
fn test_real_consts() {
222+
let pi: f32 = consts::PI;
223+
let frac_pi_2: f32 = consts::FRAC_PI_2;
224+
let frac_pi_3: f32 = consts::FRAC_PI_3;
225+
let frac_pi_4: f32 = consts::FRAC_PI_4;
226+
let frac_pi_6: f32 = consts::FRAC_PI_6;
227+
let frac_pi_8: f32 = consts::FRAC_PI_8;
228+
let frac_1_pi: f32 = consts::FRAC_1_PI;
229+
let frac_2_pi: f32 = consts::FRAC_2_PI;
230+
let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
231+
let sqrt2: f32 = consts::SQRT_2;
232+
let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
233+
let e: f32 = consts::E;
234+
let log2_e: f32 = consts::LOG2_E;
235+
let log10_e: f32 = consts::LOG10_E;
236+
let ln_2: f32 = consts::LN_2;
237+
let ln_10: f32 = consts::LN_10;
238+
239+
assert_approx_eq!(frac_pi_2, pi / 2f32);
240+
assert_approx_eq!(frac_pi_3, pi / 3f32);
241+
assert_approx_eq!(frac_pi_4, pi / 4f32);
242+
assert_approx_eq!(frac_pi_6, pi / 6f32);
243+
assert_approx_eq!(frac_pi_8, pi / 8f32);
244+
assert_approx_eq!(frac_1_pi, 1f32 / pi);
245+
assert_approx_eq!(frac_2_pi, 2f32 / pi);
246+
assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
247+
assert_approx_eq!(sqrt2, 2f32.sqrt());
248+
assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
249+
assert_approx_eq!(log2_e, e.log2());
250+
assert_approx_eq!(log10_e, e.log10());
251+
assert_approx_eq!(ln_2, 2f32.ln());
252+
assert_approx_eq!(ln_10, 10f32.ln());
253+
}

‎library/std/tests/floats/f64.rs

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
use std::f64::consts;
2+
3+
#[allow(unused_macros)]
4+
macro_rules! assert_f64_biteq {
5+
($left : expr, $right : expr) => {
6+
let l: &f64 = &$left;
7+
let r: &f64 = &$right;
8+
let lb = l.to_bits();
9+
let rb = r.to_bits();
10+
assert_eq!(lb, rb, "float {l} ({lb:#018x}) is not bitequal to {r} ({rb:#018x})");
11+
};
12+
}
13+
14+
#[test]
15+
fn test_powf() {
16+
let nan: f64 = f64::NAN;
17+
let inf: f64 = f64::INFINITY;
18+
let neg_inf: f64 = f64::NEG_INFINITY;
19+
assert_eq!(1.0f64.powf(1.0), 1.0);
20+
assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
21+
assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
22+
assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
23+
assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
24+
assert_eq!(8.3f64.powf(0.0), 1.0);
25+
assert!(nan.powf(2.0).is_nan());
26+
assert_eq!(inf.powf(2.0), inf);
27+
assert_eq!(neg_inf.powf(3.0), neg_inf);
28+
}
29+
30+
#[test]
31+
fn test_exp() {
32+
assert_eq!(1.0, 0.0f64.exp());
33+
assert_approx_eq!(2.718282, 1.0f64.exp());
34+
assert_approx_eq!(148.413159, 5.0f64.exp());
35+
36+
let inf: f64 = f64::INFINITY;
37+
let neg_inf: f64 = f64::NEG_INFINITY;
38+
let nan: f64 = f64::NAN;
39+
assert_eq!(inf, inf.exp());
40+
assert_eq!(0.0, neg_inf.exp());
41+
assert!(nan.exp().is_nan());
42+
}
43+
44+
#[test]
45+
fn test_exp2() {
46+
assert_eq!(32.0, 5.0f64.exp2());
47+
assert_eq!(1.0, 0.0f64.exp2());
48+
49+
let inf: f64 = f64::INFINITY;
50+
let neg_inf: f64 = f64::NEG_INFINITY;
51+
let nan: f64 = f64::NAN;
52+
assert_eq!(inf, inf.exp2());
53+
assert_eq!(0.0, neg_inf.exp2());
54+
assert!(nan.exp2().is_nan());
55+
}
56+
57+
#[test]
58+
fn test_ln() {
59+
let nan: f64 = f64::NAN;
60+
let inf: f64 = f64::INFINITY;
61+
let neg_inf: f64 = f64::NEG_INFINITY;
62+
assert_approx_eq!(1.0f64.exp().ln(), 1.0);
63+
assert!(nan.ln().is_nan());
64+
assert_eq!(inf.ln(), inf);
65+
assert!(neg_inf.ln().is_nan());
66+
assert!((-2.3f64).ln().is_nan());
67+
assert_eq!((-0.0f64).ln(), neg_inf);
68+
assert_eq!(0.0f64.ln(), neg_inf);
69+
assert_approx_eq!(4.0f64.ln(), 1.386294);
70+
}
71+
72+
#[test]
73+
fn test_log() {
74+
let nan: f64 = f64::NAN;
75+
let inf: f64 = f64::INFINITY;
76+
let neg_inf: f64 = f64::NEG_INFINITY;
77+
assert_eq!(10.0f64.log(10.0), 1.0);
78+
assert_approx_eq!(2.3f64.log(3.5), 0.664858);
79+
assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
80+
assert!(1.0f64.log(1.0).is_nan());
81+
assert!(1.0f64.log(-13.9).is_nan());
82+
assert!(nan.log(2.3).is_nan());
83+
assert_eq!(inf.log(10.0), inf);
84+
assert!(neg_inf.log(8.8).is_nan());
85+
assert!((-2.3f64).log(0.1).is_nan());
86+
assert_eq!((-0.0f64).log(2.0), neg_inf);
87+
assert_eq!(0.0f64.log(7.0), neg_inf);
88+
}
89+
90+
#[test]
91+
fn test_log2() {
92+
let nan: f64 = f64::NAN;
93+
let inf: f64 = f64::INFINITY;
94+
let neg_inf: f64 = f64::NEG_INFINITY;
95+
assert_approx_eq!(10.0f64.log2(), 3.321928);
96+
assert_approx_eq!(2.3f64.log2(), 1.201634);
97+
assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
98+
assert!(nan.log2().is_nan());
99+
assert_eq!(inf.log2(), inf);
100+
assert!(neg_inf.log2().is_nan());
101+
assert!((-2.3f64).log2().is_nan());
102+
assert_eq!((-0.0f64).log2(), neg_inf);
103+
assert_eq!(0.0f64.log2(), neg_inf);
104+
}
105+
106+
#[test]
107+
fn test_log10() {
108+
let nan: f64 = f64::NAN;
109+
let inf: f64 = f64::INFINITY;
110+
let neg_inf: f64 = f64::NEG_INFINITY;
111+
assert_eq!(10.0f64.log10(), 1.0);
112+
assert_approx_eq!(2.3f64.log10(), 0.361728);
113+
assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
114+
assert_eq!(1.0f64.log10(), 0.0);
115+
assert!(nan.log10().is_nan());
116+
assert_eq!(inf.log10(), inf);
117+
assert!(neg_inf.log10().is_nan());
118+
assert!((-2.3f64).log10().is_nan());
119+
assert_eq!((-0.0f64).log10(), neg_inf);
120+
assert_eq!(0.0f64.log10(), neg_inf);
121+
}
122+
123+
#[test]
124+
fn test_asinh() {
125+
assert_eq!(0.0f64.asinh(), 0.0f64);
126+
assert_eq!((-0.0f64).asinh(), -0.0f64);
127+
128+
let inf: f64 = f64::INFINITY;
129+
let neg_inf: f64 = f64::NEG_INFINITY;
130+
let nan: f64 = f64::NAN;
131+
assert_eq!(inf.asinh(), inf);
132+
assert_eq!(neg_inf.asinh(), neg_inf);
133+
assert!(nan.asinh().is_nan());
134+
assert!((-0.0f64).asinh().is_sign_negative());
135+
// issue 63271
136+
assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
137+
assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
138+
// regression test for the catastrophic cancellation fixed in 72486
139+
assert_approx_eq!((-67452098.07139316f64).asinh(), -18.72007542627454439398548429400083);
140+
141+
// test for low accuracy from issue 104548
142+
assert_approx_eq!(60.0f64, 60.0f64.sinh().asinh());
143+
// mul needed for approximate comparison to be meaningful
144+
assert_approx_eq!(1.0f64, 1e-15f64.sinh().asinh() * 1e15f64);
145+
}
146+
147+
#[test]
148+
fn test_acosh() {
149+
assert_eq!(1.0f64.acosh(), 0.0f64);
150+
assert!(0.999f64.acosh().is_nan());
151+
152+
let inf: f64 = f64::INFINITY;
153+
let neg_inf: f64 = f64::NEG_INFINITY;
154+
let nan: f64 = f64::NAN;
155+
assert_eq!(inf.acosh(), inf);
156+
assert!(neg_inf.acosh().is_nan());
157+
assert!(nan.acosh().is_nan());
158+
assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
159+
assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
160+
161+
// test for low accuracy from issue 104548
162+
assert_approx_eq!(60.0f64, 60.0f64.cosh().acosh());
163+
}
164+
165+
#[test]
166+
fn test_atanh() {
167+
assert_eq!(0.0f64.atanh(), 0.0f64);
168+
assert_eq!((-0.0f64).atanh(), -0.0f64);
169+
170+
let inf: f64 = f64::INFINITY;
171+
let neg_inf: f64 = f64::NEG_INFINITY;
172+
let nan: f64 = f64::NAN;
173+
assert_eq!(1.0f64.atanh(), inf);
174+
assert_eq!((-1.0f64).atanh(), neg_inf);
175+
assert!(2f64.atanh().atanh().is_nan());
176+
assert!((-2f64).atanh().atanh().is_nan());
177+
assert!(inf.atanh().is_nan());
178+
assert!(neg_inf.atanh().is_nan());
179+
assert!(nan.atanh().is_nan());
180+
assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
181+
assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
182+
}
183+
184+
#[test]
185+
fn test_gamma() {
186+
// precision can differ between platforms
187+
assert_approx_eq!(1.0f64.gamma(), 1.0f64);
188+
assert_approx_eq!(2.0f64.gamma(), 1.0f64);
189+
assert_approx_eq!(3.0f64.gamma(), 2.0f64);
190+
assert_approx_eq!(4.0f64.gamma(), 6.0f64);
191+
assert_approx_eq!(5.0f64.gamma(), 24.0f64);
192+
assert_approx_eq!(0.5f64.gamma(), consts::PI.sqrt());
193+
assert_approx_eq!((-0.5f64).gamma(), -2.0 * consts::PI.sqrt());
194+
assert_eq!(0.0f64.gamma(), f64::INFINITY);
195+
assert_eq!((-0.0f64).gamma(), f64::NEG_INFINITY);
196+
assert!((-1.0f64).gamma().is_nan());
197+
assert!((-2.0f64).gamma().is_nan());
198+
assert!(f64::NAN.gamma().is_nan());
199+
assert!(f64::NEG_INFINITY.gamma().is_nan());
200+
assert_eq!(f64::INFINITY.gamma(), f64::INFINITY);
201+
assert_eq!(171.71f64.gamma(), f64::INFINITY);
202+
}
203+
204+
#[test]
205+
fn test_ln_gamma() {
206+
assert_approx_eq!(1.0f64.ln_gamma().0, 0.0f64);
207+
assert_eq!(1.0f64.ln_gamma().1, 1);
208+
assert_approx_eq!(2.0f64.ln_gamma().0, 0.0f64);
209+
assert_eq!(2.0f64.ln_gamma().1, 1);
210+
assert_approx_eq!(3.0f64.ln_gamma().0, 2.0f64.ln());
211+
assert_eq!(3.0f64.ln_gamma().1, 1);
212+
assert_approx_eq!((-0.5f64).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln());
213+
assert_eq!((-0.5f64).ln_gamma().1, -1);
214+
}
215+
216+
#[test]
217+
fn test_real_consts() {
218+
let pi: f64 = consts::PI;
219+
let frac_pi_2: f64 = consts::FRAC_PI_2;
220+
let frac_pi_3: f64 = consts::FRAC_PI_3;
221+
let frac_pi_4: f64 = consts::FRAC_PI_4;
222+
let frac_pi_6: f64 = consts::FRAC_PI_6;
223+
let frac_pi_8: f64 = consts::FRAC_PI_8;
224+
let frac_1_pi: f64 = consts::FRAC_1_PI;
225+
let frac_2_pi: f64 = consts::FRAC_2_PI;
226+
let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
227+
let sqrt2: f64 = consts::SQRT_2;
228+
let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
229+
let e: f64 = consts::E;
230+
let log2_e: f64 = consts::LOG2_E;
231+
let log10_e: f64 = consts::LOG10_E;
232+
let ln_2: f64 = consts::LN_2;
233+
let ln_10: f64 = consts::LN_10;
234+
235+
assert_approx_eq!(frac_pi_2, pi / 2f64);
236+
assert_approx_eq!(frac_pi_3, pi / 3f64);
237+
assert_approx_eq!(frac_pi_4, pi / 4f64);
238+
assert_approx_eq!(frac_pi_6, pi / 6f64);
239+
assert_approx_eq!(frac_pi_8, pi / 8f64);
240+
assert_approx_eq!(frac_1_pi, 1f64 / pi);
241+
assert_approx_eq!(frac_2_pi, 2f64 / pi);
242+
assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
243+
assert_approx_eq!(sqrt2, 2f64.sqrt());
244+
assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
245+
assert_approx_eq!(log2_e, e.log2());
246+
assert_approx_eq!(log10_e, e.log10());
247+
assert_approx_eq!(ln_2, 2f64.ln());
248+
assert_approx_eq!(ln_10, 10f64.ln());
249+
}

‎library/std/tests/floats/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![feature(f16, f128, float_gamma, float_minimum_maximum)]
2+
#![cfg_attr(not(bootstrap), feature(cfg_target_has_reliable_f16_f128))]
3+
#![cfg_attr(not(bootstrap), expect(internal_features))] // reliable_f16_f128
4+
5+
use std::fmt;
6+
use std::ops::{Add, Div, Mul, Rem, Sub};
7+
8+
/// Verify that floats are within a tolerance of each other, 1.0e-6 by default.
9+
macro_rules! assert_approx_eq {
10+
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, 1.0e-6) }};
11+
($a:expr, $b:expr, $lim:expr) => {{
12+
let (a, b) = (&$a, &$b);
13+
let diff = (*a - *b).abs();
14+
assert!(
15+
diff < $lim,
16+
"{a:?} is not approximately equal to {b:?} (threshold {lim:?}, difference {diff:?})",
17+
lim = $lim
18+
);
19+
}};
20+
}
21+
22+
/// Helper function for testing numeric operations
23+
pub fn test_num<T>(ten: T, two: T)
24+
where
25+
T: PartialEq
26+
+ Add<Output = T>
27+
+ Sub<Output = T>
28+
+ Mul<Output = T>
29+
+ Div<Output = T>
30+
+ Rem<Output = T>
31+
+ fmt::Debug
32+
+ Copy,
33+
{
34+
assert_eq!(ten.add(two), ten + two);
35+
assert_eq!(ten.sub(two), ten - two);
36+
assert_eq!(ten.mul(two), ten * two);
37+
assert_eq!(ten.div(two), ten / two);
38+
assert_eq!(ten.rem(two), ten % two);
39+
}
40+
41+
mod f128;
42+
mod f16;
43+
mod f32;
44+
mod f64;

0 commit comments

Comments
 (0)
Please sign in to comment.