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.
116 /// let y = -3.6_f32;
117 /// let abs_difference_x = (x.fract() - 0.6).abs();
118 /// let abs_difference_y = (y.fract() - (-0.6)).abs();
120 /// assert!(abs_difference_x <= f32::EPSILON);
121 /// assert!(abs_difference_y <= f32::EPSILON);
123 #[must_use = "method returns a new number and does not mutate the original value"]
124 #[stable(feature = "rust1", since = "1.0.0")]
126 pub fn fract(self) -> f32 {
130 /// Computes the absolute value of `self`. Returns `NAN` if the
137 /// let y = -3.5_f32;
139 /// let abs_difference_x = (x.abs() - x).abs();
140 /// let abs_difference_y = (y.abs() - (-y)).abs();
142 /// assert!(abs_difference_x <= f32::EPSILON);
143 /// assert!(abs_difference_y <= f32::EPSILON);
145 /// assert!(f32::NAN.abs().is_nan());
147 #[must_use = "method returns a new number and does not mutate the original value"]
148 #[stable(feature = "rust1", since = "1.0.0")]
150 pub fn abs(self) -> f32 {
151 unsafe { intrinsics::fabsf32(self) }
154 /// Returns a number that represents the sign of `self`.
156 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
157 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
158 /// - `NAN` if the number is `NAN`
165 /// assert_eq!(f.signum(), 1.0);
166 /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
168 /// assert!(f32::NAN.signum().is_nan());
170 #[must_use = "method returns a new number and does not mutate the original value"]
171 #[stable(feature = "rust1", since = "1.0.0")]
173 pub fn signum(self) -> f32 {
174 if self.is_nan() { Self::NAN } else { 1.0_f32.copysign(self) }
177 /// Returns a number composed of the magnitude of `self` and the sign of
180 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
181 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
182 /// `sign` is returned.
189 /// assert_eq!(f.copysign(0.42), 3.5_f32);
190 /// assert_eq!(f.copysign(-0.42), -3.5_f32);
191 /// assert_eq!((-f).copysign(0.42), 3.5_f32);
192 /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
194 /// assert!(f32::NAN.copysign(1.0).is_nan());
196 #[must_use = "method returns a new number and does not mutate the original value"]
198 #[stable(feature = "copysign", since = "1.35.0")]
199 pub fn copysign(self, sign: f32) -> f32 {
200 unsafe { intrinsics::copysignf32(self, sign) }
203 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
204 /// error, yielding a more accurate result than an unfused multiply-add.
206 /// Using `mul_add` can be more performant than an unfused multiply-add if
207 /// the target architecture has a dedicated `fma` CPU instruction.
212 /// let m = 10.0_f32;
214 /// let b = 60.0_f32;
217 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
219 /// assert!(abs_difference <= f32::EPSILON);
221 #[must_use = "method returns a new number and does not mutate the original value"]
222 #[stable(feature = "rust1", since = "1.0.0")]
224 pub fn mul_add(self, a: f32, b: f32) -> f32 {
225 unsafe { intrinsics::fmaf32(self, a, b) }
228 /// Calculates Euclidean division, the matching method for `rem_euclid`.
230 /// This computes the integer `n` such that
231 /// `self = n * rhs + self.rem_euclid(rhs)`.
232 /// In other words, the result is `self / rhs` rounded to the integer `n`
233 /// such that `self >= n * rhs`.
238 /// let a: f32 = 7.0;
240 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
241 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
242 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
243 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
245 #[must_use = "method returns a new number and does not mutate the original value"]
247 #[stable(feature = "euclidean_division", since = "1.38.0")]
248 pub fn div_euclid(self, rhs: f32) -> f32 {
249 let q = (self / rhs).trunc();
250 if self % rhs < 0.0 {
251 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
256 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
258 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
259 /// most cases. However, due to a floating point round-off error it can
260 /// result in `r == rhs.abs()`, violating the mathematical definition, if
261 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
262 /// This result is not an element of the function's codomain, but it is the
263 /// closest floating point number in the real numbers and thus fulfills the
264 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
270 /// let a: f32 = 7.0;
272 /// assert_eq!(a.rem_euclid(b), 3.0);
273 /// assert_eq!((-a).rem_euclid(b), 1.0);
274 /// assert_eq!(a.rem_euclid(-b), 3.0);
275 /// assert_eq!((-a).rem_euclid(-b), 1.0);
276 /// // limitation due to round-off error
277 /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
279 #[must_use = "method returns a new number and does not mutate the original value"]
281 #[stable(feature = "euclidean_division", since = "1.38.0")]
282 pub fn rem_euclid(self, rhs: f32) -> f32 {
284 if r < 0.0 { r + rhs.abs() } else { r }
287 /// Raises a number to an integer power.
289 /// Using this function is generally faster than using `powf`
295 /// let abs_difference = (x.powi(2) - (x * x)).abs();
297 /// assert!(abs_difference <= f32::EPSILON);
299 #[must_use = "method returns a new number and does not mutate the original value"]
300 #[stable(feature = "rust1", since = "1.0.0")]
302 pub fn powi(self, n: i32) -> f32 {
303 unsafe { intrinsics::powif32(self, n) }
306 /// Raises a number to a floating point power.
312 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
314 /// assert!(abs_difference <= f32::EPSILON);
316 #[must_use = "method returns a new number and does not mutate the original value"]
317 #[stable(feature = "rust1", since = "1.0.0")]
319 pub fn powf(self, n: f32) -> f32 {
320 unsafe { intrinsics::powf32(self, n) }
323 /// Returns the square root of a number.
325 /// Returns NaN if `self` is a negative number.
330 /// let positive = 4.0_f32;
331 /// let negative = -4.0_f32;
333 /// let abs_difference = (positive.sqrt() - 2.0).abs();
335 /// assert!(abs_difference <= f32::EPSILON);
336 /// assert!(negative.sqrt().is_nan());
338 #[must_use = "method returns a new number and does not mutate the original value"]
339 #[stable(feature = "rust1", since = "1.0.0")]
341 pub fn sqrt(self) -> f32 {
342 unsafe { intrinsics::sqrtf32(self) }
345 /// Returns `e^(self)`, (the exponential function).
350 /// let one = 1.0f32;
352 /// let e = one.exp();
354 /// // ln(e) - 1 == 0
355 /// let abs_difference = (e.ln() - 1.0).abs();
357 /// assert!(abs_difference <= f32::EPSILON);
359 #[must_use = "method returns a new number and does not mutate the original value"]
360 #[stable(feature = "rust1", since = "1.0.0")]
362 pub fn exp(self) -> f32 {
363 unsafe { intrinsics::expf32(self) }
366 /// Returns `2^(self)`.
374 /// let abs_difference = (f.exp2() - 4.0).abs();
376 /// assert!(abs_difference <= f32::EPSILON);
378 #[must_use = "method returns a new number and does not mutate the original value"]
379 #[stable(feature = "rust1", since = "1.0.0")]
381 pub fn exp2(self) -> f32 {
382 unsafe { intrinsics::exp2f32(self) }
385 /// Returns the natural logarithm of the number.
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 #[must_use = "method returns a new number and does not mutate the original value"]
400 #[stable(feature = "rust1", since = "1.0.0")]
402 pub fn ln(self) -> f32 {
403 unsafe { intrinsics::logf32(self) }
406 /// Returns the logarithm of the number with respect to an arbitrary base.
408 /// The result may not be correctly rounded owing to implementation details;
409 /// `self.log2()` can produce more accurate results for base 2, and
410 /// `self.log10()` can produce more accurate results for base 10.
415 /// let five = 5.0f32;
417 /// // log5(5) - 1 == 0
418 /// let abs_difference = (five.log(5.0) - 1.0).abs();
420 /// assert!(abs_difference <= f32::EPSILON);
422 #[must_use = "method returns a new number and does not mutate the original value"]
423 #[stable(feature = "rust1", since = "1.0.0")]
425 pub fn log(self, base: f32) -> f32 {
426 self.ln() / base.ln()
429 /// Returns the base 2 logarithm of the number.
434 /// let two = 2.0f32;
436 /// // log2(2) - 1 == 0
437 /// let abs_difference = (two.log2() - 1.0).abs();
439 /// assert!(abs_difference <= f32::EPSILON);
441 #[must_use = "method returns a new number and does not mutate the original value"]
442 #[stable(feature = "rust1", since = "1.0.0")]
444 pub fn log2(self) -> f32 {
445 #[cfg(target_os = "android")]
446 return crate::sys::android::log2f32(self);
447 #[cfg(not(target_os = "android"))]
448 return unsafe { intrinsics::log2f32(self) };
451 /// Returns the base 10 logarithm of the number.
456 /// let ten = 10.0f32;
458 /// // log10(10) - 1 == 0
459 /// let abs_difference = (ten.log10() - 1.0).abs();
461 /// assert!(abs_difference <= f32::EPSILON);
463 #[must_use = "method returns a new number and does not mutate the original value"]
464 #[stable(feature = "rust1", since = "1.0.0")]
466 pub fn log10(self) -> f32 {
467 unsafe { intrinsics::log10f32(self) }
470 /// The positive difference of two numbers.
472 /// * If `self <= other`: `0:0`
473 /// * Else: `self - other`
481 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
482 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
484 /// assert!(abs_difference_x <= f32::EPSILON);
485 /// assert!(abs_difference_y <= f32::EPSILON);
487 #[must_use = "method returns a new number and does not mutate the original value"]
488 #[stable(feature = "rust1", since = "1.0.0")]
492 reason = "you probably meant `(self - other).abs()`: \
493 this operation is `(self - other).max(0.0)` \
494 except that `abs_sub` also propagates NaNs (also \
495 known as `fdimf` in C). If you truly need the positive \
496 difference, consider using that expression or the C function \
497 `fdimf`, depending on how you wish to handle NaN (please consider \
498 filing an issue describing your use-case too)."
500 pub fn abs_sub(self, other: f32) -> f32 {
501 unsafe { cmath::fdimf(self, other) }
504 /// Returns the cubic root of a number.
511 /// // x^(1/3) - 2 == 0
512 /// let abs_difference = (x.cbrt() - 2.0).abs();
514 /// assert!(abs_difference <= f32::EPSILON);
516 #[must_use = "method returns a new number and does not mutate the original value"]
517 #[stable(feature = "rust1", since = "1.0.0")]
519 pub fn cbrt(self) -> f32 {
520 unsafe { cmath::cbrtf(self) }
523 /// Calculates the length of the hypotenuse of a right-angle triangle given
524 /// legs of length `x` and `y`.
532 /// // sqrt(x^2 + y^2)
533 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
535 /// assert!(abs_difference <= f32::EPSILON);
537 #[must_use = "method returns a new number and does not mutate the original value"]
538 #[stable(feature = "rust1", since = "1.0.0")]
540 pub fn hypot(self, other: f32) -> f32 {
541 unsafe { cmath::hypotf(self, other) }
544 /// Computes the sine of a number (in radians).
549 /// let x = std::f32::consts::FRAC_PI_2;
551 /// let abs_difference = (x.sin() - 1.0).abs();
553 /// assert!(abs_difference <= f32::EPSILON);
555 #[must_use = "method returns a new number and does not mutate the original value"]
556 #[stable(feature = "rust1", since = "1.0.0")]
558 pub fn sin(self) -> f32 {
559 unsafe { intrinsics::sinf32(self) }
562 /// Computes the cosine of a number (in radians).
567 /// let x = 2.0 * std::f32::consts::PI;
569 /// let abs_difference = (x.cos() - 1.0).abs();
571 /// assert!(abs_difference <= f32::EPSILON);
573 #[must_use = "method returns a new number and does not mutate the original value"]
574 #[stable(feature = "rust1", since = "1.0.0")]
576 pub fn cos(self) -> f32 {
577 unsafe { intrinsics::cosf32(self) }
580 /// Computes the tangent of a number (in radians).
585 /// let x = std::f32::consts::FRAC_PI_4;
586 /// let abs_difference = (x.tan() - 1.0).abs();
588 /// assert!(abs_difference <= f32::EPSILON);
590 #[must_use = "method returns a new number and does not mutate the original value"]
591 #[stable(feature = "rust1", since = "1.0.0")]
593 pub fn tan(self) -> f32 {
594 unsafe { cmath::tanf(self) }
597 /// Computes the arcsine of a number. Return value is in radians in
598 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
604 /// let f = std::f32::consts::FRAC_PI_2;
606 /// // asin(sin(pi/2))
607 /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).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 asin(self) -> f32 {
615 unsafe { cmath::asinf(self) }
618 /// Computes the arccosine of a number. Return value is in radians in
619 /// the range [0, pi] or NaN if the number is outside the range
625 /// let f = std::f32::consts::FRAC_PI_4;
627 /// // acos(cos(pi/4))
628 /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
630 /// assert!(abs_difference <= f32::EPSILON);
632 #[must_use = "method returns a new number and does not mutate the original value"]
633 #[stable(feature = "rust1", since = "1.0.0")]
635 pub fn acos(self) -> f32 {
636 unsafe { cmath::acosf(self) }
639 /// Computes the arctangent of a number. Return value is in radians in the
640 /// range [-pi/2, pi/2];
648 /// let abs_difference = (f.tan().atan() - 1.0).abs();
650 /// assert!(abs_difference <= f32::EPSILON);
652 #[must_use = "method returns a new number and does not mutate the original value"]
653 #[stable(feature = "rust1", since = "1.0.0")]
655 pub fn atan(self) -> f32 {
656 unsafe { cmath::atanf(self) }
659 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
661 /// * `x = 0`, `y = 0`: `0`
662 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
663 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
664 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
669 /// // Positive angles measured counter-clockwise
670 /// // from positive x axis
671 /// // -pi/4 radians (45 deg clockwise)
673 /// let y1 = -3.0f32;
675 /// // 3pi/4 radians (135 deg counter-clockwise)
676 /// let x2 = -3.0f32;
679 /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
680 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
682 /// assert!(abs_difference_1 <= f32::EPSILON);
683 /// assert!(abs_difference_2 <= f32::EPSILON);
685 #[must_use = "method returns a new number and does not mutate the original value"]
686 #[stable(feature = "rust1", since = "1.0.0")]
688 pub fn atan2(self, other: f32) -> f32 {
689 unsafe { cmath::atan2f(self, other) }
692 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
693 /// `(sin(x), cos(x))`.
698 /// let x = std::f32::consts::FRAC_PI_4;
699 /// let f = x.sin_cos();
701 /// let abs_difference_0 = (f.0 - x.sin()).abs();
702 /// let abs_difference_1 = (f.1 - x.cos()).abs();
704 /// assert!(abs_difference_0 <= f32::EPSILON);
705 /// assert!(abs_difference_1 <= f32::EPSILON);
707 #[stable(feature = "rust1", since = "1.0.0")]
709 pub fn sin_cos(self) -> (f32, f32) {
710 (self.sin(), self.cos())
713 /// Returns `e^(self) - 1` in a way that is accurate even if the
714 /// number is close to zero.
722 /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
724 /// assert!(abs_difference <= f32::EPSILON);
726 #[must_use = "method returns a new number and does not mutate the original value"]
727 #[stable(feature = "rust1", since = "1.0.0")]
729 pub fn exp_m1(self) -> f32 {
730 unsafe { cmath::expm1f(self) }
733 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
734 /// the operations were performed separately.
739 /// let x = std::f32::consts::E - 1.0;
741 /// // ln(1 + (e - 1)) == ln(e) == 1
742 /// let abs_difference = (x.ln_1p() - 1.0).abs();
744 /// assert!(abs_difference <= f32::EPSILON);
746 #[must_use = "method returns a new number and does not mutate the original value"]
747 #[stable(feature = "rust1", since = "1.0.0")]
749 pub fn ln_1p(self) -> f32 {
750 unsafe { cmath::log1pf(self) }
753 /// Hyperbolic sine function.
758 /// let e = std::f32::consts::E;
761 /// let f = x.sinh();
762 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
763 /// let g = ((e * e) - 1.0) / (2.0 * e);
764 /// let abs_difference = (f - g).abs();
766 /// assert!(abs_difference <= f32::EPSILON);
768 #[must_use = "method returns a new number and does not mutate the original value"]
769 #[stable(feature = "rust1", since = "1.0.0")]
771 pub fn sinh(self) -> f32 {
772 unsafe { cmath::sinhf(self) }
775 /// Hyperbolic cosine function.
780 /// let e = std::f32::consts::E;
782 /// let f = x.cosh();
783 /// // Solving cosh() at 1 gives this result
784 /// let g = ((e * e) + 1.0) / (2.0 * e);
785 /// let abs_difference = (f - g).abs();
788 /// assert!(abs_difference <= f32::EPSILON);
790 #[must_use = "method returns a new number and does not mutate the original value"]
791 #[stable(feature = "rust1", since = "1.0.0")]
793 pub fn cosh(self) -> f32 {
794 unsafe { cmath::coshf(self) }
797 /// Hyperbolic tangent function.
802 /// let e = std::f32::consts::E;
805 /// let f = x.tanh();
806 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
807 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
808 /// let abs_difference = (f - g).abs();
810 /// assert!(abs_difference <= f32::EPSILON);
812 #[must_use = "method returns a new number and does not mutate the original value"]
813 #[stable(feature = "rust1", since = "1.0.0")]
815 pub fn tanh(self) -> f32 {
816 unsafe { cmath::tanhf(self) }
819 /// Inverse hyperbolic sine function.
825 /// let f = x.sinh().asinh();
827 /// let abs_difference = (f - x).abs();
829 /// assert!(abs_difference <= f32::EPSILON);
831 #[must_use = "method returns a new number and does not mutate the original value"]
832 #[stable(feature = "rust1", since = "1.0.0")]
834 pub fn asinh(self) -> f32 {
835 if self == Self::NEG_INFINITY {
838 (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
842 /// Inverse hyperbolic cosine function.
848 /// let f = x.cosh().acosh();
850 /// let abs_difference = (f - x).abs();
852 /// assert!(abs_difference <= f32::EPSILON);
854 #[must_use = "method returns a new number and does not mutate the original value"]
855 #[stable(feature = "rust1", since = "1.0.0")]
857 pub fn acosh(self) -> f32 {
858 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
861 /// Inverse hyperbolic tangent function.
866 /// let e = std::f32::consts::E;
867 /// let f = e.tanh().atanh();
869 /// let abs_difference = (f - e).abs();
871 /// assert!(abs_difference <= 1e-5);
873 #[must_use = "method returns a new number and does not mutate the original value"]
874 #[stable(feature = "rust1", since = "1.0.0")]
876 pub fn atanh(self) -> f32 {
877 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
880 /// Restrict a value to a certain interval unless it is NaN.
882 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
883 /// less than `min`. Otherwise this returns `self`.
885 /// Note that this function returns NaN if the initial value was NaN as
890 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
895 /// #![feature(clamp)]
896 /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
897 /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
898 /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
899 /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
901 #[must_use = "method returns a new number and does not mutate the original value"]
902 #[unstable(feature = "clamp", issue = "44095")]
904 pub fn clamp(self, min: f32, max: f32) -> f32 {
921 use crate::num::FpCategory as Fp;
926 test_num(10f32, 2f32);
931 assert_eq!(NAN.min(2.0), 2.0);
932 assert_eq!(2.0f32.min(NAN), 2.0);
937 assert_eq!(NAN.max(2.0), 2.0);
938 assert_eq!(2.0f32.max(NAN), 2.0);
943 let nan: f32 = f32::NAN;
944 assert!(nan.is_nan());
945 assert!(!nan.is_infinite());
946 assert!(!nan.is_finite());
947 assert!(!nan.is_normal());
948 assert!(nan.is_sign_positive());
949 assert!(!nan.is_sign_negative());
950 assert_eq!(Fp::Nan, nan.classify());
955 let inf: f32 = f32::INFINITY;
956 assert!(inf.is_infinite());
957 assert!(!inf.is_finite());
958 assert!(inf.is_sign_positive());
959 assert!(!inf.is_sign_negative());
960 assert!(!inf.is_nan());
961 assert!(!inf.is_normal());
962 assert_eq!(Fp::Infinite, inf.classify());
966 fn test_neg_infinity() {
967 let neg_inf: f32 = f32::NEG_INFINITY;
968 assert!(neg_inf.is_infinite());
969 assert!(!neg_inf.is_finite());
970 assert!(!neg_inf.is_sign_positive());
971 assert!(neg_inf.is_sign_negative());
972 assert!(!neg_inf.is_nan());
973 assert!(!neg_inf.is_normal());
974 assert_eq!(Fp::Infinite, neg_inf.classify());
979 let zero: f32 = 0.0f32;
980 assert_eq!(0.0, zero);
981 assert!(!zero.is_infinite());
982 assert!(zero.is_finite());
983 assert!(zero.is_sign_positive());
984 assert!(!zero.is_sign_negative());
985 assert!(!zero.is_nan());
986 assert!(!zero.is_normal());
987 assert_eq!(Fp::Zero, zero.classify());
992 let neg_zero: f32 = -0.0;
993 assert_eq!(0.0, neg_zero);
994 assert!(!neg_zero.is_infinite());
995 assert!(neg_zero.is_finite());
996 assert!(!neg_zero.is_sign_positive());
997 assert!(neg_zero.is_sign_negative());
998 assert!(!neg_zero.is_nan());
999 assert!(!neg_zero.is_normal());
1000 assert_eq!(Fp::Zero, neg_zero.classify());
1005 let one: f32 = 1.0f32;
1006 assert_eq!(1.0, one);
1007 assert!(!one.is_infinite());
1008 assert!(one.is_finite());
1009 assert!(one.is_sign_positive());
1010 assert!(!one.is_sign_negative());
1011 assert!(!one.is_nan());
1012 assert!(one.is_normal());
1013 assert_eq!(Fp::Normal, one.classify());
1018 let nan: f32 = f32::NAN;
1019 let inf: f32 = f32::INFINITY;
1020 let neg_inf: f32 = f32::NEG_INFINITY;
1021 assert!(nan.is_nan());
1022 assert!(!0.0f32.is_nan());
1023 assert!(!5.3f32.is_nan());
1024 assert!(!(-10.732f32).is_nan());
1025 assert!(!inf.is_nan());
1026 assert!(!neg_inf.is_nan());
1030 fn test_is_infinite() {
1031 let nan: f32 = f32::NAN;
1032 let inf: f32 = f32::INFINITY;
1033 let neg_inf: f32 = f32::NEG_INFINITY;
1034 assert!(!nan.is_infinite());
1035 assert!(inf.is_infinite());
1036 assert!(neg_inf.is_infinite());
1037 assert!(!0.0f32.is_infinite());
1038 assert!(!42.8f32.is_infinite());
1039 assert!(!(-109.2f32).is_infinite());
1043 fn test_is_finite() {
1044 let nan: f32 = f32::NAN;
1045 let inf: f32 = f32::INFINITY;
1046 let neg_inf: f32 = f32::NEG_INFINITY;
1047 assert!(!nan.is_finite());
1048 assert!(!inf.is_finite());
1049 assert!(!neg_inf.is_finite());
1050 assert!(0.0f32.is_finite());
1051 assert!(42.8f32.is_finite());
1052 assert!((-109.2f32).is_finite());
1056 fn test_is_normal() {
1057 let nan: f32 = f32::NAN;
1058 let inf: f32 = f32::INFINITY;
1059 let neg_inf: f32 = f32::NEG_INFINITY;
1060 let zero: f32 = 0.0f32;
1061 let neg_zero: f32 = -0.0;
1062 assert!(!nan.is_normal());
1063 assert!(!inf.is_normal());
1064 assert!(!neg_inf.is_normal());
1065 assert!(!zero.is_normal());
1066 assert!(!neg_zero.is_normal());
1067 assert!(1f32.is_normal());
1068 assert!(1e-37f32.is_normal());
1069 assert!(!1e-38f32.is_normal());
1073 fn test_classify() {
1074 let nan: f32 = f32::NAN;
1075 let inf: f32 = f32::INFINITY;
1076 let neg_inf: f32 = f32::NEG_INFINITY;
1077 let zero: f32 = 0.0f32;
1078 let neg_zero: f32 = -0.0;
1079 assert_eq!(nan.classify(), Fp::Nan);
1080 assert_eq!(inf.classify(), Fp::Infinite);
1081 assert_eq!(neg_inf.classify(), Fp::Infinite);
1082 assert_eq!(zero.classify(), Fp::Zero);
1083 assert_eq!(neg_zero.classify(), Fp::Zero);
1084 assert_eq!(1f32.classify(), Fp::Normal);
1085 assert_eq!(1e-37f32.classify(), Fp::Normal);
1086 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1091 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1092 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1093 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1094 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1095 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1096 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1097 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1098 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1099 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1100 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1105 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1106 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1107 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1108 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1109 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1110 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1111 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1112 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1113 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1114 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1119 assert_approx_eq!(1.0f32.round(), 1.0f32);
1120 assert_approx_eq!(1.3f32.round(), 1.0f32);
1121 assert_approx_eq!(1.5f32.round(), 2.0f32);
1122 assert_approx_eq!(1.7f32.round(), 2.0f32);
1123 assert_approx_eq!(0.0f32.round(), 0.0f32);
1124 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1125 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1126 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1127 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1128 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1133 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1134 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1135 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1136 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1137 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1138 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1139 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1140 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1141 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1142 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1147 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1148 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1149 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1150 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1151 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1152 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1153 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1154 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1155 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1156 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1161 assert_eq!(INFINITY.abs(), INFINITY);
1162 assert_eq!(1f32.abs(), 1f32);
1163 assert_eq!(0f32.abs(), 0f32);
1164 assert_eq!((-0f32).abs(), 0f32);
1165 assert_eq!((-1f32).abs(), 1f32);
1166 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1167 assert_eq!((1f32 / NEG_INFINITY).abs(), 0f32);
1168 assert!(NAN.abs().is_nan());
1173 assert_eq!(INFINITY.signum(), 1f32);
1174 assert_eq!(1f32.signum(), 1f32);
1175 assert_eq!(0f32.signum(), 1f32);
1176 assert_eq!((-0f32).signum(), -1f32);
1177 assert_eq!((-1f32).signum(), -1f32);
1178 assert_eq!(NEG_INFINITY.signum(), -1f32);
1179 assert_eq!((1f32 / NEG_INFINITY).signum(), -1f32);
1180 assert!(NAN.signum().is_nan());
1184 fn test_is_sign_positive() {
1185 assert!(INFINITY.is_sign_positive());
1186 assert!(1f32.is_sign_positive());
1187 assert!(0f32.is_sign_positive());
1188 assert!(!(-0f32).is_sign_positive());
1189 assert!(!(-1f32).is_sign_positive());
1190 assert!(!NEG_INFINITY.is_sign_positive());
1191 assert!(!(1f32 / NEG_INFINITY).is_sign_positive());
1192 assert!(NAN.is_sign_positive());
1193 assert!(!(-NAN).is_sign_positive());
1197 fn test_is_sign_negative() {
1198 assert!(!INFINITY.is_sign_negative());
1199 assert!(!1f32.is_sign_negative());
1200 assert!(!0f32.is_sign_negative());
1201 assert!((-0f32).is_sign_negative());
1202 assert!((-1f32).is_sign_negative());
1203 assert!(NEG_INFINITY.is_sign_negative());
1204 assert!((1f32 / NEG_INFINITY).is_sign_negative());
1205 assert!(!NAN.is_sign_negative());
1206 assert!((-NAN).is_sign_negative());
1211 let nan: f32 = f32::NAN;
1212 let inf: f32 = f32::INFINITY;
1213 let neg_inf: f32 = f32::NEG_INFINITY;
1214 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1215 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1216 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1217 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1218 assert!(nan.mul_add(7.8, 9.0).is_nan());
1219 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1220 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1221 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1222 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1227 let nan: f32 = f32::NAN;
1228 let inf: f32 = f32::INFINITY;
1229 let neg_inf: f32 = f32::NEG_INFINITY;
1230 assert_eq!(1.0f32.recip(), 1.0);
1231 assert_eq!(2.0f32.recip(), 0.5);
1232 assert_eq!((-0.4f32).recip(), -2.5);
1233 assert_eq!(0.0f32.recip(), inf);
1234 assert!(nan.recip().is_nan());
1235 assert_eq!(inf.recip(), 0.0);
1236 assert_eq!(neg_inf.recip(), 0.0);
1241 let nan: f32 = f32::NAN;
1242 let inf: f32 = f32::INFINITY;
1243 let neg_inf: f32 = f32::NEG_INFINITY;
1244 assert_eq!(1.0f32.powi(1), 1.0);
1245 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1246 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1247 assert_eq!(8.3f32.powi(0), 1.0);
1248 assert!(nan.powi(2).is_nan());
1249 assert_eq!(inf.powi(3), inf);
1250 assert_eq!(neg_inf.powi(2), inf);
1255 let nan: f32 = f32::NAN;
1256 let inf: f32 = f32::INFINITY;
1257 let neg_inf: f32 = f32::NEG_INFINITY;
1258 assert_eq!(1.0f32.powf(1.0), 1.0);
1259 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1260 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1261 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1262 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1263 assert_eq!(8.3f32.powf(0.0), 1.0);
1264 assert!(nan.powf(2.0).is_nan());
1265 assert_eq!(inf.powf(2.0), inf);
1266 assert_eq!(neg_inf.powf(3.0), neg_inf);
1270 fn test_sqrt_domain() {
1271 assert!(NAN.sqrt().is_nan());
1272 assert!(NEG_INFINITY.sqrt().is_nan());
1273 assert!((-1.0f32).sqrt().is_nan());
1274 assert_eq!((-0.0f32).sqrt(), -0.0);
1275 assert_eq!(0.0f32.sqrt(), 0.0);
1276 assert_eq!(1.0f32.sqrt(), 1.0);
1277 assert_eq!(INFINITY.sqrt(), INFINITY);
1282 assert_eq!(1.0, 0.0f32.exp());
1283 assert_approx_eq!(2.718282, 1.0f32.exp());
1284 assert_approx_eq!(148.413162, 5.0f32.exp());
1286 let inf: f32 = f32::INFINITY;
1287 let neg_inf: f32 = f32::NEG_INFINITY;
1288 let nan: f32 = f32::NAN;
1289 assert_eq!(inf, inf.exp());
1290 assert_eq!(0.0, neg_inf.exp());
1291 assert!(nan.exp().is_nan());
1296 assert_eq!(32.0, 5.0f32.exp2());
1297 assert_eq!(1.0, 0.0f32.exp2());
1299 let inf: f32 = f32::INFINITY;
1300 let neg_inf: f32 = f32::NEG_INFINITY;
1301 let nan: f32 = f32::NAN;
1302 assert_eq!(inf, inf.exp2());
1303 assert_eq!(0.0, neg_inf.exp2());
1304 assert!(nan.exp2().is_nan());
1309 let nan: f32 = f32::NAN;
1310 let inf: f32 = f32::INFINITY;
1311 let neg_inf: f32 = f32::NEG_INFINITY;
1312 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1313 assert!(nan.ln().is_nan());
1314 assert_eq!(inf.ln(), inf);
1315 assert!(neg_inf.ln().is_nan());
1316 assert!((-2.3f32).ln().is_nan());
1317 assert_eq!((-0.0f32).ln(), neg_inf);
1318 assert_eq!(0.0f32.ln(), neg_inf);
1319 assert_approx_eq!(4.0f32.ln(), 1.386294);
1324 let nan: f32 = f32::NAN;
1325 let inf: f32 = f32::INFINITY;
1326 let neg_inf: f32 = f32::NEG_INFINITY;
1327 assert_eq!(10.0f32.log(10.0), 1.0);
1328 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1329 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1330 assert!(1.0f32.log(1.0).is_nan());
1331 assert!(1.0f32.log(-13.9).is_nan());
1332 assert!(nan.log(2.3).is_nan());
1333 assert_eq!(inf.log(10.0), inf);
1334 assert!(neg_inf.log(8.8).is_nan());
1335 assert!((-2.3f32).log(0.1).is_nan());
1336 assert_eq!((-0.0f32).log(2.0), neg_inf);
1337 assert_eq!(0.0f32.log(7.0), neg_inf);
1342 let nan: f32 = f32::NAN;
1343 let inf: f32 = f32::INFINITY;
1344 let neg_inf: f32 = f32::NEG_INFINITY;
1345 assert_approx_eq!(10.0f32.log2(), 3.321928);
1346 assert_approx_eq!(2.3f32.log2(), 1.201634);
1347 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1348 assert!(nan.log2().is_nan());
1349 assert_eq!(inf.log2(), inf);
1350 assert!(neg_inf.log2().is_nan());
1351 assert!((-2.3f32).log2().is_nan());
1352 assert_eq!((-0.0f32).log2(), neg_inf);
1353 assert_eq!(0.0f32.log2(), neg_inf);
1358 let nan: f32 = f32::NAN;
1359 let inf: f32 = f32::INFINITY;
1360 let neg_inf: f32 = f32::NEG_INFINITY;
1361 assert_eq!(10.0f32.log10(), 1.0);
1362 assert_approx_eq!(2.3f32.log10(), 0.361728);
1363 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1364 assert_eq!(1.0f32.log10(), 0.0);
1365 assert!(nan.log10().is_nan());
1366 assert_eq!(inf.log10(), inf);
1367 assert!(neg_inf.log10().is_nan());
1368 assert!((-2.3f32).log10().is_nan());
1369 assert_eq!((-0.0f32).log10(), neg_inf);
1370 assert_eq!(0.0f32.log10(), neg_inf);
1374 fn test_to_degrees() {
1375 let pi: f32 = consts::PI;
1376 let nan: f32 = f32::NAN;
1377 let inf: f32 = f32::INFINITY;
1378 let neg_inf: f32 = f32::NEG_INFINITY;
1379 assert_eq!(0.0f32.to_degrees(), 0.0);
1380 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1381 assert_eq!(pi.to_degrees(), 180.0);
1382 assert!(nan.to_degrees().is_nan());
1383 assert_eq!(inf.to_degrees(), inf);
1384 assert_eq!(neg_inf.to_degrees(), neg_inf);
1385 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1389 fn test_to_radians() {
1390 let pi: f32 = consts::PI;
1391 let nan: f32 = f32::NAN;
1392 let inf: f32 = f32::INFINITY;
1393 let neg_inf: f32 = f32::NEG_INFINITY;
1394 assert_eq!(0.0f32.to_radians(), 0.0);
1395 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1396 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1397 assert_eq!(180.0f32.to_radians(), pi);
1398 assert!(nan.to_radians().is_nan());
1399 assert_eq!(inf.to_radians(), inf);
1400 assert_eq!(neg_inf.to_radians(), neg_inf);
1405 assert_eq!(0.0f32.asinh(), 0.0f32);
1406 assert_eq!((-0.0f32).asinh(), -0.0f32);
1408 let inf: f32 = f32::INFINITY;
1409 let neg_inf: f32 = f32::NEG_INFINITY;
1410 let nan: f32 = f32::NAN;
1411 assert_eq!(inf.asinh(), inf);
1412 assert_eq!(neg_inf.asinh(), neg_inf);
1413 assert!(nan.asinh().is_nan());
1414 assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
1415 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1416 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1421 assert_eq!(1.0f32.acosh(), 0.0f32);
1422 assert!(0.999f32.acosh().is_nan());
1424 let inf: f32 = f32::INFINITY;
1425 let neg_inf: f32 = f32::NEG_INFINITY;
1426 let nan: f32 = f32::NAN;
1427 assert_eq!(inf.acosh(), inf);
1428 assert!(neg_inf.acosh().is_nan());
1429 assert!(nan.acosh().is_nan());
1430 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1431 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1436 assert_eq!(0.0f32.atanh(), 0.0f32);
1437 assert_eq!((-0.0f32).atanh(), -0.0f32);
1439 let inf32: f32 = f32::INFINITY;
1440 let neg_inf32: f32 = f32::NEG_INFINITY;
1441 assert_eq!(1.0f32.atanh(), inf32);
1442 assert_eq!((-1.0f32).atanh(), neg_inf32);
1444 assert!(2f64.atanh().atanh().is_nan());
1445 assert!((-2f64).atanh().atanh().is_nan());
1447 let inf64: f32 = f32::INFINITY;
1448 let neg_inf64: f32 = f32::NEG_INFINITY;
1449 let nan32: f32 = f32::NAN;
1450 assert!(inf64.atanh().is_nan());
1451 assert!(neg_inf64.atanh().is_nan());
1452 assert!(nan32.atanh().is_nan());
1454 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1455 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1459 fn test_real_consts() {
1462 let pi: f32 = consts::PI;
1463 let frac_pi_2: f32 = consts::FRAC_PI_2;
1464 let frac_pi_3: f32 = consts::FRAC_PI_3;
1465 let frac_pi_4: f32 = consts::FRAC_PI_4;
1466 let frac_pi_6: f32 = consts::FRAC_PI_6;
1467 let frac_pi_8: f32 = consts::FRAC_PI_8;
1468 let frac_1_pi: f32 = consts::FRAC_1_PI;
1469 let frac_2_pi: f32 = consts::FRAC_2_PI;
1470 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1471 let sqrt2: f32 = consts::SQRT_2;
1472 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1473 let e: f32 = consts::E;
1474 let log2_e: f32 = consts::LOG2_E;
1475 let log10_e: f32 = consts::LOG10_E;
1476 let ln_2: f32 = consts::LN_2;
1477 let ln_10: f32 = consts::LN_10;
1479 assert_approx_eq!(frac_pi_2, pi / 2f32);
1480 assert_approx_eq!(frac_pi_3, pi / 3f32);
1481 assert_approx_eq!(frac_pi_4, pi / 4f32);
1482 assert_approx_eq!(frac_pi_6, pi / 6f32);
1483 assert_approx_eq!(frac_pi_8, pi / 8f32);
1484 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1485 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1486 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1487 assert_approx_eq!(sqrt2, 2f32.sqrt());
1488 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1489 assert_approx_eq!(log2_e, e.log2());
1490 assert_approx_eq!(log10_e, e.log10());
1491 assert_approx_eq!(ln_2, 2f32.ln());
1492 assert_approx_eq!(ln_10, 10f32.ln());
1496 fn test_float_bits_conv() {
1497 assert_eq!((1f32).to_bits(), 0x3f800000);
1498 assert_eq!((12.5f32).to_bits(), 0x41480000);
1499 assert_eq!((1337f32).to_bits(), 0x44a72000);
1500 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1501 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1502 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1503 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1504 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1506 // Check that NaNs roundtrip their bits regardless of signalingness
1507 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1508 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1509 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1510 assert!(f32::from_bits(masked_nan1).is_nan());
1511 assert!(f32::from_bits(masked_nan2).is_nan());
1513 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1514 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1519 fn test_clamp_min_greater_than_max() {
1520 let _ = 1.0f32.clamp(3.0, 1.0);
1525 fn test_clamp_min_is_nan() {
1526 let _ = 1.0f32.clamp(NAN, 1.0);
1531 fn test_clamp_max_is_nan() {
1532 let _ = 1.0f32.clamp(3.0, NAN);
1536 fn test_total_cmp() {
1537 use core::cmp::Ordering;
1539 fn quiet_bit_mask() -> u32 {
1540 1 << (f32::MANTISSA_DIGITS - 2)
1543 fn min_subnorm() -> f32 {
1544 f32::MIN_POSITIVE / f32::powf(2.0, f32::MANTISSA_DIGITS as f32 - 1.0)
1547 fn max_subnorm() -> f32 {
1548 f32::MIN_POSITIVE - min_subnorm()
1552 f32::from_bits(f32::NAN.to_bits() | quiet_bit_mask())
1556 f32::from_bits((f32::NAN.to_bits() & !quiet_bit_mask()) + 42)
1559 assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan()));
1560 assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan()));
1561 assert_eq!(Ordering::Equal, (-f32::INFINITY).total_cmp(&-f32::INFINITY));
1562 assert_eq!(Ordering::Equal, (-f32::MAX).total_cmp(&-f32::MAX));
1563 assert_eq!(Ordering::Equal, (-2.5_f32).total_cmp(&-2.5));
1564 assert_eq!(Ordering::Equal, (-1.0_f32).total_cmp(&-1.0));
1565 assert_eq!(Ordering::Equal, (-1.5_f32).total_cmp(&-1.5));
1566 assert_eq!(Ordering::Equal, (-0.5_f32).total_cmp(&-0.5));
1567 assert_eq!(Ordering::Equal, (-f32::MIN_POSITIVE).total_cmp(&-f32::MIN_POSITIVE));
1568 assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm()));
1569 assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm()));
1570 assert_eq!(Ordering::Equal, (-0.0_f32).total_cmp(&-0.0));
1571 assert_eq!(Ordering::Equal, 0.0_f32.total_cmp(&0.0));
1572 assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm()));
1573 assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm()));
1574 assert_eq!(Ordering::Equal, f32::MIN_POSITIVE.total_cmp(&f32::MIN_POSITIVE));
1575 assert_eq!(Ordering::Equal, 0.5_f32.total_cmp(&0.5));
1576 assert_eq!(Ordering::Equal, 1.0_f32.total_cmp(&1.0));
1577 assert_eq!(Ordering::Equal, 1.5_f32.total_cmp(&1.5));
1578 assert_eq!(Ordering::Equal, 2.5_f32.total_cmp(&2.5));
1579 assert_eq!(Ordering::Equal, f32::MAX.total_cmp(&f32::MAX));
1580 assert_eq!(Ordering::Equal, f32::INFINITY.total_cmp(&f32::INFINITY));
1581 assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan()));
1582 assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan()));
1584 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
1585 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
1586 assert_eq!(Ordering::Less, (-f32::INFINITY).total_cmp(&-f32::MAX));
1587 assert_eq!(Ordering::Less, (-f32::MAX).total_cmp(&-2.5));
1588 assert_eq!(Ordering::Less, (-2.5_f32).total_cmp(&-1.5));
1589 assert_eq!(Ordering::Less, (-1.5_f32).total_cmp(&-1.0));
1590 assert_eq!(Ordering::Less, (-1.0_f32).total_cmp(&-0.5));
1591 assert_eq!(Ordering::Less, (-0.5_f32).total_cmp(&-f32::MIN_POSITIVE));
1592 assert_eq!(Ordering::Less, (-f32::MIN_POSITIVE).total_cmp(&-max_subnorm()));
1593 assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm()));
1594 assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0));
1595 assert_eq!(Ordering::Less, (-0.0_f32).total_cmp(&0.0));
1596 assert_eq!(Ordering::Less, 0.0_f32.total_cmp(&min_subnorm()));
1597 assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm()));
1598 assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f32::MIN_POSITIVE));
1599 assert_eq!(Ordering::Less, f32::MIN_POSITIVE.total_cmp(&0.5));
1600 assert_eq!(Ordering::Less, 0.5_f32.total_cmp(&1.0));
1601 assert_eq!(Ordering::Less, 1.0_f32.total_cmp(&1.5));
1602 assert_eq!(Ordering::Less, 1.5_f32.total_cmp(&2.5));
1603 assert_eq!(Ordering::Less, 2.5_f32.total_cmp(&f32::MAX));
1604 assert_eq!(Ordering::Less, f32::MAX.total_cmp(&f32::INFINITY));
1605 assert_eq!(Ordering::Less, f32::INFINITY.total_cmp(&s_nan()));
1606 assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan()));
1608 assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan()));
1609 assert_eq!(Ordering::Greater, (-f32::INFINITY).total_cmp(&-s_nan()));
1610 assert_eq!(Ordering::Greater, (-f32::MAX).total_cmp(&-f32::INFINITY));
1611 assert_eq!(Ordering::Greater, (-2.5_f32).total_cmp(&-f32::MAX));
1612 assert_eq!(Ordering::Greater, (-1.5_f32).total_cmp(&-2.5));
1613 assert_eq!(Ordering::Greater, (-1.0_f32).total_cmp(&-1.5));
1614 assert_eq!(Ordering::Greater, (-0.5_f32).total_cmp(&-1.0));
1615 assert_eq!(Ordering::Greater, (-f32::MIN_POSITIVE).total_cmp(&-0.5));
1616 assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f32::MIN_POSITIVE));
1617 assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm()));
1618 assert_eq!(Ordering::Greater, (-0.0_f32).total_cmp(&-min_subnorm()));
1619 assert_eq!(Ordering::Greater, 0.0_f32.total_cmp(&-0.0));
1620 assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0));
1621 assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm()));
1622 assert_eq!(Ordering::Greater, f32::MIN_POSITIVE.total_cmp(&max_subnorm()));
1623 assert_eq!(Ordering::Greater, 0.5_f32.total_cmp(&f32::MIN_POSITIVE));
1624 assert_eq!(Ordering::Greater, 1.0_f32.total_cmp(&0.5));
1625 assert_eq!(Ordering::Greater, 1.5_f32.total_cmp(&1.0));
1626 assert_eq!(Ordering::Greater, 2.5_f32.total_cmp(&1.5));
1627 assert_eq!(Ordering::Greater, f32::MAX.total_cmp(&2.5));
1628 assert_eq!(Ordering::Greater, f32::INFINITY.total_cmp(&f32::MAX));
1629 assert_eq!(Ordering::Greater, s_nan().total_cmp(&f32::INFINITY));
1630 assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan()));
1632 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
1633 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::INFINITY));
1634 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MAX));
1635 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5));
1636 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5));
1637 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0));
1638 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5));
1639 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MIN_POSITIVE));
1640 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm()));
1641 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm()));
1642 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0));
1643 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0));
1644 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm()));
1645 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm()));
1646 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MIN_POSITIVE));
1647 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5));
1648 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0));
1649 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5));
1650 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5));
1651 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MAX));
1652 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::INFINITY));
1653 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan()));
1655 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
1656 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MAX));
1657 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5));
1658 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5));
1659 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0));
1660 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5));
1661 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MIN_POSITIVE));
1662 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm()));
1663 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm()));
1664 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0));
1665 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0));
1666 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm()));
1667 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm()));
1668 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MIN_POSITIVE));
1669 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5));
1670 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0));
1671 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5));
1672 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5));
1673 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MAX));
1674 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::INFINITY));
1675 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan()));