@@ -511,28 +511,7 @@ impl f64 {
511
511
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
512
512
#[ inline]
513
513
pub fn ln ( self ) -> f64 {
514
- if !cfg ! ( target_os = "sunos" ) {
515
- unsafe { intrinsics:: logf64 ( self ) }
516
- } else {
517
- // Illumos requires a wrapper around log, log2, and log10 functions
518
- // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
519
- // of expected NaN).
520
- if self . is_finite ( ) {
521
- if self > 0.0 {
522
- unsafe { intrinsics:: logf64 ( self ) }
523
- } else if self == 0.0 {
524
- NEG_INFINITY // log(0) = -Inf
525
- } else {
526
- NAN // log(-n) = NaN
527
- }
528
- } else if self . is_nan ( ) {
529
- self // log(NaN) = NaN
530
- } else if self > 0.0 {
531
- self // log(Inf) = Inf
532
- } else {
533
- NAN // log(-Inf) = NaN
534
- }
535
- }
514
+ self . log_wrapper ( |n| { unsafe { intrinsics:: logf64 ( n) } } )
536
515
}
537
516
538
517
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -567,27 +546,7 @@ impl f64 {
567
546
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
568
547
#[ inline]
569
548
pub fn log2 ( self ) -> f64 {
570
- if !cfg ! ( target_os = "sunos" ) {
571
- unsafe { intrinsics:: log2f64 ( self ) }
572
- } else {
573
- // Illumos requires a wrapper around the log2 function because of
574
- // its non-standard behavior
575
- if self . is_finite ( ) {
576
- if self > 0.0 {
577
- unsafe { intrinsics:: log2f64 ( self ) }
578
- } else if self == 0.0 {
579
- NEG_INFINITY // log2(0) = -Inf
580
- } else {
581
- NAN // log2(-n) = NaN
582
- }
583
- } else if self . is_nan ( ) {
584
- self // log2(NaN) = NaN
585
- } else if self > 0.0 {
586
- self // log2(Inf) = Inf
587
- } else {
588
- NAN // log2(-Inf) = NaN
589
- }
590
- }
549
+ self . log_wrapper ( |n| { unsafe { intrinsics:: log2f64 ( n) } } )
591
550
}
592
551
593
552
/// Returns the base 10 logarithm of the number.
@@ -603,27 +562,7 @@ impl f64 {
603
562
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
604
563
#[ inline]
605
564
pub fn log10 ( self ) -> f64 {
606
- if !cfg ! ( target_os = "sunos" ) {
607
- unsafe { intrinsics:: log10f64 ( self ) }
608
- } else {
609
- // Illumos requires a wrapper around the log10 function because of
610
- // its non-standard behavior.
611
- if self . is_finite ( ) {
612
- if self > 0.0 {
613
- unsafe { intrinsics:: log10f64 ( self ) }
614
- } else if self == 0.0 {
615
- NEG_INFINITY // log10(0) = -Inf
616
- } else {
617
- NAN // log10(-n) = NaN
618
- }
619
- } else if self . is_nan ( ) {
620
- self // log10(NaN) = NaN
621
- } else if self > 0.0 {
622
- self // log10(Inf) = Inf
623
- } else {
624
- NAN // log10(-Inf) = NaN
625
- }
626
- }
565
+ self . log_wrapper ( |n| { unsafe { intrinsics:: log10f64 ( n) } } )
627
566
}
628
567
629
568
/// Converts radians to degrees.
@@ -1126,6 +1065,31 @@ impl f64 {
1126
1065
pub fn atanh ( self ) -> f64 {
1127
1066
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
1128
1067
}
1068
+
1069
+ // Illumos requires a wrapper around log, log2, and log10 functions
1070
+ // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
1071
+ // of expected NaN).
1072
+ fn log_wrapper < F : Fn ( f64 ) -> f64 > ( self , log_fn : F ) -> f64 {
1073
+ if !cfg ! ( target_os = "sunos" ) {
1074
+ log_fn ( self )
1075
+ } else {
1076
+ if self . is_finite ( ) {
1077
+ if self > 0.0 {
1078
+ log_fn ( self )
1079
+ } else if self == 0.0 {
1080
+ NEG_INFINITY // log(0) = -Inf
1081
+ } else {
1082
+ NAN // log(-n) = NaN
1083
+ }
1084
+ } else if self . is_nan ( ) {
1085
+ self // log(NaN) = NaN
1086
+ } else if self > 0.0 {
1087
+ self // log(Inf) = Inf
1088
+ } else {
1089
+ NAN // log(-Inf) = NaN
1090
+ }
1091
+ }
1092
+ }
1129
1093
}
1130
1094
1131
1095
#[ cfg( test) ]
0 commit comments