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::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
20 #[stable(feature = "rust1", since = "1.0.0")]
21 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use core::f32::consts;
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 #[stable(feature = "rust1", since = "1.0.0")]
45 pub fn floor(self) -> f32 {
46 // On MSVC LLVM will lower many math intrinsics to a call to the
47 // corresponding function. On MSVC, however, many of these functions
48 // aren't actually available as symbols to call, but rather they are all
49 // `static inline` functions in header files. This means that from a C
50 // perspective it's "compatible", but not so much from an ABI
51 // perspective (which we're worried about).
53 // The inline header functions always just cast to a f64 and do their
54 // operation, so we do that here as well, but only for MSVC targets.
56 // Note that there are many MSVC-specific float operations which
57 // redirect to this comment, so `floorf` is just one case of a missing
58 // function on MSVC, but there are many others elsewhere.
59 #[cfg(target_env = "msvc")]
60 return (self as f64).floor() as f32;
61 #[cfg(not(target_env = "msvc"))]
62 return unsafe { intrinsics::floorf32(self) };
65 /// Returns the smallest integer greater than or equal to a number.
73 /// assert_eq!(f.ceil(), 4.0);
74 /// assert_eq!(g.ceil(), 4.0);
76 #[stable(feature = "rust1", since = "1.0.0")]
78 pub fn ceil(self) -> f32 {
79 // see notes above in `floor`
80 #[cfg(target_env = "msvc")]
81 return (self as f64).ceil() as f32;
82 #[cfg(not(target_env = "msvc"))]
83 return unsafe { intrinsics::ceilf32(self) };
86 /// Returns the nearest integer to a number. Round half-way cases away from
95 /// assert_eq!(f.round(), 3.0);
96 /// assert_eq!(g.round(), -3.0);
98 #[stable(feature = "rust1", since = "1.0.0")]
100 pub fn round(self) -> f32 {
101 unsafe { intrinsics::roundf32(self) }
104 /// Returns the integer part of a number.
111 /// let h = -3.7_f32;
113 /// assert_eq!(f.trunc(), 3.0);
114 /// assert_eq!(g.trunc(), 3.0);
115 /// assert_eq!(h.trunc(), -3.0);
117 #[stable(feature = "rust1", since = "1.0.0")]
119 pub fn trunc(self) -> f32 {
120 unsafe { intrinsics::truncf32(self) }
123 /// Returns the fractional part of a number.
131 /// let y = -3.5_f32;
132 /// let abs_difference_x = (x.fract() - 0.5).abs();
133 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
135 /// assert!(abs_difference_x <= f32::EPSILON);
136 /// assert!(abs_difference_y <= f32::EPSILON);
138 #[stable(feature = "rust1", since = "1.0.0")]
140 pub fn fract(self) -> f32 { self - self.trunc() }
142 /// Computes the absolute value of `self`. Returns `NAN` if the
151 /// let y = -3.5_f32;
153 /// let abs_difference_x = (x.abs() - x).abs();
154 /// let abs_difference_y = (y.abs() - (-y)).abs();
156 /// assert!(abs_difference_x <= f32::EPSILON);
157 /// assert!(abs_difference_y <= f32::EPSILON);
159 /// assert!(f32::NAN.abs().is_nan());
161 #[stable(feature = "rust1", since = "1.0.0")]
163 pub fn abs(self) -> f32 {
164 unsafe { intrinsics::fabsf32(self) }
167 /// Returns a number that represents the sign of `self`.
169 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
170 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
171 /// - `NAN` if the number is `NAN`
180 /// assert_eq!(f.signum(), 1.0);
181 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
183 /// assert!(f32::NAN.signum().is_nan());
185 #[stable(feature = "rust1", since = "1.0.0")]
187 pub fn signum(self) -> f32 {
191 unsafe { intrinsics::copysignf32(1.0, self) }
195 /// Returns a number composed of the magnitude of `self` and the sign of
198 /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
199 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
205 /// #![feature(copysign)]
210 /// assert_eq!(f.copysign(0.42), 3.5_f32);
211 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
212 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
213 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
215 /// assert!(f32::NAN.copysign(1.0).is_nan());
219 #[unstable(feature="copysign", issue="55169")]
220 pub fn copysign(self, y: f32) -> f32 {
221 unsafe { intrinsics::copysignf32(self, y) }
224 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
225 /// error, yielding a more accurate result than an unfused multiply-add.
227 /// Using `mul_add` can be more performant than an unfused multiply-add if
228 /// the target architecture has a dedicated `fma` CPU instruction.
235 /// let m = 10.0_f32;
237 /// let b = 60.0_f32;
240 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
242 /// assert!(abs_difference <= f32::EPSILON);
244 #[stable(feature = "rust1", since = "1.0.0")]
246 pub fn mul_add(self, a: f32, b: f32) -> f32 {
247 unsafe { intrinsics::fmaf32(self, a, b) }
250 /// Calculates Euclidean division, the matching method for `rem_euclid`.
252 /// This computes the integer `n` such that
253 /// `self = n * rhs + self.rem_euclid(rhs)`.
254 /// In other words, the result is `self / rhs` rounded to the integer `n`
255 /// such that `self >= n * rhs`.
260 /// #![feature(euclidean_division)]
261 /// let a: f32 = 7.0;
263 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
264 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
265 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
266 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
269 #[unstable(feature = "euclidean_division", issue = "49048")]
270 pub fn div_euclid(self, rhs: f32) -> f32 {
271 let q = (self / rhs).trunc();
272 if self % rhs < 0.0 {
273 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
278 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
280 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
281 /// most cases. However, due to a floating point round-off error it can
282 /// result in `r == rhs.abs()`, violating the mathematical definition, if
283 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
284 /// This result is not an element of the function's codomain, but it is the
285 /// closest floating point number in the real numbers and thus fulfills the
286 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
292 /// #![feature(euclidean_division)]
293 /// let a: f32 = 7.0;
295 /// assert_eq!(a.rem_euclid(b), 3.0);
296 /// assert_eq!((-a).rem_euclid(b), 1.0);
297 /// assert_eq!(a.rem_euclid(-b), 3.0);
298 /// assert_eq!((-a).rem_euclid(-b), 1.0);
299 /// // limitation due to round-off error
300 /// assert!((-std::f32::EPSILON).rem_euclid(3.0) != 0.0);
303 #[unstable(feature = "euclidean_division", issue = "49048")]
304 pub fn rem_euclid(self, rhs: f32) -> f32 {
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 #[stable(feature = "rust1", since = "1.0.0")]
330 pub fn powi(self, n: i32) -> f32 {
331 unsafe { intrinsics::powif32(self, n) }
334 /// Raises a number to a floating point power.
342 /// let abs_difference = (x.powf(2.0) - x*x).abs();
344 /// assert!(abs_difference <= f32::EPSILON);
346 #[stable(feature = "rust1", since = "1.0.0")]
348 pub fn powf(self, n: f32) -> f32 {
349 // see notes above in `floor`
350 #[cfg(target_env = "msvc")]
351 return (self as f64).powf(n as f64) as f32;
352 #[cfg(not(target_env = "msvc"))]
353 return unsafe { intrinsics::powf32(self, n) };
356 /// Takes the square root of a number.
358 /// Returns NaN if `self` is a negative number.
365 /// let positive = 4.0_f32;
366 /// let negative = -4.0_f32;
368 /// let abs_difference = (positive.sqrt() - 2.0).abs();
370 /// assert!(abs_difference <= f32::EPSILON);
371 /// assert!(negative.sqrt().is_nan());
373 #[stable(feature = "rust1", since = "1.0.0")]
375 pub fn sqrt(self) -> f32 {
379 unsafe { intrinsics::sqrtf32(self) }
383 /// Returns `e^(self)`, (the exponential function).
390 /// let one = 1.0f32;
392 /// let e = one.exp();
394 /// // ln(e) - 1 == 0
395 /// let abs_difference = (e.ln() - 1.0).abs();
397 /// assert!(abs_difference <= f32::EPSILON);
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 #[stable(feature = "rust1", since = "1.0.0")]
425 pub fn exp2(self) -> f32 {
426 unsafe { intrinsics::exp2f32(self) }
429 /// Returns the natural logarithm of the number.
436 /// let one = 1.0f32;
438 /// let e = one.exp();
440 /// // ln(e) - 1 == 0
441 /// let abs_difference = (e.ln() - 1.0).abs();
443 /// assert!(abs_difference <= f32::EPSILON);
445 #[stable(feature = "rust1", since = "1.0.0")]
447 pub fn ln(self) -> f32 {
448 // see notes above in `floor`
449 #[cfg(target_env = "msvc")]
450 return (self as f64).ln() as f32;
451 #[cfg(not(target_env = "msvc"))]
452 return unsafe { intrinsics::logf32(self) };
455 /// Returns the logarithm of the number with respect to an arbitrary base.
457 /// The result may not be correctly rounded owing to implementation details;
458 /// `self.log2()` can produce more accurate results for base 2, and
459 /// `self.log10()` can produce more accurate results for base 10.
466 /// let five = 5.0f32;
468 /// // log5(5) - 1 == 0
469 /// let abs_difference = (five.log(5.0) - 1.0).abs();
471 /// assert!(abs_difference <= f32::EPSILON);
473 #[stable(feature = "rust1", since = "1.0.0")]
475 pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
477 /// Returns the base 2 logarithm of the number.
484 /// let two = 2.0f32;
486 /// // log2(2) - 1 == 0
487 /// let abs_difference = (two.log2() - 1.0).abs();
489 /// assert!(abs_difference <= f32::EPSILON);
491 #[stable(feature = "rust1", since = "1.0.0")]
493 pub fn log2(self) -> f32 {
494 #[cfg(target_os = "android")]
495 return crate::sys::android::log2f32(self);
496 #[cfg(not(target_os = "android"))]
497 return unsafe { intrinsics::log2f32(self) };
500 /// Returns the base 10 logarithm of the number.
507 /// let ten = 10.0f32;
509 /// // log10(10) - 1 == 0
510 /// let abs_difference = (ten.log10() - 1.0).abs();
512 /// assert!(abs_difference <= f32::EPSILON);
514 #[stable(feature = "rust1", since = "1.0.0")]
516 pub fn log10(self) -> f32 {
517 // see notes above in `floor`
518 #[cfg(target_env = "msvc")]
519 return (self as f64).log10() as f32;
520 #[cfg(not(target_env = "msvc"))]
521 return unsafe { intrinsics::log10f32(self) };
524 /// The positive difference of two numbers.
526 /// * If `self <= other`: `0:0`
527 /// * Else: `self - other`
537 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
538 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
540 /// assert!(abs_difference_x <= f32::EPSILON);
541 /// assert!(abs_difference_y <= f32::EPSILON);
543 #[stable(feature = "rust1", since = "1.0.0")]
545 #[rustc_deprecated(since = "1.10.0",
546 reason = "you probably meant `(self - other).abs()`: \
547 this operation is `(self - other).max(0.0)` \
548 except that `abs_sub` also propagates NaNs (also \
549 known as `fdimf` in C). If you truly need the positive \
550 difference, consider using that expression or the C function \
551 `fdimf`, depending on how you wish to handle NaN (please consider \
552 filing an issue describing your use-case too).")]
553 pub fn abs_sub(self, other: f32) -> f32 {
554 unsafe { cmath::fdimf(self, other) }
557 /// Takes the cubic root of a number.
566 /// // x^(1/3) - 2 == 0
567 /// let abs_difference = (x.cbrt() - 2.0).abs();
569 /// assert!(abs_difference <= f32::EPSILON);
571 #[stable(feature = "rust1", since = "1.0.0")]
573 pub fn cbrt(self) -> f32 {
574 unsafe { cmath::cbrtf(self) }
577 /// Calculates the length of the hypotenuse of a right-angle triangle given
578 /// legs of length `x` and `y`.
588 /// // sqrt(x^2 + y^2)
589 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
591 /// assert!(abs_difference <= f32::EPSILON);
593 #[stable(feature = "rust1", since = "1.0.0")]
595 pub fn hypot(self, other: f32) -> f32 {
596 unsafe { cmath::hypotf(self, other) }
599 /// Computes the sine of a number (in radians).
606 /// let x = f32::consts::PI/2.0;
608 /// let abs_difference = (x.sin() - 1.0).abs();
610 /// assert!(abs_difference <= f32::EPSILON);
612 #[stable(feature = "rust1", since = "1.0.0")]
614 pub fn sin(self) -> f32 {
615 // see notes in `core::f32::Float::floor`
616 #[cfg(target_env = "msvc")]
617 return (self as f64).sin() as f32;
618 #[cfg(not(target_env = "msvc"))]
619 return unsafe { intrinsics::sinf32(self) };
622 /// Computes the cosine of a number (in radians).
629 /// let x = 2.0*f32::consts::PI;
631 /// let abs_difference = (x.cos() - 1.0).abs();
633 /// assert!(abs_difference <= f32::EPSILON);
635 #[stable(feature = "rust1", since = "1.0.0")]
637 pub fn cos(self) -> f32 {
638 // see notes in `core::f32::Float::floor`
639 #[cfg(target_env = "msvc")]
640 return (self as f64).cos() as f32;
641 #[cfg(not(target_env = "msvc"))]
642 return unsafe { intrinsics::cosf32(self) };
645 /// Computes the tangent of a number (in radians).
652 /// let x = f32::consts::PI / 4.0;
653 /// let abs_difference = (x.tan() - 1.0).abs();
655 /// assert!(abs_difference <= f32::EPSILON);
657 #[stable(feature = "rust1", since = "1.0.0")]
659 pub fn tan(self) -> f32 {
660 unsafe { cmath::tanf(self) }
663 /// Computes the arcsine of a number. Return value is in radians in
664 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
672 /// let f = f32::consts::PI / 2.0;
674 /// // asin(sin(pi/2))
675 /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
677 /// assert!(abs_difference <= f32::EPSILON);
679 #[stable(feature = "rust1", since = "1.0.0")]
681 pub fn asin(self) -> f32 {
682 unsafe { cmath::asinf(self) }
685 /// Computes the arccosine of a number. Return value is in radians in
686 /// the range [0, pi] or NaN if the number is outside the range
694 /// let f = f32::consts::PI / 4.0;
696 /// // acos(cos(pi/4))
697 /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
699 /// assert!(abs_difference <= f32::EPSILON);
701 #[stable(feature = "rust1", since = "1.0.0")]
703 pub fn acos(self) -> f32 {
704 unsafe { cmath::acosf(self) }
707 /// Computes the arctangent of a number. Return value is in radians in the
708 /// range [-pi/2, pi/2];
718 /// let abs_difference = (f.tan().atan() - 1.0).abs();
720 /// assert!(abs_difference <= f32::EPSILON);
722 #[stable(feature = "rust1", since = "1.0.0")]
724 pub fn atan(self) -> f32 {
725 unsafe { cmath::atanf(self) }
728 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
730 /// * `x = 0`, `y = 0`: `0`
731 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
732 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
733 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
740 /// let pi = f32::consts::PI;
741 /// // Positive angles measured counter-clockwise
742 /// // from positive x axis
743 /// // -pi/4 radians (45 deg clockwise)
745 /// let y1 = -3.0f32;
747 /// // 3pi/4 radians (135 deg counter-clockwise)
748 /// let x2 = -3.0f32;
751 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
752 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
754 /// assert!(abs_difference_1 <= f32::EPSILON);
755 /// assert!(abs_difference_2 <= f32::EPSILON);
757 #[stable(feature = "rust1", since = "1.0.0")]
759 pub fn atan2(self, other: f32) -> f32 {
760 unsafe { cmath::atan2f(self, other) }
763 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
764 /// `(sin(x), cos(x))`.
771 /// let x = f32::consts::PI/4.0;
772 /// let f = x.sin_cos();
774 /// let abs_difference_0 = (f.0 - x.sin()).abs();
775 /// let abs_difference_1 = (f.1 - x.cos()).abs();
777 /// assert!(abs_difference_0 <= f32::EPSILON);
778 /// assert!(abs_difference_1 <= f32::EPSILON);
780 #[stable(feature = "rust1", since = "1.0.0")]
782 pub fn sin_cos(self) -> (f32, f32) {
783 (self.sin(), self.cos())
786 /// Returns `e^(self) - 1` in a way that is accurate even if the
787 /// number is close to zero.
797 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
799 /// assert!(abs_difference <= f32::EPSILON);
801 #[stable(feature = "rust1", since = "1.0.0")]
803 pub fn exp_m1(self) -> f32 {
804 unsafe { cmath::expm1f(self) }
807 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
808 /// the operations were performed separately.
815 /// let x = f32::consts::E - 1.0;
817 /// // ln(1 + (e - 1)) == ln(e) == 1
818 /// let abs_difference = (x.ln_1p() - 1.0).abs();
820 /// assert!(abs_difference <= f32::EPSILON);
822 #[stable(feature = "rust1", since = "1.0.0")]
824 pub fn ln_1p(self) -> f32 {
825 unsafe { cmath::log1pf(self) }
828 /// Hyperbolic sine function.
835 /// let e = f32::consts::E;
838 /// let f = x.sinh();
839 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
840 /// let g = (e*e - 1.0)/(2.0*e);
841 /// let abs_difference = (f - g).abs();
843 /// assert!(abs_difference <= f32::EPSILON);
845 #[stable(feature = "rust1", since = "1.0.0")]
847 pub fn sinh(self) -> f32 {
848 unsafe { cmath::sinhf(self) }
851 /// Hyperbolic cosine function.
858 /// let e = f32::consts::E;
860 /// let f = x.cosh();
861 /// // Solving cosh() at 1 gives this result
862 /// let g = (e*e + 1.0)/(2.0*e);
863 /// let abs_difference = (f - g).abs();
866 /// assert!(abs_difference <= f32::EPSILON);
868 #[stable(feature = "rust1", since = "1.0.0")]
870 pub fn cosh(self) -> f32 {
871 unsafe { cmath::coshf(self) }
874 /// Hyperbolic tangent function.
881 /// let e = f32::consts::E;
884 /// let f = x.tanh();
885 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
886 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
887 /// let abs_difference = (f - g).abs();
889 /// assert!(abs_difference <= f32::EPSILON);
891 #[stable(feature = "rust1", since = "1.0.0")]
893 pub fn tanh(self) -> f32 {
894 unsafe { cmath::tanhf(self) }
897 /// Inverse hyperbolic sine function.
905 /// let f = x.sinh().asinh();
907 /// let abs_difference = (f - x).abs();
909 /// assert!(abs_difference <= f32::EPSILON);
911 #[stable(feature = "rust1", since = "1.0.0")]
913 pub fn asinh(self) -> f32 {
914 if self == NEG_INFINITY {
917 (self + ((self * self) + 1.0).sqrt()).ln()
921 /// Inverse hyperbolic cosine function.
929 /// let f = x.cosh().acosh();
931 /// let abs_difference = (f - x).abs();
933 /// assert!(abs_difference <= f32::EPSILON);
935 #[stable(feature = "rust1", since = "1.0.0")]
937 pub fn acosh(self) -> f32 {
939 x if x < 1.0 => crate::f32::NAN,
940 x => (x + ((x * x) - 1.0).sqrt()).ln(),
944 /// Inverse hyperbolic tangent function.
951 /// let e = f32::consts::E;
952 /// let f = e.tanh().atanh();
954 /// let abs_difference = (f - e).abs();
956 /// assert!(abs_difference <= 1e-5);
958 #[stable(feature = "rust1", since = "1.0.0")]
960 pub fn atanh(self) -> f32 {
961 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
963 /// Returns max if self is greater than max, and min if self is less than min.
964 /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
969 /// #![feature(clamp)]
970 /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
971 /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
972 /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
973 /// assert!((std::f32::NAN).clamp(-2.0f32, 1.0f32).is_nan());
975 #[unstable(feature = "clamp", issue = "44095")]
977 pub fn clamp(self, min: f32, max: f32) -> f32 {
980 if x < min { x = min; }
981 if x > max { x = max; }
992 use crate::num::FpCategory as Fp;
996 test_num(10f32, 2f32);
1001 assert_eq!(NAN.min(2.0), 2.0);
1002 assert_eq!(2.0f32.min(NAN), 2.0);
1007 assert_eq!(NAN.max(2.0), 2.0);
1008 assert_eq!(2.0f32.max(NAN), 2.0);
1013 let nan: f32 = f32::NAN;
1014 assert!(nan.is_nan());
1015 assert!(!nan.is_infinite());
1016 assert!(!nan.is_finite());
1017 assert!(!nan.is_normal());
1018 assert!(nan.is_sign_positive());
1019 assert!(!nan.is_sign_negative());
1020 assert_eq!(Fp::Nan, nan.classify());
1024 fn test_infinity() {
1025 let inf: f32 = f32::INFINITY;
1026 assert!(inf.is_infinite());
1027 assert!(!inf.is_finite());
1028 assert!(inf.is_sign_positive());
1029 assert!(!inf.is_sign_negative());
1030 assert!(!inf.is_nan());
1031 assert!(!inf.is_normal());
1032 assert_eq!(Fp::Infinite, inf.classify());
1036 fn test_neg_infinity() {
1037 let neg_inf: f32 = f32::NEG_INFINITY;
1038 assert!(neg_inf.is_infinite());
1039 assert!(!neg_inf.is_finite());
1040 assert!(!neg_inf.is_sign_positive());
1041 assert!(neg_inf.is_sign_negative());
1042 assert!(!neg_inf.is_nan());
1043 assert!(!neg_inf.is_normal());
1044 assert_eq!(Fp::Infinite, neg_inf.classify());
1049 let zero: f32 = 0.0f32;
1050 assert_eq!(0.0, zero);
1051 assert!(!zero.is_infinite());
1052 assert!(zero.is_finite());
1053 assert!(zero.is_sign_positive());
1054 assert!(!zero.is_sign_negative());
1055 assert!(!zero.is_nan());
1056 assert!(!zero.is_normal());
1057 assert_eq!(Fp::Zero, zero.classify());
1061 fn test_neg_zero() {
1062 let neg_zero: f32 = -0.0;
1063 assert_eq!(0.0, neg_zero);
1064 assert!(!neg_zero.is_infinite());
1065 assert!(neg_zero.is_finite());
1066 assert!(!neg_zero.is_sign_positive());
1067 assert!(neg_zero.is_sign_negative());
1068 assert!(!neg_zero.is_nan());
1069 assert!(!neg_zero.is_normal());
1070 assert_eq!(Fp::Zero, neg_zero.classify());
1075 let one: f32 = 1.0f32;
1076 assert_eq!(1.0, one);
1077 assert!(!one.is_infinite());
1078 assert!(one.is_finite());
1079 assert!(one.is_sign_positive());
1080 assert!(!one.is_sign_negative());
1081 assert!(!one.is_nan());
1082 assert!(one.is_normal());
1083 assert_eq!(Fp::Normal, one.classify());
1088 let nan: f32 = f32::NAN;
1089 let inf: f32 = f32::INFINITY;
1090 let neg_inf: f32 = f32::NEG_INFINITY;
1091 assert!(nan.is_nan());
1092 assert!(!0.0f32.is_nan());
1093 assert!(!5.3f32.is_nan());
1094 assert!(!(-10.732f32).is_nan());
1095 assert!(!inf.is_nan());
1096 assert!(!neg_inf.is_nan());
1100 fn test_is_infinite() {
1101 let nan: f32 = f32::NAN;
1102 let inf: f32 = f32::INFINITY;
1103 let neg_inf: f32 = f32::NEG_INFINITY;
1104 assert!(!nan.is_infinite());
1105 assert!(inf.is_infinite());
1106 assert!(neg_inf.is_infinite());
1107 assert!(!0.0f32.is_infinite());
1108 assert!(!42.8f32.is_infinite());
1109 assert!(!(-109.2f32).is_infinite());
1113 fn test_is_finite() {
1114 let nan: f32 = f32::NAN;
1115 let inf: f32 = f32::INFINITY;
1116 let neg_inf: f32 = f32::NEG_INFINITY;
1117 assert!(!nan.is_finite());
1118 assert!(!inf.is_finite());
1119 assert!(!neg_inf.is_finite());
1120 assert!(0.0f32.is_finite());
1121 assert!(42.8f32.is_finite());
1122 assert!((-109.2f32).is_finite());
1126 fn test_is_normal() {
1127 let nan: f32 = f32::NAN;
1128 let inf: f32 = f32::INFINITY;
1129 let neg_inf: f32 = f32::NEG_INFINITY;
1130 let zero: f32 = 0.0f32;
1131 let neg_zero: f32 = -0.0;
1132 assert!(!nan.is_normal());
1133 assert!(!inf.is_normal());
1134 assert!(!neg_inf.is_normal());
1135 assert!(!zero.is_normal());
1136 assert!(!neg_zero.is_normal());
1137 assert!(1f32.is_normal());
1138 assert!(1e-37f32.is_normal());
1139 assert!(!1e-38f32.is_normal());
1143 fn test_classify() {
1144 let nan: f32 = f32::NAN;
1145 let inf: f32 = f32::INFINITY;
1146 let neg_inf: f32 = f32::NEG_INFINITY;
1147 let zero: f32 = 0.0f32;
1148 let neg_zero: f32 = -0.0;
1149 assert_eq!(nan.classify(), Fp::Nan);
1150 assert_eq!(inf.classify(), Fp::Infinite);
1151 assert_eq!(neg_inf.classify(), Fp::Infinite);
1152 assert_eq!(zero.classify(), Fp::Zero);
1153 assert_eq!(neg_zero.classify(), Fp::Zero);
1154 assert_eq!(1f32.classify(), Fp::Normal);
1155 assert_eq!(1e-37f32.classify(), Fp::Normal);
1156 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1161 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1162 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1163 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1164 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1165 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1166 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1167 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1168 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1169 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1170 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1175 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1176 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1177 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1178 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1179 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1180 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1181 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1182 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1183 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1184 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1189 assert_approx_eq!(1.0f32.round(), 1.0f32);
1190 assert_approx_eq!(1.3f32.round(), 1.0f32);
1191 assert_approx_eq!(1.5f32.round(), 2.0f32);
1192 assert_approx_eq!(1.7f32.round(), 2.0f32);
1193 assert_approx_eq!(0.0f32.round(), 0.0f32);
1194 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1195 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1196 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1197 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1198 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1203 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1204 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1205 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1206 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1207 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1208 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1209 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1210 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1211 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1212 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1217 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1218 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1219 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1220 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1221 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1222 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1223 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1224 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1225 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1226 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1231 assert_eq!(INFINITY.abs(), INFINITY);
1232 assert_eq!(1f32.abs(), 1f32);
1233 assert_eq!(0f32.abs(), 0f32);
1234 assert_eq!((-0f32).abs(), 0f32);
1235 assert_eq!((-1f32).abs(), 1f32);
1236 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1237 assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1238 assert!(NAN.abs().is_nan());
1243 assert_eq!(INFINITY.signum(), 1f32);
1244 assert_eq!(1f32.signum(), 1f32);
1245 assert_eq!(0f32.signum(), 1f32);
1246 assert_eq!((-0f32).signum(), -1f32);
1247 assert_eq!((-1f32).signum(), -1f32);
1248 assert_eq!(NEG_INFINITY.signum(), -1f32);
1249 assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1250 assert!(NAN.signum().is_nan());
1254 fn test_is_sign_positive() {
1255 assert!(INFINITY.is_sign_positive());
1256 assert!(1f32.is_sign_positive());
1257 assert!(0f32.is_sign_positive());
1258 assert!(!(-0f32).is_sign_positive());
1259 assert!(!(-1f32).is_sign_positive());
1260 assert!(!NEG_INFINITY.is_sign_positive());
1261 assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1262 assert!(NAN.is_sign_positive());
1263 assert!(!(-NAN).is_sign_positive());
1267 fn test_is_sign_negative() {
1268 assert!(!INFINITY.is_sign_negative());
1269 assert!(!1f32.is_sign_negative());
1270 assert!(!0f32.is_sign_negative());
1271 assert!((-0f32).is_sign_negative());
1272 assert!((-1f32).is_sign_negative());
1273 assert!(NEG_INFINITY.is_sign_negative());
1274 assert!((1f32/NEG_INFINITY).is_sign_negative());
1275 assert!(!NAN.is_sign_negative());
1276 assert!((-NAN).is_sign_negative());
1281 let nan: f32 = f32::NAN;
1282 let inf: f32 = f32::INFINITY;
1283 let neg_inf: f32 = f32::NEG_INFINITY;
1284 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1285 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1286 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1287 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1288 assert!(nan.mul_add(7.8, 9.0).is_nan());
1289 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1290 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1291 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1292 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1297 let nan: f32 = f32::NAN;
1298 let inf: f32 = f32::INFINITY;
1299 let neg_inf: f32 = f32::NEG_INFINITY;
1300 assert_eq!(1.0f32.recip(), 1.0);
1301 assert_eq!(2.0f32.recip(), 0.5);
1302 assert_eq!((-0.4f32).recip(), -2.5);
1303 assert_eq!(0.0f32.recip(), inf);
1304 assert!(nan.recip().is_nan());
1305 assert_eq!(inf.recip(), 0.0);
1306 assert_eq!(neg_inf.recip(), 0.0);
1311 let nan: f32 = f32::NAN;
1312 let inf: f32 = f32::INFINITY;
1313 let neg_inf: f32 = f32::NEG_INFINITY;
1314 assert_eq!(1.0f32.powi(1), 1.0);
1315 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1316 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1317 assert_eq!(8.3f32.powi(0), 1.0);
1318 assert!(nan.powi(2).is_nan());
1319 assert_eq!(inf.powi(3), inf);
1320 assert_eq!(neg_inf.powi(2), inf);
1325 let nan: f32 = f32::NAN;
1326 let inf: f32 = f32::INFINITY;
1327 let neg_inf: f32 = f32::NEG_INFINITY;
1328 assert_eq!(1.0f32.powf(1.0), 1.0);
1329 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1330 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1331 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1332 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1333 assert_eq!(8.3f32.powf(0.0), 1.0);
1334 assert!(nan.powf(2.0).is_nan());
1335 assert_eq!(inf.powf(2.0), inf);
1336 assert_eq!(neg_inf.powf(3.0), neg_inf);
1340 fn test_sqrt_domain() {
1341 assert!(NAN.sqrt().is_nan());
1342 assert!(NEG_INFINITY.sqrt().is_nan());
1343 assert!((-1.0f32).sqrt().is_nan());
1344 assert_eq!((-0.0f32).sqrt(), -0.0);
1345 assert_eq!(0.0f32.sqrt(), 0.0);
1346 assert_eq!(1.0f32.sqrt(), 1.0);
1347 assert_eq!(INFINITY.sqrt(), INFINITY);
1352 assert_eq!(1.0, 0.0f32.exp());
1353 assert_approx_eq!(2.718282, 1.0f32.exp());
1354 assert_approx_eq!(148.413162, 5.0f32.exp());
1356 let inf: f32 = f32::INFINITY;
1357 let neg_inf: f32 = f32::NEG_INFINITY;
1358 let nan: f32 = f32::NAN;
1359 assert_eq!(inf, inf.exp());
1360 assert_eq!(0.0, neg_inf.exp());
1361 assert!(nan.exp().is_nan());
1366 assert_eq!(32.0, 5.0f32.exp2());
1367 assert_eq!(1.0, 0.0f32.exp2());
1369 let inf: f32 = f32::INFINITY;
1370 let neg_inf: f32 = f32::NEG_INFINITY;
1371 let nan: f32 = f32::NAN;
1372 assert_eq!(inf, inf.exp2());
1373 assert_eq!(0.0, neg_inf.exp2());
1374 assert!(nan.exp2().is_nan());
1379 let nan: f32 = f32::NAN;
1380 let inf: f32 = f32::INFINITY;
1381 let neg_inf: f32 = f32::NEG_INFINITY;
1382 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1383 assert!(nan.ln().is_nan());
1384 assert_eq!(inf.ln(), inf);
1385 assert!(neg_inf.ln().is_nan());
1386 assert!((-2.3f32).ln().is_nan());
1387 assert_eq!((-0.0f32).ln(), neg_inf);
1388 assert_eq!(0.0f32.ln(), neg_inf);
1389 assert_approx_eq!(4.0f32.ln(), 1.386294);
1394 let nan: f32 = f32::NAN;
1395 let inf: f32 = f32::INFINITY;
1396 let neg_inf: f32 = f32::NEG_INFINITY;
1397 assert_eq!(10.0f32.log(10.0), 1.0);
1398 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1399 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1400 assert!(1.0f32.log(1.0).is_nan());
1401 assert!(1.0f32.log(-13.9).is_nan());
1402 assert!(nan.log(2.3).is_nan());
1403 assert_eq!(inf.log(10.0), inf);
1404 assert!(neg_inf.log(8.8).is_nan());
1405 assert!((-2.3f32).log(0.1).is_nan());
1406 assert_eq!((-0.0f32).log(2.0), neg_inf);
1407 assert_eq!(0.0f32.log(7.0), neg_inf);
1412 let nan: f32 = f32::NAN;
1413 let inf: f32 = f32::INFINITY;
1414 let neg_inf: f32 = f32::NEG_INFINITY;
1415 assert_approx_eq!(10.0f32.log2(), 3.321928);
1416 assert_approx_eq!(2.3f32.log2(), 1.201634);
1417 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1418 assert!(nan.log2().is_nan());
1419 assert_eq!(inf.log2(), inf);
1420 assert!(neg_inf.log2().is_nan());
1421 assert!((-2.3f32).log2().is_nan());
1422 assert_eq!((-0.0f32).log2(), neg_inf);
1423 assert_eq!(0.0f32.log2(), neg_inf);
1428 let nan: f32 = f32::NAN;
1429 let inf: f32 = f32::INFINITY;
1430 let neg_inf: f32 = f32::NEG_INFINITY;
1431 assert_eq!(10.0f32.log10(), 1.0);
1432 assert_approx_eq!(2.3f32.log10(), 0.361728);
1433 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1434 assert_eq!(1.0f32.log10(), 0.0);
1435 assert!(nan.log10().is_nan());
1436 assert_eq!(inf.log10(), inf);
1437 assert!(neg_inf.log10().is_nan());
1438 assert!((-2.3f32).log10().is_nan());
1439 assert_eq!((-0.0f32).log10(), neg_inf);
1440 assert_eq!(0.0f32.log10(), neg_inf);
1444 fn test_to_degrees() {
1445 let pi: f32 = consts::PI;
1446 let nan: f32 = f32::NAN;
1447 let inf: f32 = f32::INFINITY;
1448 let neg_inf: f32 = f32::NEG_INFINITY;
1449 assert_eq!(0.0f32.to_degrees(), 0.0);
1450 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1451 assert_eq!(pi.to_degrees(), 180.0);
1452 assert!(nan.to_degrees().is_nan());
1453 assert_eq!(inf.to_degrees(), inf);
1454 assert_eq!(neg_inf.to_degrees(), neg_inf);
1455 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1459 fn test_to_radians() {
1460 let pi: f32 = consts::PI;
1461 let nan: f32 = f32::NAN;
1462 let inf: f32 = f32::INFINITY;
1463 let neg_inf: f32 = f32::NEG_INFINITY;
1464 assert_eq!(0.0f32.to_radians(), 0.0);
1465 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1466 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1467 assert_eq!(180.0f32.to_radians(), pi);
1468 assert!(nan.to_radians().is_nan());
1469 assert_eq!(inf.to_radians(), inf);
1470 assert_eq!(neg_inf.to_radians(), neg_inf);
1475 assert_eq!(0.0f32.asinh(), 0.0f32);
1476 assert_eq!((-0.0f32).asinh(), -0.0f32);
1478 let inf: f32 = f32::INFINITY;
1479 let neg_inf: f32 = f32::NEG_INFINITY;
1480 let nan: f32 = f32::NAN;
1481 assert_eq!(inf.asinh(), inf);
1482 assert_eq!(neg_inf.asinh(), neg_inf);
1483 assert!(nan.asinh().is_nan());
1484 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1485 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1490 assert_eq!(1.0f32.acosh(), 0.0f32);
1491 assert!(0.999f32.acosh().is_nan());
1493 let inf: f32 = f32::INFINITY;
1494 let neg_inf: f32 = f32::NEG_INFINITY;
1495 let nan: f32 = f32::NAN;
1496 assert_eq!(inf.acosh(), inf);
1497 assert!(neg_inf.acosh().is_nan());
1498 assert!(nan.acosh().is_nan());
1499 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1500 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1505 assert_eq!(0.0f32.atanh(), 0.0f32);
1506 assert_eq!((-0.0f32).atanh(), -0.0f32);
1508 let inf32: f32 = f32::INFINITY;
1509 let neg_inf32: f32 = f32::NEG_INFINITY;
1510 assert_eq!(1.0f32.atanh(), inf32);
1511 assert_eq!((-1.0f32).atanh(), neg_inf32);
1513 assert!(2f64.atanh().atanh().is_nan());
1514 assert!((-2f64).atanh().atanh().is_nan());
1516 let inf64: f32 = f32::INFINITY;
1517 let neg_inf64: f32 = f32::NEG_INFINITY;
1518 let nan32: f32 = f32::NAN;
1519 assert!(inf64.atanh().is_nan());
1520 assert!(neg_inf64.atanh().is_nan());
1521 assert!(nan32.atanh().is_nan());
1523 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1524 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1528 fn test_real_consts() {
1531 let pi: f32 = consts::PI;
1532 let frac_pi_2: f32 = consts::FRAC_PI_2;
1533 let frac_pi_3: f32 = consts::FRAC_PI_3;
1534 let frac_pi_4: f32 = consts::FRAC_PI_4;
1535 let frac_pi_6: f32 = consts::FRAC_PI_6;
1536 let frac_pi_8: f32 = consts::FRAC_PI_8;
1537 let frac_1_pi: f32 = consts::FRAC_1_PI;
1538 let frac_2_pi: f32 = consts::FRAC_2_PI;
1539 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1540 let sqrt2: f32 = consts::SQRT_2;
1541 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1542 let e: f32 = consts::E;
1543 let log2_e: f32 = consts::LOG2_E;
1544 let log10_e: f32 = consts::LOG10_E;
1545 let ln_2: f32 = consts::LN_2;
1546 let ln_10: f32 = consts::LN_10;
1548 assert_approx_eq!(frac_pi_2, pi / 2f32);
1549 assert_approx_eq!(frac_pi_3, pi / 3f32);
1550 assert_approx_eq!(frac_pi_4, pi / 4f32);
1551 assert_approx_eq!(frac_pi_6, pi / 6f32);
1552 assert_approx_eq!(frac_pi_8, pi / 8f32);
1553 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1554 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1555 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1556 assert_approx_eq!(sqrt2, 2f32.sqrt());
1557 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1558 assert_approx_eq!(log2_e, e.log2());
1559 assert_approx_eq!(log10_e, e.log10());
1560 assert_approx_eq!(ln_2, 2f32.ln());
1561 assert_approx_eq!(ln_10, 10f32.ln());
1565 fn test_float_bits_conv() {
1566 assert_eq!((1f32).to_bits(), 0x3f800000);
1567 assert_eq!((12.5f32).to_bits(), 0x41480000);
1568 assert_eq!((1337f32).to_bits(), 0x44a72000);
1569 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1570 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1571 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1572 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1573 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1575 // Check that NaNs roundtrip their bits regardless of signalingness
1576 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1577 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1578 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1579 assert!(f32::from_bits(masked_nan1).is_nan());
1580 assert!(f32::from_bits(masked_nan2).is_nan());
1582 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1583 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1588 fn test_clamp_min_greater_than_max() {
1589 1.0f32.clamp(3.0, 1.0);
1594 fn test_clamp_min_is_nan() {
1595 1.0f32.clamp(NAN, 1.0);
1600 fn test_clamp_max_is_nan() {
1601 1.0f32.clamp(3.0, NAN);