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,
31 #[cfg_attr(bootstrap, lang = "f32_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) -> f32 {
51 unsafe { intrinsics::floorf32(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) -> f32 {
70 unsafe { intrinsics::ceilf32(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) -> f32 {
90 unsafe { intrinsics::roundf32(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_f32;
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) -> f32 {
112 unsafe { intrinsics::truncf32(self) }
115 /// Returns the fractional part of `self`.
121 /// let y = -3.6_f32;
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 <= f32::EPSILON);
126 /// assert!(abs_difference_y <= f32::EPSILON);
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) -> f32 {
136 /// Computes the absolute value of `self`.
142 /// let y = -3.5_f32;
144 /// let abs_difference_x = (x.abs() - x).abs();
145 /// let abs_difference_y = (y.abs() - (-y)).abs();
147 /// assert!(abs_difference_x <= f32::EPSILON);
148 /// assert!(abs_difference_y <= f32::EPSILON);
150 /// assert!(f32::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) -> f32 {
157 unsafe { intrinsics::fabsf32(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!(f32::NEG_INFINITY.signum(), -1.0);
174 /// assert!(f32::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) -> f32 {
181 if self.is_nan() { Self::NAN } else { 1.0_f32.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_f32);
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);
203 /// assert!(f32::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"]
208 #[stable(feature = "copysign", since = "1.35.0")]
209 pub fn copysign(self, sign: f32) -> f32 {
210 unsafe { intrinsics::copysignf32(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_f32;
226 /// let b = 60.0_f32;
229 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
231 /// assert!(abs_difference <= f32::EPSILON);
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: f32, b: f32) -> f32 {
238 unsafe { intrinsics::fmaf32(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: f32 = 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: f32) -> f32 {
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: f32 = 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!((-f32::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: f32) -> f32 {
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 <= f32::EPSILON);
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) -> f32 {
321 unsafe { intrinsics::powif32(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 <= f32::EPSILON);
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: f32) -> f32 {
339 unsafe { intrinsics::powf32(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_f32;
350 /// let negative = -4.0_f32;
351 /// let negative_zero = -0.0_f32;
353 /// let abs_difference = (positive.sqrt() - 2.0).abs();
355 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
364 unsafe { intrinsics::sqrtf32(self) }
367 /// Returns `e^(self)`, (the exponential function).
372 /// let one = 1.0f32;
374 /// let e = one.exp();
376 /// // ln(e) - 1 == 0
377 /// let abs_difference = (e.ln() - 1.0).abs();
379 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
386 unsafe { intrinsics::expf32(self) }
389 /// Returns `2^(self)`.
397 /// let abs_difference = (f.exp2() - 4.0).abs();
399 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
406 unsafe { intrinsics::exp2f32(self) }
409 /// Returns the natural logarithm of the number.
414 /// let one = 1.0f32;
416 /// let e = one.exp();
418 /// // ln(e) - 1 == 0
419 /// let abs_difference = (e.ln() - 1.0).abs();
421 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
428 unsafe { intrinsics::logf32(self) }
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 five = 5.0f32;
442 /// // log5(5) - 1 == 0
443 /// let abs_difference = (five.log(5.0) - 1.0).abs();
445 /// assert!(abs_difference <= f32::EPSILON);
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: f32) -> f32 {
452 self.ln() / base.ln()
455 /// Returns the base 2 logarithm of the number.
460 /// let two = 2.0f32;
462 /// // log2(2) - 1 == 0
463 /// let abs_difference = (two.log2() - 1.0).abs();
465 /// assert!(abs_difference <= f32::EPSILON);
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) -> f32 {
472 #[cfg(target_os = "android")]
473 return crate::sys::android::log2f32(self);
474 #[cfg(not(target_os = "android"))]
475 return unsafe { intrinsics::log2f32(self) };
478 /// Returns the base 10 logarithm of the number.
483 /// let ten = 10.0f32;
485 /// // log10(10) - 1 == 0
486 /// let abs_difference = (ten.log10() - 1.0).abs();
488 /// assert!(abs_difference <= f32::EPSILON);
490 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
491 #[must_use = "method returns a new number and does not mutate the original value"]
492 #[stable(feature = "rust1", since = "1.0.0")]
494 pub fn log10(self) -> f32 {
495 unsafe { intrinsics::log10f32(self) }
498 /// The positive difference of two numbers.
500 /// * If `self <= other`: `0:0`
501 /// * Else: `self - other`
509 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
510 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
512 /// assert!(abs_difference_x <= f32::EPSILON);
513 /// assert!(abs_difference_y <= f32::EPSILON);
515 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
516 #[must_use = "method returns a new number and does not mutate the original value"]
517 #[stable(feature = "rust1", since = "1.0.0")]
521 reason = "you probably meant `(self - other).abs()`: \
522 this operation is `(self - other).max(0.0)` \
523 except that `abs_sub` also propagates NaNs (also \
524 known as `fdimf` in C). If you truly need the positive \
525 difference, consider using that expression or the C function \
526 `fdimf`, depending on how you wish to handle NaN (please consider \
527 filing an issue describing your use-case too)."
529 pub fn abs_sub(self, other: f32) -> f32 {
530 unsafe { cmath::fdimf(self, other) }
533 /// Returns the cube root of a number.
540 /// // x^(1/3) - 2 == 0
541 /// let abs_difference = (x.cbrt() - 2.0).abs();
543 /// assert!(abs_difference <= f32::EPSILON);
545 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
546 #[must_use = "method returns a new number and does not mutate the original value"]
547 #[stable(feature = "rust1", since = "1.0.0")]
549 pub fn cbrt(self) -> f32 {
550 unsafe { cmath::cbrtf(self) }
553 /// Calculates the length of the hypotenuse of a right-angle triangle given
554 /// legs of length `x` and `y`.
562 /// // sqrt(x^2 + y^2)
563 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
565 /// assert!(abs_difference <= f32::EPSILON);
567 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
568 #[must_use = "method returns a new number and does not mutate the original value"]
569 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn hypot(self, other: f32) -> f32 {
572 unsafe { cmath::hypotf(self, other) }
575 /// Computes the sine of a number (in radians).
580 /// let x = std::f32::consts::FRAC_PI_2;
582 /// let abs_difference = (x.sin() - 1.0).abs();
584 /// assert!(abs_difference <= f32::EPSILON);
586 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
587 #[must_use = "method returns a new number and does not mutate the original value"]
588 #[stable(feature = "rust1", since = "1.0.0")]
590 pub fn sin(self) -> f32 {
591 unsafe { intrinsics::sinf32(self) }
594 /// Computes the cosine of a number (in radians).
599 /// let x = 2.0 * std::f32::consts::PI;
601 /// let abs_difference = (x.cos() - 1.0).abs();
603 /// assert!(abs_difference <= f32::EPSILON);
605 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
606 #[must_use = "method returns a new number and does not mutate the original value"]
607 #[stable(feature = "rust1", since = "1.0.0")]
609 pub fn cos(self) -> f32 {
610 unsafe { intrinsics::cosf32(self) }
613 /// Computes the tangent of a number (in radians).
618 /// let x = std::f32::consts::FRAC_PI_4;
619 /// let abs_difference = (x.tan() - 1.0).abs();
621 /// assert!(abs_difference <= f32::EPSILON);
623 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
624 #[must_use = "method returns a new number and does not mutate the original value"]
625 #[stable(feature = "rust1", since = "1.0.0")]
627 pub fn tan(self) -> f32 {
628 unsafe { cmath::tanf(self) }
631 /// Computes the arcsine of a number. Return value is in radians in
632 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
638 /// let f = std::f32::consts::FRAC_PI_2;
640 /// // asin(sin(pi/2))
641 /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
643 /// assert!(abs_difference <= f32::EPSILON);
645 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
646 #[must_use = "method returns a new number and does not mutate the original value"]
647 #[stable(feature = "rust1", since = "1.0.0")]
649 pub fn asin(self) -> f32 {
650 unsafe { cmath::asinf(self) }
653 /// Computes the arccosine of a number. Return value is in radians in
654 /// the range [0, pi] or NaN if the number is outside the range
660 /// let f = std::f32::consts::FRAC_PI_4;
662 /// // acos(cos(pi/4))
663 /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
665 /// assert!(abs_difference <= f32::EPSILON);
667 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
668 #[must_use = "method returns a new number and does not mutate the original value"]
669 #[stable(feature = "rust1", since = "1.0.0")]
671 pub fn acos(self) -> f32 {
672 unsafe { cmath::acosf(self) }
675 /// Computes the arctangent of a number. Return value is in radians in the
676 /// range [-pi/2, pi/2];
684 /// let abs_difference = (f.tan().atan() - 1.0).abs();
686 /// assert!(abs_difference <= f32::EPSILON);
688 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
689 #[must_use = "method returns a new number and does not mutate the original value"]
690 #[stable(feature = "rust1", since = "1.0.0")]
692 pub fn atan(self) -> f32 {
693 unsafe { cmath::atanf(self) }
696 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
698 /// * `x = 0`, `y = 0`: `0`
699 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
700 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
701 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
706 /// // Positive angles measured counter-clockwise
707 /// // from positive x axis
708 /// // -pi/4 radians (45 deg clockwise)
710 /// let y1 = -3.0f32;
712 /// // 3pi/4 radians (135 deg counter-clockwise)
713 /// let x2 = -3.0f32;
716 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
717 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
719 /// assert!(abs_difference_1 <= f32::EPSILON);
720 /// assert!(abs_difference_2 <= f32::EPSILON);
722 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
723 #[must_use = "method returns a new number and does not mutate the original value"]
724 #[stable(feature = "rust1", since = "1.0.0")]
726 pub fn atan2(self, other: f32) -> f32 {
727 unsafe { cmath::atan2f(self, other) }
730 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
731 /// `(sin(x), cos(x))`.
736 /// let x = std::f32::consts::FRAC_PI_4;
737 /// let f = x.sin_cos();
739 /// let abs_difference_0 = (f.0 - x.sin()).abs();
740 /// let abs_difference_1 = (f.1 - x.cos()).abs();
742 /// assert!(abs_difference_0 <= f32::EPSILON);
743 /// assert!(abs_difference_1 <= f32::EPSILON);
745 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
746 #[stable(feature = "rust1", since = "1.0.0")]
748 pub fn sin_cos(self) -> (f32, f32) {
749 (self.sin(), self.cos())
752 /// Returns `e^(self) - 1` in a way that is accurate even if the
753 /// number is close to zero.
758 /// let x = 1e-8_f32;
760 /// // for very small x, e^x is approximately 1 + x + x^2 / 2
761 /// let approx = x + x * x / 2.0;
762 /// let abs_difference = (x.exp_m1() - approx).abs();
764 /// assert!(abs_difference < 1e-10);
766 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
767 #[must_use = "method returns a new number and does not mutate the original value"]
768 #[stable(feature = "rust1", since = "1.0.0")]
770 pub fn exp_m1(self) -> f32 {
771 unsafe { cmath::expm1f(self) }
774 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
775 /// the operations were performed separately.
780 /// let x = 1e-8_f32;
782 /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
783 /// let approx = x - x * x / 2.0;
784 /// let abs_difference = (x.ln_1p() - approx).abs();
786 /// assert!(abs_difference < 1e-10);
788 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
789 #[must_use = "method returns a new number and does not mutate the original value"]
790 #[stable(feature = "rust1", since = "1.0.0")]
792 pub fn ln_1p(self) -> f32 {
793 unsafe { cmath::log1pf(self) }
796 /// Hyperbolic sine function.
801 /// let e = std::f32::consts::E;
804 /// let f = x.sinh();
805 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
806 /// let g = ((e * e) - 1.0) / (2.0 * e);
807 /// let abs_difference = (f - g).abs();
809 /// assert!(abs_difference <= f32::EPSILON);
811 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
812 #[must_use = "method returns a new number and does not mutate the original value"]
813 #[stable(feature = "rust1", since = "1.0.0")]
815 pub fn sinh(self) -> f32 {
816 unsafe { cmath::sinhf(self) }
819 /// Hyperbolic cosine function.
824 /// let e = std::f32::consts::E;
826 /// let f = x.cosh();
827 /// // Solving cosh() at 1 gives this result
828 /// let g = ((e * e) + 1.0) / (2.0 * e);
829 /// let abs_difference = (f - g).abs();
832 /// assert!(abs_difference <= f32::EPSILON);
834 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
835 #[must_use = "method returns a new number and does not mutate the original value"]
836 #[stable(feature = "rust1", since = "1.0.0")]
838 pub fn cosh(self) -> f32 {
839 unsafe { cmath::coshf(self) }
842 /// Hyperbolic tangent function.
847 /// let e = std::f32::consts::E;
850 /// let f = x.tanh();
851 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
852 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
853 /// let abs_difference = (f - g).abs();
855 /// assert!(abs_difference <= f32::EPSILON);
857 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
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 tanh(self) -> f32 {
862 unsafe { cmath::tanhf(self) }
865 /// Inverse hyperbolic sine function.
871 /// let f = x.sinh().asinh();
873 /// let abs_difference = (f - x).abs();
875 /// assert!(abs_difference <= f32::EPSILON);
877 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
878 #[must_use = "method returns a new number and does not mutate the original value"]
879 #[stable(feature = "rust1", since = "1.0.0")]
881 pub fn asinh(self) -> f32 {
882 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
885 /// Inverse hyperbolic cosine function.
891 /// let f = x.cosh().acosh();
893 /// let abs_difference = (f - x).abs();
895 /// assert!(abs_difference <= f32::EPSILON);
897 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
898 #[must_use = "method returns a new number and does not mutate the original value"]
899 #[stable(feature = "rust1", since = "1.0.0")]
901 pub fn acosh(self) -> f32 {
902 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
905 /// Inverse hyperbolic tangent function.
910 /// let e = std::f32::consts::E;
911 /// let f = e.tanh().atanh();
913 /// let abs_difference = (f - e).abs();
915 /// assert!(abs_difference <= 1e-5);
917 #[cfg_attr(not(bootstrap), rustc_allow_incoherent_impl)]
918 #[must_use = "method returns a new number and does not mutate the original value"]
919 #[stable(feature = "rust1", since = "1.0.0")]
921 pub fn atanh(self) -> f32 {
922 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()