1 //! This module provides constants which are specific to the implementation
2 //! of the `f64` floating point data type.
4 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
8 //! Although using these constants won’t cause compilation warnings,
9 //! new code should use the associated constants directly on the primitive type.
11 #![stable(feature = "rust1", since = "1.0.0")]
12 #![allow(missing_docs)]
18 use crate::intrinsics
;
20 use crate::sys
::cmath
;
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use core
::f64::consts
;
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use core
::f64::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX}
;
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub use core
::f64::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY}
;
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use core
::f64::{MAX, MIN, MIN_POSITIVE}
;
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core
::f64::{MAX_EXP, MIN_10_EXP, MIN_EXP}
;
34 #[lang = "f64_runtime"]
36 /// Returns the largest integer less than or equal to a number.
45 /// assert_eq!(f.floor(), 3.0);
46 /// assert_eq!(g.floor(), 3.0);
47 /// assert_eq!(h.floor(), -4.0);
49 #[must_use = "method returns a new number and does not mutate the original value"]
50 #[stable(feature = "rust1", since = "1.0.0")]
52 pub fn floor(self) -> f64 {
53 unsafe { intrinsics::floorf64(self) }
56 /// Returns the smallest integer greater than or equal to a number.
64 /// assert_eq!(f.ceil(), 4.0);
65 /// assert_eq!(g.ceil(), 4.0);
67 #[must_use = "method returns a new number and does not mutate the original value"]
68 #[stable(feature = "rust1", since = "1.0.0")]
70 pub fn ceil(self) -> f64 {
71 unsafe { intrinsics::ceilf64(self) }
74 /// Returns the nearest integer to a number. Round half-way cases away from
83 /// assert_eq!(f.round(), 3.0);
84 /// assert_eq!(g.round(), -3.0);
86 #[must_use = "method returns a new number and does not mutate the original value"]
87 #[stable(feature = "rust1", since = "1.0.0")]
89 pub fn round(self) -> f64 {
90 unsafe { intrinsics::roundf64(self) }
93 /// Returns the integer part of a number.
100 /// let h = -3.7_f64;
102 /// assert_eq!(f.trunc(), 3.0);
103 /// assert_eq!(g.trunc(), 3.0);
104 /// assert_eq!(h.trunc(), -3.0);
106 #[must_use = "method returns a new number and does not mutate the original value"]
107 #[stable(feature = "rust1", since = "1.0.0")]
109 pub fn trunc(self) -> f64 {
110 unsafe { intrinsics::truncf64(self) }
113 /// Returns the fractional part of a number.
119 /// let y = -3.6_f64;
120 /// let abs_difference_x = (x.fract() - 0.6).abs();
121 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
123 /// assert!(abs_difference_x < 1e-10);
124 /// assert!(abs_difference_y < 1e-10);
126 #[must_use = "method returns a new number and does not mutate the original value"]
127 #[stable(feature = "rust1", since = "1.0.0")]
129 pub fn fract(self) -> f64 {
133 /// Computes the absolute value of `self`. Returns `NAN` if the
140 /// let y = -3.5_f64;
142 /// let abs_difference_x = (x.abs() - x).abs();
143 /// let abs_difference_y = (y.abs() - (-y)).abs();
145 /// assert!(abs_difference_x < 1e-10);
146 /// assert!(abs_difference_y < 1e-10);
148 /// assert!(f64::NAN.abs().is_nan());
150 #[must_use = "method returns a new number and does not mutate the original value"]
151 #[stable(feature = "rust1", since = "1.0.0")]
153 pub fn abs(self) -> f64 {
154 unsafe { intrinsics::fabsf64(self) }
157 /// Returns a number that represents the sign of `self`.
159 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
160 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
161 /// - `NAN` if the number is `NAN`
168 /// assert_eq!(f.signum(), 1.0);
169 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
171 /// assert!(f64::NAN.signum().is_nan());
173 #[must_use = "method returns a new number and does not mutate the original value"]
174 #[stable(feature = "rust1", since = "1.0.0")]
176 pub fn signum(self) -> f64 {
177 if self.is_nan() { Self::NAN }
else { 1.0_f64.copysign(self) }
180 /// Returns a number composed of the magnitude of `self` and the sign of
183 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
184 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
185 /// `sign` is returned.
192 /// assert_eq!(f.copysign(0.42), 3.5_f64);
193 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
194 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
195 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
197 /// assert!(f64::NAN.copysign(1.0).is_nan());
199 #[must_use = "method returns a new number and does not mutate the original value"]
200 #[stable(feature = "copysign", since = "1.35.0")]
202 pub fn copysign(self, sign
: f64) -> f64 {
203 unsafe { intrinsics::copysignf64(self, sign) }
206 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
207 /// error, yielding a more accurate result than an unfused multiply-add.
209 /// Using `mul_add` can be more performant than an unfused multiply-add if
210 /// the target architecture has a dedicated `fma` CPU instruction.
215 /// let m = 10.0_f64;
217 /// let b = 60.0_f64;
220 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
222 /// assert!(abs_difference < 1e-10);
224 #[must_use = "method returns a new number and does not mutate the original value"]
225 #[stable(feature = "rust1", since = "1.0.0")]
227 pub fn mul_add(self, a
: f64, b
: f64) -> f64 {
228 unsafe { intrinsics::fmaf64(self, a, b) }
231 /// Calculates Euclidean division, the matching method for `rem_euclid`.
233 /// This computes the integer `n` such that
234 /// `self = n * rhs + self.rem_euclid(rhs)`.
235 /// In other words, the result is `self / rhs` rounded to the integer `n`
236 /// such that `self >= n * rhs`.
241 /// let a: f64 = 7.0;
243 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
244 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
245 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
246 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
248 #[must_use = "method returns a new number and does not mutate the original value"]
250 #[stable(feature = "euclidean_division", since = "1.38.0")]
251 pub fn div_euclid(self, rhs
: f64) -> f64 {
252 let q
= (self / rhs
).trunc();
253 if self % rhs
< 0.0 {
254 return if rhs
> 0.0 { q - 1.0 }
else { q + 1.0 }
;
259 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
261 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
262 /// most cases. However, due to a floating point round-off error it can
263 /// result in `r == rhs.abs()`, violating the mathematical definition, if
264 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
265 /// This result is not an element of the function's codomain, but it is the
266 /// closest floating point number in the real numbers and thus fulfills the
267 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
273 /// let a: f64 = 7.0;
275 /// assert_eq!(a.rem_euclid(b), 3.0);
276 /// assert_eq!((-a).rem_euclid(b), 1.0);
277 /// assert_eq!(a.rem_euclid(-b), 3.0);
278 /// assert_eq!((-a).rem_euclid(-b), 1.0);
279 /// // limitation due to round-off error
280 /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
282 #[must_use = "method returns a new number and does not mutate the original value"]
284 #[stable(feature = "euclidean_division", since = "1.38.0")]
285 pub fn rem_euclid(self, rhs
: f64) -> f64 {
287 if r
< 0.0 { r + rhs.abs() }
else { r }
290 /// Raises a number to an integer power.
292 /// Using this function is generally faster than using `powf`
298 /// let abs_difference = (x.powi(2) - (x * x)).abs();
300 /// assert!(abs_difference < 1e-10);
302 #[must_use = "method returns a new number and does not mutate the original value"]
303 #[stable(feature = "rust1", since = "1.0.0")]
305 pub fn powi(self, n
: i32) -> f64 {
306 unsafe { intrinsics::powif64(self, n) }
309 /// Raises a number to a floating point power.
315 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
317 /// assert!(abs_difference < 1e-10);
319 #[must_use = "method returns a new number and does not mutate the original value"]
320 #[stable(feature = "rust1", since = "1.0.0")]
322 pub fn powf(self, n
: f64) -> f64 {
323 unsafe { intrinsics::powf64(self, n) }
326 /// Returns the square root of a number.
328 /// Returns NaN if `self` is a negative number.
333 /// let positive = 4.0_f64;
334 /// let negative = -4.0_f64;
336 /// let abs_difference = (positive.sqrt() - 2.0).abs();
338 /// assert!(abs_difference < 1e-10);
339 /// assert!(negative.sqrt().is_nan());
341 #[must_use = "method returns a new number and does not mutate the original value"]
342 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn sqrt(self) -> f64 {
345 unsafe { intrinsics::sqrtf64(self) }
348 /// Returns `e^(self)`, (the exponential function).
353 /// let one = 1.0_f64;
355 /// let e = one.exp();
357 /// // ln(e) - 1 == 0
358 /// let abs_difference = (e.ln() - 1.0).abs();
360 /// assert!(abs_difference < 1e-10);
362 #[must_use = "method returns a new number and does not mutate the original value"]
363 #[stable(feature = "rust1", since = "1.0.0")]
365 pub fn exp(self) -> f64 {
366 unsafe { intrinsics::expf64(self) }
369 /// Returns `2^(self)`.
377 /// let abs_difference = (f.exp2() - 4.0).abs();
379 /// assert!(abs_difference < 1e-10);
381 #[must_use = "method returns a new number and does not mutate the original value"]
382 #[stable(feature = "rust1", since = "1.0.0")]
384 pub fn exp2(self) -> f64 {
385 unsafe { intrinsics::exp2f64(self) }
388 /// Returns the natural logarithm of the number.
393 /// let one = 1.0_f64;
395 /// let e = one.exp();
397 /// // ln(e) - 1 == 0
398 /// let abs_difference = (e.ln() - 1.0).abs();
400 /// assert!(abs_difference < 1e-10);
402 #[must_use = "method returns a new number and does not mutate the original value"]
403 #[stable(feature = "rust1", since = "1.0.0")]
405 pub fn ln(self) -> f64 {
406 self.log_wrapper(|n
| unsafe { intrinsics::logf64(n) }
)
409 /// Returns the logarithm of the number with respect to an arbitrary base.
411 /// The result may not be correctly rounded owing to implementation details;
412 /// `self.log2()` can produce more accurate results for base 2, and
413 /// `self.log10()` can produce more accurate results for base 10.
418 /// let twenty_five = 25.0_f64;
420 /// // log5(25) - 2 == 0
421 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
423 /// assert!(abs_difference < 1e-10);
425 #[must_use = "method returns a new number and does not mutate the original value"]
426 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn log(self, base
: f64) -> f64 {
429 self.ln() / base
.ln()
432 /// Returns the base 2 logarithm of the number.
437 /// let four = 4.0_f64;
439 /// // log2(4) - 2 == 0
440 /// let abs_difference = (four.log2() - 2.0).abs();
442 /// assert!(abs_difference < 1e-10);
444 #[must_use = "method returns a new number and does not mutate the original value"]
445 #[stable(feature = "rust1", since = "1.0.0")]
447 pub fn log2(self) -> f64 {
448 self.log_wrapper(|n
| {
449 #[cfg(target_os = "android")]
450 return crate::sys
::android
::log2f64(n
);
451 #[cfg(not(target_os = "android"))]
452 return unsafe { intrinsics::log2f64(n) }
;
456 /// Returns the base 10 logarithm of the number.
461 /// let hundred = 100.0_f64;
463 /// // log10(100) - 2 == 0
464 /// let abs_difference = (hundred.log10() - 2.0).abs();
466 /// assert!(abs_difference < 1e-10);
468 #[must_use = "method returns a new number and does not mutate the original value"]
469 #[stable(feature = "rust1", since = "1.0.0")]
471 pub fn log10(self) -> f64 {
472 self.log_wrapper(|n
| unsafe { intrinsics::log10f64(n) }
)
475 /// The positive difference of two numbers.
477 /// * If `self <= other`: `0:0`
478 /// * Else: `self - other`
484 /// let y = -3.0_f64;
486 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
487 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
489 /// assert!(abs_difference_x < 1e-10);
490 /// assert!(abs_difference_y < 1e-10);
492 #[must_use = "method returns a new number and does not mutate the original value"]
493 #[stable(feature = "rust1", since = "1.0.0")]
497 reason
= "you probably meant `(self - other).abs()`: \
498 this operation is `(self - other).max(0.0)` \
499 except that `abs_sub` also propagates NaNs (also \
500 known as `fdim` in C). If you truly need the positive \
501 difference, consider using that expression or the C function \
502 `fdim`, depending on how you wish to handle NaN (please consider \
503 filing an issue describing your use-case too)."
505 pub fn abs_sub(self, other
: f64) -> f64 {
506 unsafe { cmath::fdim(self, other) }
509 /// Returns the cubic root of a number.
516 /// // x^(1/3) - 2 == 0
517 /// let abs_difference = (x.cbrt() - 2.0).abs();
519 /// assert!(abs_difference < 1e-10);
521 #[must_use = "method returns a new number and does not mutate the original value"]
522 #[stable(feature = "rust1", since = "1.0.0")]
524 pub fn cbrt(self) -> f64 {
525 unsafe { cmath::cbrt(self) }
528 /// Calculates the length of the hypotenuse of a right-angle triangle given
529 /// legs of length `x` and `y`.
537 /// // sqrt(x^2 + y^2)
538 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
540 /// assert!(abs_difference < 1e-10);
542 #[must_use = "method returns a new number and does not mutate the original value"]
543 #[stable(feature = "rust1", since = "1.0.0")]
545 pub fn hypot(self, other
: f64) -> f64 {
546 unsafe { cmath::hypot(self, other) }
549 /// Computes the sine of a number (in radians).
554 /// let x = std::f64::consts::FRAC_PI_2;
556 /// let abs_difference = (x.sin() - 1.0).abs();
558 /// assert!(abs_difference < 1e-10);
560 #[must_use = "method returns a new number and does not mutate the original value"]
561 #[stable(feature = "rust1", since = "1.0.0")]
563 pub fn sin(self) -> f64 {
564 unsafe { intrinsics::sinf64(self) }
567 /// Computes the cosine of a number (in radians).
572 /// let x = 2.0 * std::f64::consts::PI;
574 /// let abs_difference = (x.cos() - 1.0).abs();
576 /// assert!(abs_difference < 1e-10);
578 #[must_use = "method returns a new number and does not mutate the original value"]
579 #[stable(feature = "rust1", since = "1.0.0")]
581 pub fn cos(self) -> f64 {
582 unsafe { intrinsics::cosf64(self) }
585 /// Computes the tangent of a number (in radians).
590 /// let x = std::f64::consts::FRAC_PI_4;
591 /// let abs_difference = (x.tan() - 1.0).abs();
593 /// assert!(abs_difference < 1e-14);
595 #[must_use = "method returns a new number and does not mutate the original value"]
596 #[stable(feature = "rust1", since = "1.0.0")]
598 pub fn tan(self) -> f64 {
599 unsafe { cmath::tan(self) }
602 /// Computes the arcsine of a number. Return value is in radians in
603 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
609 /// let f = std::f64::consts::FRAC_PI_2;
611 /// // asin(sin(pi/2))
612 /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
614 /// assert!(abs_difference < 1e-10);
616 #[must_use = "method returns a new number and does not mutate the original value"]
617 #[stable(feature = "rust1", since = "1.0.0")]
619 pub fn asin(self) -> f64 {
620 unsafe { cmath::asin(self) }
623 /// Computes the arccosine of a number. Return value is in radians in
624 /// the range [0, pi] or NaN if the number is outside the range
630 /// let f = std::f64::consts::FRAC_PI_4;
632 /// // acos(cos(pi/4))
633 /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
635 /// assert!(abs_difference < 1e-10);
637 #[must_use = "method returns a new number and does not mutate the original value"]
638 #[stable(feature = "rust1", since = "1.0.0")]
640 pub fn acos(self) -> f64 {
641 unsafe { cmath::acos(self) }
644 /// Computes the arctangent of a number. Return value is in radians in the
645 /// range [-pi/2, pi/2];
653 /// let abs_difference = (f.tan().atan() - 1.0).abs();
655 /// assert!(abs_difference < 1e-10);
657 #[must_use = "method returns a new number and does not mutate the original value"]
658 #[stable(feature = "rust1", since = "1.0.0")]
660 pub fn atan(self) -> f64 {
661 unsafe { cmath::atan(self) }
664 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
666 /// * `x = 0`, `y = 0`: `0`
667 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
668 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
669 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
674 /// // Positive angles measured counter-clockwise
675 /// // from positive x axis
676 /// // -pi/4 radians (45 deg clockwise)
677 /// let x1 = 3.0_f64;
678 /// let y1 = -3.0_f64;
680 /// // 3pi/4 radians (135 deg counter-clockwise)
681 /// let x2 = -3.0_f64;
682 /// let y2 = 3.0_f64;
684 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
685 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
687 /// assert!(abs_difference_1 < 1e-10);
688 /// assert!(abs_difference_2 < 1e-10);
690 #[must_use = "method returns a new number and does not mutate the original value"]
691 #[stable(feature = "rust1", since = "1.0.0")]
693 pub fn atan2(self, other
: f64) -> f64 {
694 unsafe { cmath::atan2(self, other) }
697 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
698 /// `(sin(x), cos(x))`.
703 /// let x = std::f64::consts::FRAC_PI_4;
704 /// let f = x.sin_cos();
706 /// let abs_difference_0 = (f.0 - x.sin()).abs();
707 /// let abs_difference_1 = (f.1 - x.cos()).abs();
709 /// assert!(abs_difference_0 < 1e-10);
710 /// assert!(abs_difference_1 < 1e-10);
712 #[stable(feature = "rust1", since = "1.0.0")]
714 pub fn sin_cos(self) -> (f64, f64) {
715 (self.sin(), self.cos())
718 /// Returns `e^(self) - 1` in a way that is accurate even if the
719 /// number is close to zero.
727 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
729 /// assert!(abs_difference < 1e-10);
731 #[must_use = "method returns a new number and does not mutate the original value"]
732 #[stable(feature = "rust1", since = "1.0.0")]
734 pub fn exp_m1(self) -> f64 {
735 unsafe { cmath::expm1(self) }
738 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
739 /// the operations were performed separately.
744 /// let x = std::f64::consts::E - 1.0;
746 /// // ln(1 + (e - 1)) == ln(e) == 1
747 /// let abs_difference = (x.ln_1p() - 1.0).abs();
749 /// assert!(abs_difference < 1e-10);
751 #[must_use = "method returns a new number and does not mutate the original value"]
752 #[stable(feature = "rust1", since = "1.0.0")]
754 pub fn ln_1p(self) -> f64 {
755 unsafe { cmath::log1p(self) }
758 /// Hyperbolic sine function.
763 /// let e = std::f64::consts::E;
766 /// let f = x.sinh();
767 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
768 /// let g = ((e * e) - 1.0) / (2.0 * e);
769 /// let abs_difference = (f - g).abs();
771 /// assert!(abs_difference < 1e-10);
773 #[must_use = "method returns a new number and does not mutate the original value"]
774 #[stable(feature = "rust1", since = "1.0.0")]
776 pub fn sinh(self) -> f64 {
777 unsafe { cmath::sinh(self) }
780 /// Hyperbolic cosine function.
785 /// let e = std::f64::consts::E;
787 /// let f = x.cosh();
788 /// // Solving cosh() at 1 gives this result
789 /// let g = ((e * e) + 1.0) / (2.0 * e);
790 /// let abs_difference = (f - g).abs();
793 /// assert!(abs_difference < 1.0e-10);
795 #[must_use = "method returns a new number and does not mutate the original value"]
796 #[stable(feature = "rust1", since = "1.0.0")]
798 pub fn cosh(self) -> f64 {
799 unsafe { cmath::cosh(self) }
802 /// Hyperbolic tangent function.
807 /// let e = std::f64::consts::E;
810 /// let f = x.tanh();
811 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
812 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
813 /// let abs_difference = (f - g).abs();
815 /// assert!(abs_difference < 1.0e-10);
817 #[must_use = "method returns a new number and does not mutate the original value"]
818 #[stable(feature = "rust1", since = "1.0.0")]
820 pub fn tanh(self) -> f64 {
821 unsafe { cmath::tanh(self) }
824 /// Inverse hyperbolic sine function.
830 /// let f = x.sinh().asinh();
832 /// let abs_difference = (f - x).abs();
834 /// assert!(abs_difference < 1.0e-10);
836 #[must_use = "method returns a new number and does not mutate the original value"]
837 #[stable(feature = "rust1", since = "1.0.0")]
839 pub fn asinh(self) -> f64 {
840 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
843 /// Inverse hyperbolic cosine function.
849 /// let f = x.cosh().acosh();
851 /// let abs_difference = (f - x).abs();
853 /// assert!(abs_difference < 1.0e-10);
855 #[must_use = "method returns a new number and does not mutate the original value"]
856 #[stable(feature = "rust1", since = "1.0.0")]
858 pub fn acosh(self) -> f64 {
859 if self < 1.0 { Self::NAN }
else { (self + ((self * self) - 1.0).sqrt()).ln() }
862 /// Inverse hyperbolic tangent function.
867 /// let e = std::f64::consts::E;
868 /// let f = e.tanh().atanh();
870 /// let abs_difference = (f - e).abs();
872 /// assert!(abs_difference < 1.0e-10);
874 #[must_use = "method returns a new number and does not mutate the original value"]
875 #[stable(feature = "rust1", since = "1.0.0")]
877 pub fn atanh(self) -> f64 {
878 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
881 /// Restrict a value to a certain interval unless it is NaN.
883 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
884 /// less than `min`. Otherwise this returns `self`.
886 /// Note that this function returns NaN if the initial value was NaN as
891 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
896 /// #![feature(clamp)]
897 /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
898 /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
899 /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
900 /// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
902 #[must_use = "method returns a new number and does not mutate the original value"]
903 #[unstable(feature = "clamp", issue = "44095")]
905 pub fn clamp(self, min
: f64, max
: f64) -> f64 {
917 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
918 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
920 fn log_wrapper
<F
: Fn(f64) -> f64>(self, log_fn
: F
) -> f64 {
921 if !cfg
!(any(target_os
= "solaris", target_os
= "illumos")) {
924 if self.is_finite() {
927 } else if self == 0.0 {
928 Self::NEG_INFINITY
// log(0) = -Inf
930 Self::NAN
// log(-n) = NaN
932 } else if self.is_nan() {
933 self // log(NaN) = NaN
934 } else if self > 0.0 {
935 self // log(Inf) = Inf
937 Self::NAN
// log(-Inf) = NaN