1 //! Constants for 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 `self`.
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 `self`.
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 `self`. Round half-way cases away from
82 /// assert_eq!(f.round(), 3.0);
83 /// assert_eq!(g.round(), -3.0);
84 /// assert_eq!(h.round(), -4.0);
86 #[rustc_allow_incoherent_impl]
87 #[must_use = "method returns a new number and does not mutate the original value"]
88 #[stable(feature = "rust1", since = "1.0.0")]
90 pub fn round(self) -> f64 {
91 unsafe { intrinsics::roundf64(self) }
94 /// Returns the integer part of `self`.
95 /// This means that non-integer numbers are always truncated towards zero.
102 /// let h = -3.7_f64;
104 /// assert_eq!(f.trunc(), 3.0);
105 /// assert_eq!(g.trunc(), 3.0);
106 /// assert_eq!(h.trunc(), -3.0);
108 #[rustc_allow_incoherent_impl]
109 #[must_use = "method returns a new number and does not mutate the original value"]
110 #[stable(feature = "rust1", since = "1.0.0")]
112 pub fn trunc(self) -> f64 {
113 unsafe { intrinsics::truncf64(self) }
116 /// Returns the fractional part of `self`.
122 /// let y = -3.6_f64;
123 /// let abs_difference_x = (x.fract() - 0.6).abs();
124 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
126 /// assert!(abs_difference_x < 1e-10);
127 /// assert!(abs_difference_y < 1e-10);
129 #[rustc_allow_incoherent_impl]
130 #[must_use = "method returns a new number and does not mutate the original value"]
131 #[stable(feature = "rust1", since = "1.0.0")]
133 pub fn fract(self) -> f64 {
137 /// Computes the absolute value of `self`.
143 /// let y = -3.5_f64;
145 /// let abs_difference_x = (x.abs() - x).abs();
146 /// let abs_difference_y = (y.abs() - (-y)).abs();
148 /// assert!(abs_difference_x < 1e-10);
149 /// assert!(abs_difference_y < 1e-10);
151 /// assert!(f64::NAN.abs().is_nan());
153 #[rustc_allow_incoherent_impl]
154 #[must_use = "method returns a new number and does not mutate the original value"]
155 #[stable(feature = "rust1", since = "1.0.0")]
157 pub fn abs(self) -> f64 {
158 unsafe { intrinsics::fabsf64(self) }
161 /// Returns a number that represents the sign of `self`.
163 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
164 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
165 /// - NaN if the number is NaN
172 /// assert_eq!(f.signum(), 1.0);
173 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
175 /// assert!(f64::NAN.signum().is_nan());
177 #[rustc_allow_incoherent_impl]
178 #[must_use = "method returns a new number and does not mutate the original value"]
179 #[stable(feature = "rust1", since = "1.0.0")]
181 pub fn signum(self) -> f64 {
182 if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
185 /// Returns a number composed of the magnitude of `self` and the sign of
188 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
189 /// equal to `-self`. If `self` is a NaN, then a NaN with the sign bit of
190 /// `sign` is returned. Note, however, that conserving the sign bit on NaN
191 /// across arithmetical operations is not generally guaranteed.
192 /// See [explanation of NaN as a special value](primitive@f32) for more info.
199 /// assert_eq!(f.copysign(0.42), 3.5_f64);
200 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
201 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
202 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
204 /// assert!(f64::NAN.copysign(1.0).is_nan());
206 #[rustc_allow_incoherent_impl]
207 #[must_use = "method returns a new number and does not mutate the original value"]
208 #[stable(feature = "copysign", since = "1.35.0")]
210 pub fn copysign(self, sign: f64) -> f64 {
211 unsafe { intrinsics::copysignf64(self, sign) }
214 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
215 /// error, yielding a more accurate result than an unfused multiply-add.
217 /// Using `mul_add` *may* be more performant than an unfused multiply-add if
218 /// the target architecture has a dedicated `fma` CPU instruction. However,
219 /// this is not always true, and will be heavily dependant on designing
220 /// algorithms with specific target hardware in mind.
225 /// let m = 10.0_f64;
227 /// let b = 60.0_f64;
230 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
232 /// assert!(abs_difference < 1e-10);
234 #[rustc_allow_incoherent_impl]
235 #[must_use = "method returns a new number and does not mutate the original value"]
236 #[stable(feature = "rust1", since = "1.0.0")]
238 pub fn mul_add(self, a: f64, b: f64) -> f64 {
239 unsafe { intrinsics::fmaf64(self, a, b) }
242 /// Calculates Euclidean division, the matching method for `rem_euclid`.
244 /// This computes the integer `n` such that
245 /// `self = n * rhs + self.rem_euclid(rhs)`.
246 /// In other words, the result is `self / rhs` rounded to the integer `n`
247 /// such that `self >= n * rhs`.
252 /// let a: f64 = 7.0;
254 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
255 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
256 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
257 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
259 #[rustc_allow_incoherent_impl]
260 #[must_use = "method returns a new number and does not mutate the original value"]
262 #[stable(feature = "euclidean_division", since = "1.38.0")]
263 pub fn div_euclid(self, rhs: f64) -> f64 {
264 let q = (self / rhs).trunc();
265 if self % rhs < 0.0 {
266 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
271 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
273 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
274 /// most cases. However, due to a floating point round-off error it can
275 /// result in `r == rhs.abs()`, violating the mathematical definition, if
276 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
277 /// This result is not an element of the function's codomain, but it is the
278 /// closest floating point number in the real numbers and thus fulfills the
279 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
285 /// let a: f64 = 7.0;
287 /// assert_eq!(a.rem_euclid(b), 3.0);
288 /// assert_eq!((-a).rem_euclid(b), 1.0);
289 /// assert_eq!(a.rem_euclid(-b), 3.0);
290 /// assert_eq!((-a).rem_euclid(-b), 1.0);
291 /// // limitation due to round-off error
292 /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
294 #[rustc_allow_incoherent_impl]
295 #[must_use = "method returns a new number and does not mutate the original value"]
297 #[stable(feature = "euclidean_division", since = "1.38.0")]
298 pub fn rem_euclid(self, rhs: f64) -> f64 {
300 if r < 0.0 { r + rhs.abs() } else { r }
303 /// Raises a number to an integer power.
305 /// Using this function is generally faster than using `powf`.
306 /// It might have a different sequence of rounding operations than `powf`,
307 /// so the results are not guaranteed to agree.
313 /// let abs_difference = (x.powi(2) - (x * x)).abs();
315 /// assert!(abs_difference < 1e-10);
317 #[rustc_allow_incoherent_impl]
318 #[must_use = "method returns a new number and does not mutate the original value"]
319 #[stable(feature = "rust1", since = "1.0.0")]
321 pub fn powi(self, n: i32) -> f64 {
322 unsafe { intrinsics::powif64(self, n) }
325 /// Raises a number to a floating point power.
331 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
333 /// assert!(abs_difference < 1e-10);
335 #[rustc_allow_incoherent_impl]
336 #[must_use = "method returns a new number and does not mutate the original value"]
337 #[stable(feature = "rust1", since = "1.0.0")]
339 pub fn powf(self, n: f64) -> f64 {
340 unsafe { intrinsics::powf64(self, n) }
343 /// Returns the square root of a number.
345 /// Returns NaN if `self` is a negative number other than `-0.0`.
350 /// let positive = 4.0_f64;
351 /// let negative = -4.0_f64;
352 /// let negative_zero = -0.0_f64;
354 /// let abs_difference = (positive.sqrt() - 2.0).abs();
356 /// assert!(abs_difference < 1e-10);
357 /// assert!(negative.sqrt().is_nan());
358 /// assert!(negative_zero.sqrt() == negative_zero);
360 #[rustc_allow_incoherent_impl]
361 #[must_use = "method returns a new number and does not mutate the original value"]
362 #[stable(feature = "rust1", since = "1.0.0")]
364 pub fn sqrt(self) -> f64 {
365 unsafe { intrinsics::sqrtf64(self) }
368 /// Returns `e^(self)`, (the exponential function).
373 /// let one = 1.0_f64;
375 /// let e = one.exp();
377 /// // ln(e) - 1 == 0
378 /// let abs_difference = (e.ln() - 1.0).abs();
380 /// assert!(abs_difference < 1e-10);
382 #[rustc_allow_incoherent_impl]
383 #[must_use = "method returns a new number and does not mutate the original value"]
384 #[stable(feature = "rust1", since = "1.0.0")]
386 pub fn exp(self) -> f64 {
387 unsafe { intrinsics::expf64(self) }
390 /// Returns `2^(self)`.
398 /// let abs_difference = (f.exp2() - 4.0).abs();
400 /// assert!(abs_difference < 1e-10);
402 #[rustc_allow_incoherent_impl]
403 #[must_use = "method returns a new number and does not mutate the original value"]
404 #[stable(feature = "rust1", since = "1.0.0")]
406 pub fn exp2(self) -> f64 {
407 unsafe { intrinsics::exp2f64(self) }
410 /// Returns the natural logarithm of the number.
415 /// let one = 1.0_f64;
417 /// let e = one.exp();
419 /// // ln(e) - 1 == 0
420 /// let abs_difference = (e.ln() - 1.0).abs();
422 /// assert!(abs_difference < 1e-10);
424 #[rustc_allow_incoherent_impl]
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 ln(self) -> f64 {
429 self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
432 /// Returns the logarithm of the number with respect to an arbitrary base.
434 /// The result might not be correctly rounded owing to implementation details;
435 /// `self.log2()` can produce more accurate results for base 2, and
436 /// `self.log10()` can produce more accurate results for base 10.
441 /// let twenty_five = 25.0_f64;
443 /// // log5(25) - 2 == 0
444 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
446 /// assert!(abs_difference < 1e-10);
448 #[rustc_allow_incoherent_impl]
449 #[must_use = "method returns a new number and does not mutate the original value"]
450 #[stable(feature = "rust1", since = "1.0.0")]
452 pub fn log(self, base: f64) -> f64 {
453 self.ln() / base.ln()
456 /// Returns the base 2 logarithm of the number.
461 /// let four = 4.0_f64;
463 /// // log2(4) - 2 == 0
464 /// let abs_difference = (four.log2() - 2.0).abs();
466 /// assert!(abs_difference < 1e-10);
468 #[rustc_allow_incoherent_impl]
469 #[must_use = "method returns a new number and does not mutate the original value"]
470 #[stable(feature = "rust1", since = "1.0.0")]
472 pub fn log2(self) -> f64 {
473 self.log_wrapper(|n| {
474 #[cfg(target_os = "android")]
475 return crate::sys::android::log2f64(n);
476 #[cfg(not(target_os = "android"))]
477 return unsafe { intrinsics::log2f64(n) };
481 /// Returns the base 10 logarithm of the number.
486 /// let hundred = 100.0_f64;
488 /// // log10(100) - 2 == 0
489 /// let abs_difference = (hundred.log10() - 2.0).abs();
491 /// assert!(abs_difference < 1e-10);
493 #[rustc_allow_incoherent_impl]
494 #[must_use = "method returns a new number and does not mutate the original value"]
495 #[stable(feature = "rust1", since = "1.0.0")]
497 pub fn log10(self) -> f64 {
498 self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
501 /// The positive difference of two numbers.
503 /// * If `self <= other`: `0:0`
504 /// * Else: `self - other`
510 /// let y = -3.0_f64;
512 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
513 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
515 /// assert!(abs_difference_x < 1e-10);
516 /// assert!(abs_difference_y < 1e-10);
518 #[rustc_allow_incoherent_impl]
519 #[must_use = "method returns a new number and does not mutate the original value"]
520 #[stable(feature = "rust1", since = "1.0.0")]
524 note = "you probably meant `(self - other).abs()`: \
525 this operation is `(self - other).max(0.0)` \
526 except that `abs_sub` also propagates NaNs (also \
527 known as `fdim` in C). If you truly need the positive \
528 difference, consider using that expression or the C function \
529 `fdim`, depending on how you wish to handle NaN (please consider \
530 filing an issue describing your use-case too)."
532 pub fn abs_sub(self, other: f64) -> f64 {
533 unsafe { cmath::fdim(self, other) }
536 /// Returns the cube root of a number.
543 /// // x^(1/3) - 2 == 0
544 /// let abs_difference = (x.cbrt() - 2.0).abs();
546 /// assert!(abs_difference < 1e-10);
548 #[rustc_allow_incoherent_impl]
549 #[must_use = "method returns a new number and does not mutate the original value"]
550 #[stable(feature = "rust1", since = "1.0.0")]
552 pub fn cbrt(self) -> f64 {
553 unsafe { cmath::cbrt(self) }
556 /// Calculates the length of the hypotenuse of a right-angle triangle given
557 /// legs of length `x` and `y`.
565 /// // sqrt(x^2 + y^2)
566 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
568 /// assert!(abs_difference < 1e-10);
570 #[rustc_allow_incoherent_impl]
571 #[must_use = "method returns a new number and does not mutate the original value"]
572 #[stable(feature = "rust1", since = "1.0.0")]
574 pub fn hypot(self, other: f64) -> f64 {
575 unsafe { cmath::hypot(self, other) }
578 /// Computes the sine of a number (in radians).
583 /// let x = std::f64::consts::FRAC_PI_2;
585 /// let abs_difference = (x.sin() - 1.0).abs();
587 /// assert!(abs_difference < 1e-10);
589 #[rustc_allow_incoherent_impl]
590 #[must_use = "method returns a new number and does not mutate the original value"]
591 #[stable(feature = "rust1", since = "1.0.0")]
593 pub fn sin(self) -> f64 {
594 unsafe { intrinsics::sinf64(self) }
597 /// Computes the cosine of a number (in radians).
602 /// let x = 2.0 * std::f64::consts::PI;
604 /// let abs_difference = (x.cos() - 1.0).abs();
606 /// assert!(abs_difference < 1e-10);
608 #[rustc_allow_incoherent_impl]
609 #[must_use = "method returns a new number and does not mutate the original value"]
610 #[stable(feature = "rust1", since = "1.0.0")]
612 pub fn cos(self) -> f64 {
613 unsafe { intrinsics::cosf64(self) }
616 /// Computes the tangent of a number (in radians).
621 /// let x = std::f64::consts::FRAC_PI_4;
622 /// let abs_difference = (x.tan() - 1.0).abs();
624 /// assert!(abs_difference < 1e-14);
626 #[rustc_allow_incoherent_impl]
627 #[must_use = "method returns a new number and does not mutate the original value"]
628 #[stable(feature = "rust1", since = "1.0.0")]
630 pub fn tan(self) -> f64 {
631 unsafe { cmath::tan(self) }
634 /// Computes the arcsine of a number. Return value is in radians in
635 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
641 /// let f = std::f64::consts::FRAC_PI_2;
643 /// // asin(sin(pi/2))
644 /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
646 /// assert!(abs_difference < 1e-10);
648 #[rustc_allow_incoherent_impl]
649 #[must_use = "method returns a new number and does not mutate the original value"]
650 #[stable(feature = "rust1", since = "1.0.0")]
652 pub fn asin(self) -> f64 {
653 unsafe { cmath::asin(self) }
656 /// Computes the arccosine of a number. Return value is in radians in
657 /// the range [0, pi] or NaN if the number is outside the range
663 /// let f = std::f64::consts::FRAC_PI_4;
665 /// // acos(cos(pi/4))
666 /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
668 /// assert!(abs_difference < 1e-10);
670 #[rustc_allow_incoherent_impl]
671 #[must_use = "method returns a new number and does not mutate the original value"]
672 #[stable(feature = "rust1", since = "1.0.0")]
674 pub fn acos(self) -> f64 {
675 unsafe { cmath::acos(self) }
678 /// Computes the arctangent of a number. Return value is in radians in the
679 /// range [-pi/2, pi/2];
687 /// let abs_difference = (f.tan().atan() - 1.0).abs();
689 /// assert!(abs_difference < 1e-10);
691 #[rustc_allow_incoherent_impl]
692 #[must_use = "method returns a new number and does not mutate the original value"]
693 #[stable(feature = "rust1", since = "1.0.0")]
695 pub fn atan(self) -> f64 {
696 unsafe { cmath::atan(self) }
699 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
701 /// * `x = 0`, `y = 0`: `0`
702 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
703 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
704 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
709 /// // Positive angles measured counter-clockwise
710 /// // from positive x axis
711 /// // -pi/4 radians (45 deg clockwise)
712 /// let x1 = 3.0_f64;
713 /// let y1 = -3.0_f64;
715 /// // 3pi/4 radians (135 deg counter-clockwise)
716 /// let x2 = -3.0_f64;
717 /// let y2 = 3.0_f64;
719 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
720 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
722 /// assert!(abs_difference_1 < 1e-10);
723 /// assert!(abs_difference_2 < 1e-10);
725 #[rustc_allow_incoherent_impl]
726 #[must_use = "method returns a new number and does not mutate the original value"]
727 #[stable(feature = "rust1", since = "1.0.0")]
729 pub fn atan2(self, other: f64) -> f64 {
730 unsafe { cmath::atan2(self, other) }
733 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
734 /// `(sin(x), cos(x))`.
739 /// let x = std::f64::consts::FRAC_PI_4;
740 /// let f = x.sin_cos();
742 /// let abs_difference_0 = (f.0 - x.sin()).abs();
743 /// let abs_difference_1 = (f.1 - x.cos()).abs();
745 /// assert!(abs_difference_0 < 1e-10);
746 /// assert!(abs_difference_1 < 1e-10);
748 #[rustc_allow_incoherent_impl]
749 #[stable(feature = "rust1", since = "1.0.0")]
751 pub fn sin_cos(self) -> (f64, f64) {
752 (self.sin(), self.cos())
755 /// Returns `e^(self) - 1` in a way that is accurate even if the
756 /// number is close to zero.
761 /// let x = 1e-16_f64;
763 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
764 /// let approx = x + x * x / 2.0;
765 /// let abs_difference = (x.exp_m1() - approx).abs();
767 /// assert!(abs_difference < 1e-20);
769 #[rustc_allow_incoherent_impl]
770 #[must_use = "method returns a new number and does not mutate the original value"]
771 #[stable(feature = "rust1", since = "1.0.0")]
773 pub fn exp_m1(self) -> f64 {
774 unsafe { cmath::expm1(self) }
777 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
778 /// the operations were performed separately.
783 /// let x = 1e-16_f64;
785 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
786 /// let approx = x - x * x / 2.0;
787 /// let abs_difference = (x.ln_1p() - approx).abs();
789 /// assert!(abs_difference < 1e-20);
791 #[rustc_allow_incoherent_impl]
792 #[must_use = "method returns a new number and does not mutate the original value"]
793 #[stable(feature = "rust1", since = "1.0.0")]
795 pub fn ln_1p(self) -> f64 {
796 unsafe { cmath::log1p(self) }
799 /// Hyperbolic sine function.
804 /// let e = std::f64::consts::E;
807 /// let f = x.sinh();
808 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
809 /// let g = ((e * e) - 1.0) / (2.0 * e);
810 /// let abs_difference = (f - g).abs();
812 /// assert!(abs_difference < 1e-10);
814 #[rustc_allow_incoherent_impl]
815 #[must_use = "method returns a new number and does not mutate the original value"]
816 #[stable(feature = "rust1", since = "1.0.0")]
818 pub fn sinh(self) -> f64 {
819 unsafe { cmath::sinh(self) }
822 /// Hyperbolic cosine function.
827 /// let e = std::f64::consts::E;
829 /// let f = x.cosh();
830 /// // Solving cosh() at 1 gives this result
831 /// let g = ((e * e) + 1.0) / (2.0 * e);
832 /// let abs_difference = (f - g).abs();
835 /// assert!(abs_difference < 1.0e-10);
837 #[rustc_allow_incoherent_impl]
838 #[must_use = "method returns a new number and does not mutate the original value"]
839 #[stable(feature = "rust1", since = "1.0.0")]
841 pub fn cosh(self) -> f64 {
842 unsafe { cmath::cosh(self) }
845 /// Hyperbolic tangent function.
850 /// let e = std::f64::consts::E;
853 /// let f = x.tanh();
854 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
855 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
856 /// let abs_difference = (f - g).abs();
858 /// assert!(abs_difference < 1.0e-10);
860 #[rustc_allow_incoherent_impl]
861 #[must_use = "method returns a new number and does not mutate the original value"]
862 #[stable(feature = "rust1", since = "1.0.0")]
864 pub fn tanh(self) -> f64 {
865 unsafe { cmath::tanh(self) }
868 /// Inverse hyperbolic sine function.
874 /// let f = x.sinh().asinh();
876 /// let abs_difference = (f - x).abs();
878 /// assert!(abs_difference < 1.0e-10);
880 #[rustc_allow_incoherent_impl]
881 #[must_use = "method returns a new number and does not mutate the original value"]
882 #[stable(feature = "rust1", since = "1.0.0")]
884 pub fn asinh(self) -> f64 {
887 (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
890 /// Inverse hyperbolic cosine function.
896 /// let f = x.cosh().acosh();
898 /// let abs_difference = (f - x).abs();
900 /// assert!(abs_difference < 1.0e-10);
902 #[rustc_allow_incoherent_impl]
903 #[must_use = "method returns a new number and does not mutate the original value"]
904 #[stable(feature = "rust1", since = "1.0.0")]
906 pub fn acosh(self) -> f64 {
910 (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
914 /// Inverse hyperbolic tangent function.
919 /// let e = std::f64::consts::E;
920 /// let f = e.tanh().atanh();
922 /// let abs_difference = (f - e).abs();
924 /// assert!(abs_difference < 1.0e-10);
926 #[rustc_allow_incoherent_impl]
927 #[must_use = "method returns a new number and does not mutate the original value"]
928 #[stable(feature = "rust1", since = "1.0.0")]
930 pub fn atanh(self) -> f64 {
931 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
934 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
935 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
937 #[rustc_allow_incoherent_impl]
938 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
939 if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
941 } else if self.is_finite() {
944 } else if self == 0.0 {
945 Self::NEG_INFINITY // log(0) = -Inf
947 Self::NAN // log(-n) = NaN
949 } else if self.is_nan() {
950 self // log(NaN) = NaN
951 } else if self > 0.0 {
952 self // log(Inf) = Inf
954 Self::NAN // log(-Inf) = NaN