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,
31 #[cfg_attr(bootstrap, lang = "f64_runtime")]
33 /// Returns the largest integer less than or equal to `self`.
42 /// assert_eq!(f.floor(), 3.0);
43 /// assert_eq!(g.floor(), 3.0);
44 /// assert_eq!(h.floor(), -4.0);
46 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
47 #[must_use = "method returns a new number and does not mutate the original value"]
48 #[stable(feature = "rust1", since = "1.0.0")]
50 pub fn floor(self) -> f64 {
51 unsafe { intrinsics::floorf64(self) }
54 /// Returns the smallest integer greater than or equal to `self`.
62 /// assert_eq!(f.ceil(), 4.0);
63 /// assert_eq!(g.ceil(), 4.0);
65 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
66 #[must_use = "method returns a new number and does not mutate the original value"]
67 #[stable(feature = "rust1", since = "1.0.0")]
69 pub fn ceil(self) -> f64 {
70 unsafe { intrinsics::ceilf64(self) }
73 /// 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);
85 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
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 `self`.
94 /// This means that non-integer numbers are always truncated towards zero.
101 /// let h = -3.7_f64;
103 /// assert_eq!(f.trunc(), 3.0);
104 /// assert_eq!(g.trunc(), 3.0);
105 /// assert_eq!(h.trunc(), -3.0);
107 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
108 #[must_use = "method returns a new number and does not mutate the original value"]
109 #[stable(feature = "rust1", since = "1.0.0")]
111 pub fn trunc(self) -> f64 {
112 unsafe { intrinsics::truncf64(self) }
115 /// Returns the fractional part of `self`.
121 /// let y = -3.6_f64;
122 /// let abs_difference_x = (x.fract() - 0.6).abs();
123 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
125 /// assert!(abs_difference_x < 1e-10);
126 /// assert!(abs_difference_y < 1e-10);
128 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
129 #[must_use = "method returns a new number and does not mutate the original value"]
130 #[stable(feature = "rust1", since = "1.0.0")]
132 pub fn fract(self) -> f64 {
136 /// Computes the absolute value of `self`.
142 /// let y = -3.5_f64;
144 /// let abs_difference_x = (x.abs() - x).abs();
145 /// let abs_difference_y = (y.abs() - (-y)).abs();
147 /// assert!(abs_difference_x < 1e-10);
148 /// assert!(abs_difference_y < 1e-10);
150 /// assert!(f64::NAN.abs().is_nan());
152 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
153 #[must_use = "method returns a new number and does not mutate the original value"]
154 #[stable(feature = "rust1", since = "1.0.0")]
156 pub fn abs(self) -> f64 {
157 unsafe { intrinsics::fabsf64(self) }
160 /// Returns a number that represents the sign of `self`.
162 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
163 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
164 /// - NaN if the number is NaN
171 /// assert_eq!(f.signum(), 1.0);
172 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
174 /// assert!(f64::NAN.signum().is_nan());
176 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
177 #[must_use = "method returns a new number and does not mutate the original value"]
178 #[stable(feature = "rust1", since = "1.0.0")]
180 pub fn signum(self) -> f64 {
181 if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
184 /// Returns a number composed of the magnitude of `self` and the sign of
187 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
188 /// equal to `-self`. If `self` is a NaN, then a NaN with the sign bit of
189 /// `sign` is returned. Note, however, that conserving the sign bit on NaN
190 /// across arithmetical operations is not generally guaranteed.
191 /// See [explanation of NaN as a special value](primitive@f32) for more info.
198 /// assert_eq!(f.copysign(0.42), 3.5_f64);
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);
203 /// assert!(f64::NAN.copysign(1.0).is_nan());
205 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
206 #[must_use = "method returns a new number and does not mutate the original value"]
207 #[stable(feature = "copysign", since = "1.35.0")]
209 pub fn copysign(self, sign: f64) -> f64 {
210 unsafe { intrinsics::copysignf64(self, sign) }
213 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
214 /// error, yielding a more accurate result than an unfused multiply-add.
216 /// Using `mul_add` *may* be more performant than an unfused multiply-add if
217 /// the target architecture has a dedicated `fma` CPU instruction. However,
218 /// this is not always true, and will be heavily dependant on designing
219 /// algorithms with specific target hardware in mind.
224 /// let m = 10.0_f64;
226 /// let b = 60.0_f64;
229 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
231 /// assert!(abs_difference < 1e-10);
233 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
234 #[must_use = "method returns a new number and does not mutate the original value"]
235 #[stable(feature = "rust1", since = "1.0.0")]
237 pub fn mul_add(self, a: f64, b: f64) -> f64 {
238 unsafe { intrinsics::fmaf64(self, a, b) }
241 /// Calculates Euclidean division, the matching method for `rem_euclid`.
243 /// This computes the integer `n` such that
244 /// `self = n * rhs + self.rem_euclid(rhs)`.
245 /// In other words, the result is `self / rhs` rounded to the integer `n`
246 /// such that `self >= n * rhs`.
251 /// let a: f64 = 7.0;
253 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
254 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
255 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
256 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
258 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
259 #[must_use = "method returns a new number and does not mutate the original value"]
261 #[stable(feature = "euclidean_division", since = "1.38.0")]
262 pub fn div_euclid(self, rhs: f64) -> f64 {
263 let q = (self / rhs).trunc();
264 if self % rhs < 0.0 {
265 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
270 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
272 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
273 /// most cases. However, due to a floating point round-off error it can
274 /// result in `r == rhs.abs()`, violating the mathematical definition, if
275 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
276 /// This result is not an element of the function's codomain, but it is the
277 /// closest floating point number in the real numbers and thus fulfills the
278 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
284 /// let a: f64 = 7.0;
286 /// assert_eq!(a.rem_euclid(b), 3.0);
287 /// assert_eq!((-a).rem_euclid(b), 1.0);
288 /// assert_eq!(a.rem_euclid(-b), 3.0);
289 /// assert_eq!((-a).rem_euclid(-b), 1.0);
290 /// // limitation due to round-off error
291 /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
293 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
294 #[must_use = "method returns a new number and does not mutate the original value"]
296 #[stable(feature = "euclidean_division", since = "1.38.0")]
297 pub fn rem_euclid(self, rhs: f64) -> f64 {
299 if r < 0.0 { r + rhs.abs() } else { r }
302 /// Raises a number to an integer power.
304 /// Using this function is generally faster than using `powf`.
305 /// It might have a different sequence of rounding operations than `powf`,
306 /// so the results are not guaranteed to agree.
312 /// let abs_difference = (x.powi(2) - (x * x)).abs();
314 /// assert!(abs_difference < 1e-10);
316 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
317 #[must_use = "method returns a new number and does not mutate the original value"]
318 #[stable(feature = "rust1", since = "1.0.0")]
320 pub fn powi(self, n: i32) -> f64 {
321 unsafe { intrinsics::powif64(self, n) }
324 /// Raises a number to a floating point power.
330 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
332 /// assert!(abs_difference < 1e-10);
334 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
335 #[must_use = "method returns a new number and does not mutate the original value"]
336 #[stable(feature = "rust1", since = "1.0.0")]
338 pub fn powf(self, n: f64) -> f64 {
339 unsafe { intrinsics::powf64(self, n) }
342 /// Returns the square root of a number.
344 /// Returns NaN if `self` is a negative number other than `-0.0`.
349 /// let positive = 4.0_f64;
350 /// let negative = -4.0_f64;
351 /// let negative_zero = -0.0_f64;
353 /// let abs_difference = (positive.sqrt() - 2.0).abs();
355 /// assert!(abs_difference < 1e-10);
356 /// assert!(negative.sqrt().is_nan());
357 /// assert!(negative_zero.sqrt() == negative_zero);
359 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
360 #[must_use = "method returns a new number and does not mutate the original value"]
361 #[stable(feature = "rust1", since = "1.0.0")]
363 pub fn sqrt(self) -> f64 {
364 unsafe { intrinsics::sqrtf64(self) }
367 /// Returns `e^(self)`, (the exponential function).
372 /// let one = 1.0_f64;
374 /// let e = one.exp();
376 /// // ln(e) - 1 == 0
377 /// let abs_difference = (e.ln() - 1.0).abs();
379 /// assert!(abs_difference < 1e-10);
381 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
382 #[must_use = "method returns a new number and does not mutate the original value"]
383 #[stable(feature = "rust1", since = "1.0.0")]
385 pub fn exp(self) -> f64 {
386 unsafe { intrinsics::expf64(self) }
389 /// Returns `2^(self)`.
397 /// let abs_difference = (f.exp2() - 4.0).abs();
399 /// assert!(abs_difference < 1e-10);
401 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
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 exp2(self) -> f64 {
406 unsafe { intrinsics::exp2f64(self) }
409 /// Returns the natural logarithm of the number.
414 /// let one = 1.0_f64;
416 /// let e = one.exp();
418 /// // ln(e) - 1 == 0
419 /// let abs_difference = (e.ln() - 1.0).abs();
421 /// assert!(abs_difference < 1e-10);
423 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
424 #[must_use = "method returns a new number and does not mutate the original value"]
425 #[stable(feature = "rust1", since = "1.0.0")]
427 pub fn ln(self) -> f64 {
428 self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
431 /// Returns the logarithm of the number with respect to an arbitrary base.
433 /// The result might not be correctly rounded owing to implementation details;
434 /// `self.log2()` can produce more accurate results for base 2, and
435 /// `self.log10()` can produce more accurate results for base 10.
440 /// let twenty_five = 25.0_f64;
442 /// // log5(25) - 2 == 0
443 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
445 /// assert!(abs_difference < 1e-10);
447 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
448 #[must_use = "method returns a new number and does not mutate the original value"]
449 #[stable(feature = "rust1", since = "1.0.0")]
451 pub fn log(self, base: f64) -> f64 {
452 self.ln() / base.ln()
455 /// Returns the base 2 logarithm of the number.
460 /// let four = 4.0_f64;
462 /// // log2(4) - 2 == 0
463 /// let abs_difference = (four.log2() - 2.0).abs();
465 /// assert!(abs_difference < 1e-10);
467 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
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 log2(self) -> f64 {
472 self.log_wrapper(|n| {
473 #[cfg(target_os = "android")]
474 return crate::sys::android::log2f64(n);
475 #[cfg(not(target_os = "android"))]
476 return unsafe { intrinsics::log2f64(n) };
480 /// Returns the base 10 logarithm of the number.
485 /// let hundred = 100.0_f64;
487 /// // log10(100) - 2 == 0
488 /// let abs_difference = (hundred.log10() - 2.0).abs();
490 /// assert!(abs_difference < 1e-10);
492 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
493 #[must_use = "method returns a new number and does not mutate the original value"]
494 #[stable(feature = "rust1", since = "1.0.0")]
496 pub fn log10(self) -> f64 {
497 self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
500 /// The positive difference of two numbers.
502 /// * If `self <= other`: `0:0`
503 /// * Else: `self - other`
509 /// let y = -3.0_f64;
511 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
512 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
514 /// assert!(abs_difference_x < 1e-10);
515 /// assert!(abs_difference_y < 1e-10);
517 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
518 #[must_use = "method returns a new number and does not mutate the original value"]
519 #[stable(feature = "rust1", since = "1.0.0")]
523 reason = "you probably meant `(self - other).abs()`: \
524 this operation is `(self - other).max(0.0)` \
525 except that `abs_sub` also propagates NaNs (also \
526 known as `fdim` in C). If you truly need the positive \
527 difference, consider using that expression or the C function \
528 `fdim`, depending on how you wish to handle NaN (please consider \
529 filing an issue describing your use-case too)."
531 pub fn abs_sub(self, other: f64) -> f64 {
532 unsafe { cmath::fdim(self, other) }
535 /// Returns the cube root of a number.
542 /// // x^(1/3) - 2 == 0
543 /// let abs_difference = (x.cbrt() - 2.0).abs();
545 /// assert!(abs_difference < 1e-10);
547 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
548 #[must_use = "method returns a new number and does not mutate the original value"]
549 #[stable(feature = "rust1", since = "1.0.0")]
551 pub fn cbrt(self) -> f64 {
552 unsafe { cmath::cbrt(self) }
555 /// Calculates the length of the hypotenuse of a right-angle triangle given
556 /// legs of length `x` and `y`.
564 /// // sqrt(x^2 + y^2)
565 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
567 /// assert!(abs_difference < 1e-10);
569 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
570 #[must_use = "method returns a new number and does not mutate the original value"]
571 #[stable(feature = "rust1", since = "1.0.0")]
573 pub fn hypot(self, other: f64) -> f64 {
574 unsafe { cmath::hypot(self, other) }
577 /// Computes the sine of a number (in radians).
582 /// let x = std::f64::consts::FRAC_PI_2;
584 /// let abs_difference = (x.sin() - 1.0).abs();
586 /// assert!(abs_difference < 1e-10);
588 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
589 #[must_use = "method returns a new number and does not mutate the original value"]
590 #[stable(feature = "rust1", since = "1.0.0")]
592 pub fn sin(self) -> f64 {
593 unsafe { intrinsics::sinf64(self) }
596 /// Computes the cosine of a number (in radians).
601 /// let x = 2.0 * std::f64::consts::PI;
603 /// let abs_difference = (x.cos() - 1.0).abs();
605 /// assert!(abs_difference < 1e-10);
607 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
608 #[must_use = "method returns a new number and does not mutate the original value"]
609 #[stable(feature = "rust1", since = "1.0.0")]
611 pub fn cos(self) -> f64 {
612 unsafe { intrinsics::cosf64(self) }
615 /// Computes the tangent of a number (in radians).
620 /// let x = std::f64::consts::FRAC_PI_4;
621 /// let abs_difference = (x.tan() - 1.0).abs();
623 /// assert!(abs_difference < 1e-14);
625 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
626 #[must_use = "method returns a new number and does not mutate the original value"]
627 #[stable(feature = "rust1", since = "1.0.0")]
629 pub fn tan(self) -> f64 {
630 unsafe { cmath::tan(self) }
633 /// Computes the arcsine of a number. Return value is in radians in
634 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
640 /// let f = std::f64::consts::FRAC_PI_2;
642 /// // asin(sin(pi/2))
643 /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
645 /// assert!(abs_difference < 1e-10);
647 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
648 #[must_use = "method returns a new number and does not mutate the original value"]
649 #[stable(feature = "rust1", since = "1.0.0")]
651 pub fn asin(self) -> f64 {
652 unsafe { cmath::asin(self) }
655 /// Computes the arccosine of a number. Return value is in radians in
656 /// the range [0, pi] or NaN if the number is outside the range
662 /// let f = std::f64::consts::FRAC_PI_4;
664 /// // acos(cos(pi/4))
665 /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
667 /// assert!(abs_difference < 1e-10);
669 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
670 #[must_use = "method returns a new number and does not mutate the original value"]
671 #[stable(feature = "rust1", since = "1.0.0")]
673 pub fn acos(self) -> f64 {
674 unsafe { cmath::acos(self) }
677 /// Computes the arctangent of a number. Return value is in radians in the
678 /// range [-pi/2, pi/2];
686 /// let abs_difference = (f.tan().atan() - 1.0).abs();
688 /// assert!(abs_difference < 1e-10);
690 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
691 #[must_use = "method returns a new number and does not mutate the original value"]
692 #[stable(feature = "rust1", since = "1.0.0")]
694 pub fn atan(self) -> f64 {
695 unsafe { cmath::atan(self) }
698 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
700 /// * `x = 0`, `y = 0`: `0`
701 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
702 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
703 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
708 /// // Positive angles measured counter-clockwise
709 /// // from positive x axis
710 /// // -pi/4 radians (45 deg clockwise)
711 /// let x1 = 3.0_f64;
712 /// let y1 = -3.0_f64;
714 /// // 3pi/4 radians (135 deg counter-clockwise)
715 /// let x2 = -3.0_f64;
716 /// let y2 = 3.0_f64;
718 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
719 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
721 /// assert!(abs_difference_1 < 1e-10);
722 /// assert!(abs_difference_2 < 1e-10);
724 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
725 #[must_use = "method returns a new number and does not mutate the original value"]
726 #[stable(feature = "rust1", since = "1.0.0")]
728 pub fn atan2(self, other: f64) -> f64 {
729 unsafe { cmath::atan2(self, other) }
732 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
733 /// `(sin(x), cos(x))`.
738 /// let x = std::f64::consts::FRAC_PI_4;
739 /// let f = x.sin_cos();
741 /// let abs_difference_0 = (f.0 - x.sin()).abs();
742 /// let abs_difference_1 = (f.1 - x.cos()).abs();
744 /// assert!(abs_difference_0 < 1e-10);
745 /// assert!(abs_difference_1 < 1e-10);
747 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
748 #[stable(feature = "rust1", since = "1.0.0")]
750 pub fn sin_cos(self) -> (f64, f64) {
751 (self.sin(), self.cos())
754 /// Returns `e^(self) - 1` in a way that is accurate even if the
755 /// number is close to zero.
760 /// let x = 1e-16_f64;
762 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
763 /// let approx = x + x * x / 2.0;
764 /// let abs_difference = (x.exp_m1() - approx).abs();
766 /// assert!(abs_difference < 1e-20);
768 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
769 #[must_use = "method returns a new number and does not mutate the original value"]
770 #[stable(feature = "rust1", since = "1.0.0")]
772 pub fn exp_m1(self) -> f64 {
773 unsafe { cmath::expm1(self) }
776 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
777 /// the operations were performed separately.
782 /// let x = 1e-16_f64;
784 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
785 /// let approx = x - x * x / 2.0;
786 /// let abs_difference = (x.ln_1p() - approx).abs();
788 /// assert!(abs_difference < 1e-20);
790 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
791 #[must_use = "method returns a new number and does not mutate the original value"]
792 #[stable(feature = "rust1", since = "1.0.0")]
794 pub fn ln_1p(self) -> f64 {
795 unsafe { cmath::log1p(self) }
798 /// Hyperbolic sine function.
803 /// let e = std::f64::consts::E;
806 /// let f = x.sinh();
807 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
808 /// let g = ((e * e) - 1.0) / (2.0 * e);
809 /// let abs_difference = (f - g).abs();
811 /// assert!(abs_difference < 1e-10);
813 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
814 #[must_use = "method returns a new number and does not mutate the original value"]
815 #[stable(feature = "rust1", since = "1.0.0")]
817 pub fn sinh(self) -> f64 {
818 unsafe { cmath::sinh(self) }
821 /// Hyperbolic cosine function.
826 /// let e = std::f64::consts::E;
828 /// let f = x.cosh();
829 /// // Solving cosh() at 1 gives this result
830 /// let g = ((e * e) + 1.0) / (2.0 * e);
831 /// let abs_difference = (f - g).abs();
834 /// assert!(abs_difference < 1.0e-10);
836 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
837 #[must_use = "method returns a new number and does not mutate the original value"]
838 #[stable(feature = "rust1", since = "1.0.0")]
840 pub fn cosh(self) -> f64 {
841 unsafe { cmath::cosh(self) }
844 /// Hyperbolic tangent function.
849 /// let e = std::f64::consts::E;
852 /// let f = x.tanh();
853 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
854 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
855 /// let abs_difference = (f - g).abs();
857 /// assert!(abs_difference < 1.0e-10);
859 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
860 #[must_use = "method returns a new number and does not mutate the original value"]
861 #[stable(feature = "rust1", since = "1.0.0")]
863 pub fn tanh(self) -> f64 {
864 unsafe { cmath::tanh(self) }
867 /// Inverse hyperbolic sine function.
873 /// let f = x.sinh().asinh();
875 /// let abs_difference = (f - x).abs();
877 /// assert!(abs_difference < 1.0e-10);
879 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
880 #[must_use = "method returns a new number and does not mutate the original value"]
881 #[stable(feature = "rust1", since = "1.0.0")]
883 pub fn asinh(self) -> f64 {
884 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
887 /// Inverse hyperbolic cosine function.
893 /// let f = x.cosh().acosh();
895 /// let abs_difference = (f - x).abs();
897 /// assert!(abs_difference < 1.0e-10);
899 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
900 #[must_use = "method returns a new number and does not mutate the original value"]
901 #[stable(feature = "rust1", since = "1.0.0")]
903 pub fn acosh(self) -> f64 {
904 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
907 /// Inverse hyperbolic tangent function.
912 /// let e = std::f64::consts::E;
913 /// let f = e.tanh().atanh();
915 /// let abs_difference = (f - e).abs();
917 /// assert!(abs_difference < 1.0e-10);
919 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
920 #[must_use = "method returns a new number and does not mutate the original value"]
921 #[stable(feature = "rust1", since = "1.0.0")]
923 pub fn atanh(self) -> f64 {
924 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
927 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
928 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
930 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
931 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
932 if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
934 } else if self.is_finite() {
937 } else if self == 0.0 {
938 Self::NEG_INFINITY // log(0) = -Inf
940 Self::NAN // log(-n) = NaN
942 } else if self.is_nan() {
943 self // log(NaN) = NaN
944 } else if self > 0.0 {
945 self // log(Inf) = Inf
947 Self::NAN // log(-Inf) = NaN