1 //! This module provides constants which are specific to the implementation
2 //! of the `f32` floating point data type.
4 //! *[See also the `f32` primitive type](../../std/primitive.f32.html).*
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
8 #![stable(feature = "rust1", since = "1.0.0")]
9 #![allow(missing_docs)]
12 use crate::intrinsics
;
14 use crate::sys
::cmath
;
16 #[stable(feature = "rust1", since = "1.0.0")]
17 pub use core
::f32::consts
;
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub use core
::f32::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX}
;
20 #[stable(feature = "rust1", since = "1.0.0")]
21 pub use core
::f32::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY}
;
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use core
::f32::{MAX, MIN, MIN_POSITIVE}
;
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use core
::f32::{MAX_EXP, MIN_10_EXP, MIN_EXP}
;
28 #[lang = "f32_runtime"]
30 /// Returns the largest integer less than or equal to a number.
39 /// assert_eq!(f.floor(), 3.0);
40 /// assert_eq!(g.floor(), 3.0);
41 /// assert_eq!(h.floor(), -4.0);
43 #[must_use = "method returns a new number and does not mutate the original value"]
44 #[stable(feature = "rust1", since = "1.0.0")]
46 pub fn floor(self) -> f32 {
47 // On MSVC LLVM will lower many math intrinsics to a call to the
48 // corresponding function. On MSVC, however, many of these functions
49 // aren't actually available as symbols to call, but rather they are all
50 // `static inline` functions in header files. This means that from a C
51 // perspective it's "compatible", but not so much from an ABI
52 // perspective (which we're worried about).
54 // The inline header functions always just cast to a f64 and do their
55 // operation, so we do that here as well, but only for MSVC targets.
57 // Note that there are many MSVC-specific float operations which
58 // redirect to this comment, so `floorf` is just one case of a missing
59 // function on MSVC, but there are many others elsewhere.
60 #[cfg(target_env = "msvc")]
61 return (self as f64).floor() as f32;
62 #[cfg(not(target_env = "msvc"))]
63 return unsafe { intrinsics::floorf32(self) }
;
66 /// Returns the smallest integer greater than or equal to a number.
74 /// assert_eq!(f.ceil(), 4.0);
75 /// assert_eq!(g.ceil(), 4.0);
77 #[must_use = "method returns a new number and does not mutate the original value"]
78 #[stable(feature = "rust1", since = "1.0.0")]
80 pub fn ceil(self) -> f32 {
81 // see notes above in `floor`
82 #[cfg(target_env = "msvc")]
83 return (self as f64).ceil() as f32;
84 #[cfg(not(target_env = "msvc"))]
85 return unsafe { intrinsics::ceilf32(self) }
;
88 /// Returns the nearest integer to a number. Round half-way cases away from
97 /// assert_eq!(f.round(), 3.0);
98 /// assert_eq!(g.round(), -3.0);
100 #[must_use = "method returns a new number and does not mutate the original value"]
101 #[stable(feature = "rust1", since = "1.0.0")]
103 pub fn round(self) -> f32 {
104 unsafe { intrinsics::roundf32(self) }
107 /// Returns the integer part of a number.
114 /// let h = -3.7_f32;
116 /// assert_eq!(f.trunc(), 3.0);
117 /// assert_eq!(g.trunc(), 3.0);
118 /// assert_eq!(h.trunc(), -3.0);
120 #[must_use = "method returns a new number and does not mutate the original value"]
121 #[stable(feature = "rust1", since = "1.0.0")]
123 pub fn trunc(self) -> f32 {
124 unsafe { intrinsics::truncf32(self) }
127 /// Returns the fractional part of a number.
135 /// let y = -3.5_f32;
136 /// let abs_difference_x = (x.fract() - 0.5).abs();
137 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
139 /// assert!(abs_difference_x <= f32::EPSILON);
140 /// assert!(abs_difference_y <= f32::EPSILON);
142 #[must_use = "method returns a new number and does not mutate the original value"]
143 #[stable(feature = "rust1", since = "1.0.0")]
145 pub fn fract(self) -> f32 {
149 /// Computes the absolute value of `self`. Returns `NAN` if the
158 /// let y = -3.5_f32;
160 /// let abs_difference_x = (x.abs() - x).abs();
161 /// let abs_difference_y = (y.abs() - (-y)).abs();
163 /// assert!(abs_difference_x <= f32::EPSILON);
164 /// assert!(abs_difference_y <= f32::EPSILON);
166 /// assert!(f32::NAN.abs().is_nan());
168 #[must_use = "method returns a new number and does not mutate the original value"]
169 #[stable(feature = "rust1", since = "1.0.0")]
171 pub fn abs(self) -> f32 {
172 unsafe { intrinsics::fabsf32(self) }
175 /// Returns a number that represents the sign of `self`.
177 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
178 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
179 /// - `NAN` if the number is `NAN`
188 /// assert_eq!(f.signum(), 1.0);
189 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
191 /// assert!(f32::NAN.signum().is_nan());
193 #[must_use = "method returns a new number and does not mutate the original value"]
194 #[stable(feature = "rust1", since = "1.0.0")]
196 pub fn signum(self) -> f32 {
197 if self.is_nan() { NAN }
else { 1.0_f32.copysign(self) }
200 /// Returns a number composed of the magnitude of `self` and the sign of
203 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
204 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
205 /// `sign` is returned.
214 /// assert_eq!(f.copysign(0.42), 3.5_f32);
215 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
216 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
217 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
219 /// assert!(f32::NAN.copysign(1.0).is_nan());
221 #[must_use = "method returns a new number and does not mutate the original value"]
223 #[stable(feature = "copysign", since = "1.35.0")]
224 pub fn copysign(self, sign
: f32) -> f32 {
225 unsafe { intrinsics::copysignf32(self, sign) }
228 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
229 /// error, yielding a more accurate result than an unfused multiply-add.
231 /// Using `mul_add` can be more performant than an unfused multiply-add if
232 /// the target architecture has a dedicated `fma` CPU instruction.
239 /// let m = 10.0_f32;
241 /// let b = 60.0_f32;
244 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
246 /// assert!(abs_difference <= f32::EPSILON);
248 #[must_use = "method returns a new number and does not mutate the original value"]
249 #[stable(feature = "rust1", since = "1.0.0")]
251 pub fn mul_add(self, a
: f32, b
: f32) -> f32 {
252 unsafe { intrinsics::fmaf32(self, a, b) }
255 /// Calculates Euclidean division, the matching method for `rem_euclid`.
257 /// This computes the integer `n` such that
258 /// `self = n * rhs + self.rem_euclid(rhs)`.
259 /// In other words, the result is `self / rhs` rounded to the integer `n`
260 /// such that `self >= n * rhs`.
265 /// let a: f32 = 7.0;
267 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
268 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
269 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
270 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
272 #[must_use = "method returns a new number and does not mutate the original value"]
274 #[stable(feature = "euclidean_division", since = "1.38.0")]
275 pub fn div_euclid(self, rhs
: f32) -> f32 {
276 let q
= (self / rhs
).trunc();
277 if self % rhs
< 0.0 {
278 return if rhs
> 0.0 { q - 1.0 }
else { q + 1.0 }
;
283 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
285 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
286 /// most cases. However, due to a floating point round-off error it can
287 /// result in `r == rhs.abs()`, violating the mathematical definition, if
288 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
289 /// This result is not an element of the function's codomain, but it is the
290 /// closest floating point number in the real numbers and thus fulfills the
291 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
297 /// let a: f32 = 7.0;
299 /// assert_eq!(a.rem_euclid(b), 3.0);
300 /// assert_eq!((-a).rem_euclid(b), 1.0);
301 /// assert_eq!(a.rem_euclid(-b), 3.0);
302 /// assert_eq!((-a).rem_euclid(-b), 1.0);
303 /// // limitation due to round-off error
304 /// assert!((-std::f32::EPSILON).rem_euclid(3.0) != 0.0);
306 #[must_use = "method returns a new number and does not mutate the original value"]
308 #[stable(feature = "euclidean_division", since = "1.38.0")]
309 pub fn rem_euclid(self, rhs
: f32) -> f32 {
311 if r
< 0.0 { r + rhs.abs() }
else { r }
314 /// Raises a number to an integer power.
316 /// Using this function is generally faster than using `powf`
324 /// let abs_difference = (x.powi(2) - (x * x)).abs();
326 /// assert!(abs_difference <= f32::EPSILON);
328 #[must_use = "method returns a new number and does not mutate the original value"]
329 #[stable(feature = "rust1", since = "1.0.0")]
331 pub fn powi(self, n
: i32) -> f32 {
332 unsafe { intrinsics::powif32(self, n) }
335 /// Raises a number to a floating point power.
343 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
345 /// assert!(abs_difference <= f32::EPSILON);
347 #[must_use = "method returns a new number and does not mutate the original value"]
348 #[stable(feature = "rust1", since = "1.0.0")]
350 pub fn powf(self, n
: f32) -> f32 {
351 // see notes above in `floor`
352 #[cfg(target_env = "msvc")]
353 return (self as f64).powf(n
as f64) as f32;
354 #[cfg(not(target_env = "msvc"))]
355 return unsafe { intrinsics::powf32(self, n) }
;
358 /// Takes the square root of a number.
360 /// Returns NaN if `self` is a negative number.
367 /// let positive = 4.0_f32;
368 /// let negative = -4.0_f32;
370 /// let abs_difference = (positive.sqrt() - 2.0).abs();
372 /// assert!(abs_difference <= f32::EPSILON);
373 /// assert!(negative.sqrt().is_nan());
375 #[must_use = "method returns a new number and does not mutate the original value"]
376 #[stable(feature = "rust1", since = "1.0.0")]
378 pub fn sqrt(self) -> f32 {
379 if self < 0.0 { NAN }
else { unsafe { intrinsics::sqrtf32(self) }
}
382 /// Returns `e^(self)`, (the exponential function).
389 /// let one = 1.0f32;
391 /// let e = one.exp();
393 /// // ln(e) - 1 == 0
394 /// let abs_difference = (e.ln() - 1.0).abs();
396 /// assert!(abs_difference <= f32::EPSILON);
398 #[must_use = "method returns a new number and does not mutate the original value"]
399 #[stable(feature = "rust1", since = "1.0.0")]
401 pub fn exp(self) -> f32 {
402 // see notes above in `floor`
403 #[cfg(target_env = "msvc")]
404 return (self as f64).exp() as f32;
405 #[cfg(not(target_env = "msvc"))]
406 return unsafe { intrinsics::expf32(self) }
;
409 /// Returns `2^(self)`.
419 /// let abs_difference = (f.exp2() - 4.0).abs();
421 /// assert!(abs_difference <= f32::EPSILON);
423 #[must_use = "method returns a new number and does not mutate the original value"]
424 #[stable(feature = "rust1", since = "1.0.0")]
426 pub fn exp2(self) -> f32 {
427 unsafe { intrinsics::exp2f32(self) }
430 /// Returns the natural logarithm of the number.
437 /// let one = 1.0f32;
439 /// let e = one.exp();
441 /// // ln(e) - 1 == 0
442 /// let abs_difference = (e.ln() - 1.0).abs();
444 /// assert!(abs_difference <= f32::EPSILON);
446 #[must_use = "method returns a new number and does not mutate the original value"]
447 #[stable(feature = "rust1", since = "1.0.0")]
449 pub fn ln(self) -> f32 {
450 // see notes above in `floor`
451 #[cfg(target_env = "msvc")]
452 return (self as f64).ln() as f32;
453 #[cfg(not(target_env = "msvc"))]
454 return unsafe { intrinsics::logf32(self) }
;
457 /// Returns the logarithm of the number with respect to an arbitrary base.
459 /// The result may not be correctly rounded owing to implementation details;
460 /// `self.log2()` can produce more accurate results for base 2, and
461 /// `self.log10()` can produce more accurate results for base 10.
468 /// let five = 5.0f32;
470 /// // log5(5) - 1 == 0
471 /// let abs_difference = (five.log(5.0) - 1.0).abs();
473 /// assert!(abs_difference <= f32::EPSILON);
475 #[must_use = "method returns a new number and does not mutate the original value"]
476 #[stable(feature = "rust1", since = "1.0.0")]
478 pub fn log(self, base
: f32) -> f32 {
479 self.ln() / base
.ln()
482 /// Returns the base 2 logarithm of the number.
489 /// let two = 2.0f32;
491 /// // log2(2) - 1 == 0
492 /// let abs_difference = (two.log2() - 1.0).abs();
494 /// assert!(abs_difference <= f32::EPSILON);
496 #[must_use = "method returns a new number and does not mutate the original value"]
497 #[stable(feature = "rust1", since = "1.0.0")]
499 pub fn log2(self) -> f32 {
500 #[cfg(target_os = "android")]
501 return crate::sys
::android
::log2f32(self);
502 #[cfg(not(target_os = "android"))]
503 return unsafe { intrinsics::log2f32(self) }
;
506 /// Returns the base 10 logarithm of the number.
513 /// let ten = 10.0f32;
515 /// // log10(10) - 1 == 0
516 /// let abs_difference = (ten.log10() - 1.0).abs();
518 /// assert!(abs_difference <= f32::EPSILON);
520 #[must_use = "method returns a new number and does not mutate the original value"]
521 #[stable(feature = "rust1", since = "1.0.0")]
523 pub fn log10(self) -> f32 {
524 // see notes above in `floor`
525 #[cfg(target_env = "msvc")]
526 return (self as f64).log10() as f32;
527 #[cfg(not(target_env = "msvc"))]
528 return unsafe { intrinsics::log10f32(self) }
;
531 /// The positive difference of two numbers.
533 /// * If `self <= other`: `0:0`
534 /// * Else: `self - other`
544 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
545 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
547 /// assert!(abs_difference_x <= f32::EPSILON);
548 /// assert!(abs_difference_y <= f32::EPSILON);
550 #[must_use = "method returns a new number and does not mutate the original value"]
551 #[stable(feature = "rust1", since = "1.0.0")]
555 reason
= "you probably meant `(self - other).abs()`: \
556 this operation is `(self - other).max(0.0)` \
557 except that `abs_sub` also propagates NaNs (also \
558 known as `fdimf` in C). If you truly need the positive \
559 difference, consider using that expression or the C function \
560 `fdimf`, depending on how you wish to handle NaN (please consider \
561 filing an issue describing your use-case too)."
563 pub fn abs_sub(self, other
: f32) -> f32 {
564 unsafe { cmath::fdimf(self, other) }
567 /// Takes the cubic root of a number.
576 /// // x^(1/3) - 2 == 0
577 /// let abs_difference = (x.cbrt() - 2.0).abs();
579 /// assert!(abs_difference <= f32::EPSILON);
581 #[must_use = "method returns a new number and does not mutate the original value"]
582 #[stable(feature = "rust1", since = "1.0.0")]
584 pub fn cbrt(self) -> f32 {
585 unsafe { cmath::cbrtf(self) }
588 /// Calculates the length of the hypotenuse of a right-angle triangle given
589 /// legs of length `x` and `y`.
599 /// // sqrt(x^2 + y^2)
600 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
602 /// assert!(abs_difference <= f32::EPSILON);
604 #[must_use = "method returns a new number and does not mutate the original value"]
605 #[stable(feature = "rust1", since = "1.0.0")]
607 pub fn hypot(self, other
: f32) -> f32 {
608 unsafe { cmath::hypotf(self, other) }
611 /// Computes the sine of a number (in radians).
618 /// let x = f32::consts::FRAC_PI_2;
620 /// let abs_difference = (x.sin() - 1.0).abs();
622 /// assert!(abs_difference <= f32::EPSILON);
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 sin(self) -> f32 {
628 // see notes in `core::f32::Float::floor`
629 #[cfg(target_env = "msvc")]
630 return (self as f64).sin() as f32;
631 #[cfg(not(target_env = "msvc"))]
632 return unsafe { intrinsics::sinf32(self) }
;
635 /// Computes the cosine of a number (in radians).
642 /// let x = 2.0 * f32::consts::PI;
644 /// let abs_difference = (x.cos() - 1.0).abs();
646 /// assert!(abs_difference <= f32::EPSILON);
648 #[must_use = "method returns a new number and does not mutate the original value"]
649 #[stable(feature = "rust1", since = "1.0.0")]
651 pub fn cos(self) -> f32 {
652 // see notes in `core::f32::Float::floor`
653 #[cfg(target_env = "msvc")]
654 return (self as f64).cos() as f32;
655 #[cfg(not(target_env = "msvc"))]
656 return unsafe { intrinsics::cosf32(self) }
;
659 /// Computes the tangent of a number (in radians).
666 /// let x = f32::consts::FRAC_PI_4;
667 /// let abs_difference = (x.tan() - 1.0).abs();
669 /// assert!(abs_difference <= f32::EPSILON);
671 #[must_use = "method returns a new number and does not mutate the original value"]
672 #[stable(feature = "rust1", since = "1.0.0")]
674 pub fn tan(self) -> f32 {
675 unsafe { cmath::tanf(self) }
678 /// Computes the arcsine of a number. Return value is in radians in
679 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
687 /// let f = f32::consts::FRAC_PI_2;
689 /// // asin(sin(pi/2))
690 /// let abs_difference = (f.sin().asin() - f32::consts::FRAC_PI_2).abs();
692 /// assert!(abs_difference <= f32::EPSILON);
694 #[must_use = "method returns a new number and does not mutate the original value"]
695 #[stable(feature = "rust1", since = "1.0.0")]
697 pub fn asin(self) -> f32 {
698 unsafe { cmath::asinf(self) }
701 /// Computes the arccosine of a number. Return value is in radians in
702 /// the range [0, pi] or NaN if the number is outside the range
710 /// let f = f32::consts::FRAC_PI_4;
712 /// // acos(cos(pi/4))
713 /// let abs_difference = (f.cos().acos() - f32::consts::FRAC_PI_4).abs();
715 /// assert!(abs_difference <= f32::EPSILON);
717 #[must_use = "method returns a new number and does not mutate the original value"]
718 #[stable(feature = "rust1", since = "1.0.0")]
720 pub fn acos(self) -> f32 {
721 unsafe { cmath::acosf(self) }
724 /// Computes the arctangent of a number. Return value is in radians in the
725 /// range [-pi/2, pi/2];
735 /// let abs_difference = (f.tan().atan() - 1.0).abs();
737 /// assert!(abs_difference <= f32::EPSILON);
739 #[must_use = "method returns a new number and does not mutate the original value"]
740 #[stable(feature = "rust1", since = "1.0.0")]
742 pub fn atan(self) -> f32 {
743 unsafe { cmath::atanf(self) }
746 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
748 /// * `x = 0`, `y = 0`: `0`
749 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
750 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
751 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
758 /// // Positive angles measured counter-clockwise
759 /// // from positive x axis
760 /// // -pi/4 radians (45 deg clockwise)
762 /// let y1 = -3.0f32;
764 /// // 3pi/4 radians (135 deg counter-clockwise)
765 /// let x2 = -3.0f32;
768 /// let abs_difference_1 = (y1.atan2(x1) - (-f32::consts::FRAC_PI_4)).abs();
769 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * f32::consts::FRAC_PI_4)).abs();
771 /// assert!(abs_difference_1 <= f32::EPSILON);
772 /// assert!(abs_difference_2 <= f32::EPSILON);
774 #[must_use = "method returns a new number and does not mutate the original value"]
775 #[stable(feature = "rust1", since = "1.0.0")]
777 pub fn atan2(self, other
: f32) -> f32 {
778 unsafe { cmath::atan2f(self, other) }
781 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
782 /// `(sin(x), cos(x))`.
789 /// let x = f32::consts::FRAC_PI_4;
790 /// let f = x.sin_cos();
792 /// let abs_difference_0 = (f.0 - x.sin()).abs();
793 /// let abs_difference_1 = (f.1 - x.cos()).abs();
795 /// assert!(abs_difference_0 <= f32::EPSILON);
796 /// assert!(abs_difference_1 <= f32::EPSILON);
798 #[stable(feature = "rust1", since = "1.0.0")]
800 pub fn sin_cos(self) -> (f32, f32) {
801 (self.sin(), self.cos())
804 /// Returns `e^(self) - 1` in a way that is accurate even if the
805 /// number is close to zero.
815 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
817 /// assert!(abs_difference <= f32::EPSILON);
819 #[must_use = "method returns a new number and does not mutate the original value"]
820 #[stable(feature = "rust1", since = "1.0.0")]
822 pub fn exp_m1(self) -> f32 {
823 unsafe { cmath::expm1f(self) }
826 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
827 /// the operations were performed separately.
834 /// let x = f32::consts::E - 1.0;
836 /// // ln(1 + (e - 1)) == ln(e) == 1
837 /// let abs_difference = (x.ln_1p() - 1.0).abs();
839 /// assert!(abs_difference <= f32::EPSILON);
841 #[must_use = "method returns a new number and does not mutate the original value"]
842 #[stable(feature = "rust1", since = "1.0.0")]
844 pub fn ln_1p(self) -> f32 {
845 unsafe { cmath::log1pf(self) }
848 /// Hyperbolic sine function.
855 /// let e = f32::consts::E;
858 /// let f = x.sinh();
859 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
860 /// let g = ((e * e) - 1.0) / (2.0 * e);
861 /// let abs_difference = (f - g).abs();
863 /// assert!(abs_difference <= f32::EPSILON);
865 #[must_use = "method returns a new number and does not mutate the original value"]
866 #[stable(feature = "rust1", since = "1.0.0")]
868 pub fn sinh(self) -> f32 {
869 unsafe { cmath::sinhf(self) }
872 /// Hyperbolic cosine function.
879 /// let e = f32::consts::E;
881 /// let f = x.cosh();
882 /// // Solving cosh() at 1 gives this result
883 /// let g = ((e * e) + 1.0) / (2.0 * e);
884 /// let abs_difference = (f - g).abs();
887 /// assert!(abs_difference <= f32::EPSILON);
889 #[must_use = "method returns a new number and does not mutate the original value"]
890 #[stable(feature = "rust1", since = "1.0.0")]
892 pub fn cosh(self) -> f32 {
893 unsafe { cmath::coshf(self) }
896 /// Hyperbolic tangent function.
903 /// let e = f32::consts::E;
906 /// let f = x.tanh();
907 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
908 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
909 /// let abs_difference = (f - g).abs();
911 /// assert!(abs_difference <= f32::EPSILON);
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 tanh(self) -> f32 {
917 unsafe { cmath::tanhf(self) }
920 /// Inverse hyperbolic sine function.
928 /// let f = x.sinh().asinh();
930 /// let abs_difference = (f - x).abs();
932 /// assert!(abs_difference <= f32::EPSILON);
934 #[must_use = "method returns a new number and does not mutate the original value"]
935 #[stable(feature = "rust1", since = "1.0.0")]
937 pub fn asinh(self) -> f32 {
938 if self == NEG_INFINITY
{
941 (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
945 /// Inverse hyperbolic cosine function.
953 /// let f = x.cosh().acosh();
955 /// let abs_difference = (f - x).abs();
957 /// assert!(abs_difference <= f32::EPSILON);
959 #[must_use = "method returns a new number and does not mutate the original value"]
960 #[stable(feature = "rust1", since = "1.0.0")]
962 pub fn acosh(self) -> f32 {
963 if self < 1.0 { crate::f32::NAN }
else { (self + ((self * self) - 1.0).sqrt()).ln() }
966 /// Inverse hyperbolic tangent function.
973 /// let e = f32::consts::E;
974 /// let f = e.tanh().atanh();
976 /// let abs_difference = (f - e).abs();
978 /// assert!(abs_difference <= 1e-5);
980 #[must_use = "method returns a new number and does not mutate the original value"]
981 #[stable(feature = "rust1", since = "1.0.0")]
983 pub fn atanh(self) -> f32 {
984 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
987 /// Restrict a value to a certain interval unless it is NaN.
989 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
990 /// less than `min`. Otherwise this returns `self`.
992 /// Not that this function returns NaN if the initial value was NaN as
997 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
1002 /// #![feature(clamp)]
1003 /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
1004 /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
1005 /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
1006 /// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan());
1008 #[must_use = "method returns a new number and does not mutate the original value"]
1009 #[unstable(feature = "clamp", issue = "44095")]
1011 pub fn clamp(self, min
: f32, max
: f32) -> f32 {
1012 assert
!(min
<= max
);
1028 use crate::num
::FpCategory
as Fp
;
1033 test_num(10f32, 2f32);
1038 assert_eq
!(NAN
.min(2.0), 2.0);
1039 assert_eq
!(2.0f32.min(NAN
), 2.0);
1044 assert_eq
!(NAN
.max(2.0), 2.0);
1045 assert_eq
!(2.0f32.max(NAN
), 2.0);
1050 let nan
: f32 = f32::NAN
;
1051 assert
!(nan
.is_nan());
1052 assert
!(!nan
.is_infinite());
1053 assert
!(!nan
.is_finite());
1054 assert
!(!nan
.is_normal());
1055 assert
!(nan
.is_sign_positive());
1056 assert
!(!nan
.is_sign_negative());
1057 assert_eq
!(Fp
::Nan
, nan
.classify());
1061 fn test_infinity() {
1062 let inf
: f32 = f32::INFINITY
;
1063 assert
!(inf
.is_infinite());
1064 assert
!(!inf
.is_finite());
1065 assert
!(inf
.is_sign_positive());
1066 assert
!(!inf
.is_sign_negative());
1067 assert
!(!inf
.is_nan());
1068 assert
!(!inf
.is_normal());
1069 assert_eq
!(Fp
::Infinite
, inf
.classify());
1073 fn test_neg_infinity() {
1074 let neg_inf
: f32 = f32::NEG_INFINITY
;
1075 assert
!(neg_inf
.is_infinite());
1076 assert
!(!neg_inf
.is_finite());
1077 assert
!(!neg_inf
.is_sign_positive());
1078 assert
!(neg_inf
.is_sign_negative());
1079 assert
!(!neg_inf
.is_nan());
1080 assert
!(!neg_inf
.is_normal());
1081 assert_eq
!(Fp
::Infinite
, neg_inf
.classify());
1086 let zero
: f32 = 0.0f32;
1087 assert_eq
!(0.0, zero
);
1088 assert
!(!zero
.is_infinite());
1089 assert
!(zero
.is_finite());
1090 assert
!(zero
.is_sign_positive());
1091 assert
!(!zero
.is_sign_negative());
1092 assert
!(!zero
.is_nan());
1093 assert
!(!zero
.is_normal());
1094 assert_eq
!(Fp
::Zero
, zero
.classify());
1098 fn test_neg_zero() {
1099 let neg_zero
: f32 = -0.0;
1100 assert_eq
!(0.0, neg_zero
);
1101 assert
!(!neg_zero
.is_infinite());
1102 assert
!(neg_zero
.is_finite());
1103 assert
!(!neg_zero
.is_sign_positive());
1104 assert
!(neg_zero
.is_sign_negative());
1105 assert
!(!neg_zero
.is_nan());
1106 assert
!(!neg_zero
.is_normal());
1107 assert_eq
!(Fp
::Zero
, neg_zero
.classify());
1112 let one
: f32 = 1.0f32;
1113 assert_eq
!(1.0, one
);
1114 assert
!(!one
.is_infinite());
1115 assert
!(one
.is_finite());
1116 assert
!(one
.is_sign_positive());
1117 assert
!(!one
.is_sign_negative());
1118 assert
!(!one
.is_nan());
1119 assert
!(one
.is_normal());
1120 assert_eq
!(Fp
::Normal
, one
.classify());
1125 let nan
: f32 = f32::NAN
;
1126 let inf
: f32 = f32::INFINITY
;
1127 let neg_inf
: f32 = f32::NEG_INFINITY
;
1128 assert
!(nan
.is_nan());
1129 assert
!(!0.0f32.is_nan());
1130 assert
!(!5.3f32.is_nan());
1131 assert
!(!(-10.732f32).is_nan());
1132 assert
!(!inf
.is_nan());
1133 assert
!(!neg_inf
.is_nan());
1137 fn test_is_infinite() {
1138 let nan
: f32 = f32::NAN
;
1139 let inf
: f32 = f32::INFINITY
;
1140 let neg_inf
: f32 = f32::NEG_INFINITY
;
1141 assert
!(!nan
.is_infinite());
1142 assert
!(inf
.is_infinite());
1143 assert
!(neg_inf
.is_infinite());
1144 assert
!(!0.0f32.is_infinite());
1145 assert
!(!42.8f32.is_infinite());
1146 assert
!(!(-109.2f32).is_infinite());
1150 fn test_is_finite() {
1151 let nan
: f32 = f32::NAN
;
1152 let inf
: f32 = f32::INFINITY
;
1153 let neg_inf
: f32 = f32::NEG_INFINITY
;
1154 assert
!(!nan
.is_finite());
1155 assert
!(!inf
.is_finite());
1156 assert
!(!neg_inf
.is_finite());
1157 assert
!(0.0f32.is_finite());
1158 assert
!(42.8f32.is_finite());
1159 assert
!((-109.2f32).is_finite());
1163 fn test_is_normal() {
1164 let nan
: f32 = f32::NAN
;
1165 let inf
: f32 = f32::INFINITY
;
1166 let neg_inf
: f32 = f32::NEG_INFINITY
;
1167 let zero
: f32 = 0.0f32;
1168 let neg_zero
: f32 = -0.0;
1169 assert
!(!nan
.is_normal());
1170 assert
!(!inf
.is_normal());
1171 assert
!(!neg_inf
.is_normal());
1172 assert
!(!zero
.is_normal());
1173 assert
!(!neg_zero
.is_normal());
1174 assert
!(1f32.is_normal());
1175 assert
!(1e
-37f32.is_normal());
1176 assert
!(!1e
-38f32.is_normal());
1180 fn test_classify() {
1181 let nan
: f32 = f32::NAN
;
1182 let inf
: f32 = f32::INFINITY
;
1183 let neg_inf
: f32 = f32::NEG_INFINITY
;
1184 let zero
: f32 = 0.0f32;
1185 let neg_zero
: f32 = -0.0;
1186 assert_eq
!(nan
.classify(), Fp
::Nan
);
1187 assert_eq
!(inf
.classify(), Fp
::Infinite
);
1188 assert_eq
!(neg_inf
.classify(), Fp
::Infinite
);
1189 assert_eq
!(zero
.classify(), Fp
::Zero
);
1190 assert_eq
!(neg_zero
.classify(), Fp
::Zero
);
1191 assert_eq
!(1f32.classify(), Fp
::Normal
);
1192 assert_eq
!(1e
-37f32.classify(), Fp
::Normal
);
1193 assert_eq
!(1e
-38f32.classify(), Fp
::Subnormal
);
1198 assert_approx_eq
!(1.0f32.floor(), 1.0f32);
1199 assert_approx_eq
!(1.3f32.floor(), 1.0f32);
1200 assert_approx_eq
!(1.5f32.floor(), 1.0f32);
1201 assert_approx_eq
!(1.7f32.floor(), 1.0f32);
1202 assert_approx_eq
!(0.0f32.floor(), 0.0f32);
1203 assert_approx_eq
!((-0.0f32).floor(), -0.0f32);
1204 assert_approx_eq
!((-1.0f32).floor(), -1.0f32);
1205 assert_approx_eq
!((-1.3f32).floor(), -2.0f32);
1206 assert_approx_eq
!((-1.5f32).floor(), -2.0f32);
1207 assert_approx_eq
!((-1.7f32).floor(), -2.0f32);
1212 assert_approx_eq
!(1.0f32.ceil(), 1.0f32);
1213 assert_approx_eq
!(1.3f32.ceil(), 2.0f32);
1214 assert_approx_eq
!(1.5f32.ceil(), 2.0f32);
1215 assert_approx_eq
!(1.7f32.ceil(), 2.0f32);
1216 assert_approx_eq
!(0.0f32.ceil(), 0.0f32);
1217 assert_approx_eq
!((-0.0f32).ceil(), -0.0f32);
1218 assert_approx_eq
!((-1.0f32).ceil(), -1.0f32);
1219 assert_approx_eq
!((-1.3f32).ceil(), -1.0f32);
1220 assert_approx_eq
!((-1.5f32).ceil(), -1.0f32);
1221 assert_approx_eq
!((-1.7f32).ceil(), -1.0f32);
1226 assert_approx_eq
!(1.0f32.round(), 1.0f32);
1227 assert_approx_eq
!(1.3f32.round(), 1.0f32);
1228 assert_approx_eq
!(1.5f32.round(), 2.0f32);
1229 assert_approx_eq
!(1.7f32.round(), 2.0f32);
1230 assert_approx_eq
!(0.0f32.round(), 0.0f32);
1231 assert_approx_eq
!((-0.0f32).round(), -0.0f32);
1232 assert_approx_eq
!((-1.0f32).round(), -1.0f32);
1233 assert_approx_eq
!((-1.3f32).round(), -1.0f32);
1234 assert_approx_eq
!((-1.5f32).round(), -2.0f32);
1235 assert_approx_eq
!((-1.7f32).round(), -2.0f32);
1240 assert_approx_eq
!(1.0f32.trunc(), 1.0f32);
1241 assert_approx_eq
!(1.3f32.trunc(), 1.0f32);
1242 assert_approx_eq
!(1.5f32.trunc(), 1.0f32);
1243 assert_approx_eq
!(1.7f32.trunc(), 1.0f32);
1244 assert_approx_eq
!(0.0f32.trunc(), 0.0f32);
1245 assert_approx_eq
!((-0.0f32).trunc(), -0.0f32);
1246 assert_approx_eq
!((-1.0f32).trunc(), -1.0f32);
1247 assert_approx_eq
!((-1.3f32).trunc(), -1.0f32);
1248 assert_approx_eq
!((-1.5f32).trunc(), -1.0f32);
1249 assert_approx_eq
!((-1.7f32).trunc(), -1.0f32);
1254 assert_approx_eq
!(1.0f32.fract(), 0.0f32);
1255 assert_approx_eq
!(1.3f32.fract(), 0.3f32);
1256 assert_approx_eq
!(1.5f32.fract(), 0.5f32);
1257 assert_approx_eq
!(1.7f32.fract(), 0.7f32);
1258 assert_approx_eq
!(0.0f32.fract(), 0.0f32);
1259 assert_approx_eq
!((-0.0f32).fract(), -0.0f32);
1260 assert_approx_eq
!((-1.0f32).fract(), -0.0f32);
1261 assert_approx_eq
!((-1.3f32).fract(), -0.3f32);
1262 assert_approx_eq
!((-1.5f32).fract(), -0.5f32);
1263 assert_approx_eq
!((-1.7f32).fract(), -0.7f32);
1268 assert_eq
!(INFINITY
.abs(), INFINITY
);
1269 assert_eq
!(1f32.abs(), 1f32);
1270 assert_eq
!(0f32.abs(), 0f32);
1271 assert_eq
!((-0f32).abs(), 0f32);
1272 assert_eq
!((-1f32).abs(), 1f32);
1273 assert_eq
!(NEG_INFINITY
.abs(), INFINITY
);
1274 assert_eq
!((1f32 / NEG_INFINITY
).abs(), 0f32);
1275 assert
!(NAN
.abs().is_nan());
1280 assert_eq
!(INFINITY
.signum(), 1f32);
1281 assert_eq
!(1f32.signum(), 1f32);
1282 assert_eq
!(0f32.signum(), 1f32);
1283 assert_eq
!((-0f32).signum(), -1f32);
1284 assert_eq
!((-1f32).signum(), -1f32);
1285 assert_eq
!(NEG_INFINITY
.signum(), -1f32);
1286 assert_eq
!((1f32 / NEG_INFINITY
).signum(), -1f32);
1287 assert
!(NAN
.signum().is_nan());
1291 fn test_is_sign_positive() {
1292 assert
!(INFINITY
.is_sign_positive());
1293 assert
!(1f32.is_sign_positive());
1294 assert
!(0f32.is_sign_positive());
1295 assert
!(!(-0f32).is_sign_positive());
1296 assert
!(!(-1f32).is_sign_positive());
1297 assert
!(!NEG_INFINITY
.is_sign_positive());
1298 assert
!(!(1f32 / NEG_INFINITY
).is_sign_positive());
1299 assert
!(NAN
.is_sign_positive());
1300 assert
!(!(-NAN
).is_sign_positive());
1304 fn test_is_sign_negative() {
1305 assert
!(!INFINITY
.is_sign_negative());
1306 assert
!(!1f32.is_sign_negative());
1307 assert
!(!0f32.is_sign_negative());
1308 assert
!((-0f32).is_sign_negative());
1309 assert
!((-1f32).is_sign_negative());
1310 assert
!(NEG_INFINITY
.is_sign_negative());
1311 assert
!((1f32 / NEG_INFINITY
).is_sign_negative());
1312 assert
!(!NAN
.is_sign_negative());
1313 assert
!((-NAN
).is_sign_negative());
1318 let nan
: f32 = f32::NAN
;
1319 let inf
: f32 = f32::INFINITY
;
1320 let neg_inf
: f32 = f32::NEG_INFINITY
;
1321 assert_approx_eq
!(12.3f32.mul_add(4.5, 6.7), 62.05);
1322 assert_approx_eq
!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1323 assert_approx_eq
!(0.0f32.mul_add(8.9, 1.2), 1.2);
1324 assert_approx_eq
!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1325 assert
!(nan
.mul_add(7.8, 9.0).is_nan());
1326 assert_eq
!(inf
.mul_add(7.8, 9.0), inf
);
1327 assert_eq
!(neg_inf
.mul_add(7.8, 9.0), neg_inf
);
1328 assert_eq
!(8.9f32.mul_add(inf
, 3.2), inf
);
1329 assert_eq
!((-3.2f32).mul_add(2.4, neg_inf
), neg_inf
);
1334 let nan
: f32 = f32::NAN
;
1335 let inf
: f32 = f32::INFINITY
;
1336 let neg_inf
: f32 = f32::NEG_INFINITY
;
1337 assert_eq
!(1.0f32.recip(), 1.0);
1338 assert_eq
!(2.0f32.recip(), 0.5);
1339 assert_eq
!((-0.4f32).recip(), -2.5);
1340 assert_eq
!(0.0f32.recip(), inf
);
1341 assert
!(nan
.recip().is_nan());
1342 assert_eq
!(inf
.recip(), 0.0);
1343 assert_eq
!(neg_inf
.recip(), 0.0);
1348 let nan
: f32 = f32::NAN
;
1349 let inf
: f32 = f32::INFINITY
;
1350 let neg_inf
: f32 = f32::NEG_INFINITY
;
1351 assert_eq
!(1.0f32.powi(1), 1.0);
1352 assert_approx_eq
!((-3.1f32).powi(2), 9.61);
1353 assert_approx_eq
!(5.9f32.powi(-2), 0.028727);
1354 assert_eq
!(8.3f32.powi(0), 1.0);
1355 assert
!(nan
.powi(2).is_nan());
1356 assert_eq
!(inf
.powi(3), inf
);
1357 assert_eq
!(neg_inf
.powi(2), inf
);
1362 let nan
: f32 = f32::NAN
;
1363 let inf
: f32 = f32::INFINITY
;
1364 let neg_inf
: f32 = f32::NEG_INFINITY
;
1365 assert_eq
!(1.0f32.powf(1.0), 1.0);
1366 assert_approx_eq
!(3.4f32.powf(4.5), 246.408218);
1367 assert_approx_eq
!(2.7f32.powf(-3.2), 0.041652);
1368 assert_approx_eq
!((-3.1f32).powf(2.0), 9.61);
1369 assert_approx_eq
!(5.9f32.powf(-2.0), 0.028727);
1370 assert_eq
!(8.3f32.powf(0.0), 1.0);
1371 assert
!(nan
.powf(2.0).is_nan());
1372 assert_eq
!(inf
.powf(2.0), inf
);
1373 assert_eq
!(neg_inf
.powf(3.0), neg_inf
);
1377 fn test_sqrt_domain() {
1378 assert
!(NAN
.sqrt().is_nan());
1379 assert
!(NEG_INFINITY
.sqrt().is_nan());
1380 assert
!((-1.0f32).sqrt().is_nan());
1381 assert_eq
!((-0.0f32).sqrt(), -0.0);
1382 assert_eq
!(0.0f32.sqrt(), 0.0);
1383 assert_eq
!(1.0f32.sqrt(), 1.0);
1384 assert_eq
!(INFINITY
.sqrt(), INFINITY
);
1389 assert_eq
!(1.0, 0.0f32.exp());
1390 assert_approx_eq
!(2.718282, 1.0f32.exp());
1391 assert_approx_eq
!(148.413162, 5.0f32.exp());
1393 let inf
: f32 = f32::INFINITY
;
1394 let neg_inf
: f32 = f32::NEG_INFINITY
;
1395 let nan
: f32 = f32::NAN
;
1396 assert_eq
!(inf
, inf
.exp());
1397 assert_eq
!(0.0, neg_inf
.exp());
1398 assert
!(nan
.exp().is_nan());
1403 assert_eq
!(32.0, 5.0f32.exp2());
1404 assert_eq
!(1.0, 0.0f32.exp2());
1406 let inf
: f32 = f32::INFINITY
;
1407 let neg_inf
: f32 = f32::NEG_INFINITY
;
1408 let nan
: f32 = f32::NAN
;
1409 assert_eq
!(inf
, inf
.exp2());
1410 assert_eq
!(0.0, neg_inf
.exp2());
1411 assert
!(nan
.exp2().is_nan());
1416 let nan
: f32 = f32::NAN
;
1417 let inf
: f32 = f32::INFINITY
;
1418 let neg_inf
: f32 = f32::NEG_INFINITY
;
1419 assert_approx_eq
!(1.0f32.exp().ln(), 1.0);
1420 assert
!(nan
.ln().is_nan());
1421 assert_eq
!(inf
.ln(), inf
);
1422 assert
!(neg_inf
.ln().is_nan());
1423 assert
!((-2.3f32).ln().is_nan());
1424 assert_eq
!((-0.0f32).ln(), neg_inf
);
1425 assert_eq
!(0.0f32.ln(), neg_inf
);
1426 assert_approx_eq
!(4.0f32.ln(), 1.386294);
1431 let nan
: f32 = f32::NAN
;
1432 let inf
: f32 = f32::INFINITY
;
1433 let neg_inf
: f32 = f32::NEG_INFINITY
;
1434 assert_eq
!(10.0f32.log(10.0), 1.0);
1435 assert_approx_eq
!(2.3f32.log(3.5), 0.664858);
1436 assert_eq
!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1437 assert
!(1.0f32.log(1.0).is_nan());
1438 assert
!(1.0f32.log(-13.9).is_nan());
1439 assert
!(nan
.log(2.3).is_nan());
1440 assert_eq
!(inf
.log(10.0), inf
);
1441 assert
!(neg_inf
.log(8.8).is_nan());
1442 assert
!((-2.3f32).log(0.1).is_nan());
1443 assert_eq
!((-0.0f32).log(2.0), neg_inf
);
1444 assert_eq
!(0.0f32.log(7.0), neg_inf
);
1449 let nan
: f32 = f32::NAN
;
1450 let inf
: f32 = f32::INFINITY
;
1451 let neg_inf
: f32 = f32::NEG_INFINITY
;
1452 assert_approx_eq
!(10.0f32.log2(), 3.321928);
1453 assert_approx_eq
!(2.3f32.log2(), 1.201634);
1454 assert_approx_eq
!(1.0f32.exp().log2(), 1.442695);
1455 assert
!(nan
.log2().is_nan());
1456 assert_eq
!(inf
.log2(), inf
);
1457 assert
!(neg_inf
.log2().is_nan());
1458 assert
!((-2.3f32).log2().is_nan());
1459 assert_eq
!((-0.0f32).log2(), neg_inf
);
1460 assert_eq
!(0.0f32.log2(), neg_inf
);
1465 let nan
: f32 = f32::NAN
;
1466 let inf
: f32 = f32::INFINITY
;
1467 let neg_inf
: f32 = f32::NEG_INFINITY
;
1468 assert_eq
!(10.0f32.log10(), 1.0);
1469 assert_approx_eq
!(2.3f32.log10(), 0.361728);
1470 assert_approx_eq
!(1.0f32.exp().log10(), 0.434294);
1471 assert_eq
!(1.0f32.log10(), 0.0);
1472 assert
!(nan
.log10().is_nan());
1473 assert_eq
!(inf
.log10(), inf
);
1474 assert
!(neg_inf
.log10().is_nan());
1475 assert
!((-2.3f32).log10().is_nan());
1476 assert_eq
!((-0.0f32).log10(), neg_inf
);
1477 assert_eq
!(0.0f32.log10(), neg_inf
);
1481 fn test_to_degrees() {
1482 let pi
: f32 = consts
::PI
;
1483 let nan
: f32 = f32::NAN
;
1484 let inf
: f32 = f32::INFINITY
;
1485 let neg_inf
: f32 = f32::NEG_INFINITY
;
1486 assert_eq
!(0.0f32.to_degrees(), 0.0);
1487 assert_approx_eq
!((-5.8f32).to_degrees(), -332.315521);
1488 assert_eq
!(pi
.to_degrees(), 180.0);
1489 assert
!(nan
.to_degrees().is_nan());
1490 assert_eq
!(inf
.to_degrees(), inf
);
1491 assert_eq
!(neg_inf
.to_degrees(), neg_inf
);
1492 assert_eq
!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1496 fn test_to_radians() {
1497 let pi
: f32 = consts
::PI
;
1498 let nan
: f32 = f32::NAN
;
1499 let inf
: f32 = f32::INFINITY
;
1500 let neg_inf
: f32 = f32::NEG_INFINITY
;
1501 assert_eq
!(0.0f32.to_radians(), 0.0);
1502 assert_approx_eq
!(154.6f32.to_radians(), 2.698279);
1503 assert_approx_eq
!((-332.31f32).to_radians(), -5.799903);
1504 assert_eq
!(180.0f32.to_radians(), pi
);
1505 assert
!(nan
.to_radians().is_nan());
1506 assert_eq
!(inf
.to_radians(), inf
);
1507 assert_eq
!(neg_inf
.to_radians(), neg_inf
);
1512 assert_eq
!(0.0f32.asinh(), 0.0f32);
1513 assert_eq
!((-0.0f32).asinh(), -0.0f32);
1515 let inf
: f32 = f32::INFINITY
;
1516 let neg_inf
: f32 = f32::NEG_INFINITY
;
1517 let nan
: f32 = f32::NAN
;
1518 assert_eq
!(inf
.asinh(), inf
);
1519 assert_eq
!(neg_inf
.asinh(), neg_inf
);
1520 assert
!(nan
.asinh().is_nan());
1521 assert
!((-0.0f32).asinh().is_sign_negative()); // issue 63271
1522 assert_approx_eq
!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1523 assert_approx_eq
!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1528 assert_eq
!(1.0f32.acosh(), 0.0f32);
1529 assert
!(0.999f32.acosh().is_nan());
1531 let inf
: f32 = f32::INFINITY
;
1532 let neg_inf
: f32 = f32::NEG_INFINITY
;
1533 let nan
: f32 = f32::NAN
;
1534 assert_eq
!(inf
.acosh(), inf
);
1535 assert
!(neg_inf
.acosh().is_nan());
1536 assert
!(nan
.acosh().is_nan());
1537 assert_approx_eq
!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1538 assert_approx_eq
!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1543 assert_eq
!(0.0f32.atanh(), 0.0f32);
1544 assert_eq
!((-0.0f32).atanh(), -0.0f32);
1546 let inf32
: f32 = f32::INFINITY
;
1547 let neg_inf32
: f32 = f32::NEG_INFINITY
;
1548 assert_eq
!(1.0f32.atanh(), inf32
);
1549 assert_eq
!((-1.0f32).atanh(), neg_inf32
);
1551 assert
!(2f64.atanh().atanh().is_nan());
1552 assert
!((-2f64).atanh().atanh().is_nan());
1554 let inf64
: f32 = f32::INFINITY
;
1555 let neg_inf64
: f32 = f32::NEG_INFINITY
;
1556 let nan32
: f32 = f32::NAN
;
1557 assert
!(inf64
.atanh().is_nan());
1558 assert
!(neg_inf64
.atanh().is_nan());
1559 assert
!(nan32
.atanh().is_nan());
1561 assert_approx_eq
!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1562 assert_approx_eq
!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1566 fn test_real_consts() {
1569 let pi
: f32 = consts
::PI
;
1570 let frac_pi_2
: f32 = consts
::FRAC_PI_2
;
1571 let frac_pi_3
: f32 = consts
::FRAC_PI_3
;
1572 let frac_pi_4
: f32 = consts
::FRAC_PI_4
;
1573 let frac_pi_6
: f32 = consts
::FRAC_PI_6
;
1574 let frac_pi_8
: f32 = consts
::FRAC_PI_8
;
1575 let frac_1_pi
: f32 = consts
::FRAC_1_PI
;
1576 let frac_2_pi
: f32 = consts
::FRAC_2_PI
;
1577 let frac_2_sqrtpi
: f32 = consts
::FRAC_2_SQRT_PI
;
1578 let sqrt2
: f32 = consts
::SQRT_2
;
1579 let frac_1_sqrt2
: f32 = consts
::FRAC_1_SQRT_2
;
1580 let e
: f32 = consts
::E
;
1581 let log2_e
: f32 = consts
::LOG2_E
;
1582 let log10_e
: f32 = consts
::LOG10_E
;
1583 let ln_2
: f32 = consts
::LN_2
;
1584 let ln_10
: f32 = consts
::LN_10
;
1586 assert_approx_eq
!(frac_pi_2
, pi
/ 2f32);
1587 assert_approx_eq
!(frac_pi_3
, pi
/ 3f32);
1588 assert_approx_eq
!(frac_pi_4
, pi
/ 4f32);
1589 assert_approx_eq
!(frac_pi_6
, pi
/ 6f32);
1590 assert_approx_eq
!(frac_pi_8
, pi
/ 8f32);
1591 assert_approx_eq
!(frac_1_pi
, 1f32 / pi
);
1592 assert_approx_eq
!(frac_2_pi
, 2f32 / pi
);
1593 assert_approx_eq
!(frac_2_sqrtpi
, 2f32 / pi
.sqrt());
1594 assert_approx_eq
!(sqrt2
, 2f32.sqrt());
1595 assert_approx_eq
!(frac_1_sqrt2
, 1f32 / 2f32.sqrt());
1596 assert_approx_eq
!(log2_e
, e
.log2());
1597 assert_approx_eq
!(log10_e
, e
.log10());
1598 assert_approx_eq
!(ln_2
, 2f32.ln());
1599 assert_approx_eq
!(ln_10
, 10f32.ln());
1603 fn test_float_bits_conv() {
1604 assert_eq
!((1f32).to_bits(), 0x3f800000);
1605 assert_eq
!((12.5f32).to_bits(), 0x41480000);
1606 assert_eq
!((1337f32).to_bits(), 0x44a72000);
1607 assert_eq
!((-14.25f32).to_bits(), 0xc1640000);
1608 assert_approx_eq
!(f32::from_bits(0x3f800000), 1.0);
1609 assert_approx_eq
!(f32::from_bits(0x41480000), 12.5);
1610 assert_approx_eq
!(f32::from_bits(0x44a72000), 1337.0);
1611 assert_approx_eq
!(f32::from_bits(0xc1640000), -14.25);
1613 // Check that NaNs roundtrip their bits regardless of signalingness
1614 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1615 let masked_nan1
= f32::NAN
.to_bits() ^
0x002A_AAAA;
1616 let masked_nan2
= f32::NAN
.to_bits() ^
0x0055_5555;
1617 assert
!(f32::from_bits(masked_nan1
).is_nan());
1618 assert
!(f32::from_bits(masked_nan2
).is_nan());
1620 assert_eq
!(f32::from_bits(masked_nan1
).to_bits(), masked_nan1
);
1621 assert_eq
!(f32::from_bits(masked_nan2
).to_bits(), masked_nan2
);
1626 fn test_clamp_min_greater_than_max() {
1627 let _
= 1.0f32.clamp(3.0, 1.0);
1632 fn test_clamp_min_is_nan() {
1633 let _
= 1.0f32.clamp(NAN
, 1.0);
1638 fn test_clamp_max_is_nan() {
1639 let _
= 1.0f32.clamp(3.0, NAN
);