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 #[lang = "f64_runtime"]
33 /// Returns the largest integer less than or equal to a number.
42 /// assert_eq!(f.floor(), 3.0);
43 /// assert_eq!(g.floor(), 3.0);
44 /// assert_eq!(h.floor(), -4.0);
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 #[must_use = "method returns a new number and does not mutate the original value"]
65 #[stable(feature = "rust1", since = "1.0.0")]
67 pub fn ceil(self) -> f64 {
68 unsafe { intrinsics::ceilf64(self) }
71 /// Returns the nearest integer to a number. Round half-way cases away from
80 /// assert_eq!(f.round(), 3.0);
81 /// assert_eq!(g.round(), -3.0);
83 #[must_use = "method returns a new number and does not mutate the original value"]
84 #[stable(feature = "rust1", since = "1.0.0")]
86 pub fn round(self) -> f64 {
87 unsafe { intrinsics::roundf64(self) }
90 /// Returns the integer part of a number.
99 /// assert_eq!(f.trunc(), 3.0);
100 /// assert_eq!(g.trunc(), 3.0);
101 /// assert_eq!(h.trunc(), -3.0);
103 #[must_use = "method returns a new number and does not mutate the original value"]
104 #[stable(feature = "rust1", since = "1.0.0")]
106 pub fn trunc(self) -> f64 {
107 unsafe { intrinsics::truncf64(self) }
110 /// Returns the fractional part of a number.
116 /// let y = -3.6_f64;
117 /// let abs_difference_x = (x.fract() - 0.6).abs();
118 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
120 /// assert!(abs_difference_x < 1e-10);
121 /// assert!(abs_difference_y < 1e-10);
123 #[must_use = "method returns a new number and does not mutate the original value"]
124 #[stable(feature = "rust1", since = "1.0.0")]
126 pub fn fract(self) -> f64 {
130 /// Computes the absolute value of `self`. Returns `NAN` if the
137 /// let y = -3.5_f64;
139 /// let abs_difference_x = (x.abs() - x).abs();
140 /// let abs_difference_y = (y.abs() - (-y)).abs();
142 /// assert!(abs_difference_x < 1e-10);
143 /// assert!(abs_difference_y < 1e-10);
145 /// assert!(f64::NAN.abs().is_nan());
147 #[must_use = "method returns a new number and does not mutate the original value"]
148 #[stable(feature = "rust1", since = "1.0.0")]
150 pub fn abs(self) -> f64 {
151 unsafe { intrinsics::fabsf64(self) }
154 /// Returns a number that represents the sign of `self`.
156 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
157 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
158 /// - `NAN` if the number is `NAN`
165 /// assert_eq!(f.signum(), 1.0);
166 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
168 /// assert!(f64::NAN.signum().is_nan());
170 #[must_use = "method returns a new number and does not mutate the original value"]
171 #[stable(feature = "rust1", since = "1.0.0")]
173 pub fn signum(self) -> f64 {
174 if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
177 /// Returns a number composed of the magnitude of `self` and the sign of
180 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
181 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
182 /// `sign` is returned.
189 /// assert_eq!(f.copysign(0.42), 3.5_f64);
190 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
191 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
192 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
194 /// assert!(f64::NAN.copysign(1.0).is_nan());
196 #[must_use = "method returns a new number and does not mutate the original value"]
197 #[stable(feature = "copysign", since = "1.35.0")]
199 pub fn copysign(self, sign: f64) -> f64 {
200 unsafe { intrinsics::copysignf64(self, sign) }
203 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
204 /// error, yielding a more accurate result than an unfused multiply-add.
206 /// Using `mul_add` *may* be more performant than an unfused multiply-add if
207 /// the target architecture has a dedicated `fma` CPU instruction. However,
208 /// this is not always true, and will be heavily dependant on designing
209 /// algorithms with specific target hardware in mind.
214 /// let m = 10.0_f64;
216 /// let b = 60.0_f64;
219 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
221 /// assert!(abs_difference < 1e-10);
223 #[must_use = "method returns a new number and does not mutate the original value"]
224 #[stable(feature = "rust1", since = "1.0.0")]
226 pub fn mul_add(self, a: f64, b: f64) -> f64 {
227 unsafe { intrinsics::fmaf64(self, a, b) }
230 /// Calculates Euclidean division, the matching method for `rem_euclid`.
232 /// This computes the integer `n` such that
233 /// `self = n * rhs + self.rem_euclid(rhs)`.
234 /// In other words, the result is `self / rhs` rounded to the integer `n`
235 /// such that `self >= n * rhs`.
240 /// let a: f64 = 7.0;
242 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
243 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
244 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
245 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
247 #[must_use = "method returns a new number and does not mutate the original value"]
249 #[stable(feature = "euclidean_division", since = "1.38.0")]
250 pub fn div_euclid(self, rhs: f64) -> f64 {
251 let q = (self / rhs).trunc();
252 if self % rhs < 0.0 {
253 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
258 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
260 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
261 /// most cases. However, due to a floating point round-off error it can
262 /// result in `r == rhs.abs()`, violating the mathematical definition, if
263 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
264 /// This result is not an element of the function's codomain, but it is the
265 /// closest floating point number in the real numbers and thus fulfills the
266 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
272 /// let a: f64 = 7.0;
274 /// assert_eq!(a.rem_euclid(b), 3.0);
275 /// assert_eq!((-a).rem_euclid(b), 1.0);
276 /// assert_eq!(a.rem_euclid(-b), 3.0);
277 /// assert_eq!((-a).rem_euclid(-b), 1.0);
278 /// // limitation due to round-off error
279 /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
281 #[must_use = "method returns a new number and does not mutate the original value"]
283 #[stable(feature = "euclidean_division", since = "1.38.0")]
284 pub fn rem_euclid(self, rhs: f64) -> f64 {
286 if r < 0.0 { r + rhs.abs() } else { r }
289 /// Raises a number to an integer power.
291 /// Using this function is generally faster than using `powf`
297 /// let abs_difference = (x.powi(2) - (x * x)).abs();
299 /// assert!(abs_difference < 1e-10);
301 #[must_use = "method returns a new number and does not mutate the original value"]
302 #[stable(feature = "rust1", since = "1.0.0")]
304 pub fn powi(self, n: i32) -> f64 {
305 unsafe { intrinsics::powif64(self, n) }
308 /// Raises a number to a floating point power.
314 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
316 /// assert!(abs_difference < 1e-10);
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 powf(self, n: f64) -> f64 {
322 unsafe { intrinsics::powf64(self, n) }
325 /// Returns the square root of a number.
327 /// Returns NaN if `self` is a negative number other than `-0.0`.
332 /// let positive = 4.0_f64;
333 /// let negative = -4.0_f64;
334 /// let negative_zero = -0.0_f64;
336 /// let abs_difference = (positive.sqrt() - 2.0).abs();
338 /// assert!(abs_difference < 1e-10);
339 /// assert!(negative.sqrt().is_nan());
340 /// assert!(negative_zero.sqrt() == negative_zero);
342 #[must_use = "method returns a new number and does not mutate the original value"]
343 #[stable(feature = "rust1", since = "1.0.0")]
345 pub fn sqrt(self) -> f64 {
346 unsafe { intrinsics::sqrtf64(self) }
349 /// Returns `e^(self)`, (the exponential function).
354 /// let one = 1.0_f64;
356 /// let e = one.exp();
358 /// // ln(e) - 1 == 0
359 /// let abs_difference = (e.ln() - 1.0).abs();
361 /// assert!(abs_difference < 1e-10);
363 #[must_use = "method returns a new number and does not mutate the original value"]
364 #[stable(feature = "rust1", since = "1.0.0")]
366 pub fn exp(self) -> f64 {
367 unsafe { intrinsics::expf64(self) }
370 /// Returns `2^(self)`.
378 /// let abs_difference = (f.exp2() - 4.0).abs();
380 /// assert!(abs_difference < 1e-10);
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 exp2(self) -> f64 {
386 unsafe { intrinsics::exp2f64(self) }
389 /// Returns the natural logarithm of the number.
394 /// let one = 1.0_f64;
396 /// let e = one.exp();
398 /// // ln(e) - 1 == 0
399 /// let abs_difference = (e.ln() - 1.0).abs();
401 /// assert!(abs_difference < 1e-10);
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 ln(self) -> f64 {
407 self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
410 /// Returns the logarithm of the number with respect to an arbitrary base.
412 /// The result may not be correctly rounded owing to implementation details;
413 /// `self.log2()` can produce more accurate results for base 2, and
414 /// `self.log10()` can produce more accurate results for base 10.
419 /// let twenty_five = 25.0_f64;
421 /// // log5(25) - 2 == 0
422 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
424 /// assert!(abs_difference < 1e-10);
426 #[must_use = "method returns a new number and does not mutate the original value"]
427 #[stable(feature = "rust1", since = "1.0.0")]
429 pub fn log(self, base: f64) -> f64 {
430 self.ln() / base.ln()
433 /// Returns the base 2 logarithm of the number.
438 /// let four = 4.0_f64;
440 /// // log2(4) - 2 == 0
441 /// let abs_difference = (four.log2() - 2.0).abs();
443 /// assert!(abs_difference < 1e-10);
445 #[must_use = "method returns a new number and does not mutate the original value"]
446 #[stable(feature = "rust1", since = "1.0.0")]
448 pub fn log2(self) -> f64 {
449 self.log_wrapper(|n| {
450 #[cfg(target_os = "android")]
451 return crate::sys::android::log2f64(n);
452 #[cfg(not(target_os = "android"))]
453 return unsafe { intrinsics::log2f64(n) };
457 /// Returns the base 10 logarithm of the number.
462 /// let hundred = 100.0_f64;
464 /// // log10(100) - 2 == 0
465 /// let abs_difference = (hundred.log10() - 2.0).abs();
467 /// assert!(abs_difference < 1e-10);
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 log10(self) -> f64 {
473 self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
476 /// The positive difference of two numbers.
478 /// * If `self <= other`: `0:0`
479 /// * Else: `self - other`
485 /// let y = -3.0_f64;
487 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
488 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
490 /// assert!(abs_difference_x < 1e-10);
491 /// assert!(abs_difference_y < 1e-10);
493 #[must_use = "method returns a new number and does not mutate the original value"]
494 #[stable(feature = "rust1", since = "1.0.0")]
498 reason = "you probably meant `(self - other).abs()`: \
499 this operation is `(self - other).max(0.0)` \
500 except that `abs_sub` also propagates NaNs (also \
501 known as `fdim` in C). If you truly need the positive \
502 difference, consider using that expression or the C function \
503 `fdim`, depending on how you wish to handle NaN (please consider \
504 filing an issue describing your use-case too)."
506 pub fn abs_sub(self, other: f64) -> f64 {
507 unsafe { cmath::fdim(self, other) }
510 /// Returns the cube root of a number.
517 /// // x^(1/3) - 2 == 0
518 /// let abs_difference = (x.cbrt() - 2.0).abs();
520 /// assert!(abs_difference < 1e-10);
522 #[must_use = "method returns a new number and does not mutate the original value"]
523 #[stable(feature = "rust1", since = "1.0.0")]
525 pub fn cbrt(self) -> f64 {
526 unsafe { cmath::cbrt(self) }
529 /// Calculates the length of the hypotenuse of a right-angle triangle given
530 /// legs of length `x` and `y`.
538 /// // sqrt(x^2 + y^2)
539 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
541 /// assert!(abs_difference < 1e-10);
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 hypot(self, other: f64) -> f64 {
547 unsafe { cmath::hypot(self, other) }
550 /// Computes the sine of a number (in radians).
555 /// let x = std::f64::consts::FRAC_PI_2;
557 /// let abs_difference = (x.sin() - 1.0).abs();
559 /// assert!(abs_difference < 1e-10);
561 #[must_use = "method returns a new number and does not mutate the original value"]
562 #[stable(feature = "rust1", since = "1.0.0")]
564 pub fn sin(self) -> f64 {
565 unsafe { intrinsics::sinf64(self) }
568 /// Computes the cosine of a number (in radians).
573 /// let x = 2.0 * std::f64::consts::PI;
575 /// let abs_difference = (x.cos() - 1.0).abs();
577 /// assert!(abs_difference < 1e-10);
579 #[must_use = "method returns a new number and does not mutate the original value"]
580 #[stable(feature = "rust1", since = "1.0.0")]
582 pub fn cos(self) -> f64 {
583 unsafe { intrinsics::cosf64(self) }
586 /// Computes the tangent of a number (in radians).
591 /// let x = std::f64::consts::FRAC_PI_4;
592 /// let abs_difference = (x.tan() - 1.0).abs();
594 /// assert!(abs_difference < 1e-14);
596 #[must_use = "method returns a new number and does not mutate the original value"]
597 #[stable(feature = "rust1", since = "1.0.0")]
599 pub fn tan(self) -> f64 {
600 unsafe { cmath::tan(self) }
603 /// Computes the arcsine of a number. Return value is in radians in
604 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
610 /// let f = std::f64::consts::FRAC_PI_2;
612 /// // asin(sin(pi/2))
613 /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
615 /// assert!(abs_difference < 1e-10);
617 #[must_use = "method returns a new number and does not mutate the original value"]
618 #[stable(feature = "rust1", since = "1.0.0")]
620 pub fn asin(self) -> f64 {
621 unsafe { cmath::asin(self) }
624 /// Computes the arccosine of a number. Return value is in radians in
625 /// the range [0, pi] or NaN if the number is outside the range
631 /// let f = std::f64::consts::FRAC_PI_4;
633 /// // acos(cos(pi/4))
634 /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
636 /// assert!(abs_difference < 1e-10);
638 #[must_use = "method returns a new number and does not mutate the original value"]
639 #[stable(feature = "rust1", since = "1.0.0")]
641 pub fn acos(self) -> f64 {
642 unsafe { cmath::acos(self) }
645 /// Computes the arctangent of a number. Return value is in radians in the
646 /// range [-pi/2, pi/2];
654 /// let abs_difference = (f.tan().atan() - 1.0).abs();
656 /// assert!(abs_difference < 1e-10);
658 #[must_use = "method returns a new number and does not mutate the original value"]
659 #[stable(feature = "rust1", since = "1.0.0")]
661 pub fn atan(self) -> f64 {
662 unsafe { cmath::atan(self) }
665 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
667 /// * `x = 0`, `y = 0`: `0`
668 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
669 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
670 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
675 /// // Positive angles measured counter-clockwise
676 /// // from positive x axis
677 /// // -pi/4 radians (45 deg clockwise)
678 /// let x1 = 3.0_f64;
679 /// let y1 = -3.0_f64;
681 /// // 3pi/4 radians (135 deg counter-clockwise)
682 /// let x2 = -3.0_f64;
683 /// let y2 = 3.0_f64;
685 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
686 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
688 /// assert!(abs_difference_1 < 1e-10);
689 /// assert!(abs_difference_2 < 1e-10);
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 atan2(self, other: f64) -> f64 {
695 unsafe { cmath::atan2(self, other) }
698 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
699 /// `(sin(x), cos(x))`.
704 /// let x = std::f64::consts::FRAC_PI_4;
705 /// let f = x.sin_cos();
707 /// let abs_difference_0 = (f.0 - x.sin()).abs();
708 /// let abs_difference_1 = (f.1 - x.cos()).abs();
710 /// assert!(abs_difference_0 < 1e-10);
711 /// assert!(abs_difference_1 < 1e-10);
713 #[stable(feature = "rust1", since = "1.0.0")]
715 pub fn sin_cos(self) -> (f64, f64) {
716 (self.sin(), self.cos())
719 /// Returns `e^(self) - 1` in a way that is accurate even if the
720 /// number is close to zero.
725 /// let x = 1e-16_f64;
727 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
728 /// let approx = x + x * x / 2.0;
729 /// let abs_difference = (x.exp_m1() - approx).abs();
731 /// assert!(abs_difference < 1e-20);
733 #[must_use = "method returns a new number and does not mutate the original value"]
734 #[stable(feature = "rust1", since = "1.0.0")]
736 pub fn exp_m1(self) -> f64 {
737 unsafe { cmath::expm1(self) }
740 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
741 /// the operations were performed separately.
746 /// let x = 1e-16_f64;
748 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
749 /// let approx = x - x * x / 2.0;
750 /// let abs_difference = (x.ln_1p() - approx).abs();
752 /// assert!(abs_difference < 1e-20);
754 #[must_use = "method returns a new number and does not mutate the original value"]
755 #[stable(feature = "rust1", since = "1.0.0")]
757 pub fn ln_1p(self) -> f64 {
758 unsafe { cmath::log1p(self) }
761 /// Hyperbolic sine function.
766 /// let e = std::f64::consts::E;
769 /// let f = x.sinh();
770 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
771 /// let g = ((e * e) - 1.0) / (2.0 * e);
772 /// let abs_difference = (f - g).abs();
774 /// assert!(abs_difference < 1e-10);
776 #[must_use = "method returns a new number and does not mutate the original value"]
777 #[stable(feature = "rust1", since = "1.0.0")]
779 pub fn sinh(self) -> f64 {
780 unsafe { cmath::sinh(self) }
783 /// Hyperbolic cosine function.
788 /// let e = std::f64::consts::E;
790 /// let f = x.cosh();
791 /// // Solving cosh() at 1 gives this result
792 /// let g = ((e * e) + 1.0) / (2.0 * e);
793 /// let abs_difference = (f - g).abs();
796 /// assert!(abs_difference < 1.0e-10);
798 #[must_use = "method returns a new number and does not mutate the original value"]
799 #[stable(feature = "rust1", since = "1.0.0")]
801 pub fn cosh(self) -> f64 {
802 unsafe { cmath::cosh(self) }
805 /// Hyperbolic tangent function.
810 /// let e = std::f64::consts::E;
813 /// let f = x.tanh();
814 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
815 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
816 /// let abs_difference = (f - g).abs();
818 /// assert!(abs_difference < 1.0e-10);
820 #[must_use = "method returns a new number and does not mutate the original value"]
821 #[stable(feature = "rust1", since = "1.0.0")]
823 pub fn tanh(self) -> f64 {
824 unsafe { cmath::tanh(self) }
827 /// Inverse hyperbolic sine function.
833 /// let f = x.sinh().asinh();
835 /// let abs_difference = (f - x).abs();
837 /// assert!(abs_difference < 1.0e-10);
839 #[must_use = "method returns a new number and does not mutate the original value"]
840 #[stable(feature = "rust1", since = "1.0.0")]
842 pub fn asinh(self) -> f64 {
843 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
846 /// Inverse hyperbolic cosine function.
852 /// let f = x.cosh().acosh();
854 /// let abs_difference = (f - x).abs();
856 /// assert!(abs_difference < 1.0e-10);
858 #[must_use = "method returns a new number and does not mutate the original value"]
859 #[stable(feature = "rust1", since = "1.0.0")]
861 pub fn acosh(self) -> f64 {
862 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
865 /// Inverse hyperbolic tangent function.
870 /// let e = std::f64::consts::E;
871 /// let f = e.tanh().atanh();
873 /// let abs_difference = (f - e).abs();
875 /// assert!(abs_difference < 1.0e-10);
877 #[must_use = "method returns a new number and does not mutate the original value"]
878 #[stable(feature = "rust1", since = "1.0.0")]
880 pub fn atanh(self) -> f64 {
881 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
884 /// Linear interpolation between `start` and `end`.
886 /// This enables linear interpolation between `start` and `end`, where start is represented by
887 /// `self == 0.0` and `end` is represented by `self == 1.0`. This is the basis of all
888 /// "transition", "easing", or "step" functions; if you change `self` from 0.0 to 1.0
889 /// at a given rate, the result will change from `start` to `end` at a similar rate.
891 /// Values below 0.0 or above 1.0 are allowed, allowing you to extrapolate values outside the
892 /// range from `start` to `end`. This also is useful for transition functions which might
893 /// move slightly past the end or start for a desired effect. Mathematically, the values
894 /// returned are equivalent to `start + self * (end - start)`, although we make a few specific
895 /// guarantees that are useful specifically to linear interpolation.
897 /// These guarantees are:
899 /// * If `start` and `end` are [finite], the value at 0.0 is always `start` and the
900 /// value at 1.0 is always `end`. (exactness)
901 /// * If `start` and `end` are [finite], the values will always move in the direction from
902 /// `start` to `end` (monotonicity)
903 /// * If `self` is [finite] and `start == end`, the value at any point will always be
904 /// `start == end`. (consistency)
906 /// [finite]: #method.is_finite
907 #[must_use = "method returns a new number and does not mutate the original value"]
908 #[unstable(feature = "float_interpolation", issue = "86269")]
909 pub fn lerp(self, start: f64, end: f64) -> f64 {
916 self.mul_add(end, (-self).mul_add(start, start))
920 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
921 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
923 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
924 if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
926 } else if self.is_finite() {
929 } else if self == 0.0 {
930 Self::NEG_INFINITY // log(0) = -Inf
932 Self::NAN // log(-n) = NaN
934 } else if self.is_nan() {
935 self // log(NaN) = NaN
936 } else if self > 0.0 {
937 self // log(Inf) = Inf
939 Self::NAN // log(-Inf) = NaN