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 //! Although using these constants won’t cause compilation warnings,
9 //! new code should use the associated constants directly on the primitive type.
11 #![stable(feature = "rust1", since = "1.0.0")]
12 #![allow(missing_docs)]
15 use crate::intrinsics;
17 use crate::sys::cmath;
19 #[stable(feature = "rust1", since = "1.0.0")]
20 pub use core::f32::consts;
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub use core::f32::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX};
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub use core::f32::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY};
25 #[stable(feature = "rust1", since = "1.0.0")]
26 pub use core::f32::{MAX, MIN, MIN_POSITIVE};
27 #[stable(feature = "rust1", since = "1.0.0")]
28 pub use core::f32::{MAX_EXP, MIN_10_EXP, MIN_EXP};
31 #[lang = "f32_runtime"]
33 /// Returns the largest integer less than or equal to a number.
42 /// assert_eq!(f.floor(), 3.0);
43 /// assert_eq!(g.floor(), 3.0);
44 /// assert_eq!(h.floor(), -4.0);
46 #[must_use = "method returns a new number and does not mutate the original value"]
47 #[stable(feature = "rust1", since = "1.0.0")]
49 pub fn floor(self) -> 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 #[must_use = "method returns a new number and does not mutate the original value"]
65 #[stable(feature = "rust1", since = "1.0.0")]
67 pub fn ceil(self) -> f32 {
68 unsafe { intrinsics::ceilf32(self) }
71 /// Returns the nearest integer to a number. Round half-way cases away from
80 /// assert_eq!(f.round(), 3.0);
81 /// assert_eq!(g.round(), -3.0);
83 #[must_use = "method returns a new number and does not mutate the original value"]
84 #[stable(feature = "rust1", since = "1.0.0")]
86 pub fn round(self) -> f32 {
87 unsafe { intrinsics::roundf32(self) }
90 /// Returns the integer part of a number.
99 /// assert_eq!(f.trunc(), 3.0);
100 /// assert_eq!(g.trunc(), 3.0);
101 /// assert_eq!(h.trunc(), -3.0);
103 #[must_use = "method returns a new number and does not mutate the original value"]
104 #[stable(feature = "rust1", since = "1.0.0")]
106 pub fn trunc(self) -> f32 {
107 unsafe { intrinsics::truncf32(self) }
110 /// Returns the fractional part of a number.
118 /// let y = -3.6_f32;
119 /// let abs_difference_x = (x.fract() - 0.6).abs();
120 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
122 /// assert!(abs_difference_x <= f32::EPSILON);
123 /// assert!(abs_difference_y <= f32::EPSILON);
125 #[must_use = "method returns a new number and does not mutate the original value"]
126 #[stable(feature = "rust1", since = "1.0.0")]
128 pub fn fract(self) -> f32 {
132 /// 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 #[must_use = "method returns a new number and does not mutate the original value"]
152 #[stable(feature = "rust1", since = "1.0.0")]
154 pub fn abs(self) -> f32 {
155 unsafe { intrinsics::fabsf32(self) }
158 /// Returns a number that represents the sign of `self`.
160 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
161 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
162 /// - `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 #[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() { 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.
197 /// assert_eq!(f.copysign(0.42), 3.5_f32);
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);
202 /// assert!(f32::NAN.copysign(1.0).is_nan());
204 #[must_use = "method returns a new number and does not mutate the original value"]
206 #[stable(feature = "copysign", since = "1.35.0")]
207 pub fn copysign(self, sign: f32) -> f32 {
208 unsafe { intrinsics::copysignf32(self, sign) }
211 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
212 /// error, yielding a more accurate result than an unfused multiply-add.
214 /// Using `mul_add` can be more performant than an unfused multiply-add if
215 /// the target architecture has a dedicated `fma` CPU instruction.
222 /// let m = 10.0_f32;
224 /// let b = 60.0_f32;
227 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
229 /// assert!(abs_difference <= f32::EPSILON);
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 #[must_use = "method returns a new number and does not mutate the original value"]
257 #[stable(feature = "euclidean_division", since = "1.38.0")]
258 pub fn div_euclid(self, rhs: f32) -> f32 {
259 let q = (self / rhs).trunc();
260 if self % rhs < 0.0 {
261 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
266 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
268 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
269 /// most cases. However, due to a floating point round-off error it can
270 /// result in `r == rhs.abs()`, violating the mathematical definition, if
271 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
272 /// This result is not an element of the function's codomain, but it is the
273 /// closest floating point number in the real numbers and thus fulfills the
274 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
280 /// let a: f32 = 7.0;
282 /// assert_eq!(a.rem_euclid(b), 3.0);
283 /// assert_eq!((-a).rem_euclid(b), 1.0);
284 /// assert_eq!(a.rem_euclid(-b), 3.0);
285 /// assert_eq!((-a).rem_euclid(-b), 1.0);
286 /// // limitation due to round-off error
287 /// assert!((-std::f32::EPSILON).rem_euclid(3.0) != 0.0);
289 #[must_use = "method returns a new number and does not mutate the original value"]
291 #[stable(feature = "euclidean_division", since = "1.38.0")]
292 pub fn rem_euclid(self, rhs: f32) -> f32 {
294 if r < 0.0 { r + rhs.abs() } else { r }
297 /// Raises a number to an integer power.
299 /// 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 #[must_use = "method returns a new number and does not mutate the original value"]
312 #[stable(feature = "rust1", since = "1.0.0")]
314 pub fn powi(self, n: i32) -> f32 {
315 unsafe { intrinsics::powif32(self, n) }
318 /// Raises a number to a floating point power.
326 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
328 /// assert!(abs_difference <= f32::EPSILON);
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.
346 /// let positive = 4.0_f32;
347 /// let negative = -4.0_f32;
349 /// let abs_difference = (positive.sqrt() - 2.0).abs();
351 /// assert!(abs_difference <= f32::EPSILON);
352 /// assert!(negative.sqrt().is_nan());
354 #[must_use = "method returns a new number and does not mutate the original value"]
355 #[stable(feature = "rust1", since = "1.0.0")]
357 pub fn sqrt(self) -> f32 {
358 unsafe { intrinsics::sqrtf32(self) }
361 /// Returns `e^(self)`, (the exponential function).
368 /// let one = 1.0f32;
370 /// let e = one.exp();
372 /// // ln(e) - 1 == 0
373 /// let abs_difference = (e.ln() - 1.0).abs();
375 /// assert!(abs_difference <= f32::EPSILON);
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)`.
394 /// let abs_difference = (f.exp2() - 4.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 exp2(self) -> f32 {
402 unsafe { intrinsics::exp2f32(self) }
405 /// Returns the natural logarithm of the number.
412 /// let one = 1.0f32;
414 /// let e = one.exp();
416 /// // ln(e) - 1 == 0
417 /// let abs_difference = (e.ln() - 1.0).abs();
419 /// assert!(abs_difference <= f32::EPSILON);
421 #[must_use = "method returns a new number and does not mutate the original value"]
422 #[stable(feature = "rust1", since = "1.0.0")]
424 pub fn ln(self) -> f32 {
425 unsafe { intrinsics::logf32(self) }
428 /// Returns the logarithm of the number with respect to an arbitrary base.
430 /// The result may not be correctly rounded owing to implementation details;
431 /// `self.log2()` can produce more accurate results for base 2, and
432 /// `self.log10()` can produce more accurate results for base 10.
439 /// let five = 5.0f32;
441 /// // log5(5) - 1 == 0
442 /// let abs_difference = (five.log(5.0) - 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 log(self, base: f32) -> f32 {
450 self.ln() / base.ln()
453 /// 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 #[must_use = "method returns a new number and does not mutate the original value"]
468 #[stable(feature = "rust1", since = "1.0.0")]
470 pub fn log2(self) -> f32 {
471 #[cfg(target_os = "android")]
472 return crate::sys::android::log2f32(self);
473 #[cfg(not(target_os = "android"))]
474 return unsafe { intrinsics::log2f32(self) };
477 /// 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 #[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`
511 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
512 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
514 /// assert!(abs_difference_x <= f32::EPSILON);
515 /// assert!(abs_difference_y <= f32::EPSILON);
517 #[must_use = "method returns a new number and does not mutate the original value"]
518 #[stable(feature = "rust1", since = "1.0.0")]
522 reason = "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 cubic root of a number.
543 /// // x^(1/3) - 2 == 0
544 /// let abs_difference = (x.cbrt() - 2.0).abs();
546 /// assert!(abs_difference <= f32::EPSILON);
548 #[must_use = "method returns a new number and does not mutate the original value"]
549 #[stable(feature = "rust1", since = "1.0.0")]
551 pub fn cbrt(self) -> f32 {
552 unsafe { cmath::cbrtf(self) }
555 /// Calculates the length of the hypotenuse of a right-angle triangle given
556 /// legs of length `x` and `y`.
566 /// // sqrt(x^2 + y^2)
567 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
569 /// assert!(abs_difference <= f32::EPSILON);
571 #[must_use = "method returns a new number and does not mutate the original value"]
572 #[stable(feature = "rust1", since = "1.0.0")]
574 pub fn hypot(self, other: f32) -> f32 {
575 unsafe { cmath::hypotf(self, other) }
578 /// Computes the sine of a number (in radians).
585 /// let x = f32::consts::FRAC_PI_2;
587 /// let abs_difference = (x.sin() - 1.0).abs();
589 /// assert!(abs_difference <= f32::EPSILON);
591 #[must_use = "method returns a new number and does not mutate the original value"]
592 #[stable(feature = "rust1", since = "1.0.0")]
594 pub fn sin(self) -> f32 {
595 unsafe { intrinsics::sinf32(self) }
598 /// Computes the cosine of a number (in radians).
605 /// let x = 2.0 * f32::consts::PI;
607 /// let abs_difference = (x.cos() - 1.0).abs();
609 /// assert!(abs_difference <= f32::EPSILON);
611 #[must_use = "method returns a new number and does not mutate the original value"]
612 #[stable(feature = "rust1", since = "1.0.0")]
614 pub fn cos(self) -> f32 {
615 unsafe { intrinsics::cosf32(self) }
618 /// Computes the tangent of a number (in radians).
625 /// let x = f32::consts::FRAC_PI_4;
626 /// let abs_difference = (x.tan() - 1.0).abs();
628 /// assert!(abs_difference <= f32::EPSILON);
630 #[must_use = "method returns a new number and does not mutate the original value"]
631 #[stable(feature = "rust1", since = "1.0.0")]
633 pub fn tan(self) -> f32 {
634 unsafe { cmath::tanf(self) }
637 /// Computes the arcsine of a number. Return value is in radians in
638 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
646 /// let f = f32::consts::FRAC_PI_2;
648 /// // asin(sin(pi/2))
649 /// let abs_difference = (f.sin().asin() - f32::consts::FRAC_PI_2).abs();
651 /// assert!(abs_difference <= f32::EPSILON);
653 #[must_use = "method returns a new number and does not mutate the original value"]
654 #[stable(feature = "rust1", since = "1.0.0")]
656 pub fn asin(self) -> f32 {
657 unsafe { cmath::asinf(self) }
660 /// Computes the arccosine of a number. Return value is in radians in
661 /// the range [0, pi] or NaN if the number is outside the range
669 /// let f = f32::consts::FRAC_PI_4;
671 /// // acos(cos(pi/4))
672 /// let abs_difference = (f.cos().acos() - f32::consts::FRAC_PI_4).abs();
674 /// assert!(abs_difference <= f32::EPSILON);
676 #[must_use = "method returns a new number and does not mutate the original value"]
677 #[stable(feature = "rust1", since = "1.0.0")]
679 pub fn acos(self) -> f32 {
680 unsafe { cmath::acosf(self) }
683 /// Computes the arctangent of a number. Return value is in radians in the
684 /// range [-pi/2, pi/2];
694 /// let abs_difference = (f.tan().atan() - 1.0).abs();
696 /// assert!(abs_difference <= f32::EPSILON);
698 #[must_use = "method returns a new number and does not mutate the original value"]
699 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn atan(self) -> f32 {
702 unsafe { cmath::atanf(self) }
705 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
707 /// * `x = 0`, `y = 0`: `0`
708 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
709 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
710 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
717 /// // Positive angles measured counter-clockwise
718 /// // from positive x axis
719 /// // -pi/4 radians (45 deg clockwise)
721 /// let y1 = -3.0f32;
723 /// // 3pi/4 radians (135 deg counter-clockwise)
724 /// let x2 = -3.0f32;
727 /// let abs_difference_1 = (y1.atan2(x1) - (-f32::consts::FRAC_PI_4)).abs();
728 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * f32::consts::FRAC_PI_4)).abs();
730 /// assert!(abs_difference_1 <= f32::EPSILON);
731 /// assert!(abs_difference_2 <= f32::EPSILON);
733 #[must_use = "method returns a new number and does not mutate the original value"]
734 #[stable(feature = "rust1", since = "1.0.0")]
736 pub fn atan2(self, other: f32) -> f32 {
737 unsafe { cmath::atan2f(self, other) }
740 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
741 /// `(sin(x), cos(x))`.
748 /// let x = f32::consts::FRAC_PI_4;
749 /// let f = x.sin_cos();
751 /// let abs_difference_0 = (f.0 - x.sin()).abs();
752 /// let abs_difference_1 = (f.1 - x.cos()).abs();
754 /// assert!(abs_difference_0 <= f32::EPSILON);
755 /// assert!(abs_difference_1 <= f32::EPSILON);
757 #[stable(feature = "rust1", since = "1.0.0")]
759 pub fn sin_cos(self) -> (f32, f32) {
760 (self.sin(), self.cos())
763 /// Returns `e^(self) - 1` in a way that is accurate even if the
764 /// number is close to zero.
774 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
776 /// assert!(abs_difference <= f32::EPSILON);
778 #[must_use = "method returns a new number and does not mutate the original value"]
779 #[stable(feature = "rust1", since = "1.0.0")]
781 pub fn exp_m1(self) -> f32 {
782 unsafe { cmath::expm1f(self) }
785 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
786 /// the operations were performed separately.
793 /// let x = f32::consts::E - 1.0;
795 /// // ln(1 + (e - 1)) == ln(e) == 1
796 /// let abs_difference = (x.ln_1p() - 1.0).abs();
798 /// assert!(abs_difference <= f32::EPSILON);
800 #[must_use = "method returns a new number and does not mutate the original value"]
801 #[stable(feature = "rust1", since = "1.0.0")]
803 pub fn ln_1p(self) -> f32 {
804 unsafe { cmath::log1pf(self) }
807 /// Hyperbolic sine function.
814 /// let e = f32::consts::E;
817 /// let f = x.sinh();
818 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
819 /// let g = ((e * e) - 1.0) / (2.0 * e);
820 /// let abs_difference = (f - g).abs();
822 /// assert!(abs_difference <= f32::EPSILON);
824 #[must_use = "method returns a new number and does not mutate the original value"]
825 #[stable(feature = "rust1", since = "1.0.0")]
827 pub fn sinh(self) -> f32 {
828 unsafe { cmath::sinhf(self) }
831 /// Hyperbolic cosine function.
838 /// let e = f32::consts::E;
840 /// let f = x.cosh();
841 /// // Solving cosh() at 1 gives this result
842 /// let g = ((e * e) + 1.0) / (2.0 * e);
843 /// let abs_difference = (f - g).abs();
846 /// assert!(abs_difference <= f32::EPSILON);
848 #[must_use = "method returns a new number and does not mutate the original value"]
849 #[stable(feature = "rust1", since = "1.0.0")]
851 pub fn cosh(self) -> f32 {
852 unsafe { cmath::coshf(self) }
855 /// Hyperbolic tangent function.
862 /// let e = f32::consts::E;
865 /// let f = x.tanh();
866 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
867 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
868 /// let abs_difference = (f - g).abs();
870 /// assert!(abs_difference <= f32::EPSILON);
872 #[must_use = "method returns a new number and does not mutate the original value"]
873 #[stable(feature = "rust1", since = "1.0.0")]
875 pub fn tanh(self) -> f32 {
876 unsafe { cmath::tanhf(self) }
879 /// Inverse hyperbolic sine function.
887 /// let f = x.sinh().asinh();
889 /// let abs_difference = (f - x).abs();
891 /// assert!(abs_difference <= f32::EPSILON);
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 asinh(self) -> f32 {
897 if self == NEG_INFINITY {
900 (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
904 /// Inverse hyperbolic cosine function.
912 /// let f = x.cosh().acosh();
914 /// let abs_difference = (f - x).abs();
916 /// assert!(abs_difference <= f32::EPSILON);
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 acosh(self) -> f32 {
922 if self < 1.0 { crate::f32::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
925 /// Inverse hyperbolic tangent function.
932 /// let e = f32::consts::E;
933 /// let f = e.tanh().atanh();
935 /// let abs_difference = (f - e).abs();
937 /// assert!(abs_difference <= 1e-5);
939 #[must_use = "method returns a new number and does not mutate the original value"]
940 #[stable(feature = "rust1", since = "1.0.0")]
942 pub fn atanh(self) -> f32 {
943 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
946 /// Restrict a value to a certain interval unless it is NaN.
948 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
949 /// less than `min`. Otherwise this returns `self`.
951 /// Not that this function returns NaN if the initial value was NaN as
956 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
961 /// #![feature(clamp)]
962 /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
963 /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
964 /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
965 /// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan());
967 #[must_use = "method returns a new number and does not mutate the original value"]
968 #[unstable(feature = "clamp", issue = "44095")]
970 pub fn clamp(self, min: f32, max: f32) -> f32 {
987 use crate::num::FpCategory as Fp;
992 test_num(10f32, 2f32);
997 assert_eq!(NAN.min(2.0), 2.0);
998 assert_eq!(2.0f32.min(NAN), 2.0);
1003 assert_eq!(NAN.max(2.0), 2.0);
1004 assert_eq!(2.0f32.max(NAN), 2.0);
1009 let nan: f32 = f32::NAN;
1010 assert!(nan.is_nan());
1011 assert!(!nan.is_infinite());
1012 assert!(!nan.is_finite());
1013 assert!(!nan.is_normal());
1014 assert!(nan.is_sign_positive());
1015 assert!(!nan.is_sign_negative());
1016 assert_eq!(Fp::Nan, nan.classify());
1020 fn test_infinity() {
1021 let inf: f32 = f32::INFINITY;
1022 assert!(inf.is_infinite());
1023 assert!(!inf.is_finite());
1024 assert!(inf.is_sign_positive());
1025 assert!(!inf.is_sign_negative());
1026 assert!(!inf.is_nan());
1027 assert!(!inf.is_normal());
1028 assert_eq!(Fp::Infinite, inf.classify());
1032 fn test_neg_infinity() {
1033 let neg_inf: f32 = f32::NEG_INFINITY;
1034 assert!(neg_inf.is_infinite());
1035 assert!(!neg_inf.is_finite());
1036 assert!(!neg_inf.is_sign_positive());
1037 assert!(neg_inf.is_sign_negative());
1038 assert!(!neg_inf.is_nan());
1039 assert!(!neg_inf.is_normal());
1040 assert_eq!(Fp::Infinite, neg_inf.classify());
1045 let zero: f32 = 0.0f32;
1046 assert_eq!(0.0, zero);
1047 assert!(!zero.is_infinite());
1048 assert!(zero.is_finite());
1049 assert!(zero.is_sign_positive());
1050 assert!(!zero.is_sign_negative());
1051 assert!(!zero.is_nan());
1052 assert!(!zero.is_normal());
1053 assert_eq!(Fp::Zero, zero.classify());
1057 fn test_neg_zero() {
1058 let neg_zero: f32 = -0.0;
1059 assert_eq!(0.0, neg_zero);
1060 assert!(!neg_zero.is_infinite());
1061 assert!(neg_zero.is_finite());
1062 assert!(!neg_zero.is_sign_positive());
1063 assert!(neg_zero.is_sign_negative());
1064 assert!(!neg_zero.is_nan());
1065 assert!(!neg_zero.is_normal());
1066 assert_eq!(Fp::Zero, neg_zero.classify());
1071 let one: f32 = 1.0f32;
1072 assert_eq!(1.0, one);
1073 assert!(!one.is_infinite());
1074 assert!(one.is_finite());
1075 assert!(one.is_sign_positive());
1076 assert!(!one.is_sign_negative());
1077 assert!(!one.is_nan());
1078 assert!(one.is_normal());
1079 assert_eq!(Fp::Normal, one.classify());
1084 let nan: f32 = f32::NAN;
1085 let inf: f32 = f32::INFINITY;
1086 let neg_inf: f32 = f32::NEG_INFINITY;
1087 assert!(nan.is_nan());
1088 assert!(!0.0f32.is_nan());
1089 assert!(!5.3f32.is_nan());
1090 assert!(!(-10.732f32).is_nan());
1091 assert!(!inf.is_nan());
1092 assert!(!neg_inf.is_nan());
1096 fn test_is_infinite() {
1097 let nan: f32 = f32::NAN;
1098 let inf: f32 = f32::INFINITY;
1099 let neg_inf: f32 = f32::NEG_INFINITY;
1100 assert!(!nan.is_infinite());
1101 assert!(inf.is_infinite());
1102 assert!(neg_inf.is_infinite());
1103 assert!(!0.0f32.is_infinite());
1104 assert!(!42.8f32.is_infinite());
1105 assert!(!(-109.2f32).is_infinite());
1109 fn test_is_finite() {
1110 let nan: f32 = f32::NAN;
1111 let inf: f32 = f32::INFINITY;
1112 let neg_inf: f32 = f32::NEG_INFINITY;
1113 assert!(!nan.is_finite());
1114 assert!(!inf.is_finite());
1115 assert!(!neg_inf.is_finite());
1116 assert!(0.0f32.is_finite());
1117 assert!(42.8f32.is_finite());
1118 assert!((-109.2f32).is_finite());
1122 fn test_is_normal() {
1123 let nan: f32 = f32::NAN;
1124 let inf: f32 = f32::INFINITY;
1125 let neg_inf: f32 = f32::NEG_INFINITY;
1126 let zero: f32 = 0.0f32;
1127 let neg_zero: f32 = -0.0;
1128 assert!(!nan.is_normal());
1129 assert!(!inf.is_normal());
1130 assert!(!neg_inf.is_normal());
1131 assert!(!zero.is_normal());
1132 assert!(!neg_zero.is_normal());
1133 assert!(1f32.is_normal());
1134 assert!(1e-37f32.is_normal());
1135 assert!(!1e-38f32.is_normal());
1139 fn test_classify() {
1140 let nan: f32 = f32::NAN;
1141 let inf: f32 = f32::INFINITY;
1142 let neg_inf: f32 = f32::NEG_INFINITY;
1143 let zero: f32 = 0.0f32;
1144 let neg_zero: f32 = -0.0;
1145 assert_eq!(nan.classify(), Fp::Nan);
1146 assert_eq!(inf.classify(), Fp::Infinite);
1147 assert_eq!(neg_inf.classify(), Fp::Infinite);
1148 assert_eq!(zero.classify(), Fp::Zero);
1149 assert_eq!(neg_zero.classify(), Fp::Zero);
1150 assert_eq!(1f32.classify(), Fp::Normal);
1151 assert_eq!(1e-37f32.classify(), Fp::Normal);
1152 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1157 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1158 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1159 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1160 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1161 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1162 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1163 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1164 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1165 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1166 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1171 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1172 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1173 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1174 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1175 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1176 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1177 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1178 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1179 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1180 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1185 assert_approx_eq!(1.0f32.round(), 1.0f32);
1186 assert_approx_eq!(1.3f32.round(), 1.0f32);
1187 assert_approx_eq!(1.5f32.round(), 2.0f32);
1188 assert_approx_eq!(1.7f32.round(), 2.0f32);
1189 assert_approx_eq!(0.0f32.round(), 0.0f32);
1190 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1191 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1192 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1193 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1194 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1199 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1200 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1201 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1202 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1203 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1204 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1205 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1206 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1207 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1208 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1213 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1214 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1215 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1216 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1217 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1218 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1219 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1220 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1221 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1222 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1227 assert_eq!(INFINITY.abs(), INFINITY);
1228 assert_eq!(1f32.abs(), 1f32);
1229 assert_eq!(0f32.abs(), 0f32);
1230 assert_eq!((-0f32).abs(), 0f32);
1231 assert_eq!((-1f32).abs(), 1f32);
1232 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1233 assert_eq!((1f32 / NEG_INFINITY).abs(), 0f32);
1234 assert!(NAN.abs().is_nan());
1239 assert_eq!(INFINITY.signum(), 1f32);
1240 assert_eq!(1f32.signum(), 1f32);
1241 assert_eq!(0f32.signum(), 1f32);
1242 assert_eq!((-0f32).signum(), -1f32);
1243 assert_eq!((-1f32).signum(), -1f32);
1244 assert_eq!(NEG_INFINITY.signum(), -1f32);
1245 assert_eq!((1f32 / NEG_INFINITY).signum(), -1f32);
1246 assert!(NAN.signum().is_nan());
1250 fn test_is_sign_positive() {
1251 assert!(INFINITY.is_sign_positive());
1252 assert!(1f32.is_sign_positive());
1253 assert!(0f32.is_sign_positive());
1254 assert!(!(-0f32).is_sign_positive());
1255 assert!(!(-1f32).is_sign_positive());
1256 assert!(!NEG_INFINITY.is_sign_positive());
1257 assert!(!(1f32 / NEG_INFINITY).is_sign_positive());
1258 assert!(NAN.is_sign_positive());
1259 assert!(!(-NAN).is_sign_positive());
1263 fn test_is_sign_negative() {
1264 assert!(!INFINITY.is_sign_negative());
1265 assert!(!1f32.is_sign_negative());
1266 assert!(!0f32.is_sign_negative());
1267 assert!((-0f32).is_sign_negative());
1268 assert!((-1f32).is_sign_negative());
1269 assert!(NEG_INFINITY.is_sign_negative());
1270 assert!((1f32 / NEG_INFINITY).is_sign_negative());
1271 assert!(!NAN.is_sign_negative());
1272 assert!((-NAN).is_sign_negative());
1277 let nan: f32 = f32::NAN;
1278 let inf: f32 = f32::INFINITY;
1279 let neg_inf: f32 = f32::NEG_INFINITY;
1280 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1281 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1282 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1283 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1284 assert!(nan.mul_add(7.8, 9.0).is_nan());
1285 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1286 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1287 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1288 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1293 let nan: f32 = f32::NAN;
1294 let inf: f32 = f32::INFINITY;
1295 let neg_inf: f32 = f32::NEG_INFINITY;
1296 assert_eq!(1.0f32.recip(), 1.0);
1297 assert_eq!(2.0f32.recip(), 0.5);
1298 assert_eq!((-0.4f32).recip(), -2.5);
1299 assert_eq!(0.0f32.recip(), inf);
1300 assert!(nan.recip().is_nan());
1301 assert_eq!(inf.recip(), 0.0);
1302 assert_eq!(neg_inf.recip(), 0.0);
1307 let nan: f32 = f32::NAN;
1308 let inf: f32 = f32::INFINITY;
1309 let neg_inf: f32 = f32::NEG_INFINITY;
1310 assert_eq!(1.0f32.powi(1), 1.0);
1311 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1312 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1313 assert_eq!(8.3f32.powi(0), 1.0);
1314 assert!(nan.powi(2).is_nan());
1315 assert_eq!(inf.powi(3), inf);
1316 assert_eq!(neg_inf.powi(2), inf);
1321 let nan: f32 = f32::NAN;
1322 let inf: f32 = f32::INFINITY;
1323 let neg_inf: f32 = f32::NEG_INFINITY;
1324 assert_eq!(1.0f32.powf(1.0), 1.0);
1325 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1326 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1327 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1328 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1329 assert_eq!(8.3f32.powf(0.0), 1.0);
1330 assert!(nan.powf(2.0).is_nan());
1331 assert_eq!(inf.powf(2.0), inf);
1332 assert_eq!(neg_inf.powf(3.0), neg_inf);
1336 fn test_sqrt_domain() {
1337 assert!(NAN.sqrt().is_nan());
1338 assert!(NEG_INFINITY.sqrt().is_nan());
1339 assert!((-1.0f32).sqrt().is_nan());
1340 assert_eq!((-0.0f32).sqrt(), -0.0);
1341 assert_eq!(0.0f32.sqrt(), 0.0);
1342 assert_eq!(1.0f32.sqrt(), 1.0);
1343 assert_eq!(INFINITY.sqrt(), INFINITY);
1348 assert_eq!(1.0, 0.0f32.exp());
1349 assert_approx_eq!(2.718282, 1.0f32.exp());
1350 assert_approx_eq!(148.413162, 5.0f32.exp());
1352 let inf: f32 = f32::INFINITY;
1353 let neg_inf: f32 = f32::NEG_INFINITY;
1354 let nan: f32 = f32::NAN;
1355 assert_eq!(inf, inf.exp());
1356 assert_eq!(0.0, neg_inf.exp());
1357 assert!(nan.exp().is_nan());
1362 assert_eq!(32.0, 5.0f32.exp2());
1363 assert_eq!(1.0, 0.0f32.exp2());
1365 let inf: f32 = f32::INFINITY;
1366 let neg_inf: f32 = f32::NEG_INFINITY;
1367 let nan: f32 = f32::NAN;
1368 assert_eq!(inf, inf.exp2());
1369 assert_eq!(0.0, neg_inf.exp2());
1370 assert!(nan.exp2().is_nan());
1375 let nan: f32 = f32::NAN;
1376 let inf: f32 = f32::INFINITY;
1377 let neg_inf: f32 = f32::NEG_INFINITY;
1378 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1379 assert!(nan.ln().is_nan());
1380 assert_eq!(inf.ln(), inf);
1381 assert!(neg_inf.ln().is_nan());
1382 assert!((-2.3f32).ln().is_nan());
1383 assert_eq!((-0.0f32).ln(), neg_inf);
1384 assert_eq!(0.0f32.ln(), neg_inf);
1385 assert_approx_eq!(4.0f32.ln(), 1.386294);
1390 let nan: f32 = f32::NAN;
1391 let inf: f32 = f32::INFINITY;
1392 let neg_inf: f32 = f32::NEG_INFINITY;
1393 assert_eq!(10.0f32.log(10.0), 1.0);
1394 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1395 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1396 assert!(1.0f32.log(1.0).is_nan());
1397 assert!(1.0f32.log(-13.9).is_nan());
1398 assert!(nan.log(2.3).is_nan());
1399 assert_eq!(inf.log(10.0), inf);
1400 assert!(neg_inf.log(8.8).is_nan());
1401 assert!((-2.3f32).log(0.1).is_nan());
1402 assert_eq!((-0.0f32).log(2.0), neg_inf);
1403 assert_eq!(0.0f32.log(7.0), neg_inf);
1408 let nan: f32 = f32::NAN;
1409 let inf: f32 = f32::INFINITY;
1410 let neg_inf: f32 = f32::NEG_INFINITY;
1411 assert_approx_eq!(10.0f32.log2(), 3.321928);
1412 assert_approx_eq!(2.3f32.log2(), 1.201634);
1413 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1414 assert!(nan.log2().is_nan());
1415 assert_eq!(inf.log2(), inf);
1416 assert!(neg_inf.log2().is_nan());
1417 assert!((-2.3f32).log2().is_nan());
1418 assert_eq!((-0.0f32).log2(), neg_inf);
1419 assert_eq!(0.0f32.log2(), neg_inf);
1424 let nan: f32 = f32::NAN;
1425 let inf: f32 = f32::INFINITY;
1426 let neg_inf: f32 = f32::NEG_INFINITY;
1427 assert_eq!(10.0f32.log10(), 1.0);
1428 assert_approx_eq!(2.3f32.log10(), 0.361728);
1429 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1430 assert_eq!(1.0f32.log10(), 0.0);
1431 assert!(nan.log10().is_nan());
1432 assert_eq!(inf.log10(), inf);
1433 assert!(neg_inf.log10().is_nan());
1434 assert!((-2.3f32).log10().is_nan());
1435 assert_eq!((-0.0f32).log10(), neg_inf);
1436 assert_eq!(0.0f32.log10(), neg_inf);
1440 fn test_to_degrees() {
1441 let pi: f32 = consts::PI;
1442 let nan: f32 = f32::NAN;
1443 let inf: f32 = f32::INFINITY;
1444 let neg_inf: f32 = f32::NEG_INFINITY;
1445 assert_eq!(0.0f32.to_degrees(), 0.0);
1446 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1447 assert_eq!(pi.to_degrees(), 180.0);
1448 assert!(nan.to_degrees().is_nan());
1449 assert_eq!(inf.to_degrees(), inf);
1450 assert_eq!(neg_inf.to_degrees(), neg_inf);
1451 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1455 fn test_to_radians() {
1456 let pi: f32 = consts::PI;
1457 let nan: f32 = f32::NAN;
1458 let inf: f32 = f32::INFINITY;
1459 let neg_inf: f32 = f32::NEG_INFINITY;
1460 assert_eq!(0.0f32.to_radians(), 0.0);
1461 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1462 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1463 assert_eq!(180.0f32.to_radians(), pi);
1464 assert!(nan.to_radians().is_nan());
1465 assert_eq!(inf.to_radians(), inf);
1466 assert_eq!(neg_inf.to_radians(), neg_inf);
1471 assert_eq!(0.0f32.asinh(), 0.0f32);
1472 assert_eq!((-0.0f32).asinh(), -0.0f32);
1474 let inf: f32 = f32::INFINITY;
1475 let neg_inf: f32 = f32::NEG_INFINITY;
1476 let nan: f32 = f32::NAN;
1477 assert_eq!(inf.asinh(), inf);
1478 assert_eq!(neg_inf.asinh(), neg_inf);
1479 assert!(nan.asinh().is_nan());
1480 assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
1481 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1482 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1487 assert_eq!(1.0f32.acosh(), 0.0f32);
1488 assert!(0.999f32.acosh().is_nan());
1490 let inf: f32 = f32::INFINITY;
1491 let neg_inf: f32 = f32::NEG_INFINITY;
1492 let nan: f32 = f32::NAN;
1493 assert_eq!(inf.acosh(), inf);
1494 assert!(neg_inf.acosh().is_nan());
1495 assert!(nan.acosh().is_nan());
1496 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1497 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1502 assert_eq!(0.0f32.atanh(), 0.0f32);
1503 assert_eq!((-0.0f32).atanh(), -0.0f32);
1505 let inf32: f32 = f32::INFINITY;
1506 let neg_inf32: f32 = f32::NEG_INFINITY;
1507 assert_eq!(1.0f32.atanh(), inf32);
1508 assert_eq!((-1.0f32).atanh(), neg_inf32);
1510 assert!(2f64.atanh().atanh().is_nan());
1511 assert!((-2f64).atanh().atanh().is_nan());
1513 let inf64: f32 = f32::INFINITY;
1514 let neg_inf64: f32 = f32::NEG_INFINITY;
1515 let nan32: f32 = f32::NAN;
1516 assert!(inf64.atanh().is_nan());
1517 assert!(neg_inf64.atanh().is_nan());
1518 assert!(nan32.atanh().is_nan());
1520 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1521 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1525 fn test_real_consts() {
1528 let pi: f32 = consts::PI;
1529 let frac_pi_2: f32 = consts::FRAC_PI_2;
1530 let frac_pi_3: f32 = consts::FRAC_PI_3;
1531 let frac_pi_4: f32 = consts::FRAC_PI_4;
1532 let frac_pi_6: f32 = consts::FRAC_PI_6;
1533 let frac_pi_8: f32 = consts::FRAC_PI_8;
1534 let frac_1_pi: f32 = consts::FRAC_1_PI;
1535 let frac_2_pi: f32 = consts::FRAC_2_PI;
1536 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1537 let sqrt2: f32 = consts::SQRT_2;
1538 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1539 let e: f32 = consts::E;
1540 let log2_e: f32 = consts::LOG2_E;
1541 let log10_e: f32 = consts::LOG10_E;
1542 let ln_2: f32 = consts::LN_2;
1543 let ln_10: f32 = consts::LN_10;
1545 assert_approx_eq!(frac_pi_2, pi / 2f32);
1546 assert_approx_eq!(frac_pi_3, pi / 3f32);
1547 assert_approx_eq!(frac_pi_4, pi / 4f32);
1548 assert_approx_eq!(frac_pi_6, pi / 6f32);
1549 assert_approx_eq!(frac_pi_8, pi / 8f32);
1550 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1551 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1552 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1553 assert_approx_eq!(sqrt2, 2f32.sqrt());
1554 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1555 assert_approx_eq!(log2_e, e.log2());
1556 assert_approx_eq!(log10_e, e.log10());
1557 assert_approx_eq!(ln_2, 2f32.ln());
1558 assert_approx_eq!(ln_10, 10f32.ln());
1562 fn test_float_bits_conv() {
1563 assert_eq!((1f32).to_bits(), 0x3f800000);
1564 assert_eq!((12.5f32).to_bits(), 0x41480000);
1565 assert_eq!((1337f32).to_bits(), 0x44a72000);
1566 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1567 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1568 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1569 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1570 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1572 // Check that NaNs roundtrip their bits regardless of signalingness
1573 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1574 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1575 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1576 assert!(f32::from_bits(masked_nan1).is_nan());
1577 assert!(f32::from_bits(masked_nan2).is_nan());
1579 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1580 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1585 fn test_clamp_min_greater_than_max() {
1586 let _ = 1.0f32.clamp(3.0, 1.0);
1591 fn test_clamp_min_is_nan() {
1592 let _ = 1.0f32.clamp(NAN, 1.0);
1597 fn test_clamp_max_is_nan() {
1598 let _ = 1.0f32.clamp(3.0, NAN);