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 (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
838 /// Inverse hyperbolic cosine function.
844 /// let f = x.cosh().acosh();
846 /// let abs_difference = (f - x).abs();
848 /// assert!(abs_difference <= f32::EPSILON);
850 #[must_use = "method returns a new number and does not mutate the original value"]
851 #[stable(feature = "rust1", since = "1.0.0")]
853 pub fn acosh(self) -> f32 {
854 if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
857 /// Inverse hyperbolic tangent function.
862 /// let e = std::f32::consts::E;
863 /// let f = e.tanh().atanh();
865 /// let abs_difference = (f - e).abs();
867 /// assert!(abs_difference <= 1e-5);
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 atanh(self) -> f32 {
873 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
876 /// Restrict a value to a certain interval unless it is NaN.
878 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
879 /// less than `min`. Otherwise this returns `self`.
881 /// Note that this function returns NaN if the initial value was NaN as
886 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
891 /// #![feature(clamp)]
892 /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
893 /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
894 /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
895 /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
897 #[must_use = "method returns a new number and does not mutate the original value"]
898 #[unstable(feature = "clamp", issue = "44095")]
900 pub fn clamp(self, min: f32, max: f32) -> f32 {
915 use crate::f32::consts;
916 use crate::num::FpCategory as Fp;
921 test_num(10f32, 2f32);
926 assert_eq!(f32::NAN.min(2.0), 2.0);
927 assert_eq!(2.0f32.min(f32::NAN), 2.0);
932 assert_eq!(f32::NAN.max(2.0), 2.0);
933 assert_eq!(2.0f32.max(f32::NAN), 2.0);
938 let nan: f32 = f32::NAN;
939 assert!(nan.is_nan());
940 assert!(!nan.is_infinite());
941 assert!(!nan.is_finite());
942 assert!(!nan.is_normal());
943 assert!(nan.is_sign_positive());
944 assert!(!nan.is_sign_negative());
945 assert_eq!(Fp::Nan, nan.classify());
950 let inf: f32 = f32::INFINITY;
951 assert!(inf.is_infinite());
952 assert!(!inf.is_finite());
953 assert!(inf.is_sign_positive());
954 assert!(!inf.is_sign_negative());
955 assert!(!inf.is_nan());
956 assert!(!inf.is_normal());
957 assert_eq!(Fp::Infinite, inf.classify());
961 fn test_neg_infinity() {
962 let neg_inf: f32 = f32::NEG_INFINITY;
963 assert!(neg_inf.is_infinite());
964 assert!(!neg_inf.is_finite());
965 assert!(!neg_inf.is_sign_positive());
966 assert!(neg_inf.is_sign_negative());
967 assert!(!neg_inf.is_nan());
968 assert!(!neg_inf.is_normal());
969 assert_eq!(Fp::Infinite, neg_inf.classify());
974 let zero: f32 = 0.0f32;
975 assert_eq!(0.0, zero);
976 assert!(!zero.is_infinite());
977 assert!(zero.is_finite());
978 assert!(zero.is_sign_positive());
979 assert!(!zero.is_sign_negative());
980 assert!(!zero.is_nan());
981 assert!(!zero.is_normal());
982 assert_eq!(Fp::Zero, zero.classify());
987 let neg_zero: f32 = -0.0;
988 assert_eq!(0.0, neg_zero);
989 assert!(!neg_zero.is_infinite());
990 assert!(neg_zero.is_finite());
991 assert!(!neg_zero.is_sign_positive());
992 assert!(neg_zero.is_sign_negative());
993 assert!(!neg_zero.is_nan());
994 assert!(!neg_zero.is_normal());
995 assert_eq!(Fp::Zero, neg_zero.classify());
1000 let one: f32 = 1.0f32;
1001 assert_eq!(1.0, one);
1002 assert!(!one.is_infinite());
1003 assert!(one.is_finite());
1004 assert!(one.is_sign_positive());
1005 assert!(!one.is_sign_negative());
1006 assert!(!one.is_nan());
1007 assert!(one.is_normal());
1008 assert_eq!(Fp::Normal, one.classify());
1013 let nan: f32 = f32::NAN;
1014 let inf: f32 = f32::INFINITY;
1015 let neg_inf: f32 = f32::NEG_INFINITY;
1016 assert!(nan.is_nan());
1017 assert!(!0.0f32.is_nan());
1018 assert!(!5.3f32.is_nan());
1019 assert!(!(-10.732f32).is_nan());
1020 assert!(!inf.is_nan());
1021 assert!(!neg_inf.is_nan());
1025 fn test_is_infinite() {
1026 let nan: f32 = f32::NAN;
1027 let inf: f32 = f32::INFINITY;
1028 let neg_inf: f32 = f32::NEG_INFINITY;
1029 assert!(!nan.is_infinite());
1030 assert!(inf.is_infinite());
1031 assert!(neg_inf.is_infinite());
1032 assert!(!0.0f32.is_infinite());
1033 assert!(!42.8f32.is_infinite());
1034 assert!(!(-109.2f32).is_infinite());
1038 fn test_is_finite() {
1039 let nan: f32 = f32::NAN;
1040 let inf: f32 = f32::INFINITY;
1041 let neg_inf: f32 = f32::NEG_INFINITY;
1042 assert!(!nan.is_finite());
1043 assert!(!inf.is_finite());
1044 assert!(!neg_inf.is_finite());
1045 assert!(0.0f32.is_finite());
1046 assert!(42.8f32.is_finite());
1047 assert!((-109.2f32).is_finite());
1051 fn test_is_normal() {
1052 let nan: f32 = f32::NAN;
1053 let inf: f32 = f32::INFINITY;
1054 let neg_inf: f32 = f32::NEG_INFINITY;
1055 let zero: f32 = 0.0f32;
1056 let neg_zero: f32 = -0.0;
1057 assert!(!nan.is_normal());
1058 assert!(!inf.is_normal());
1059 assert!(!neg_inf.is_normal());
1060 assert!(!zero.is_normal());
1061 assert!(!neg_zero.is_normal());
1062 assert!(1f32.is_normal());
1063 assert!(1e-37f32.is_normal());
1064 assert!(!1e-38f32.is_normal());
1068 fn test_classify() {
1069 let nan: f32 = f32::NAN;
1070 let inf: f32 = f32::INFINITY;
1071 let neg_inf: f32 = f32::NEG_INFINITY;
1072 let zero: f32 = 0.0f32;
1073 let neg_zero: f32 = -0.0;
1074 assert_eq!(nan.classify(), Fp::Nan);
1075 assert_eq!(inf.classify(), Fp::Infinite);
1076 assert_eq!(neg_inf.classify(), Fp::Infinite);
1077 assert_eq!(zero.classify(), Fp::Zero);
1078 assert_eq!(neg_zero.classify(), Fp::Zero);
1079 assert_eq!(1f32.classify(), Fp::Normal);
1080 assert_eq!(1e-37f32.classify(), Fp::Normal);
1081 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1086 assert_approx_eq!(1.0f32.floor(), 1.0f32);
1087 assert_approx_eq!(1.3f32.floor(), 1.0f32);
1088 assert_approx_eq!(1.5f32.floor(), 1.0f32);
1089 assert_approx_eq!(1.7f32.floor(), 1.0f32);
1090 assert_approx_eq!(0.0f32.floor(), 0.0f32);
1091 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1092 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1093 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1094 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1095 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1100 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1101 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1102 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1103 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1104 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1105 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1106 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1107 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1108 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1109 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1114 assert_approx_eq!(1.0f32.round(), 1.0f32);
1115 assert_approx_eq!(1.3f32.round(), 1.0f32);
1116 assert_approx_eq!(1.5f32.round(), 2.0f32);
1117 assert_approx_eq!(1.7f32.round(), 2.0f32);
1118 assert_approx_eq!(0.0f32.round(), 0.0f32);
1119 assert_approx_eq!((-0.0f32).round(), -0.0f32);
1120 assert_approx_eq!((-1.0f32).round(), -1.0f32);
1121 assert_approx_eq!((-1.3f32).round(), -1.0f32);
1122 assert_approx_eq!((-1.5f32).round(), -2.0f32);
1123 assert_approx_eq!((-1.7f32).round(), -2.0f32);
1128 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1129 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1130 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1131 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1132 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1133 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1134 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1135 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1136 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1137 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1142 assert_approx_eq!(1.0f32.fract(), 0.0f32);
1143 assert_approx_eq!(1.3f32.fract(), 0.3f32);
1144 assert_approx_eq!(1.5f32.fract(), 0.5f32);
1145 assert_approx_eq!(1.7f32.fract(), 0.7f32);
1146 assert_approx_eq!(0.0f32.fract(), 0.0f32);
1147 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1148 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1149 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1150 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1151 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1156 assert_eq!(f32::INFINITY.abs(), f32::INFINITY);
1157 assert_eq!(1f32.abs(), 1f32);
1158 assert_eq!(0f32.abs(), 0f32);
1159 assert_eq!((-0f32).abs(), 0f32);
1160 assert_eq!((-1f32).abs(), 1f32);
1161 assert_eq!(f32::NEG_INFINITY.abs(), f32::INFINITY);
1162 assert_eq!((1f32 / f32::NEG_INFINITY).abs(), 0f32);
1163 assert!(f32::NAN.abs().is_nan());
1168 assert_eq!(f32::INFINITY.signum(), 1f32);
1169 assert_eq!(1f32.signum(), 1f32);
1170 assert_eq!(0f32.signum(), 1f32);
1171 assert_eq!((-0f32).signum(), -1f32);
1172 assert_eq!((-1f32).signum(), -1f32);
1173 assert_eq!(f32::NEG_INFINITY.signum(), -1f32);
1174 assert_eq!((1f32 / f32::NEG_INFINITY).signum(), -1f32);
1175 assert!(f32::NAN.signum().is_nan());
1179 fn test_is_sign_positive() {
1180 assert!(f32::INFINITY.is_sign_positive());
1181 assert!(1f32.is_sign_positive());
1182 assert!(0f32.is_sign_positive());
1183 assert!(!(-0f32).is_sign_positive());
1184 assert!(!(-1f32).is_sign_positive());
1185 assert!(!f32::NEG_INFINITY.is_sign_positive());
1186 assert!(!(1f32 / f32::NEG_INFINITY).is_sign_positive());
1187 assert!(f32::NAN.is_sign_positive());
1188 assert!(!(-f32::NAN).is_sign_positive());
1192 fn test_is_sign_negative() {
1193 assert!(!f32::INFINITY.is_sign_negative());
1194 assert!(!1f32.is_sign_negative());
1195 assert!(!0f32.is_sign_negative());
1196 assert!((-0f32).is_sign_negative());
1197 assert!((-1f32).is_sign_negative());
1198 assert!(f32::NEG_INFINITY.is_sign_negative());
1199 assert!((1f32 / f32::NEG_INFINITY).is_sign_negative());
1200 assert!(!f32::NAN.is_sign_negative());
1201 assert!((-f32::NAN).is_sign_negative());
1206 let nan: f32 = f32::NAN;
1207 let inf: f32 = f32::INFINITY;
1208 let neg_inf: f32 = f32::NEG_INFINITY;
1209 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1210 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1211 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1212 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1213 assert!(nan.mul_add(7.8, 9.0).is_nan());
1214 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1215 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1216 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1217 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1222 let nan: f32 = f32::NAN;
1223 let inf: f32 = f32::INFINITY;
1224 let neg_inf: f32 = f32::NEG_INFINITY;
1225 assert_eq!(1.0f32.recip(), 1.0);
1226 assert_eq!(2.0f32.recip(), 0.5);
1227 assert_eq!((-0.4f32).recip(), -2.5);
1228 assert_eq!(0.0f32.recip(), inf);
1229 assert!(nan.recip().is_nan());
1230 assert_eq!(inf.recip(), 0.0);
1231 assert_eq!(neg_inf.recip(), 0.0);
1236 let nan: f32 = f32::NAN;
1237 let inf: f32 = f32::INFINITY;
1238 let neg_inf: f32 = f32::NEG_INFINITY;
1239 assert_eq!(1.0f32.powi(1), 1.0);
1240 assert_approx_eq!((-3.1f32).powi(2), 9.61);
1241 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1242 assert_eq!(8.3f32.powi(0), 1.0);
1243 assert!(nan.powi(2).is_nan());
1244 assert_eq!(inf.powi(3), inf);
1245 assert_eq!(neg_inf.powi(2), inf);
1250 let nan: f32 = f32::NAN;
1251 let inf: f32 = f32::INFINITY;
1252 let neg_inf: f32 = f32::NEG_INFINITY;
1253 assert_eq!(1.0f32.powf(1.0), 1.0);
1254 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1255 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1256 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1257 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1258 assert_eq!(8.3f32.powf(0.0), 1.0);
1259 assert!(nan.powf(2.0).is_nan());
1260 assert_eq!(inf.powf(2.0), inf);
1261 assert_eq!(neg_inf.powf(3.0), neg_inf);
1265 fn test_sqrt_domain() {
1266 assert!(f32::NAN.sqrt().is_nan());
1267 assert!(f32::NEG_INFINITY.sqrt().is_nan());
1268 assert!((-1.0f32).sqrt().is_nan());
1269 assert_eq!((-0.0f32).sqrt(), -0.0);
1270 assert_eq!(0.0f32.sqrt(), 0.0);
1271 assert_eq!(1.0f32.sqrt(), 1.0);
1272 assert_eq!(f32::INFINITY.sqrt(), f32::INFINITY);
1277 assert_eq!(1.0, 0.0f32.exp());
1278 assert_approx_eq!(2.718282, 1.0f32.exp());
1279 assert_approx_eq!(148.413162, 5.0f32.exp());
1281 let inf: f32 = f32::INFINITY;
1282 let neg_inf: f32 = f32::NEG_INFINITY;
1283 let nan: f32 = f32::NAN;
1284 assert_eq!(inf, inf.exp());
1285 assert_eq!(0.0, neg_inf.exp());
1286 assert!(nan.exp().is_nan());
1291 assert_eq!(32.0, 5.0f32.exp2());
1292 assert_eq!(1.0, 0.0f32.exp2());
1294 let inf: f32 = f32::INFINITY;
1295 let neg_inf: f32 = f32::NEG_INFINITY;
1296 let nan: f32 = f32::NAN;
1297 assert_eq!(inf, inf.exp2());
1298 assert_eq!(0.0, neg_inf.exp2());
1299 assert!(nan.exp2().is_nan());
1304 let nan: f32 = f32::NAN;
1305 let inf: f32 = f32::INFINITY;
1306 let neg_inf: f32 = f32::NEG_INFINITY;
1307 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1308 assert!(nan.ln().is_nan());
1309 assert_eq!(inf.ln(), inf);
1310 assert!(neg_inf.ln().is_nan());
1311 assert!((-2.3f32).ln().is_nan());
1312 assert_eq!((-0.0f32).ln(), neg_inf);
1313 assert_eq!(0.0f32.ln(), neg_inf);
1314 assert_approx_eq!(4.0f32.ln(), 1.386294);
1319 let nan: f32 = f32::NAN;
1320 let inf: f32 = f32::INFINITY;
1321 let neg_inf: f32 = f32::NEG_INFINITY;
1322 assert_eq!(10.0f32.log(10.0), 1.0);
1323 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1324 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1325 assert!(1.0f32.log(1.0).is_nan());
1326 assert!(1.0f32.log(-13.9).is_nan());
1327 assert!(nan.log(2.3).is_nan());
1328 assert_eq!(inf.log(10.0), inf);
1329 assert!(neg_inf.log(8.8).is_nan());
1330 assert!((-2.3f32).log(0.1).is_nan());
1331 assert_eq!((-0.0f32).log(2.0), neg_inf);
1332 assert_eq!(0.0f32.log(7.0), neg_inf);
1337 let nan: f32 = f32::NAN;
1338 let inf: f32 = f32::INFINITY;
1339 let neg_inf: f32 = f32::NEG_INFINITY;
1340 assert_approx_eq!(10.0f32.log2(), 3.321928);
1341 assert_approx_eq!(2.3f32.log2(), 1.201634);
1342 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1343 assert!(nan.log2().is_nan());
1344 assert_eq!(inf.log2(), inf);
1345 assert!(neg_inf.log2().is_nan());
1346 assert!((-2.3f32).log2().is_nan());
1347 assert_eq!((-0.0f32).log2(), neg_inf);
1348 assert_eq!(0.0f32.log2(), neg_inf);
1353 let nan: f32 = f32::NAN;
1354 let inf: f32 = f32::INFINITY;
1355 let neg_inf: f32 = f32::NEG_INFINITY;
1356 assert_eq!(10.0f32.log10(), 1.0);
1357 assert_approx_eq!(2.3f32.log10(), 0.361728);
1358 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1359 assert_eq!(1.0f32.log10(), 0.0);
1360 assert!(nan.log10().is_nan());
1361 assert_eq!(inf.log10(), inf);
1362 assert!(neg_inf.log10().is_nan());
1363 assert!((-2.3f32).log10().is_nan());
1364 assert_eq!((-0.0f32).log10(), neg_inf);
1365 assert_eq!(0.0f32.log10(), neg_inf);
1369 fn test_to_degrees() {
1370 let pi: f32 = consts::PI;
1371 let nan: f32 = f32::NAN;
1372 let inf: f32 = f32::INFINITY;
1373 let neg_inf: f32 = f32::NEG_INFINITY;
1374 assert_eq!(0.0f32.to_degrees(), 0.0);
1375 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1376 assert_eq!(pi.to_degrees(), 180.0);
1377 assert!(nan.to_degrees().is_nan());
1378 assert_eq!(inf.to_degrees(), inf);
1379 assert_eq!(neg_inf.to_degrees(), neg_inf);
1380 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1384 fn test_to_radians() {
1385 let pi: f32 = consts::PI;
1386 let nan: f32 = f32::NAN;
1387 let inf: f32 = f32::INFINITY;
1388 let neg_inf: f32 = f32::NEG_INFINITY;
1389 assert_eq!(0.0f32.to_radians(), 0.0);
1390 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1391 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1392 assert_eq!(180.0f32.to_radians(), pi);
1393 assert!(nan.to_radians().is_nan());
1394 assert_eq!(inf.to_radians(), inf);
1395 assert_eq!(neg_inf.to_radians(), neg_inf);
1400 assert_eq!(0.0f32.asinh(), 0.0f32);
1401 assert_eq!((-0.0f32).asinh(), -0.0f32);
1403 let inf: f32 = f32::INFINITY;
1404 let neg_inf: f32 = f32::NEG_INFINITY;
1405 let nan: f32 = f32::NAN;
1406 assert_eq!(inf.asinh(), inf);
1407 assert_eq!(neg_inf.asinh(), neg_inf);
1408 assert!(nan.asinh().is_nan());
1409 assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
1410 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1411 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1412 // regression test for the catastrophic cancellation fixed in 72486
1413 assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32);
1418 assert_eq!(1.0f32.acosh(), 0.0f32);
1419 assert!(0.999f32.acosh().is_nan());
1421 let inf: f32 = f32::INFINITY;
1422 let neg_inf: f32 = f32::NEG_INFINITY;
1423 let nan: f32 = f32::NAN;
1424 assert_eq!(inf.acosh(), inf);
1425 assert!(neg_inf.acosh().is_nan());
1426 assert!(nan.acosh().is_nan());
1427 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1428 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1433 assert_eq!(0.0f32.atanh(), 0.0f32);
1434 assert_eq!((-0.0f32).atanh(), -0.0f32);
1436 let inf32: f32 = f32::INFINITY;
1437 let neg_inf32: f32 = f32::NEG_INFINITY;
1438 assert_eq!(1.0f32.atanh(), inf32);
1439 assert_eq!((-1.0f32).atanh(), neg_inf32);
1441 assert!(2f64.atanh().atanh().is_nan());
1442 assert!((-2f64).atanh().atanh().is_nan());
1444 let inf64: f32 = f32::INFINITY;
1445 let neg_inf64: f32 = f32::NEG_INFINITY;
1446 let nan32: f32 = f32::NAN;
1447 assert!(inf64.atanh().is_nan());
1448 assert!(neg_inf64.atanh().is_nan());
1449 assert!(nan32.atanh().is_nan());
1451 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1452 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1456 fn test_real_consts() {
1459 let pi: f32 = consts::PI;
1460 let frac_pi_2: f32 = consts::FRAC_PI_2;
1461 let frac_pi_3: f32 = consts::FRAC_PI_3;
1462 let frac_pi_4: f32 = consts::FRAC_PI_4;
1463 let frac_pi_6: f32 = consts::FRAC_PI_6;
1464 let frac_pi_8: f32 = consts::FRAC_PI_8;
1465 let frac_1_pi: f32 = consts::FRAC_1_PI;
1466 let frac_2_pi: f32 = consts::FRAC_2_PI;
1467 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1468 let sqrt2: f32 = consts::SQRT_2;
1469 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1470 let e: f32 = consts::E;
1471 let log2_e: f32 = consts::LOG2_E;
1472 let log10_e: f32 = consts::LOG10_E;
1473 let ln_2: f32 = consts::LN_2;
1474 let ln_10: f32 = consts::LN_10;
1476 assert_approx_eq!(frac_pi_2, pi / 2f32);
1477 assert_approx_eq!(frac_pi_3, pi / 3f32);
1478 assert_approx_eq!(frac_pi_4, pi / 4f32);
1479 assert_approx_eq!(frac_pi_6, pi / 6f32);
1480 assert_approx_eq!(frac_pi_8, pi / 8f32);
1481 assert_approx_eq!(frac_1_pi, 1f32 / pi);
1482 assert_approx_eq!(frac_2_pi, 2f32 / pi);
1483 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1484 assert_approx_eq!(sqrt2, 2f32.sqrt());
1485 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1486 assert_approx_eq!(log2_e, e.log2());
1487 assert_approx_eq!(log10_e, e.log10());
1488 assert_approx_eq!(ln_2, 2f32.ln());
1489 assert_approx_eq!(ln_10, 10f32.ln());
1493 fn test_float_bits_conv() {
1494 assert_eq!((1f32).to_bits(), 0x3f800000);
1495 assert_eq!((12.5f32).to_bits(), 0x41480000);
1496 assert_eq!((1337f32).to_bits(), 0x44a72000);
1497 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1498 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1499 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1500 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1501 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1503 // Check that NaNs roundtrip their bits regardless of signaling-ness
1504 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1505 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1506 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1507 assert!(f32::from_bits(masked_nan1).is_nan());
1508 assert!(f32::from_bits(masked_nan2).is_nan());
1510 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1511 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1516 fn test_clamp_min_greater_than_max() {
1517 let _ = 1.0f32.clamp(3.0, 1.0);
1522 fn test_clamp_min_is_nan() {
1523 let _ = 1.0f32.clamp(f32::NAN, 1.0);
1528 fn test_clamp_max_is_nan() {
1529 let _ = 1.0f32.clamp(3.0, f32::NAN);
1533 fn test_total_cmp() {
1534 use core::cmp::Ordering;
1536 fn quiet_bit_mask() -> u32 {
1537 1 << (f32::MANTISSA_DIGITS - 2)
1540 fn min_subnorm() -> f32 {
1541 f32::MIN_POSITIVE / f32::powf(2.0, f32::MANTISSA_DIGITS as f32 - 1.0)
1544 fn max_subnorm() -> f32 {
1545 f32::MIN_POSITIVE - min_subnorm()
1549 f32::from_bits(f32::NAN.to_bits() | quiet_bit_mask())
1553 f32::from_bits((f32::NAN.to_bits() & !quiet_bit_mask()) + 42)
1556 assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan()));
1557 assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan()));
1558 assert_eq!(Ordering::Equal, (-f32::INFINITY).total_cmp(&-f32::INFINITY));
1559 assert_eq!(Ordering::Equal, (-f32::MAX).total_cmp(&-f32::MAX));
1560 assert_eq!(Ordering::Equal, (-2.5_f32).total_cmp(&-2.5));
1561 assert_eq!(Ordering::Equal, (-1.0_f32).total_cmp(&-1.0));
1562 assert_eq!(Ordering::Equal, (-1.5_f32).total_cmp(&-1.5));
1563 assert_eq!(Ordering::Equal, (-0.5_f32).total_cmp(&-0.5));
1564 assert_eq!(Ordering::Equal, (-f32::MIN_POSITIVE).total_cmp(&-f32::MIN_POSITIVE));
1565 assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm()));
1566 assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm()));
1567 assert_eq!(Ordering::Equal, (-0.0_f32).total_cmp(&-0.0));
1568 assert_eq!(Ordering::Equal, 0.0_f32.total_cmp(&0.0));
1569 assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm()));
1570 assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm()));
1571 assert_eq!(Ordering::Equal, f32::MIN_POSITIVE.total_cmp(&f32::MIN_POSITIVE));
1572 assert_eq!(Ordering::Equal, 0.5_f32.total_cmp(&0.5));
1573 assert_eq!(Ordering::Equal, 1.0_f32.total_cmp(&1.0));
1574 assert_eq!(Ordering::Equal, 1.5_f32.total_cmp(&1.5));
1575 assert_eq!(Ordering::Equal, 2.5_f32.total_cmp(&2.5));
1576 assert_eq!(Ordering::Equal, f32::MAX.total_cmp(&f32::MAX));
1577 assert_eq!(Ordering::Equal, f32::INFINITY.total_cmp(&f32::INFINITY));
1578 assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan()));
1579 assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan()));
1581 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
1582 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
1583 assert_eq!(Ordering::Less, (-f32::INFINITY).total_cmp(&-f32::MAX));
1584 assert_eq!(Ordering::Less, (-f32::MAX).total_cmp(&-2.5));
1585 assert_eq!(Ordering::Less, (-2.5_f32).total_cmp(&-1.5));
1586 assert_eq!(Ordering::Less, (-1.5_f32).total_cmp(&-1.0));
1587 assert_eq!(Ordering::Less, (-1.0_f32).total_cmp(&-0.5));
1588 assert_eq!(Ordering::Less, (-0.5_f32).total_cmp(&-f32::MIN_POSITIVE));
1589 assert_eq!(Ordering::Less, (-f32::MIN_POSITIVE).total_cmp(&-max_subnorm()));
1590 assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm()));
1591 assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0));
1592 assert_eq!(Ordering::Less, (-0.0_f32).total_cmp(&0.0));
1593 assert_eq!(Ordering::Less, 0.0_f32.total_cmp(&min_subnorm()));
1594 assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm()));
1595 assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f32::MIN_POSITIVE));
1596 assert_eq!(Ordering::Less, f32::MIN_POSITIVE.total_cmp(&0.5));
1597 assert_eq!(Ordering::Less, 0.5_f32.total_cmp(&1.0));
1598 assert_eq!(Ordering::Less, 1.0_f32.total_cmp(&1.5));
1599 assert_eq!(Ordering::Less, 1.5_f32.total_cmp(&2.5));
1600 assert_eq!(Ordering::Less, 2.5_f32.total_cmp(&f32::MAX));
1601 assert_eq!(Ordering::Less, f32::MAX.total_cmp(&f32::INFINITY));
1602 assert_eq!(Ordering::Less, f32::INFINITY.total_cmp(&s_nan()));
1603 assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan()));
1605 assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan()));
1606 assert_eq!(Ordering::Greater, (-f32::INFINITY).total_cmp(&-s_nan()));
1607 assert_eq!(Ordering::Greater, (-f32::MAX).total_cmp(&-f32::INFINITY));
1608 assert_eq!(Ordering::Greater, (-2.5_f32).total_cmp(&-f32::MAX));
1609 assert_eq!(Ordering::Greater, (-1.5_f32).total_cmp(&-2.5));
1610 assert_eq!(Ordering::Greater, (-1.0_f32).total_cmp(&-1.5));
1611 assert_eq!(Ordering::Greater, (-0.5_f32).total_cmp(&-1.0));
1612 assert_eq!(Ordering::Greater, (-f32::MIN_POSITIVE).total_cmp(&-0.5));
1613 assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f32::MIN_POSITIVE));
1614 assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm()));
1615 assert_eq!(Ordering::Greater, (-0.0_f32).total_cmp(&-min_subnorm()));
1616 assert_eq!(Ordering::Greater, 0.0_f32.total_cmp(&-0.0));
1617 assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0));
1618 assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm()));
1619 assert_eq!(Ordering::Greater, f32::MIN_POSITIVE.total_cmp(&max_subnorm()));
1620 assert_eq!(Ordering::Greater, 0.5_f32.total_cmp(&f32::MIN_POSITIVE));
1621 assert_eq!(Ordering::Greater, 1.0_f32.total_cmp(&0.5));
1622 assert_eq!(Ordering::Greater, 1.5_f32.total_cmp(&1.0));
1623 assert_eq!(Ordering::Greater, 2.5_f32.total_cmp(&1.5));
1624 assert_eq!(Ordering::Greater, f32::MAX.total_cmp(&2.5));
1625 assert_eq!(Ordering::Greater, f32::INFINITY.total_cmp(&f32::MAX));
1626 assert_eq!(Ordering::Greater, s_nan().total_cmp(&f32::INFINITY));
1627 assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan()));
1629 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
1630 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::INFINITY));
1631 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MAX));
1632 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5));
1633 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5));
1634 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0));
1635 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5));
1636 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MIN_POSITIVE));
1637 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm()));
1638 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm()));
1639 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0));
1640 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0));
1641 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm()));
1642 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm()));
1643 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MIN_POSITIVE));
1644 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5));
1645 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0));
1646 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5));
1647 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5));
1648 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MAX));
1649 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::INFINITY));
1650 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan()));
1652 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
1653 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MAX));
1654 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5));
1655 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5));
1656 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0));
1657 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5));
1658 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MIN_POSITIVE));
1659 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm()));
1660 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm()));
1661 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0));
1662 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0));
1663 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm()));
1664 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm()));
1665 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MIN_POSITIVE));
1666 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5));
1667 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0));
1668 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5));
1669 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5));
1670 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MAX));
1671 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::INFINITY));
1672 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan()));