File tree 4 files changed +32
-4
lines changed
4 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -880,7 +880,9 @@ impl f32 {
880
880
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
881
881
#[ inline]
882
882
pub fn asinh ( self ) -> f32 {
883
- ( self . abs ( ) + ( ( self * self ) + 1.0 ) . sqrt ( ) ) . ln ( ) . copysign ( self )
883
+ let ax = self . abs ( ) ;
884
+ let ix = 1.0 / ax;
885
+ ( ax + ( ax / ( Self :: hypot ( 1.0 , ix) + ix) ) ) . ln_1p ( ) . copysign ( self )
884
886
}
885
887
886
888
/// Inverse hyperbolic cosine function.
@@ -900,7 +902,11 @@ impl f32 {
900
902
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
901
903
#[ inline]
902
904
pub fn acosh ( self ) -> f32 {
903
- if self < 1.0 { Self :: NAN } else { ( self + ( ( self * self ) - 1.0 ) . sqrt ( ) ) . ln ( ) }
905
+ if self < 1.0 {
906
+ Self :: NAN
907
+ } else {
908
+ ( self + ( ( self - 1.0 ) . sqrt ( ) * ( self + 1.0 ) . sqrt ( ) ) ) . ln ( )
909
+ }
904
910
}
905
911
906
912
/// Inverse hyperbolic tangent function.
Original file line number Diff line number Diff line change @@ -587,6 +587,11 @@ fn test_asinh() {
587
587
assert_approx_eq ! ( ( -2.0f32 ) . asinh( ) , -1.443635475178810342493276740273105f32 ) ;
588
588
// regression test for the catastrophic cancellation fixed in 72486
589
589
assert_approx_eq ! ( ( -3000.0f32 ) . asinh( ) , -8.699514775987968673236893537700647f32 ) ;
590
+
591
+ // test for low accuracy from issue 104548
592
+ assert_approx_eq ! ( 60.0f32 , 60.0f32 . sinh( ) . asinh( ) ) ;
593
+ // mul needed for approximate comparison to be meaningful
594
+ assert_approx_eq ! ( 1.0f32 , 1e-15f32 . sinh( ) . asinh( ) * 1e15f32 ) ;
590
595
}
591
596
592
597
#[ test]
@@ -602,6 +607,9 @@ fn test_acosh() {
602
607
assert ! ( nan. acosh( ) . is_nan( ) ) ;
603
608
assert_approx_eq ! ( 2.0f32 . acosh( ) , 1.31695789692481670862504634730796844f32 ) ;
604
609
assert_approx_eq ! ( 3.0f32 . acosh( ) , 1.76274717403908605046521864995958461f32 ) ;
610
+
611
+ // test for low accuracy from issue 104548
612
+ assert_approx_eq ! ( 60.0f32 , 60.0f32 . cosh( ) . acosh( ) ) ;
605
613
}
606
614
607
615
#[ test]
Original file line number Diff line number Diff line change @@ -882,7 +882,9 @@ impl f64 {
882
882
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
883
883
#[ inline]
884
884
pub fn asinh ( self ) -> f64 {
885
- ( self . abs ( ) + ( ( self * self ) + 1.0 ) . sqrt ( ) ) . ln ( ) . copysign ( self )
885
+ let ax = self . abs ( ) ;
886
+ let ix = 1.0 / ax;
887
+ ( ax + ( ax / ( Self :: hypot ( 1.0 , ix) + ix) ) ) . ln_1p ( ) . copysign ( self )
886
888
}
887
889
888
890
/// Inverse hyperbolic cosine function.
@@ -902,7 +904,11 @@ impl f64 {
902
904
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
903
905
#[ inline]
904
906
pub fn acosh ( self ) -> f64 {
905
- if self < 1.0 { Self :: NAN } else { ( self + ( ( self * self ) - 1.0 ) . sqrt ( ) ) . ln ( ) }
907
+ if self < 1.0 {
908
+ Self :: NAN
909
+ } else {
910
+ ( self + ( ( self - 1.0 ) . sqrt ( ) * ( self + 1.0 ) . sqrt ( ) ) ) . ln ( )
911
+ }
906
912
}
907
913
908
914
/// Inverse hyperbolic tangent function.
Original file line number Diff line number Diff line change @@ -575,6 +575,11 @@ fn test_asinh() {
575
575
assert_approx_eq ! ( ( -2.0f64 ) . asinh( ) , -1.443635475178810342493276740273105f64 ) ;
576
576
// regression test for the catastrophic cancellation fixed in 72486
577
577
assert_approx_eq ! ( ( -67452098.07139316f64 ) . asinh( ) , -18.72007542627454439398548429400083 ) ;
578
+
579
+ // test for low accuracy from issue 104548
580
+ assert_approx_eq ! ( 60.0f64 , 60.0f64 . sinh( ) . asinh( ) ) ;
581
+ // mul needed for approximate comparison to be meaningful
582
+ assert_approx_eq ! ( 1.0f64 , 1e-15f64 . sinh( ) . asinh( ) * 1e15f64 ) ;
578
583
}
579
584
580
585
#[ test]
@@ -590,6 +595,9 @@ fn test_acosh() {
590
595
assert ! ( nan. acosh( ) . is_nan( ) ) ;
591
596
assert_approx_eq ! ( 2.0f64 . acosh( ) , 1.31695789692481670862504634730796844f64 ) ;
592
597
assert_approx_eq ! ( 3.0f64 . acosh( ) , 1.76274717403908605046521864995958461f64 ) ;
598
+
599
+ // test for low accuracy from issue 104548
600
+ assert_approx_eq ! ( 60.0f64 , 60.0f64 . cosh( ) . acosh( ) ) ;
593
601
}
594
602
595
603
#[ test]
You can’t perform that action at this time.
0 commit comments