1 //! Constants specific to the `f64` double-precision floating point type.
3 //! *[See also the `f64` primitive type](primitive@f64).*
5 //! Mathematically significant numbers are provided in the `consts` sub-module.
7 //! For the constants defined directly in this module
8 //! (as distinct from those defined in the `consts` sub-module),
9 //! new code should instead use the associated constants
10 //! defined directly on the `f64` type.
12 #![stable(feature = "rust1", since = "1.0.0")]
13 #![allow(missing_docs)]
19 use crate::intrinsics;
21 use crate::sys::cmath;
23 #[stable(feature = "rust1", since = "1.0.0")]
24 #[allow(deprecated, deprecated_in_future)]
26 consts, DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP,
27 MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX,
32 /// Returns the largest integer less than or equal to a number.
41 /// assert_eq!(f.floor(), 3.0);
42 /// assert_eq!(g.floor(), 3.0);
43 /// assert_eq!(h.floor(), -4.0);
45 #[rustc_allow_incoherent_impl]
46 #[must_use = "method returns a new number and does not mutate the original value"]
47 #[stable(feature = "rust1", since = "1.0.0")]
49 pub fn floor(self) -> f64 {
50 unsafe { intrinsics::floorf64(self) }
53 /// Returns the smallest integer greater than or equal to a number.
61 /// assert_eq!(f.ceil(), 4.0);
62 /// assert_eq!(g.ceil(), 4.0);
64 #[rustc_allow_incoherent_impl]
65 #[must_use = "method returns a new number and does not mutate the original value"]
66 #[stable(feature = "rust1", since = "1.0.0")]
68 pub fn ceil(self) -> f64 {
69 unsafe { intrinsics::ceilf64(self) }
72 /// Returns the nearest integer to a number. Round half-way cases away from
81 /// assert_eq!(f.round(), 3.0);
82 /// assert_eq!(g.round(), -3.0);
84 #[rustc_allow_incoherent_impl]
85 #[must_use = "method returns a new number and does not mutate the original value"]
86 #[stable(feature = "rust1", since = "1.0.0")]
88 pub fn round(self) -> f64 {
89 unsafe { intrinsics::roundf64(self) }
92 /// Returns the integer part of a number.
101 /// assert_eq!(f.trunc(), 3.0);
102 /// assert_eq!(g.trunc(), 3.0);
103 /// assert_eq!(h.trunc(), -3.0);
105 #[rustc_allow_incoherent_impl]
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 #[rustc_allow_incoherent_impl]
127 #[must_use = "method returns a new number and does not mutate the original value"]
128 #[stable(feature = "rust1", since = "1.0.0")]
130 pub fn fract(self) -> f64 {
134 /// Computes the absolute value of `self`. Returns `NAN` if the
141 /// let y = -3.5_f64;
143 /// let abs_difference_x = (x.abs() - x).abs();
144 /// let abs_difference_y = (y.abs() - (-y)).abs();
146 /// assert!(abs_difference_x < 1e-10);
147 /// assert!(abs_difference_y < 1e-10);
149 /// assert!(f64::NAN.abs().is_nan());
151 #[rustc_allow_incoherent_impl]
152 #[must_use = "method returns a new number and does not mutate the original value"]
153 #[stable(feature = "rust1", since = "1.0.0")]
155 pub fn abs(self) -> f64 {
156 unsafe { intrinsics::fabsf64(self) }
159 /// Returns a number that represents the sign of `self`.
161 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
162 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
163 /// - `NAN` if the number is `NAN`
170 /// assert_eq!(f.signum(), 1.0);
171 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
173 /// assert!(f64::NAN.signum().is_nan());
175 #[rustc_allow_incoherent_impl]
176 #[must_use = "method returns a new number and does not mutate the original value"]
177 #[stable(feature = "rust1", since = "1.0.0")]
179 pub fn signum(self) -> f64 {
180 if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
183 /// Returns a number composed of the magnitude of `self` and the sign of
186 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
187 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
188 /// `sign` is returned.
195 /// assert_eq!(f.copysign(0.42), 3.5_f64);
196 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
197 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
198 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
200 /// assert!(f64::NAN.copysign(1.0).is_nan());
202 #[rustc_allow_incoherent_impl]
203 #[must_use = "method returns a new number and does not mutate the original value"]
204 #[stable(feature = "copysign", since = "1.35.0")]
206 pub fn copysign(self, sign: f64) -> f64 {
207 unsafe { intrinsics::copysignf64(self, sign) }
210 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
211 /// error, yielding a more accurate result than an unfused multiply-add.
213 /// Using `mul_add` *may* be more performant than an unfused multiply-add if
214 /// the target architecture has a dedicated `fma` CPU instruction. However,
215 /// this is not always true, and will be heavily dependant on designing
216 /// algorithms with specific target hardware in mind.
221 /// let m = 10.0_f64;
223 /// let b = 60.0_f64;
226 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
228 /// assert!(abs_difference < 1e-10);
230 #[rustc_allow_incoherent_impl]
231 #[must_use = "method returns a new number and does not mutate the original value"]
232 #[stable(feature = "rust1", since = "1.0.0")]
234 pub fn mul_add(self, a: f64, b: f64) -> f64 {
235 unsafe { intrinsics::fmaf64(self, a, b) }
238 /// Calculates Euclidean division, the matching method for `rem_euclid`.
240 /// This computes the integer `n` such that
241 /// `self = n * rhs + self.rem_euclid(rhs)`.
242 /// In other words, the result is `self / rhs` rounded to the integer `n`
243 /// such that `self >= n * rhs`.
248 /// let a: f64 = 7.0;
250 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
251 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
252 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
253 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
255 #[rustc_allow_incoherent_impl]
256 #[must_use = "method returns a new number and does not mutate the original value"]
258 #[stable(feature = "euclidean_division", since = "1.38.0")]
259 pub fn div_euclid(self, rhs: f64) -> f64 {
260 let q = (self / rhs).trunc();
261 if self % rhs < 0.0 {
262 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
267 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
269 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
270 /// most cases. However, due to a floating point round-off error it can
271 /// result in `r == rhs.abs()`, violating the mathematical definition, if
272 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
273 /// This result is not an element of the function's codomain, but it is the
274 /// closest floating point number in the real numbers and thus fulfills the
275 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
281 /// let a: f64 = 7.0;
283 /// assert_eq!(a.rem_euclid(b), 3.0);
284 /// assert_eq!((-a).rem_euclid(b), 1.0);
285 /// assert_eq!(a.rem_euclid(-b), 3.0);
286 /// assert_eq!((-a).rem_euclid(-b), 1.0);
287 /// // limitation due to round-off error
288 /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
290 #[rustc_allow_incoherent_impl]
291 #[must_use = "method returns a new number and does not mutate the original value"]
293 #[stable(feature = "euclidean_division", since = "1.38.0")]
294 pub fn rem_euclid(self, rhs: f64) -> f64 {
296 if r < 0.0 { r + rhs.abs() } else { r }
299 /// Raises a number to an integer power.
301 /// Using this function is generally faster than using `powf`
307 /// let abs_difference = (x.powi(2) - (x * x)).abs();
309 /// assert!(abs_difference < 1e-10);
311 #[rustc_allow_incoherent_impl]
312 #[must_use = "method returns a new number and does not mutate the original value"]
313 #[stable(feature = "rust1", since = "1.0.0")]
315 pub fn powi(self, n: i32) -> f64 {
316 unsafe { intrinsics::powif64(self, n) }
319 /// Raises a number to a floating point power.
325 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
327 /// assert!(abs_difference < 1e-10);
329 #[rustc_allow_incoherent_impl]
330 #[must_use = "method returns a new number and does not mutate the original value"]
331 #[stable(feature = "rust1", since = "1.0.0")]
333 pub fn powf(self, n: f64) -> f64 {
334 unsafe { intrinsics::powf64(self, n) }
337 /// Returns the square root of a number.
339 /// Returns NaN if `self` is a negative number other than `-0.0`.
344 /// let positive = 4.0_f64;
345 /// let negative = -4.0_f64;
346 /// let negative_zero = -0.0_f64;
348 /// let abs_difference = (positive.sqrt() - 2.0).abs();
350 /// assert!(abs_difference < 1e-10);
351 /// assert!(negative.sqrt().is_nan());
352 /// assert!(negative_zero.sqrt() == negative_zero);
354 #[rustc_allow_incoherent_impl]
355 #[must_use = "method returns a new number and does not mutate the original value"]
356 #[stable(feature = "rust1", since = "1.0.0")]
358 pub fn sqrt(self) -> f64 {
359 unsafe { intrinsics::sqrtf64(self) }
362 /// Returns `e^(self)`, (the exponential function).
367 /// let one = 1.0_f64;
369 /// let e = one.exp();
371 /// // ln(e) - 1 == 0
372 /// let abs_difference = (e.ln() - 1.0).abs();
374 /// assert!(abs_difference < 1e-10);
376 #[rustc_allow_incoherent_impl]
377 #[must_use = "method returns a new number and does not mutate the original value"]
378 #[stable(feature = "rust1", since = "1.0.0")]
380 pub fn exp(self) -> f64 {
381 unsafe { intrinsics::expf64(self) }
384 /// Returns `2^(self)`.
392 /// let abs_difference = (f.exp2() - 4.0).abs();
394 /// assert!(abs_difference < 1e-10);
396 #[rustc_allow_incoherent_impl]
397 #[must_use = "method returns a new number and does not mutate the original value"]
398 #[stable(feature = "rust1", since = "1.0.0")]
400 pub fn exp2(self) -> f64 {
401 unsafe { intrinsics::exp2f64(self) }
404 /// Returns the natural logarithm of the number.
409 /// let one = 1.0_f64;
411 /// let e = one.exp();
413 /// // ln(e) - 1 == 0
414 /// let abs_difference = (e.ln() - 1.0).abs();
416 /// assert!(abs_difference < 1e-10);
418 #[rustc_allow_incoherent_impl]
419 #[must_use = "method returns a new number and does not mutate the original value"]
420 #[stable(feature = "rust1", since = "1.0.0")]
422 pub fn ln(self) -> f64 {
423 self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
426 /// Returns the logarithm of the number with respect to an arbitrary base.
428 /// The result might not be correctly rounded owing to implementation details;
429 /// `self.log2()` can produce more accurate results for base 2, and
430 /// `self.log10()` can produce more accurate results for base 10.
435 /// let twenty_five = 25.0_f64;
437 /// // log5(25) - 2 == 0
438 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
440 /// assert!(abs_difference < 1e-10);
442 #[rustc_allow_incoherent_impl]
443 #[must_use = "method returns a new number and does not mutate the original value"]
444 #[stable(feature = "rust1", since = "1.0.0")]
446 pub fn log(self, base: f64) -> f64 {
447 self.ln() / base.ln()
450 /// Returns the base 2 logarithm of the number.
455 /// let four = 4.0_f64;
457 /// // log2(4) - 2 == 0
458 /// let abs_difference = (four.log2() - 2.0).abs();
460 /// assert!(abs_difference < 1e-10);
462 #[rustc_allow_incoherent_impl]
463 #[must_use = "method returns a new number and does not mutate the original value"]
464 #[stable(feature = "rust1", since = "1.0.0")]
466 pub fn log2(self) -> f64 {
467 self.log_wrapper(|n| {
468 #[cfg(target_os = "android")]
469 return crate::sys::android::log2f64(n);
470 #[cfg(not(target_os = "android"))]
471 return unsafe { intrinsics::log2f64(n) };
475 /// Returns the base 10 logarithm of the number.
480 /// let hundred = 100.0_f64;
482 /// // log10(100) - 2 == 0
483 /// let abs_difference = (hundred.log10() - 2.0).abs();
485 /// assert!(abs_difference < 1e-10);
487 #[rustc_allow_incoherent_impl]
488 #[must_use = "method returns a new number and does not mutate the original value"]
489 #[stable(feature = "rust1", since = "1.0.0")]
491 pub fn log10(self) -> f64 {
492 self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
495 /// The positive difference of two numbers.
497 /// * If `self <= other`: `0:0`
498 /// * Else: `self - other`
504 /// let y = -3.0_f64;
506 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
507 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
509 /// assert!(abs_difference_x < 1e-10);
510 /// assert!(abs_difference_y < 1e-10);
512 #[rustc_allow_incoherent_impl]
513 #[must_use = "method returns a new number and does not mutate the original value"]
514 #[stable(feature = "rust1", since = "1.0.0")]
518 reason = "you probably meant `(self - other).abs()`: \
519 this operation is `(self - other).max(0.0)` \
520 except that `abs_sub` also propagates NaNs (also \
521 known as `fdim` in C). If you truly need the positive \
522 difference, consider using that expression or the C function \
523 `fdim`, depending on how you wish to handle NaN (please consider \
524 filing an issue describing your use-case too)."
526 pub fn abs_sub(self, other: f64) -> f64 {
527 unsafe { cmath::fdim(self, other) }
530 /// Returns the cube root of a number.
537 /// // x^(1/3) - 2 == 0
538 /// let abs_difference = (x.cbrt() - 2.0).abs();
540 /// assert!(abs_difference < 1e-10);
542 #[rustc_allow_incoherent_impl]
543 #[must_use = "method returns a new number and does not mutate the original value"]
544 #[stable(feature = "rust1", since = "1.0.0")]
546 pub fn cbrt(self) -> f64 {
547 unsafe { cmath::cbrt(self) }
550 /// Calculates the length of the hypotenuse of a right-angle triangle given
551 /// legs of length `x` and `y`.
559 /// // sqrt(x^2 + y^2)
560 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
562 /// assert!(abs_difference < 1e-10);
564 #[rustc_allow_incoherent_impl]
565 #[must_use = "method returns a new number and does not mutate the original value"]
566 #[stable(feature = "rust1", since = "1.0.0")]
568 pub fn hypot(self, other: f64) -> f64 {
569 unsafe { cmath::hypot(self, other) }
572 /// Computes the sine of a number (in radians).
577 /// let x = std::f64::consts::FRAC_PI_2;
579 /// let abs_difference = (x.sin() - 1.0).abs();
581 /// assert!(abs_difference < 1e-10);
583 #[rustc_allow_incoherent_impl]
584 #[must_use = "method returns a new number and does not mutate the original value"]
585 #[stable(feature = "rust1", since = "1.0.0")]
587 pub fn sin(self) -> f64 {
588 unsafe { intrinsics::sinf64(self) }
591 /// Computes the cosine of a number (in radians).
596 /// let x = 2.0 * std::f64::consts::PI;
598 /// let abs_difference = (x.cos() - 1.0).abs();
600 /// assert!(abs_difference < 1e-10);
602 #[rustc_allow_incoherent_impl]
603 #[must_use = "method returns a new number and does not mutate the original value"]
604 #[stable(feature = "rust1", since = "1.0.0")]
606 pub fn cos(self) -> f64 {
607 unsafe { intrinsics::cosf64(self) }
610 /// Computes the tangent of a number (in radians).
615 /// let x = std::f64::consts::FRAC_PI_4;
616 /// let abs_difference = (x.tan() - 1.0).abs();
618 /// assert!(abs_difference < 1e-14);
620 #[rustc_allow_incoherent_impl]
621 #[must_use = "method returns a new number and does not mutate the original value"]
622 #[stable(feature = "rust1", since = "1.0.0")]
624 pub fn tan(self) -> f64 {
625 unsafe { cmath::tan(self) }
628 /// Computes the arcsine of a number. Return value is in radians in
629 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
635 /// let f = std::f64::consts::FRAC_PI_2;
637 /// // asin(sin(pi/2))
638 /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
640 /// assert!(abs_difference < 1e-10);
642 #[rustc_allow_incoherent_impl]
643 #[must_use = "method returns a new number and does not mutate the original value"]
644 #[stable(feature = "rust1", since = "1.0.0")]
646 pub fn asin(self) -> f64 {
647 unsafe { cmath::asin(self) }
650 /// Computes the arccosine of a number. Return value is in radians in
651 /// the range [0, pi] or NaN if the number is outside the range
657 /// let f = std::f64::consts::FRAC_PI_4;
659 /// // acos(cos(pi/4))
660 /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
662 /// assert!(abs_difference < 1e-10);
664 #[rustc_allow_incoherent_impl]
665 #[must_use = "method returns a new number and does not mutate the original value"]
666 #[stable(feature = "rust1", since = "1.0.0")]
668 pub fn acos(self) -> f64 {
669 unsafe { cmath::acos(self) }
672 /// Computes the arctangent of a number. Return value is in radians in the
673 /// range [-pi/2, pi/2];
681 /// let abs_difference = (f.tan().atan() - 1.0).abs();
683 /// assert!(abs_difference < 1e-10);
685 #[rustc_allow_incoherent_impl]
686 #[must_use = "method returns a new number and does not mutate the original value"]
687 #[stable(feature = "rust1", since = "1.0.0")]
689 pub fn atan(self) -> f64 {
690 unsafe { cmath::atan(self) }
693 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
695 /// * `x = 0`, `y = 0`: `0`
696 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
697 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
698 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
703 /// // Positive angles measured counter-clockwise
704 /// // from positive x axis
705 /// // -pi/4 radians (45 deg clockwise)
706 /// let x1 = 3.0_f64;
707 /// let y1 = -3.0_f64;
709 /// // 3pi/4 radians (135 deg counter-clockwise)
710 /// let x2 = -3.0_f64;
711 /// let y2 = 3.0_f64;
713 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
714 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
716 /// assert!(abs_difference_1 < 1e-10);
717 /// assert!(abs_difference_2 < 1e-10);
719 #[rustc_allow_incoherent_impl]
720 #[must_use = "method returns a new number and does not mutate the original value"]
721 #[stable(feature = "rust1", since = "1.0.0")]
723 pub fn atan2(self, other: f64) -> f64 {
724 unsafe { cmath::atan2(self, other) }
727 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
728 /// `(sin(x), cos(x))`.
733 /// let x = std::f64::consts::FRAC_PI_4;
734 /// let f = x.sin_cos();
736 /// let abs_difference_0 = (f.0 - x.sin()).abs();
737 /// let abs_difference_1 = (f.1 - x.cos()).abs();
739 /// assert!(abs_difference_0 < 1e-10);
740 /// assert!(abs_difference_1 < 1e-10);
742 #[rustc_allow_incoherent_impl]
743 #[stable(feature = "rust1", since = "1.0.0")]
745 pub fn sin_cos(self) -> (f64, f64) {
746 (self.sin(), self.cos())
749 /// Returns `e^(self) - 1` in a way that is accurate even if the
750 /// number is close to zero.
755 /// let x = 1e-16_f64;
757 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
758 /// let approx = x + x * x / 2.0;
759 /// let abs_difference = (x.exp_m1() - approx).abs();
761 /// assert!(abs_difference < 1e-20);
763 #[rustc_allow_incoherent_impl]
764 #[must_use = "method returns a new number and does not mutate the original value"]
765 #[stable(feature = "rust1", since = "1.0.0")]
767 pub fn exp_m1(self) -> f64 {
768 unsafe { cmath::expm1(self) }
771 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
772 /// the operations were performed separately.
777 /// let x = 1e-16_f64;
779 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
780 /// let approx = x - x * x / 2.0;
781 /// let abs_difference = (x.ln_1p() - approx).abs();
783 /// assert!(abs_difference < 1e-20);
785 #[rustc_allow_incoherent_impl]
786 #[must_use = "method returns a new number and does not mutate the original value"]
787 #[stable(feature = "rust1", since = "1.0.0")]
789 pub fn ln_1p(self) -> f64 {
790 unsafe { cmath::log1p(self) }
793 /// Hyperbolic sine function.
798 /// let e = std::f64::consts::E;
801 /// let f = x.sinh();
802 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
803 /// let g = ((e * e) - 1.0) / (2.0 * e);
804 /// let abs_difference = (f - g).abs();
806 /// assert!(abs_difference < 1e-10);
808 #[rustc_allow_incoherent_impl]
809 #[must_use = "method returns a new number and does not mutate the original value"]
810 #[stable(feature = "rust1", since = "1.0.0")]
812 pub fn sinh(self) -> f64 {
813 unsafe { cmath::sinh(self) }
816 /// Hyperbolic cosine function.
821 /// let e = std::f64::consts::E;
823 /// let f = x.cosh();
824 /// // Solving cosh() at 1 gives this result
825 /// let g = ((e * e) + 1.0) / (2.0 * e);
826 /// let abs_difference = (f - g).abs();
829 /// assert!(abs_difference < 1.0e-10);
831 #[rustc_allow_incoherent_impl]
832 #[must_use = "method returns a new number and does not mutate the original value"]
833 #[stable(feature = "rust1", since = "1.0.0")]
835 pub fn cosh(self) -> f64 {
836 unsafe { cmath::cosh(self) }
839 /// Hyperbolic tangent function.
844 /// let e = std::f64::consts::E;
847 /// let f = x.tanh();
848 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
849 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
850 /// let abs_difference = (f - g).abs();
852 /// assert!(abs_difference < 1.0e-10);
854 #[rustc_allow_incoherent_impl]
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 tanh(self) -> f64 {
859 unsafe { cmath::tanh(self) }
862 /// Inverse hyperbolic sine function.
868 /// let f = x.sinh().asinh();
870 /// let abs_difference = (f - x).abs();
872 /// assert!(abs_difference < 1.0e-10);
874 #[rustc_allow_incoherent_impl]
875 #[must_use = "method returns a new number and does not mutate the original value"]
876 #[stable(feature = "rust1", since = "1.0.0")]
878 pub fn asinh(self) -> f64 {
879 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
882 /// Inverse hyperbolic cosine function.
888 /// let f = x.cosh().acosh();
890 /// let abs_difference = (f - x).abs();
892 /// assert!(abs_difference < 1.0e-10);
894 #[rustc_allow_incoherent_impl]
895 #[must_use = "method returns a new number and does not mutate the original value"]
896 #[stable(feature = "rust1", since = "1.0.0")]
898 pub fn acosh(self) -> f64 {
899 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
902 /// Inverse hyperbolic tangent function.
907 /// let e = std::f64::consts::E;
908 /// let f = e.tanh().atanh();
910 /// let abs_difference = (f - e).abs();
912 /// assert!(abs_difference < 1.0e-10);
914 #[rustc_allow_incoherent_impl]
915 #[must_use = "method returns a new number and does not mutate the original value"]
916 #[stable(feature = "rust1", since = "1.0.0")]
918 pub fn atanh(self) -> f64 {
919 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
922 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
923 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
925 #[rustc_allow_incoherent_impl]
926 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
927 if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
929 } else if self.is_finite() {
932 } else if self == 0.0 {
933 Self::NEG_INFINITY // log(0) = -Inf
935 Self::NAN // log(-n) = NaN
937 } else if self.is_nan() {
938 self // log(NaN) = NaN
939 } else if self > 0.0 {
940 self // log(Inf) = Inf
942 Self::NAN // log(-Inf) = NaN