diff --git a/library/std/src/f128/tests.rs b/library/std/src/f128/tests.rs index 90f061e66a3a..3c329f7934ce 100644 --- a/library/std/src/f128/tests.rs +++ b/library/std/src/f128/tests.rs @@ -425,14 +425,16 @@ fn test_recip() { assert_eq!(neg_inf.recip(), 0.0); } +// Many math functions allow for less accurate results, so the next tolerance up is used + #[test] fn test_powi() { let nan: f128 = f128::NAN; let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(1.0f128.powi(1), 1.0); - assert_approx_eq!((-3.1f128).powi(2), 9.6100000000000005506706202140776519387, TOL_0); - assert_approx_eq!(5.9f128.powi(-2), 0.028727377190462507313100483690639638451, TOL_N2); + assert_approx_eq!((-3.1f128).powi(2), 9.6100000000000005506706202140776519387, TOL_P2); + assert_approx_eq!(5.9f128.powi(-2), 0.028727377190462507313100483690639638451, TOL_0); assert_eq!(8.3f128.powi(0), 1.0); assert!(nan.powi(2).is_nan()); assert_eq!(inf.powi(3), inf); @@ -445,10 +447,10 @@ fn test_powf() { let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(1.0f128.powf(1.0), 1.0); - assert_approx_eq!(3.4f128.powf(4.5), 246.40818323761892815995637964326426756, TOL_P2); - assert_approx_eq!(2.7f128.powf(-3.2), 0.041652009108526178281070304373500889273, TOL_N2); - assert_approx_eq!((-3.1f128).powf(2.0), 9.6100000000000005506706202140776519387, TOL_P2); - assert_approx_eq!(5.9f128.powf(-2.0), 0.028727377190462507313100483690639638451, TOL_N2); + assert_approx_eq!(3.4f128.powf(4.5), 246.40818323761892815995637964326426756, TOL_P4); + assert_approx_eq!(2.7f128.powf(-3.2), 0.041652009108526178281070304373500889273, TOL_0); + assert_approx_eq!((-3.1f128).powf(2.0), 9.6100000000000005506706202140776519387, TOL_P4); + assert_approx_eq!(5.9f128.powf(-2.0), 0.028727377190462507313100483690639638451, TOL_0); assert_eq!(8.3f128.powf(0.0), 1.0); assert!(nan.powf(2.0).is_nan()); assert_eq!(inf.powf(2.0), inf); @@ -470,7 +472,7 @@ fn test_sqrt_domain() { fn test_exp() { assert_eq!(1.0, 0.0f128.exp()); assert_approx_eq!(consts::E, 1.0f128.exp(), TOL_0); - assert_approx_eq!(148.41315910257660342111558004055227962348775, 5.0f128.exp(), TOL_0); + assert_approx_eq!(148.41315910257660342111558004055227962348775, 5.0f128.exp(), TOL_P2); let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; @@ -505,7 +507,7 @@ fn test_ln() { assert!((-2.3f128).ln().is_nan()); assert_eq!((-0.0f128).ln(), neg_inf); assert_eq!(0.0f128.ln(), neg_inf); - assert_approx_eq!(4.0f128.ln(), 1.3862943611198906188344642429163531366, TOL_0); + assert_approx_eq!(4.0f128.ln(), 1.3862943611198906188344642429163531366, TOL_P2); } #[test] @@ -514,7 +516,7 @@ fn test_log() { let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(10.0f128.log(10.0), 1.0); - assert_approx_eq!(2.3f128.log(3.5), 0.664858, TOL_0); + assert_approx_eq!(2.3f128.log(3.5), 0.66485771361478710036766645911922010272, TOL_P4); assert_eq!(1.0f128.exp().log(1.0f128.exp()), 1.0); assert!(1.0f128.log(1.0).is_nan()); assert!(1.0f128.log(-13.9).is_nan()); @@ -531,9 +533,9 @@ fn test_log2() { let nan: f128 = f128::NAN; let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; - assert_approx_eq!(10.0f128.log2(), 3.32192809488736234787031942948939017, TOL_0); - assert_approx_eq!(2.3f128.log2(), 1.2016338611696504130002982471978765921, TOL_0); - assert_approx_eq!(1.0f128.exp().log2(), 1.4426950408889634073599246810018921381, TOL_0); + assert_approx_eq!(10.0f128.log2(), 3.32192809488736234787031942948939017, TOL_P2); + assert_approx_eq!(2.3f128.log2(), 1.2016338611696504130002982471978765921, TOL_P2); + assert_approx_eq!(1.0f128.exp().log2(), 1.4426950408889634073599246810018921381, TOL_P2); assert!(nan.log2().is_nan()); assert_eq!(inf.log2(), inf); assert!(neg_inf.log2().is_nan()); @@ -548,8 +550,8 @@ fn test_log10() { let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(10.0f128.log10(), 1.0); - assert_approx_eq!(2.3f128.log10(), 0.36172783601759284532595218865859309898, TOL_0); - assert_approx_eq!(1.0f128.exp().log10(), 0.43429448190325182765112891891660508222, TOL_0); + assert_approx_eq!(2.3f128.log10(), 0.36172783601759284532595218865859309898, TOL_P2); + assert_approx_eq!(1.0f128.exp().log10(), 0.43429448190325182765112891891660508222, TOL_P2); assert_eq!(1.0f128.log10(), 0.0); assert!(nan.log10().is_nan()); assert_eq!(inf.log10(), inf); @@ -566,7 +568,7 @@ fn test_to_degrees() { let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(0.0f128.to_degrees(), 0.0); - assert_approx_eq!((-5.8f128).to_degrees(), -332.31552117587745090765431723855668471, TOL_P2); + assert_approx_eq!((-5.8f128).to_degrees(), -332.31552117587745090765431723855668471, TOL_P4); assert_approx_eq!(pi.to_degrees(), 180.0, TOL_P2); assert!(nan.to_degrees().is_nan()); assert_eq!(inf.to_degrees(), inf); @@ -581,8 +583,8 @@ fn test_to_radians() { let inf: f128 = f128::INFINITY; let neg_inf: f128 = f128::NEG_INFINITY; assert_eq!(0.0f128.to_radians(), 0.0); - assert_approx_eq!(154.6f128.to_radians(), 2.6982790235832334267135442069489767804, TOL_0); - assert_approx_eq!((-332.31f128).to_radians(), -5.7999036373023566567593094812182763013, TOL_0); + assert_approx_eq!(154.6f128.to_radians(), 2.6982790235832334267135442069489767804, TOL_P2); + assert_approx_eq!((-332.31f128).to_radians(), -5.7999036373023566567593094812182763013, TOL_P2); // check approx rather than exact because round trip for pi doesn't fall on an exactly // representable value (unlike `f32` and `f64`). assert_approx_eq!(180.0f128.to_radians(), pi, TOL_0); @@ -593,6 +595,7 @@ fn test_to_radians() { #[test] fn test_asinh() { + // Lower accuracy results are allowed, use increased tolerances assert_eq!(0.0f128.asinh(), 0.0f128); assert_eq!((-0.0f128).asinh(), -0.0f128); @@ -603,20 +606,21 @@ fn test_asinh() { assert_eq!(neg_inf.asinh(), neg_inf); assert!(nan.asinh().is_nan()); assert!((-0.0f128).asinh().is_sign_negative()); + // issue 63271 - assert_approx_eq!(2.0f128.asinh(), 1.443635475178810342493276740273105f128, TOL_0); - assert_approx_eq!((-2.0f128).asinh(), -1.443635475178810342493276740273105f128, TOL_0); + assert_approx_eq!(2.0f128.asinh(), 1.443635475178810342493276740273105f128, TOL_P4); + assert_approx_eq!((-2.0f128).asinh(), -1.443635475178810342493276740273105f128, TOL_P4); // regression test for the catastrophic cancellation fixed in 72486 assert_approx_eq!( (-67452098.07139316f128).asinh(), -18.720075426274544393985484294000831757220, - TOL_0 + TOL_P4 ); // test for low accuracy from issue 104548 - assert_approx_eq!(60.0f128, 60.0f128.sinh().asinh(), TOL_0); + assert_approx_eq!(60.0f128, 60.0f128.sinh().asinh(), TOL_P4); // mul needed for approximate comparison to be meaningful - assert_approx_eq!(1.0f128, 1e-15f128.sinh().asinh() * 1e15f128, TOL_0); + assert_approx_eq!(1.0f128, 1e-15f128.sinh().asinh() * 1e15f128, TOL_P4); } #[test] @@ -630,11 +634,11 @@ fn test_acosh() { assert_eq!(inf.acosh(), inf); assert!(neg_inf.acosh().is_nan()); assert!(nan.acosh().is_nan()); - assert_approx_eq!(2.0f128.acosh(), 1.31695789692481670862504634730796844f128, TOL_0); - assert_approx_eq!(3.0f128.acosh(), 1.76274717403908605046521864995958461f128, TOL_0); + assert_approx_eq!(2.0f128.acosh(), 1.31695789692481670862504634730796844f128, TOL_P2); + assert_approx_eq!(3.0f128.acosh(), 1.76274717403908605046521864995958461f128, TOL_P2); // test for low accuracy from issue 104548 - assert_approx_eq!(60.0f128, 60.0f128.cosh().acosh(), TOL_P2); + assert_approx_eq!(60.0f128, 60.0f128.cosh().acosh(), TOL_P4); } #[test] @@ -652,20 +656,20 @@ fn test_atanh() { assert!(inf.atanh().is_nan()); assert!(neg_inf.atanh().is_nan()); assert!(nan.atanh().is_nan()); - assert_approx_eq!(0.5f128.atanh(), 0.54930614433405484569762261846126285f128, TOL_0); - assert_approx_eq!((-0.5f128).atanh(), -0.54930614433405484569762261846126285f128, TOL_0); + assert_approx_eq!(0.5f128.atanh(), 0.54930614433405484569762261846126285f128, TOL_P2); + assert_approx_eq!((-0.5f128).atanh(), -0.54930614433405484569762261846126285f128, TOL_P2); } #[test] fn test_gamma() { - // precision can differ between platforms - assert_approx_eq!(1.0f128.gamma(), 1.0f128, TOL_0); - assert_approx_eq!(2.0f128.gamma(), 1.0f128, TOL_0); - assert_approx_eq!(3.0f128.gamma(), 2.0f128, TOL_0); - assert_approx_eq!(4.0f128.gamma(), 6.0f128, TOL_0); - assert_approx_eq!(5.0f128.gamma(), 24.0f128, TOL_0); - assert_approx_eq!(0.5f128.gamma(), consts::PI.sqrt(), TOL_0); - assert_approx_eq!((-0.5f128).gamma(), -2.0 * consts::PI.sqrt(), TOL_0); + // precision can differ among platforms + assert_approx_eq!(1.0f128.gamma(), 1.0f128, TOL_P2); + assert_approx_eq!(2.0f128.gamma(), 1.0f128, TOL_P2); + assert_approx_eq!(3.0f128.gamma(), 2.0f128, TOL_P2); + assert_approx_eq!(4.0f128.gamma(), 6.0f128, TOL_P2); + assert_approx_eq!(5.0f128.gamma(), 24.0f128, TOL_P2); + assert_approx_eq!(0.5f128.gamma(), consts::PI.sqrt(), TOL_P2); + assert_approx_eq!((-0.5f128).gamma(), -2.0 * consts::PI.sqrt(), TOL_P2); assert_eq!(0.0f128.gamma(), f128::INFINITY); assert_eq!((-0.0f128).gamma(), f128::NEG_INFINITY); assert!((-1.0f128).gamma().is_nan()); @@ -678,13 +682,13 @@ fn test_gamma() { #[test] fn test_ln_gamma() { - assert_approx_eq!(1.0f128.ln_gamma().0, 0.0f128, TOL_0); + assert_approx_eq!(1.0f128.ln_gamma().0, 0.0f128, TOL_P2); assert_eq!(1.0f128.ln_gamma().1, 1); - assert_approx_eq!(2.0f128.ln_gamma().0, 0.0f128, TOL_0); + assert_approx_eq!(2.0f128.ln_gamma().0, 0.0f128, TOL_P2); assert_eq!(2.0f128.ln_gamma().1, 1); - assert_approx_eq!(3.0f128.ln_gamma().0, 2.0f128.ln(), TOL_0); + assert_approx_eq!(3.0f128.ln_gamma().0, 2.0f128.ln(), TOL_P2); assert_eq!(3.0f128.ln_gamma().1, 1); - assert_approx_eq!((-0.5f128).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_0); + assert_approx_eq!((-0.5f128).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), TOL_P2); assert_eq!((-0.5f128).ln_gamma().1, -1); }