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 unsafe { intrinsics::floorf32(self) }
50 /// Returns the smallest integer greater than or equal to a number.
58 /// assert_eq!(f.ceil(), 4.0);
59 /// assert_eq!(g.ceil(), 4.0);
61 #[must_use = "method returns a new number and does not mutate the original value"]
62 #[stable(feature = "rust1", since = "1.0.0")]
64 pub fn ceil(self) -> f32 {
65 unsafe { intrinsics::ceilf32(self) }
68 /// Returns the nearest integer to a number. Round half-way cases away from
77 /// assert_eq!(f.round(), 3.0);
78 /// assert_eq!(g.round(), -3.0);
80 #[must_use = "method returns a new number and does not mutate the original value"]
81 #[stable(feature = "rust1", since = "1.0.0")]
83 pub fn round(self) -> f32 {
84 unsafe { intrinsics::roundf32(self) }
87 /// Returns the integer part of a number.
96 /// assert_eq!(f.trunc(), 3.0);
97 /// assert_eq!(g.trunc(), 3.0);
98 /// assert_eq!(h.trunc(), -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 trunc(self) -> f32 {
104 unsafe { intrinsics::truncf32(self) }
107 /// Returns the fractional part of a number.
115 /// let y = -3.5_f32;
116 /// let abs_difference_x = (x.fract() - 0.5).abs();
117 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
119 /// assert!(abs_difference_x <= f32::EPSILON);
120 /// assert!(abs_difference_y <= f32::EPSILON);
122 #[must_use = "method returns a new number and does not mutate the original value"]
123 #[stable(feature = "rust1", since = "1.0.0")]
125 pub fn fract(self) -> f32 {
129 /// Computes the absolute value of `self`. Returns `NAN` if the
138 /// let y = -3.5_f32;
140 /// let abs_difference_x = (x.abs() - x).abs();
141 /// let abs_difference_y = (y.abs() - (-y)).abs();
143 /// assert!(abs_difference_x <= f32::EPSILON);
144 /// assert!(abs_difference_y <= f32::EPSILON);
146 /// assert!(f32::NAN.abs().is_nan());
148 #[must_use = "method returns a new number and does not mutate the original value"]
149 #[stable(feature = "rust1", since = "1.0.0")]
151 pub fn abs(self) -> f32 {
152 unsafe { intrinsics::fabsf32(self) }
155 /// Returns a number that represents the sign of `self`.
157 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
158 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
159 /// - `NAN` if the number is `NAN`
168 /// assert_eq!(f.signum(), 1.0);
169 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
171 /// assert!(f32::NAN.signum().is_nan());
173 #[must_use = "method returns a new number and does not mutate the original value"]
174 #[stable(feature = "rust1", since = "1.0.0")]
176 pub fn signum(self) -> f32 {
177 if self.is_nan() { NAN } else { 1.0_f32.copysign(self) }
180 /// Returns a number composed of the magnitude of `self` and the sign of
183 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
184 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
185 /// `sign` is returned.
194 /// assert_eq!(f.copysign(0.42), 3.5_f32);
195 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
196 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
197 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
199 /// assert!(f32::NAN.copysign(1.0).is_nan());
201 #[must_use = "method returns a new number and does not mutate the original value"]
203 #[stable(feature = "copysign", since = "1.35.0")]
204 pub fn copysign(self, sign: f32) -> f32 {
205 unsafe { intrinsics::copysignf32(self, sign) }
208 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
209 /// error, yielding a more accurate result than an unfused multiply-add.
211 /// Using `mul_add` can be more performant than an unfused multiply-add if
212 /// the target architecture has a dedicated `fma` CPU instruction.
219 /// let m = 10.0_f32;
221 /// let b = 60.0_f32;
224 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
226 /// assert!(abs_difference <= f32::EPSILON);
228 #[must_use = "method returns a new number and does not mutate the original value"]
229 #[stable(feature = "rust1", since = "1.0.0")]
231 pub fn mul_add(self, a: f32, b: f32) -> f32 {
232 unsafe { intrinsics::fmaf32(self, a, b) }
235 /// Calculates Euclidean division, the matching method for `rem_euclid`.
237 /// This computes the integer `n` such that
238 /// `self = n * rhs + self.rem_euclid(rhs)`.
239 /// In other words, the result is `self / rhs` rounded to the integer `n`
240 /// such that `self >= n * rhs`.
245 /// let a: f32 = 7.0;
247 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
248 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
249 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
250 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
252 #[must_use = "method returns a new number and does not mutate the original value"]
254 #[stable(feature = "euclidean_division", since = "1.38.0")]
255 pub fn div_euclid(self, rhs: f32) -> f32 {
256 let q = (self / rhs).trunc();
257 if self % rhs < 0.0 {
258 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
263 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
265 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
266 /// most cases. However, due to a floating point round-off error it can
267 /// result in `r == rhs.abs()`, violating the mathematical definition, if
268 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
269 /// This result is not an element of the function's codomain, but it is the
270 /// closest floating point number in the real numbers and thus fulfills the
271 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
277 /// let a: f32 = 7.0;
279 /// assert_eq!(a.rem_euclid(b), 3.0);
280 /// assert_eq!((-a).rem_euclid(b), 1.0);
281 /// assert_eq!(a.rem_euclid(-b), 3.0);
282 /// assert_eq!((-a).rem_euclid(-b), 1.0);
283 /// // limitation due to round-off error
284 /// assert!((-std::f32::EPSILON).rem_euclid(3.0) != 0.0);
286 #[must_use = "method returns a new number and does not mutate the original value"]
288 #[stable(feature = "euclidean_division", since = "1.38.0")]
289 pub fn rem_euclid(self, rhs: f32) -> f32 {
291 if r < 0.0 { r + rhs.abs() } else { r }
294 /// Raises a number to an integer power.
296 /// Using this function is generally faster than using `powf`
304 /// let abs_difference = (x.powi(2) - (x * x)).abs();
306 /// assert!(abs_difference <= f32::EPSILON);
308 #[must_use = "method returns a new number and does not mutate the original value"]
309 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn powi(self, n: i32) -> f32 {
312 unsafe { intrinsics::powif32(self, n) }
315 /// Raises a number to a floating point power.
323 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
325 /// assert!(abs_difference <= f32::EPSILON);
327 #[must_use = "method returns a new number and does not mutate the original value"]
328 #[stable(feature = "rust1", since = "1.0.0")]
330 pub fn powf(self, n: f32) -> f32 {
331 unsafe { intrinsics::powf32(self, n) }
334 /// Takes the square root of a number.
336 /// Returns NaN if `self` is a negative number.
343 /// let positive = 4.0_f32;
344 /// let negative = -4.0_f32;
346 /// let abs_difference = (positive.sqrt() - 2.0).abs();
348 /// assert!(abs_difference <= f32::EPSILON);
349 /// assert!(negative.sqrt().is_nan());
351 #[must_use = "method returns a new number and does not mutate the original value"]
352 #[stable(feature = "rust1", since = "1.0.0")]
354 pub fn sqrt(self) -> f32 {
355 unsafe { intrinsics::sqrtf32(self) }
358 /// Returns `e^(self)`, (the exponential function).
365 /// let one = 1.0f32;
367 /// let e = one.exp();
369 /// // ln(e) - 1 == 0
370 /// let abs_difference = (e.ln() - 1.0).abs();
372 /// assert!(abs_difference <= f32::EPSILON);
374 #[must_use = "method returns a new number and does not mutate the original value"]
375 #[stable(feature = "rust1", since = "1.0.0")]
377 pub fn exp(self) -> f32 {
378 unsafe { intrinsics::expf32(self) }
381 /// Returns `2^(self)`.
391 /// let abs_difference = (f.exp2() - 4.0).abs();
393 /// assert!(abs_difference <= f32::EPSILON);
395 #[must_use = "method returns a new number and does not mutate the original value"]
396 #[stable(feature = "rust1", since = "1.0.0")]
398 pub fn exp2(self) -> f32 {
399 unsafe { intrinsics::exp2f32(self) }
402 /// Returns the natural logarithm of the number.
409 /// let one = 1.0f32;
411 /// let e = one.exp();
413 /// // ln(e) - 1 == 0
414 /// let abs_difference = (e.ln() - 1.0).abs();
416 /// assert!(abs_difference <= f32::EPSILON);
418 #[must_use = "method returns a new number and does not mutate the original value"]
419 #[stable(feature = "rust1", since = "1.0.0")]
421 pub fn ln(self) -> f32 {
422 unsafe { intrinsics::logf32(self) }
425 /// Returns the logarithm of the number with respect to an arbitrary base.
427 /// The result may not be correctly rounded owing to implementation details;
428 /// `self.log2()` can produce more accurate results for base 2, and
429 /// `self.log10()` can produce more accurate results for base 10.
436 /// let five = 5.0f32;
438 /// // log5(5) - 1 == 0
439 /// let abs_difference = (five.log(5.0) - 1.0).abs();
441 /// assert!(abs_difference <= f32::EPSILON);
443 #[must_use = "method returns a new number and does not mutate the original value"]
444 #[stable(feature = "rust1", since = "1.0.0")]
446 pub fn log(self, base: f32) -> f32 {
447 self.ln() / base.ln()
450 /// Returns the base 2 logarithm of the number.
457 /// let two = 2.0f32;
459 /// // log2(2) - 1 == 0
460 /// let abs_difference = (two.log2() - 1.0).abs();
462 /// assert!(abs_difference <= f32::EPSILON);
464 #[must_use = "method returns a new number and does not mutate the original value"]
465 #[stable(feature = "rust1", since = "1.0.0")]
467 pub fn log2(self) -> f32 {
468 #[cfg(target_os = "android")]
469 return crate::sys::android::log2f32(self);
470 #[cfg(not(target_os = "android"))]
471 return unsafe { intrinsics::log2f32(self) };
474 /// Returns the base 10 logarithm of the number.
481 /// let ten = 10.0f32;
483 /// // log10(10) - 1 == 0
484 /// let abs_difference = (ten.log10() - 1.0).abs();
486 /// assert!(abs_difference <= f32::EPSILON);
488 #[must_use = "method returns a new number and does not mutate the original value"]
489 #[stable(feature = "rust1", since = "1.0.0")]
491 pub fn log10(self) -> f32 {
492 unsafe { intrinsics::log10f32(self) }
495 /// The positive difference of two numbers.
497 /// * If `self <= other`: `0:0`
498 /// * Else: `self - other`
508 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
509 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
511 /// assert!(abs_difference_x <= f32::EPSILON);
512 /// assert!(abs_difference_y <= f32::EPSILON);
514 #[must_use = "method returns a new number and does not mutate the original value"]
515 #[stable(feature = "rust1", since = "1.0.0")]
519 reason = "you probably meant `(self - other).abs()`: \
520 this operation is `(self - other).max(0.0)` \
521 except that `abs_sub` also propagates NaNs (also \
522 known as `fdimf` in C). If you truly need the positive \
523 difference, consider using that expression or the C function \
524 `fdimf`, depending on how you wish to handle NaN (please consider \
525 filing an issue describing your use-case too)."
527 pub fn abs_sub(self, other: f32) -> f32 {
528 unsafe { cmath::fdimf(self, other) }
531 /// Takes the cubic root of a number.
540 /// // x^(1/3) - 2 == 0
541 /// let abs_difference = (x.cbrt() - 2.0).abs();
543 /// assert!(abs_difference <= f32::EPSILON);
545 #[must_use = "method returns a new number and does not mutate the original value"]
546 #[stable(feature = "rust1", since = "1.0.0")]
548 pub fn cbrt(self) -> f32 {
549 unsafe { cmath::cbrtf(self) }
552 /// Calculates the length of the hypotenuse of a right-angle triangle given
553 /// legs of length `x` and `y`.
563 /// // sqrt(x^2 + y^2)
564 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
566 /// assert!(abs_difference <= f32::EPSILON);
568 #[must_use = "method returns a new number and does not mutate the original value"]
569 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn hypot(self, other: f32) -> f32 {
572 unsafe { cmath::hypotf(self, other) }
575 /// Computes the sine of a number (in radians).
582 /// let x = f32::consts::FRAC_PI_2;
584 /// let abs_difference = (x.sin() - 1.0).abs();
586 /// assert!(abs_difference <= f32::EPSILON);
588 #[must_use = "method returns a new number and does not mutate the original value"]
589 #[stable(feature = "rust1", since = "1.0.0")]
591 pub fn sin(self) -> f32 {
592 unsafe { intrinsics::sinf32(self) }
595 /// Computes the cosine of a number (in radians).
602 /// let x = 2.0 * f32::consts::PI;
604 /// let abs_difference = (x.cos() - 1.0).abs();
606 /// assert!(abs_difference <= f32::EPSILON);
608 #[must_use = "method returns a new number and does not mutate the original value"]
609 #[stable(feature = "rust1", since = "1.0.0")]
611 pub fn cos(self) -> f32 {
612 unsafe { intrinsics::cosf32(self) }
615 /// Computes the tangent of a number (in radians).
622 /// let x = f32::consts::FRAC_PI_4;
623 /// let abs_difference = (x.tan() - 1.0).abs();
625 /// assert!(abs_difference <= f32::EPSILON);
627 #[must_use = "method returns a new number and does not mutate the original value"]
628 #[stable(feature = "rust1", since = "1.0.0")]
630 pub fn tan(self) -> f32 {
631 unsafe { cmath::tanf(self) }
634 /// Computes the arcsine of a number. Return value is in radians in
635 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
643 /// let f = f32::consts::FRAC_PI_2;
645 /// // asin(sin(pi/2))
646 /// let abs_difference = (f.sin().asin() - f32::consts::FRAC_PI_2).abs();
648 /// assert!(abs_difference <= f32::EPSILON);
650 #[must_use = "method returns a new number and does not mutate the original value"]
651 #[stable(feature = "rust1", since = "1.0.0")]
653 pub fn asin(self) -> f32 {
654 unsafe { cmath::asinf(self) }
657 /// Computes the arccosine of a number. Return value is in radians in
658 /// the range [0, pi] or NaN if the number is outside the range
666 /// let f = f32::consts::FRAC_PI_4;
668 /// // acos(cos(pi/4))
669 /// let abs_difference = (f.cos().acos() - f32::consts::FRAC_PI_4).abs();
671 /// assert!(abs_difference <= f32::EPSILON);
673 #[must_use = "method returns a new number and does not mutate the original value"]
674 #[stable(feature = "rust1", since = "1.0.0")]
676 pub fn acos(self) -> f32 {
677 unsafe { cmath::acosf(self) }
680 /// Computes the arctangent of a number. Return value is in radians in the
681 /// range [-pi/2, pi/2];
691 /// let abs_difference = (f.tan().atan() - 1.0).abs();
693 /// assert!(abs_difference <= f32::EPSILON);
695 #[must_use = "method returns a new number and does not mutate the original value"]
696 #[stable(feature = "rust1", since = "1.0.0")]
698 pub fn atan(self) -> f32 {
699 unsafe { cmath::atanf(self) }
702 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
704 /// * `x = 0`, `y = 0`: `0`
705 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
706 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
707 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
714 /// // Positive angles measured counter-clockwise
715 /// // from positive x axis
716 /// // -pi/4 radians (45 deg clockwise)
718 /// let y1 = -3.0f32;
720 /// // 3pi/4 radians (135 deg counter-clockwise)
721 /// let x2 = -3.0f32;
724 /// let abs_difference_1 = (y1.atan2(x1) - (-f32::consts::FRAC_PI_4)).abs();
725 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * f32::consts::FRAC_PI_4)).abs();
727 /// assert!(abs_difference_1 <= f32::EPSILON);
728 /// assert!(abs_difference_2 <= f32::EPSILON);
730 #[must_use = "method returns a new number and does not mutate the original value"]
731 #[stable(feature = "rust1", since = "1.0.0")]
733 pub fn atan2(self, other: f32) -> f32 {
734 unsafe { cmath::atan2f(self, other) }
737 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
738 /// `(sin(x), cos(x))`.
745 /// let x = f32::consts::FRAC_PI_4;
746 /// let f = x.sin_cos();
748 /// let abs_difference_0 = (f.0 - x.sin()).abs();
749 /// let abs_difference_1 = (f.1 - x.cos()).abs();
751 /// assert!(abs_difference_0 <= f32::EPSILON);
752 /// assert!(abs_difference_1 <= f32::EPSILON);
754 #[stable(feature = "rust1", since = "1.0.0")]
756 pub fn sin_cos(self) -> (f32, f32) {
757 (self.sin(), self.cos())
760 /// Returns `e^(self) - 1` in a way that is accurate even if the
761 /// number is close to zero.
771 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
773 /// assert!(abs_difference <= f32::EPSILON);
775 #[must_use = "method returns a new number and does not mutate the original value"]
776 #[stable(feature = "rust1", since = "1.0.0")]
778 pub fn exp_m1(self) -> f32 {
779 unsafe { cmath::expm1f(self) }
782 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
783 /// the operations were performed separately.
790 /// let x = f32::consts::E - 1.0;
792 /// // ln(1 + (e - 1)) == ln(e) == 1
793 /// let abs_difference = (x.ln_1p() - 1.0).abs();
795 /// assert!(abs_difference <= f32::EPSILON);
797 #[must_use = "method returns a new number and does not mutate the original value"]
798 #[stable(feature = "rust1", since = "1.0.0")]
800 pub fn ln_1p(self) -> f32 {
801 unsafe { cmath::log1pf(self) }
804 /// Hyperbolic sine function.
811 /// let e = f32::consts::E;
814 /// let f = x.sinh();
815 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
816 /// let g = ((e * e) - 1.0) / (2.0 * e);
817 /// let abs_difference = (f - g).abs();
819 /// assert!(abs_difference <= f32::EPSILON);
821 #[must_use = "method returns a new number and does not mutate the original value"]
822 #[stable(feature = "rust1", since = "1.0.0")]
824 pub fn sinh(self) -> f32 {
825 unsafe { cmath::sinhf(self) }
828 /// Hyperbolic cosine function.
835 /// let e = f32::consts::E;
837 /// let f = x.cosh();
838 /// // Solving cosh() at 1 gives this result
839 /// let g = ((e * e) + 1.0) / (2.0 * e);
840 /// let abs_difference = (f - g).abs();
843 /// assert!(abs_difference <= f32::EPSILON);
845 #[must_use = "method returns a new number and does not mutate the original value"]
846 #[stable(feature = "rust1", since = "1.0.0")]
848 pub fn cosh(self) -> f32 {
849 unsafe { cmath::coshf(self) }
852 /// Hyperbolic tangent function.
859 /// let e = f32::consts::E;
862 /// let f = x.tanh();
863 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
864 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
865 /// let abs_difference = (f - g).abs();
867 /// assert!(abs_difference <= f32::EPSILON);
869 #[must_use = "method returns a new number and does not mutate the original value"]
870 #[stable(feature = "rust1", since = "1.0.0")]
872 pub fn tanh(self) -> f32 {
873 unsafe { cmath::tanhf(self) }
876 /// Inverse hyperbolic sine function.
884 /// let f = x.sinh().asinh();
886 /// let abs_difference = (f - x).abs();
888 /// assert!(abs_difference <= f32::EPSILON);
890 #[must_use = "method returns a new number and does not mutate the original value"]
891 #[stable(feature = "rust1", since = "1.0.0")]
893 pub fn asinh(self) -> f32 {
894 if self == NEG_INFINITY {
897 (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
901 /// Inverse hyperbolic cosine function.
909 /// let f = x.cosh().acosh();
911 /// let abs_difference = (f - x).abs();
913 /// assert!(abs_difference <= f32::EPSILON);
915 #[must_use = "method returns a new number and does not mutate the original value"]
916 #[stable(feature = "rust1", since = "1.0.0")]
918 pub fn acosh(self) -> f32 {
919 if self < 1.0 { crate::f32::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
922 /// Inverse hyperbolic tangent function.
929 /// let e = f32::consts::E;
930 /// let f = e.tanh().atanh();
932 /// let abs_difference = (f - e).abs();
934 /// assert!(abs_difference <= 1e-5);
936 #[must_use = "method returns a new number and does not mutate the original value"]
937 #[stable(feature = "rust1", since = "1.0.0")]
939 pub fn atanh(self) -> f32 {
940 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
943 /// Restrict a value to a certain interval unless it is NaN.
945 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
946 /// less than `min`. Otherwise this returns `self`.
948 /// Not that this function returns NaN if the initial value was NaN as
953 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
958 /// #![feature(clamp)]
959 /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
960 /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
961 /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
962 /// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan());
964 #[must_use = "method returns a new number and does not mutate the original value"]
965 #[unstable(feature = "clamp", issue = "44095")]
967 pub fn clamp(self, min: f32, max: f32) -> f32 {
984 use crate::num::FpCategory as Fp;
989 test_num(10f32, 2f32);
994 assert_eq!(NAN.min(2.0), 2.0);
995 assert_eq!(2.0f32.min(NAN), 2.0);
1000 assert_eq!(NAN.max(2.0), 2.0);
1001 assert_eq!(2.0f32.max(NAN), 2.0);
1006 let nan: f32 = f32::NAN;
1007 assert!(nan.is_nan());
1008 assert!(!nan.is_infinite());
1009 assert!(!nan.is_finite());
1010 assert!(!nan.is_normal());
1011 assert!(nan.is_sign_positive());
1012 assert!(!nan.is_sign_negative());
1013 assert_eq!(Fp::Nan, nan.classify());
1017 fn test_infinity() {
1018 let inf: f32 = f32::INFINITY;
1019 assert!(inf.is_infinite());
1020 assert!(!inf.is_finite());
1021 assert!(inf.is_sign_positive());
1022 assert!(!inf.is_sign_negative());
1023 assert!(!inf.is_nan());
1024 assert!(!inf.is_normal());
1025 assert_eq!(Fp::Infinite, inf.classify());
1029 fn test_neg_infinity() {
1030 let neg_inf: f32 = f32::NEG_INFINITY;
1031 assert!(neg_inf.is_infinite());
1032 assert!(!neg_inf.is_finite());
1033 assert!(!neg_inf.is_sign_positive());
1034 assert!(neg_inf.is_sign_negative());
1035 assert!(!neg_inf.is_nan());
1036 assert!(!neg_inf.is_normal());
1037 assert_eq!(Fp::Infinite, neg_inf.classify());
1042 let zero: f32 = 0.0f32;
1043 assert_eq!(0.0, zero);
1044 assert!(!zero.is_infinite());
1045 assert!(zero.is_finite());
1046 assert!(zero.is_sign_positive());
1047 assert!(!zero.is_sign_negative());
1048 assert!(!zero.is_nan());
1049 assert!(!zero.is_normal());
1050 assert_eq!(Fp::Zero, zero.classify());
1054 fn test_neg_zero() {
1055 let neg_zero: f32 = -0.0;
1056 assert_eq!(0.0, neg_zero);
1057 assert!(!neg_zero.is_infinite());
1058 assert!(neg_zero.is_finite());
1059 assert!(!neg_zero.is_sign_positive());
1060 assert!(neg_zero.is_sign_negative());
1061 assert!(!neg_zero.is_nan());
1062 assert!(!neg_zero.is_normal());
1063 assert_eq!(Fp::Zero, neg_zero.classify());
1068 let one: f32 = 1.0f32;
1069 assert_eq!(1.0, one);
1070 assert!(!one.is_infinite());
1071 assert!(one.is_finite());
1072 assert!(one.is_sign_positive());
1073 assert!(!one.is_sign_negative());
1074 assert!(!one.is_nan());
1075 assert!(one.is_normal());
1076 assert_eq!(Fp::Normal, one.classify());
1081 let nan: f32 = f32::NAN;
1082 let inf: f32 = f32::INFINITY;
1083 let neg_inf: f32 = f32::NEG_INFINITY;
1084 assert!(nan.is_nan());
1085 assert!(!0.0f32.is_nan());
1086 assert!(!5.3f32.is_nan());
1087 assert!(!(-10.732f32).is_nan());
1088 assert!(!inf.is_nan());
1089 assert!(!neg_inf.is_nan());
1093 fn test_is_infinite() {
1094 let nan: f32 = f32::NAN;
1095 let inf: f32 = f32::INFINITY;
1096 let neg_inf: f32 = f32::NEG_INFINITY;
1097 assert!(!nan.is_infinite());
1098 assert!(inf.is_infinite());
1099 assert!(neg_inf.is_infinite());
1100 assert!(!0.0f32.is_infinite());
1101 assert!(!42.8f32.is_infinite());
1102 assert!(!(-109.2f32).is_infinite());
1106 fn test_is_finite() {
1107 let nan: f32 = f32::NAN;
1108 let inf: f32 = f32::INFINITY;
1109 let neg_inf: f32 = f32::NEG_INFINITY;
1110 assert!(!nan.is_finite());
1111 assert!(!inf.is_finite());
1112 assert!(!neg_inf.is_finite());
1113 assert!(0.0f32.is_finite());
1114 assert!(42.8f32.is_finite());
1115 assert!((-109.2f32).is_finite());
1119 fn test_is_normal() {
1120 let nan: f32 = f32::NAN;
1121 let inf: f32 = f32::INFINITY;
1122 let neg_inf: f32 = f32::NEG_INFINITY;
1123 let zero: f32 = 0.0f32;
1124 let neg_zero: f32 = -0.0;
1125 assert!(!nan.is_normal());
1126 assert!(!inf.is_normal());
1127 assert!(!neg_inf.is_normal());
1128 assert!(!zero.is_normal());
1129 assert!(!neg_zero.is_normal());
1130 assert!(1f32.is_normal());
1131 assert!(1e-37f32.is_normal());
1132 assert!(!1e-38f32.is_normal());
1136 fn test_classify() {
1137 let nan: f32 = f32::NAN;
1138 let inf: f32 = f32::INFINITY;
1139 let neg_inf: f32 = f32::NEG_INFINITY;
1140 let zero: f32 = 0.0f32;
1141 let neg_zero: f32 = -0.0;
1142 assert_eq!(nan.classify(), Fp::Nan);
1143 assert_eq!(inf.classify(), Fp::Infinite);
1144 assert_eq!(neg_inf.classify(), Fp::Infinite);
1145 assert_eq!(zero.classify(), Fp::Zero);
1146 assert_eq!(neg_zero.classify(), Fp::Zero);
1147 assert_eq!(1f32.classify(), Fp::Normal);
1148 assert_eq!(1e-37f32.classify(), Fp::Normal);
1149 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1154 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1155 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1156 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1157 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1158 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1159 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1160 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1161 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1162 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1163 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1168 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1169 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1170 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1171 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1172 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1173 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1174 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1175 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1176 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1177 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1182 assert_approx_eq!(1.0f32.round(), 1.0f32);
1183 assert_approx_eq!(1.3f32.round(), 1.0f32);
1184 assert_approx_eq!(1.5f32.round(), 2.0f32);
1185 assert_approx_eq!(1.7f32.round(), 2.0f32);
1186 assert_approx_eq!(0.0f32.round(), 0.0f32);
1187 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1188 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1189 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1190 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1191 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1196 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1197 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1198 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1199 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1200 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1201 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1202 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1203 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1204 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1205 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1210 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1211 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1212 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1213 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1214 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1215 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1216 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1217 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1218 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1219 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1224 assert_eq!(INFINITY.abs(), INFINITY);
1225 assert_eq!(1f32.abs(), 1f32);
1226 assert_eq!(0f32.abs(), 0f32);
1227 assert_eq!((-0f32).abs(), 0f32);
1228 assert_eq!((-1f32).abs(), 1f32);
1229 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1230 assert_eq!((1f32 / NEG_INFINITY).abs(), 0f32);
1231 assert!(NAN.abs().is_nan());
1236 assert_eq!(INFINITY.signum(), 1f32);
1237 assert_eq!(1f32.signum(), 1f32);
1238 assert_eq!(0f32.signum(), 1f32);
1239 assert_eq!((-0f32).signum(), -1f32);
1240 assert_eq!((-1f32).signum(), -1f32);
1241 assert_eq!(NEG_INFINITY.signum(), -1f32);
1242 assert_eq!((1f32 / NEG_INFINITY).signum(), -1f32);
1243 assert!(NAN.signum().is_nan());
1247 fn test_is_sign_positive() {
1248 assert!(INFINITY.is_sign_positive());
1249 assert!(1f32.is_sign_positive());
1250 assert!(0f32.is_sign_positive());
1251 assert!(!(-0f32).is_sign_positive());
1252 assert!(!(-1f32).is_sign_positive());
1253 assert!(!NEG_INFINITY.is_sign_positive());
1254 assert!(!(1f32 / NEG_INFINITY).is_sign_positive());
1255 assert!(NAN.is_sign_positive());
1256 assert!(!(-NAN).is_sign_positive());
1260 fn test_is_sign_negative() {
1261 assert!(!INFINITY.is_sign_negative());
1262 assert!(!1f32.is_sign_negative());
1263 assert!(!0f32.is_sign_negative());
1264 assert!((-0f32).is_sign_negative());
1265 assert!((-1f32).is_sign_negative());
1266 assert!(NEG_INFINITY.is_sign_negative());
1267 assert!((1f32 / NEG_INFINITY).is_sign_negative());
1268 assert!(!NAN.is_sign_negative());
1269 assert!((-NAN).is_sign_negative());
1274 let nan: f32 = f32::NAN;
1275 let inf: f32 = f32::INFINITY;
1276 let neg_inf: f32 = f32::NEG_INFINITY;
1277 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1278 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1279 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1280 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1281 assert!(nan.mul_add(7.8, 9.0).is_nan());
1282 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1283 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1284 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1285 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1290 let nan: f32 = f32::NAN;
1291 let inf: f32 = f32::INFINITY;
1292 let neg_inf: f32 = f32::NEG_INFINITY;
1293 assert_eq!(1.0f32.recip(), 1.0);
1294 assert_eq!(2.0f32.recip(), 0.5);
1295 assert_eq!((-0.4f32).recip(), -2.5);
1296 assert_eq!(0.0f32.recip(), inf);
1297 assert!(nan.recip().is_nan());
1298 assert_eq!(inf.recip(), 0.0);
1299 assert_eq!(neg_inf.recip(), 0.0);
1304 let nan: f32 = f32::NAN;
1305 let inf: f32 = f32::INFINITY;
1306 let neg_inf: f32 = f32::NEG_INFINITY;
1307 assert_eq!(1.0f32.powi(1), 1.0);
1308 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1309 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1310 assert_eq!(8.3f32.powi(0), 1.0);
1311 assert!(nan.powi(2).is_nan());
1312 assert_eq!(inf.powi(3), inf);
1313 assert_eq!(neg_inf.powi(2), inf);
1318 let nan: f32 = f32::NAN;
1319 let inf: f32 = f32::INFINITY;
1320 let neg_inf: f32 = f32::NEG_INFINITY;
1321 assert_eq!(1.0f32.powf(1.0), 1.0);
1322 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1323 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1324 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1325 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1326 assert_eq!(8.3f32.powf(0.0), 1.0);
1327 assert!(nan.powf(2.0).is_nan());
1328 assert_eq!(inf.powf(2.0), inf);
1329 assert_eq!(neg_inf.powf(3.0), neg_inf);
1333 fn test_sqrt_domain() {
1334 assert!(NAN.sqrt().is_nan());
1335 assert!(NEG_INFINITY.sqrt().is_nan());
1336 assert!((-1.0f32).sqrt().is_nan());
1337 assert_eq!((-0.0f32).sqrt(), -0.0);
1338 assert_eq!(0.0f32.sqrt(), 0.0);
1339 assert_eq!(1.0f32.sqrt(), 1.0);
1340 assert_eq!(INFINITY.sqrt(), INFINITY);
1345 assert_eq!(1.0, 0.0f32.exp());
1346 assert_approx_eq!(2.718282, 1.0f32.exp());
1347 assert_approx_eq!(148.413162, 5.0f32.exp());
1349 let inf: f32 = f32::INFINITY;
1350 let neg_inf: f32 = f32::NEG_INFINITY;
1351 let nan: f32 = f32::NAN;
1352 assert_eq!(inf, inf.exp());
1353 assert_eq!(0.0, neg_inf.exp());
1354 assert!(nan.exp().is_nan());
1359 assert_eq!(32.0, 5.0f32.exp2());
1360 assert_eq!(1.0, 0.0f32.exp2());
1362 let inf: f32 = f32::INFINITY;
1363 let neg_inf: f32 = f32::NEG_INFINITY;
1364 let nan: f32 = f32::NAN;
1365 assert_eq!(inf, inf.exp2());
1366 assert_eq!(0.0, neg_inf.exp2());
1367 assert!(nan.exp2().is_nan());
1372 let nan: f32 = f32::NAN;
1373 let inf: f32 = f32::INFINITY;
1374 let neg_inf: f32 = f32::NEG_INFINITY;
1375 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1376 assert!(nan.ln().is_nan());
1377 assert_eq!(inf.ln(), inf);
1378 assert!(neg_inf.ln().is_nan());
1379 assert!((-2.3f32).ln().is_nan());
1380 assert_eq!((-0.0f32).ln(), neg_inf);
1381 assert_eq!(0.0f32.ln(), neg_inf);
1382 assert_approx_eq!(4.0f32.ln(), 1.386294);
1387 let nan: f32 = f32::NAN;
1388 let inf: f32 = f32::INFINITY;
1389 let neg_inf: f32 = f32::NEG_INFINITY;
1390 assert_eq!(10.0f32.log(10.0), 1.0);
1391 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1392 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1393 assert!(1.0f32.log(1.0).is_nan());
1394 assert!(1.0f32.log(-13.9).is_nan());
1395 assert!(nan.log(2.3).is_nan());
1396 assert_eq!(inf.log(10.0), inf);
1397 assert!(neg_inf.log(8.8).is_nan());
1398 assert!((-2.3f32).log(0.1).is_nan());
1399 assert_eq!((-0.0f32).log(2.0), neg_inf);
1400 assert_eq!(0.0f32.log(7.0), neg_inf);
1405 let nan: f32 = f32::NAN;
1406 let inf: f32 = f32::INFINITY;
1407 let neg_inf: f32 = f32::NEG_INFINITY;
1408 assert_approx_eq!(10.0f32.log2(), 3.321928);
1409 assert_approx_eq!(2.3f32.log2(), 1.201634);
1410 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1411 assert!(nan.log2().is_nan());
1412 assert_eq!(inf.log2(), inf);
1413 assert!(neg_inf.log2().is_nan());
1414 assert!((-2.3f32).log2().is_nan());
1415 assert_eq!((-0.0f32).log2(), neg_inf);
1416 assert_eq!(0.0f32.log2(), neg_inf);
1421 let nan: f32 = f32::NAN;
1422 let inf: f32 = f32::INFINITY;
1423 let neg_inf: f32 = f32::NEG_INFINITY;
1424 assert_eq!(10.0f32.log10(), 1.0);
1425 assert_approx_eq!(2.3f32.log10(), 0.361728);
1426 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1427 assert_eq!(1.0f32.log10(), 0.0);
1428 assert!(nan.log10().is_nan());
1429 assert_eq!(inf.log10(), inf);
1430 assert!(neg_inf.log10().is_nan());
1431 assert!((-2.3f32).log10().is_nan());
1432 assert_eq!((-0.0f32).log10(), neg_inf);
1433 assert_eq!(0.0f32.log10(), neg_inf);
1437 fn test_to_degrees() {
1438 let pi: f32 = consts::PI;
1439 let nan: f32 = f32::NAN;
1440 let inf: f32 = f32::INFINITY;
1441 let neg_inf: f32 = f32::NEG_INFINITY;
1442 assert_eq!(0.0f32.to_degrees(), 0.0);
1443 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1444 assert_eq!(pi.to_degrees(), 180.0);
1445 assert!(nan.to_degrees().is_nan());
1446 assert_eq!(inf.to_degrees(), inf);
1447 assert_eq!(neg_inf.to_degrees(), neg_inf);
1448 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1452 fn test_to_radians() {
1453 let pi: f32 = consts::PI;
1454 let nan: f32 = f32::NAN;
1455 let inf: f32 = f32::INFINITY;
1456 let neg_inf: f32 = f32::NEG_INFINITY;
1457 assert_eq!(0.0f32.to_radians(), 0.0);
1458 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1459 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1460 assert_eq!(180.0f32.to_radians(), pi);
1461 assert!(nan.to_radians().is_nan());
1462 assert_eq!(inf.to_radians(), inf);
1463 assert_eq!(neg_inf.to_radians(), neg_inf);
1468 assert_eq!(0.0f32.asinh(), 0.0f32);
1469 assert_eq!((-0.0f32).asinh(), -0.0f32);
1471 let inf: f32 = f32::INFINITY;
1472 let neg_inf: f32 = f32::NEG_INFINITY;
1473 let nan: f32 = f32::NAN;
1474 assert_eq!(inf.asinh(), inf);
1475 assert_eq!(neg_inf.asinh(), neg_inf);
1476 assert!(nan.asinh().is_nan());
1477 assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
1478 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1479 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1484 assert_eq!(1.0f32.acosh(), 0.0f32);
1485 assert!(0.999f32.acosh().is_nan());
1487 let inf: f32 = f32::INFINITY;
1488 let neg_inf: f32 = f32::NEG_INFINITY;
1489 let nan: f32 = f32::NAN;
1490 assert_eq!(inf.acosh(), inf);
1491 assert!(neg_inf.acosh().is_nan());
1492 assert!(nan.acosh().is_nan());
1493 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1494 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1499 assert_eq!(0.0f32.atanh(), 0.0f32);
1500 assert_eq!((-0.0f32).atanh(), -0.0f32);
1502 let inf32: f32 = f32::INFINITY;
1503 let neg_inf32: f32 = f32::NEG_INFINITY;
1504 assert_eq!(1.0f32.atanh(), inf32);
1505 assert_eq!((-1.0f32).atanh(), neg_inf32);
1507 assert!(2f64.atanh().atanh().is_nan());
1508 assert!((-2f64).atanh().atanh().is_nan());
1510 let inf64: f32 = f32::INFINITY;
1511 let neg_inf64: f32 = f32::NEG_INFINITY;
1512 let nan32: f32 = f32::NAN;
1513 assert!(inf64.atanh().is_nan());
1514 assert!(neg_inf64.atanh().is_nan());
1515 assert!(nan32.atanh().is_nan());
1517 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1518 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1522 fn test_real_consts() {
1525 let pi: f32 = consts::PI;
1526 let frac_pi_2: f32 = consts::FRAC_PI_2;
1527 let frac_pi_3: f32 = consts::FRAC_PI_3;
1528 let frac_pi_4: f32 = consts::FRAC_PI_4;
1529 let frac_pi_6: f32 = consts::FRAC_PI_6;
1530 let frac_pi_8: f32 = consts::FRAC_PI_8;
1531 let frac_1_pi: f32 = consts::FRAC_1_PI;
1532 let frac_2_pi: f32 = consts::FRAC_2_PI;
1533 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1534 let sqrt2: f32 = consts::SQRT_2;
1535 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1536 let e: f32 = consts::E;
1537 let log2_e: f32 = consts::LOG2_E;
1538 let log10_e: f32 = consts::LOG10_E;
1539 let ln_2: f32 = consts::LN_2;
1540 let ln_10: f32 = consts::LN_10;
1542 assert_approx_eq!(frac_pi_2, pi / 2f32);
1543 assert_approx_eq!(frac_pi_3, pi / 3f32);
1544 assert_approx_eq!(frac_pi_4, pi / 4f32);
1545 assert_approx_eq!(frac_pi_6, pi / 6f32);
1546 assert_approx_eq!(frac_pi_8, pi / 8f32);
1547 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1548 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1549 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1550 assert_approx_eq!(sqrt2, 2f32.sqrt());
1551 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1552 assert_approx_eq!(log2_e, e.log2());
1553 assert_approx_eq!(log10_e, e.log10());
1554 assert_approx_eq!(ln_2, 2f32.ln());
1555 assert_approx_eq!(ln_10, 10f32.ln());
1559 fn test_float_bits_conv() {
1560 assert_eq!((1f32).to_bits(), 0x3f800000);
1561 assert_eq!((12.5f32).to_bits(), 0x41480000);
1562 assert_eq!((1337f32).to_bits(), 0x44a72000);
1563 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1564 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1565 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1566 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1567 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1569 // Check that NaNs roundtrip their bits regardless of signalingness
1570 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1571 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1572 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1573 assert!(f32::from_bits(masked_nan1).is_nan());
1574 assert!(f32::from_bits(masked_nan2).is_nan());
1576 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1577 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1582 fn test_clamp_min_greater_than_max() {
1583 let _ = 1.0f32.clamp(3.0, 1.0);
1588 fn test_clamp_min_is_nan() {
1589 let _ = 1.0f32.clamp(NAN, 1.0);
1594 fn test_clamp_max_is_nan() {
1595 let _ = 1.0f32.clamp(3.0, NAN);