1 //! Constants for 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 `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) -> f32 {
50 unsafe { intrinsics::floorf32(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) -> f32 {
69 unsafe { intrinsics::ceilf32(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) -> f32 {
91 unsafe { intrinsics::roundf32(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_f32;
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) -> f32 {
113 unsafe { intrinsics::truncf32(self) }
116 /// Returns the fractional part of `self`.
122 /// let y = -3.6_f32;
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 <= f32::EPSILON);
127 /// assert!(abs_difference_y <= f32::EPSILON);
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) -> f32 {
137 /// Computes the absolute value of `self`.
143 /// let y = -3.5_f32;
145 /// let abs_difference_x = (x.abs() - x).abs();
146 /// let abs_difference_y = (y.abs() - (-y)).abs();
148 /// assert!(abs_difference_x <= f32::EPSILON);
149 /// assert!(abs_difference_y <= f32::EPSILON);
151 /// assert!(f32::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) -> f32 {
158 unsafe { intrinsics::fabsf32(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!(f32::NEG_INFINITY.signum(), -1.0);
175 /// assert!(f32::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) -> f32 {
182 if self.is_nan() { Self::NAN } else { 1.0_f32.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_f32);
200 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
201 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
202 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
204 /// assert!(f32::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"]
209 #[stable(feature = "copysign", since = "1.35.0")]
210 pub fn copysign(self, sign: f32) -> f32 {
211 unsafe { intrinsics::copysignf32(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_f32;
227 /// let b = 60.0_f32;
230 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
232 /// assert!(abs_difference <= f32::EPSILON);
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: f32, b: f32) -> f32 {
239 unsafe { intrinsics::fmaf32(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: f32 = 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: f32) -> f32 {
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: f32 = 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!((-f32::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: f32) -> f32 {
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 <= f32::EPSILON);
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) -> f32 {
322 unsafe { intrinsics::powif32(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 <= f32::EPSILON);
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: f32) -> f32 {
340 unsafe { intrinsics::powf32(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_f32;
351 /// let negative = -4.0_f32;
352 /// let negative_zero = -0.0_f32;
354 /// let abs_difference = (positive.sqrt() - 2.0).abs();
356 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
365 unsafe { intrinsics::sqrtf32(self) }
368 /// Returns `e^(self)`, (the exponential function).
373 /// let one = 1.0f32;
375 /// let e = one.exp();
377 /// // ln(e) - 1 == 0
378 /// let abs_difference = (e.ln() - 1.0).abs();
380 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
387 unsafe { intrinsics::expf32(self) }
390 /// Returns `2^(self)`.
398 /// let abs_difference = (f.exp2() - 4.0).abs();
400 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
407 unsafe { intrinsics::exp2f32(self) }
410 /// Returns the natural logarithm of the number.
415 /// let one = 1.0f32;
417 /// let e = one.exp();
419 /// // ln(e) - 1 == 0
420 /// let abs_difference = (e.ln() - 1.0).abs();
422 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
429 unsafe { intrinsics::logf32(self) }
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 five = 5.0f32;
443 /// // log5(5) - 1 == 0
444 /// let abs_difference = (five.log(5.0) - 1.0).abs();
446 /// assert!(abs_difference <= f32::EPSILON);
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: f32) -> f32 {
453 self.ln() / base.ln()
456 /// Returns the base 2 logarithm of the number.
461 /// let two = 2.0f32;
463 /// // log2(2) - 1 == 0
464 /// let abs_difference = (two.log2() - 1.0).abs();
466 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
473 #[cfg(target_os = "android")]
474 return crate::sys::android::log2f32(self);
475 #[cfg(not(target_os = "android"))]
476 return unsafe { intrinsics::log2f32(self) };
479 /// Returns the base 10 logarithm of the number.
484 /// let ten = 10.0f32;
486 /// // log10(10) - 1 == 0
487 /// let abs_difference = (ten.log10() - 1.0).abs();
489 /// assert!(abs_difference <= f32::EPSILON);
491 #[rustc_allow_incoherent_impl]
492 #[must_use = "method returns a new number and does not mutate the original value"]
493 #[stable(feature = "rust1", since = "1.0.0")]
495 pub fn log10(self) -> f32 {
496 unsafe { intrinsics::log10f32(self) }
499 /// The positive difference of two numbers.
501 /// * If `self <= other`: `0:0`
502 /// * Else: `self - other`
510 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
511 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
513 /// assert!(abs_difference_x <= f32::EPSILON);
514 /// assert!(abs_difference_y <= f32::EPSILON);
516 #[rustc_allow_incoherent_impl]
517 #[must_use = "method returns a new number and does not mutate the original value"]
518 #[stable(feature = "rust1", since = "1.0.0")]
522 note = "you probably meant `(self - other).abs()`: \
523 this operation is `(self - other).max(0.0)` \
524 except that `abs_sub` also propagates NaNs (also \
525 known as `fdimf` in C). If you truly need the positive \
526 difference, consider using that expression or the C function \
527 `fdimf`, depending on how you wish to handle NaN (please consider \
528 filing an issue describing your use-case too)."
530 pub fn abs_sub(self, other: f32) -> f32 {
531 unsafe { cmath::fdimf(self, other) }
534 /// Returns the cube root of a number.
541 /// // x^(1/3) - 2 == 0
542 /// let abs_difference = (x.cbrt() - 2.0).abs();
544 /// assert!(abs_difference <= f32::EPSILON);
546 #[rustc_allow_incoherent_impl]
547 #[must_use = "method returns a new number and does not mutate the original value"]
548 #[stable(feature = "rust1", since = "1.0.0")]
550 pub fn cbrt(self) -> f32 {
551 unsafe { cmath::cbrtf(self) }
554 /// Calculates the length of the hypotenuse of a right-angle triangle given
555 /// legs of length `x` and `y`.
563 /// // sqrt(x^2 + y^2)
564 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
566 /// assert!(abs_difference <= f32::EPSILON);
568 #[rustc_allow_incoherent_impl]
569 #[must_use = "method returns a new number and does not mutate the original value"]
570 #[stable(feature = "rust1", since = "1.0.0")]
572 pub fn hypot(self, other: f32) -> f32 {
573 unsafe { cmath::hypotf(self, other) }
576 /// Computes the sine of a number (in radians).
581 /// let x = std::f32::consts::FRAC_PI_2;
583 /// let abs_difference = (x.sin() - 1.0).abs();
585 /// assert!(abs_difference <= f32::EPSILON);
587 #[rustc_allow_incoherent_impl]
588 #[must_use = "method returns a new number and does not mutate the original value"]
589 #[stable(feature = "rust1", since = "1.0.0")]
591 pub fn sin(self) -> f32 {
592 unsafe { intrinsics::sinf32(self) }
595 /// Computes the cosine of a number (in radians).
600 /// let x = 2.0 * std::f32::consts::PI;
602 /// let abs_difference = (x.cos() - 1.0).abs();
604 /// assert!(abs_difference <= f32::EPSILON);
606 #[rustc_allow_incoherent_impl]
607 #[must_use = "method returns a new number and does not mutate the original value"]
608 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn cos(self) -> f32 {
611 unsafe { intrinsics::cosf32(self) }
614 /// Computes the tangent of a number (in radians).
619 /// let x = std::f32::consts::FRAC_PI_4;
620 /// let abs_difference = (x.tan() - 1.0).abs();
622 /// assert!(abs_difference <= f32::EPSILON);
624 #[rustc_allow_incoherent_impl]
625 #[must_use = "method returns a new number and does not mutate the original value"]
626 #[stable(feature = "rust1", since = "1.0.0")]
628 pub fn tan(self) -> f32 {
629 unsafe { cmath::tanf(self) }
632 /// Computes the arcsine of a number. Return value is in radians in
633 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
639 /// let f = std::f32::consts::FRAC_PI_2;
641 /// // asin(sin(pi/2))
642 /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
644 /// assert!(abs_difference <= f32::EPSILON);
646 #[rustc_allow_incoherent_impl]
647 #[must_use = "method returns a new number and does not mutate the original value"]
648 #[stable(feature = "rust1", since = "1.0.0")]
650 pub fn asin(self) -> f32 {
651 unsafe { cmath::asinf(self) }
654 /// Computes the arccosine of a number. Return value is in radians in
655 /// the range [0, pi] or NaN if the number is outside the range
661 /// let f = std::f32::consts::FRAC_PI_4;
663 /// // acos(cos(pi/4))
664 /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
666 /// assert!(abs_difference <= f32::EPSILON);
668 #[rustc_allow_incoherent_impl]
669 #[must_use = "method returns a new number and does not mutate the original value"]
670 #[stable(feature = "rust1", since = "1.0.0")]
672 pub fn acos(self) -> f32 {
673 unsafe { cmath::acosf(self) }
676 /// Computes the arctangent of a number. Return value is in radians in the
677 /// range [-pi/2, pi/2];
685 /// let abs_difference = (f.tan().atan() - 1.0).abs();
687 /// assert!(abs_difference <= f32::EPSILON);
689 #[rustc_allow_incoherent_impl]
690 #[must_use = "method returns a new number and does not mutate the original value"]
691 #[stable(feature = "rust1", since = "1.0.0")]
693 pub fn atan(self) -> f32 {
694 unsafe { cmath::atanf(self) }
697 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
699 /// * `x = 0`, `y = 0`: `0`
700 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
701 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
702 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
707 /// // Positive angles measured counter-clockwise
708 /// // from positive x axis
709 /// // -pi/4 radians (45 deg clockwise)
711 /// let y1 = -3.0f32;
713 /// // 3pi/4 radians (135 deg counter-clockwise)
714 /// let x2 = -3.0f32;
717 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
718 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
720 /// assert!(abs_difference_1 <= f32::EPSILON);
721 /// assert!(abs_difference_2 <= f32::EPSILON);
723 #[rustc_allow_incoherent_impl]
724 #[must_use = "method returns a new number and does not mutate the original value"]
725 #[stable(feature = "rust1", since = "1.0.0")]
727 pub fn atan2(self, other: f32) -> f32 {
728 unsafe { cmath::atan2f(self, other) }
731 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
732 /// `(sin(x), cos(x))`.
737 /// let x = std::f32::consts::FRAC_PI_4;
738 /// let f = x.sin_cos();
740 /// let abs_difference_0 = (f.0 - x.sin()).abs();
741 /// let abs_difference_1 = (f.1 - x.cos()).abs();
743 /// assert!(abs_difference_0 <= f32::EPSILON);
744 /// assert!(abs_difference_1 <= f32::EPSILON);
746 #[rustc_allow_incoherent_impl]
747 #[stable(feature = "rust1", since = "1.0.0")]
749 pub fn sin_cos(self) -> (f32, f32) {
750 (self.sin(), self.cos())
753 /// Returns `e^(self) - 1` in a way that is accurate even if the
754 /// number is close to zero.
759 /// let x = 1e-8_f32;
761 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
762 /// let approx = x + x * x / 2.0;
763 /// let abs_difference = (x.exp_m1() - approx).abs();
765 /// assert!(abs_difference < 1e-10);
767 #[rustc_allow_incoherent_impl]
768 #[must_use = "method returns a new number and does not mutate the original value"]
769 #[stable(feature = "rust1", since = "1.0.0")]
771 pub fn exp_m1(self) -> f32 {
772 unsafe { cmath::expm1f(self) }
775 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
776 /// the operations were performed separately.
781 /// let x = 1e-8_f32;
783 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
784 /// let approx = x - x * x / 2.0;
785 /// let abs_difference = (x.ln_1p() - approx).abs();
787 /// assert!(abs_difference < 1e-10);
789 #[rustc_allow_incoherent_impl]
790 #[must_use = "method returns a new number and does not mutate the original value"]
791 #[stable(feature = "rust1", since = "1.0.0")]
793 pub fn ln_1p(self) -> f32 {
794 unsafe { cmath::log1pf(self) }
797 /// Hyperbolic sine function.
802 /// let e = std::f32::consts::E;
805 /// let f = x.sinh();
806 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
807 /// let g = ((e * e) - 1.0) / (2.0 * e);
808 /// let abs_difference = (f - g).abs();
810 /// assert!(abs_difference <= f32::EPSILON);
812 #[rustc_allow_incoherent_impl]
813 #[must_use = "method returns a new number and does not mutate the original value"]
814 #[stable(feature = "rust1", since = "1.0.0")]
816 pub fn sinh(self) -> f32 {
817 unsafe { cmath::sinhf(self) }
820 /// Hyperbolic cosine function.
825 /// let e = std::f32::consts::E;
827 /// let f = x.cosh();
828 /// // Solving cosh() at 1 gives this result
829 /// let g = ((e * e) + 1.0) / (2.0 * e);
830 /// let abs_difference = (f - g).abs();
833 /// assert!(abs_difference <= f32::EPSILON);
835 #[rustc_allow_incoherent_impl]
836 #[must_use = "method returns a new number and does not mutate the original value"]
837 #[stable(feature = "rust1", since = "1.0.0")]
839 pub fn cosh(self) -> f32 {
840 unsafe { cmath::coshf(self) }
843 /// Hyperbolic tangent function.
848 /// let e = std::f32::consts::E;
851 /// let f = x.tanh();
852 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
853 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
854 /// let abs_difference = (f - g).abs();
856 /// assert!(abs_difference <= f32::EPSILON);
858 #[rustc_allow_incoherent_impl]
859 #[must_use = "method returns a new number and does not mutate the original value"]
860 #[stable(feature = "rust1", since = "1.0.0")]
862 pub fn tanh(self) -> f32 {
863 unsafe { cmath::tanhf(self) }
866 /// Inverse hyperbolic sine function.
872 /// let f = x.sinh().asinh();
874 /// let abs_difference = (f - x).abs();
876 /// assert!(abs_difference <= f32::EPSILON);
878 #[rustc_allow_incoherent_impl]
879 #[must_use = "method returns a new number and does not mutate the original value"]
880 #[stable(feature = "rust1", since = "1.0.0")]
882 pub fn asinh(self) -> f32 {
885 (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
888 /// Inverse hyperbolic cosine function.
894 /// let f = x.cosh().acosh();
896 /// let abs_difference = (f - x).abs();
898 /// assert!(abs_difference <= f32::EPSILON);
900 #[rustc_allow_incoherent_impl]
901 #[must_use = "method returns a new number and does not mutate the original value"]
902 #[stable(feature = "rust1", since = "1.0.0")]
904 pub fn acosh(self) -> f32 {
908 (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
912 /// Inverse hyperbolic tangent function.
917 /// let e = std::f32::consts::E;
918 /// let f = e.tanh().atanh();
920 /// let abs_difference = (f - e).abs();
922 /// assert!(abs_difference <= 1e-5);
924 #[rustc_allow_incoherent_impl]
925 #[must_use = "method returns a new number and does not mutate the original value"]
926 #[stable(feature = "rust1", since = "1.0.0")]
928 pub fn atanh(self) -> f32 {
929 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()