1 //! Constants specific to the `f32` single-precision floating point type.
3 //! *[See also the `f32` primitive type](primitive@f32).*
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 `f32` 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) -> f32 {
50 unsafe { intrinsics::floorf32(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) -> f32 {
69 unsafe { intrinsics::ceilf32(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) -> f32 {
89 unsafe { intrinsics::roundf32(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) -> f32 {
110 unsafe { intrinsics::truncf32(self) }
113 /// Returns the fractional part of a number.
119 /// let y = -3.6_f32;
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 <= f32::EPSILON);
124 /// assert!(abs_difference_y <= f32::EPSILON);
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) -> f32 {
134 /// Computes the absolute value of `self`. Returns `NAN` if the
141 /// let y = -3.5_f32;
143 /// let abs_difference_x = (x.abs() - x).abs();
144 /// let abs_difference_y = (y.abs() - (-y)).abs();
146 /// assert!(abs_difference_x <= f32::EPSILON);
147 /// assert!(abs_difference_y <= f32::EPSILON);
149 /// assert!(f32::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) -> f32 {
156 unsafe { intrinsics::fabsf32(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!(f32::NEG_INFINITY.signum(), -1.0);
173 /// assert!(f32::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) -> f32 {
180 if self.is_nan() { Self::NAN } else { 1.0_f32.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_f32);
196 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
197 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
198 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
200 /// assert!(f32::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"]
205 #[stable(feature = "copysign", since = "1.35.0")]
206 pub fn copysign(self, sign: f32) -> f32 {
207 unsafe { intrinsics::copysignf32(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_f32;
223 /// let b = 60.0_f32;
226 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
228 /// assert!(abs_difference <= f32::EPSILON);
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: f32, b: f32) -> f32 {
235 unsafe { intrinsics::fmaf32(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: f32 = 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: f32) -> f32 {
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: f32 = 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!((-f32::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: f32) -> f32 {
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 <= f32::EPSILON);
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) -> f32 {
316 unsafe { intrinsics::powif32(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 <= f32::EPSILON);
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: f32) -> f32 {
334 unsafe { intrinsics::powf32(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_f32;
345 /// let negative = -4.0_f32;
346 /// let negative_zero = -0.0_f32;
348 /// let abs_difference = (positive.sqrt() - 2.0).abs();
350 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
359 unsafe { intrinsics::sqrtf32(self) }
362 /// Returns `e^(self)`, (the exponential function).
367 /// let one = 1.0f32;
369 /// let e = one.exp();
371 /// // ln(e) - 1 == 0
372 /// let abs_difference = (e.ln() - 1.0).abs();
374 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
381 unsafe { intrinsics::expf32(self) }
384 /// Returns `2^(self)`.
392 /// let abs_difference = (f.exp2() - 4.0).abs();
394 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
401 unsafe { intrinsics::exp2f32(self) }
404 /// Returns the natural logarithm of the number.
409 /// let one = 1.0f32;
411 /// let e = one.exp();
413 /// // ln(e) - 1 == 0
414 /// let abs_difference = (e.ln() - 1.0).abs();
416 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
423 unsafe { intrinsics::logf32(self) }
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 five = 5.0f32;
437 /// // log5(5) - 1 == 0
438 /// let abs_difference = (five.log(5.0) - 1.0).abs();
440 /// assert!(abs_difference <= f32::EPSILON);
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: f32) -> f32 {
447 self.ln() / base.ln()
450 /// Returns the base 2 logarithm of the number.
455 /// let two = 2.0f32;
457 /// // log2(2) - 1 == 0
458 /// let abs_difference = (two.log2() - 1.0).abs();
460 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
467 #[cfg(target_os = "android")]
468 return crate::sys::android::log2f32(self);
469 #[cfg(not(target_os = "android"))]
470 return unsafe { intrinsics::log2f32(self) };
473 /// Returns the base 10 logarithm of the number.
478 /// let ten = 10.0f32;
480 /// // log10(10) - 1 == 0
481 /// let abs_difference = (ten.log10() - 1.0).abs();
483 /// assert!(abs_difference <= f32::EPSILON);
485 #[rustc_allow_incoherent_impl]
486 #[must_use = "method returns a new number and does not mutate the original value"]
487 #[stable(feature = "rust1", since = "1.0.0")]
489 pub fn log10(self) -> f32 {
490 unsafe { intrinsics::log10f32(self) }
493 /// The positive difference of two numbers.
495 /// * If `self <= other`: `0:0`
496 /// * Else: `self - other`
504 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
505 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
507 /// assert!(abs_difference_x <= f32::EPSILON);
508 /// assert!(abs_difference_y <= f32::EPSILON);
510 #[rustc_allow_incoherent_impl]
511 #[must_use = "method returns a new number and does not mutate the original value"]
512 #[stable(feature = "rust1", since = "1.0.0")]
516 reason = "you probably meant `(self - other).abs()`: \
517 this operation is `(self - other).max(0.0)` \
518 except that `abs_sub` also propagates NaNs (also \
519 known as `fdimf` in C). If you truly need the positive \
520 difference, consider using that expression or the C function \
521 `fdimf`, depending on how you wish to handle NaN (please consider \
522 filing an issue describing your use-case too)."
524 pub fn abs_sub(self, other: f32) -> f32 {
525 unsafe { cmath::fdimf(self, other) }
528 /// Returns the cube root of a number.
535 /// // x^(1/3) - 2 == 0
536 /// let abs_difference = (x.cbrt() - 2.0).abs();
538 /// assert!(abs_difference <= f32::EPSILON);
540 #[rustc_allow_incoherent_impl]
541 #[must_use = "method returns a new number and does not mutate the original value"]
542 #[stable(feature = "rust1", since = "1.0.0")]
544 pub fn cbrt(self) -> f32 {
545 unsafe { cmath::cbrtf(self) }
548 /// Calculates the length of the hypotenuse of a right-angle triangle given
549 /// legs of length `x` and `y`.
557 /// // sqrt(x^2 + y^2)
558 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
560 /// assert!(abs_difference <= f32::EPSILON);
562 #[rustc_allow_incoherent_impl]
563 #[must_use = "method returns a new number and does not mutate the original value"]
564 #[stable(feature = "rust1", since = "1.0.0")]
566 pub fn hypot(self, other: f32) -> f32 {
567 unsafe { cmath::hypotf(self, other) }
570 /// Computes the sine of a number (in radians).
575 /// let x = std::f32::consts::FRAC_PI_2;
577 /// let abs_difference = (x.sin() - 1.0).abs();
579 /// assert!(abs_difference <= f32::EPSILON);
581 #[rustc_allow_incoherent_impl]
582 #[must_use = "method returns a new number and does not mutate the original value"]
583 #[stable(feature = "rust1", since = "1.0.0")]
585 pub fn sin(self) -> f32 {
586 unsafe { intrinsics::sinf32(self) }
589 /// Computes the cosine of a number (in radians).
594 /// let x = 2.0 * std::f32::consts::PI;
596 /// let abs_difference = (x.cos() - 1.0).abs();
598 /// assert!(abs_difference <= f32::EPSILON);
600 #[rustc_allow_incoherent_impl]
601 #[must_use = "method returns a new number and does not mutate the original value"]
602 #[stable(feature = "rust1", since = "1.0.0")]
604 pub fn cos(self) -> f32 {
605 unsafe { intrinsics::cosf32(self) }
608 /// Computes the tangent of a number (in radians).
613 /// let x = std::f32::consts::FRAC_PI_4;
614 /// let abs_difference = (x.tan() - 1.0).abs();
616 /// assert!(abs_difference <= f32::EPSILON);
618 #[rustc_allow_incoherent_impl]
619 #[must_use = "method returns a new number and does not mutate the original value"]
620 #[stable(feature = "rust1", since = "1.0.0")]
622 pub fn tan(self) -> f32 {
623 unsafe { cmath::tanf(self) }
626 /// Computes the arcsine of a number. Return value is in radians in
627 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
633 /// let f = std::f32::consts::FRAC_PI_2;
635 /// // asin(sin(pi/2))
636 /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
638 /// assert!(abs_difference <= f32::EPSILON);
640 #[rustc_allow_incoherent_impl]
641 #[must_use = "method returns a new number and does not mutate the original value"]
642 #[stable(feature = "rust1", since = "1.0.0")]
644 pub fn asin(self) -> f32 {
645 unsafe { cmath::asinf(self) }
648 /// Computes the arccosine of a number. Return value is in radians in
649 /// the range [0, pi] or NaN if the number is outside the range
655 /// let f = std::f32::consts::FRAC_PI_4;
657 /// // acos(cos(pi/4))
658 /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
660 /// assert!(abs_difference <= f32::EPSILON);
662 #[rustc_allow_incoherent_impl]
663 #[must_use = "method returns a new number and does not mutate the original value"]
664 #[stable(feature = "rust1", since = "1.0.0")]
666 pub fn acos(self) -> f32 {
667 unsafe { cmath::acosf(self) }
670 /// Computes the arctangent of a number. Return value is in radians in the
671 /// range [-pi/2, pi/2];
679 /// let abs_difference = (f.tan().atan() - 1.0).abs();
681 /// assert!(abs_difference <= f32::EPSILON);
683 #[rustc_allow_incoherent_impl]
684 #[must_use = "method returns a new number and does not mutate the original value"]
685 #[stable(feature = "rust1", since = "1.0.0")]
687 pub fn atan(self) -> f32 {
688 unsafe { cmath::atanf(self) }
691 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
693 /// * `x = 0`, `y = 0`: `0`
694 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
695 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
696 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
701 /// // Positive angles measured counter-clockwise
702 /// // from positive x axis
703 /// // -pi/4 radians (45 deg clockwise)
705 /// let y1 = -3.0f32;
707 /// // 3pi/4 radians (135 deg counter-clockwise)
708 /// let x2 = -3.0f32;
711 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
712 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
714 /// assert!(abs_difference_1 <= f32::EPSILON);
715 /// assert!(abs_difference_2 <= f32::EPSILON);
717 #[rustc_allow_incoherent_impl]
718 #[must_use = "method returns a new number and does not mutate the original value"]
719 #[stable(feature = "rust1", since = "1.0.0")]
721 pub fn atan2(self, other: f32) -> f32 {
722 unsafe { cmath::atan2f(self, other) }
725 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
726 /// `(sin(x), cos(x))`.
731 /// let x = std::f32::consts::FRAC_PI_4;
732 /// let f = x.sin_cos();
734 /// let abs_difference_0 = (f.0 - x.sin()).abs();
735 /// let abs_difference_1 = (f.1 - x.cos()).abs();
737 /// assert!(abs_difference_0 <= f32::EPSILON);
738 /// assert!(abs_difference_1 <= f32::EPSILON);
740 #[rustc_allow_incoherent_impl]
741 #[stable(feature = "rust1", since = "1.0.0")]
743 pub fn sin_cos(self) -> (f32, f32) {
744 (self.sin(), self.cos())
747 /// Returns `e^(self) - 1` in a way that is accurate even if the
748 /// number is close to zero.
753 /// let x = 1e-8_f32;
755 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
756 /// let approx = x + x * x / 2.0;
757 /// let abs_difference = (x.exp_m1() - approx).abs();
759 /// assert!(abs_difference < 1e-10);
761 #[rustc_allow_incoherent_impl]
762 #[must_use = "method returns a new number and does not mutate the original value"]
763 #[stable(feature = "rust1", since = "1.0.0")]
765 pub fn exp_m1(self) -> f32 {
766 unsafe { cmath::expm1f(self) }
769 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
770 /// the operations were performed separately.
775 /// let x = 1e-8_f32;
777 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
778 /// let approx = x - x * x / 2.0;
779 /// let abs_difference = (x.ln_1p() - approx).abs();
781 /// assert!(abs_difference < 1e-10);
783 #[rustc_allow_incoherent_impl]
784 #[must_use = "method returns a new number and does not mutate the original value"]
785 #[stable(feature = "rust1", since = "1.0.0")]
787 pub fn ln_1p(self) -> f32 {
788 unsafe { cmath::log1pf(self) }
791 /// Hyperbolic sine function.
796 /// let e = std::f32::consts::E;
799 /// let f = x.sinh();
800 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
801 /// let g = ((e * e) - 1.0) / (2.0 * e);
802 /// let abs_difference = (f - g).abs();
804 /// assert!(abs_difference <= f32::EPSILON);
806 #[rustc_allow_incoherent_impl]
807 #[must_use = "method returns a new number and does not mutate the original value"]
808 #[stable(feature = "rust1", since = "1.0.0")]
810 pub fn sinh(self) -> f32 {
811 unsafe { cmath::sinhf(self) }
814 /// Hyperbolic cosine function.
819 /// let e = std::f32::consts::E;
821 /// let f = x.cosh();
822 /// // Solving cosh() at 1 gives this result
823 /// let g = ((e * e) + 1.0) / (2.0 * e);
824 /// let abs_difference = (f - g).abs();
827 /// assert!(abs_difference <= f32::EPSILON);
829 #[rustc_allow_incoherent_impl]
830 #[must_use = "method returns a new number and does not mutate the original value"]
831 #[stable(feature = "rust1", since = "1.0.0")]
833 pub fn cosh(self) -> f32 {
834 unsafe { cmath::coshf(self) }
837 /// Hyperbolic tangent function.
842 /// let e = std::f32::consts::E;
845 /// let f = x.tanh();
846 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
847 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
848 /// let abs_difference = (f - g).abs();
850 /// assert!(abs_difference <= f32::EPSILON);
852 #[rustc_allow_incoherent_impl]
853 #[must_use = "method returns a new number and does not mutate the original value"]
854 #[stable(feature = "rust1", since = "1.0.0")]
856 pub fn tanh(self) -> f32 {
857 unsafe { cmath::tanhf(self) }
860 /// Inverse hyperbolic sine function.
866 /// let f = x.sinh().asinh();
868 /// let abs_difference = (f - x).abs();
870 /// assert!(abs_difference <= f32::EPSILON);
872 #[rustc_allow_incoherent_impl]
873 #[must_use = "method returns a new number and does not mutate the original value"]
874 #[stable(feature = "rust1", since = "1.0.0")]
876 pub fn asinh(self) -> f32 {
877 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
880 /// Inverse hyperbolic cosine function.
886 /// let f = x.cosh().acosh();
888 /// let abs_difference = (f - x).abs();
890 /// assert!(abs_difference <= f32::EPSILON);
892 #[rustc_allow_incoherent_impl]
893 #[must_use = "method returns a new number and does not mutate the original value"]
894 #[stable(feature = "rust1", since = "1.0.0")]
896 pub fn acosh(self) -> f32 {
897 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
900 /// Inverse hyperbolic tangent function.
905 /// let e = std::f32::consts::E;
906 /// let f = e.tanh().atanh();
908 /// let abs_difference = (f - e).abs();
910 /// assert!(abs_difference <= 1e-5);
912 #[rustc_allow_incoherent_impl]
913 #[must_use = "method returns a new number and does not mutate the original value"]
914 #[stable(feature = "rust1", since = "1.0.0")]
916 pub fn atanh(self) -> f32 {
917 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()