1 //! This module provides constants which are specific to the implementation
2 //! of the `f64` floating point data type.
4 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
8 #![stable(feature = "rust1", since = "1.0.0")]
9 #![allow(missing_docs)]
12 use crate::intrinsics;
14 use crate::sys::cmath;
16 #[stable(feature = "rust1", since = "1.0.0")]
17 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
20 #[stable(feature = "rust1", since = "1.0.0")]
21 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use core::f64::consts;
28 #[lang = "f64_runtime"]
30 /// Returns the largest integer less than or equal to a number.
39 /// assert_eq!(f.floor(), 3.0);
40 /// assert_eq!(g.floor(), 3.0);
41 /// assert_eq!(h.floor(), -4.0);
43 #[must_use = "method returns a new number and does not mutate the original value"]
44 #[stable(feature = "rust1", since = "1.0.0")]
46 pub fn floor(self) -> f64 {
47 unsafe { intrinsics::floorf64(self) }
50 /// Returns the smallest integer greater than or equal to a number.
58 /// assert_eq!(f.ceil(), 4.0);
59 /// assert_eq!(g.ceil(), 4.0);
61 #[must_use = "method returns a new number and does not mutate the original value"]
62 #[stable(feature = "rust1", since = "1.0.0")]
64 pub fn ceil(self) -> f64 {
65 unsafe { intrinsics::ceilf64(self) }
68 /// Returns the nearest integer to a number. Round half-way cases away from
77 /// assert_eq!(f.round(), 3.0);
78 /// assert_eq!(g.round(), -3.0);
80 #[must_use = "method returns a new number and does not mutate the original value"]
81 #[stable(feature = "rust1", since = "1.0.0")]
83 pub fn round(self) -> f64 {
84 unsafe { intrinsics::roundf64(self) }
87 /// Returns the integer part of a number.
96 /// assert_eq!(f.trunc(), 3.0);
97 /// assert_eq!(g.trunc(), 3.0);
98 /// assert_eq!(h.trunc(), -3.0);
100 #[must_use = "method returns a new number and does not mutate the original value"]
101 #[stable(feature = "rust1", since = "1.0.0")]
103 pub fn trunc(self) -> f64 {
104 unsafe { intrinsics::truncf64(self) }
107 /// Returns the fractional part of a number.
113 /// let y = -3.5_f64;
114 /// let abs_difference_x = (x.fract() - 0.5).abs();
115 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
117 /// assert!(abs_difference_x < 1e-10);
118 /// assert!(abs_difference_y < 1e-10);
120 #[must_use = "method returns a new number and does not mutate the original value"]
121 #[stable(feature = "rust1", since = "1.0.0")]
123 pub fn fract(self) -> f64 { self - self.trunc() }
125 /// Computes the absolute value of `self`. Returns `NAN` if the
134 /// let y = -3.5_f64;
136 /// let abs_difference_x = (x.abs() - x).abs();
137 /// let abs_difference_y = (y.abs() - (-y)).abs();
139 /// assert!(abs_difference_x < 1e-10);
140 /// assert!(abs_difference_y < 1e-10);
142 /// assert!(f64::NAN.abs().is_nan());
144 #[must_use = "method returns a new number and does not mutate the original value"]
145 #[stable(feature = "rust1", since = "1.0.0")]
147 pub fn abs(self) -> f64 {
148 unsafe { intrinsics::fabsf64(self) }
151 /// Returns a number that represents the sign of `self`.
153 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
154 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
155 /// - `NAN` if the number is `NAN`
164 /// assert_eq!(f.signum(), 1.0);
165 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
167 /// assert!(f64::NAN.signum().is_nan());
169 #[must_use = "method returns a new number and does not mutate the original value"]
170 #[stable(feature = "rust1", since = "1.0.0")]
172 pub fn signum(self) -> f64 {
176 1.0_f64.copysign(self)
180 /// Returns a number composed of the magnitude of `self` and the sign of
183 /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
184 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
185 /// `sign` is returned.
194 /// assert_eq!(f.copysign(0.42), 3.5_f64);
195 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
196 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
197 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
199 /// assert!(f64::NAN.copysign(1.0).is_nan());
201 #[must_use = "method returns a new number and does not mutate the original value"]
202 #[stable(feature = "copysign", since = "1.35.0")]
204 pub fn copysign(self, sign: f64) -> f64 {
205 unsafe { intrinsics::copysignf64(self, sign) }
208 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
209 /// error, yielding a more accurate result than an unfused multiply-add.
211 /// Using `mul_add` can be more performant than an unfused multiply-add if
212 /// the target architecture has a dedicated `fma` CPU instruction.
217 /// let m = 10.0_f64;
219 /// let b = 60.0_f64;
222 /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
224 /// assert!(abs_difference < 1e-10);
226 #[must_use = "method returns a new number and does not mutate the original value"]
227 #[stable(feature = "rust1", since = "1.0.0")]
229 pub fn mul_add(self, a: f64, b: f64) -> f64 {
230 unsafe { intrinsics::fmaf64(self, a, b) }
233 /// Calculates Euclidean division, the matching method for `rem_euclid`.
235 /// This computes the integer `n` such that
236 /// `self = n * rhs + self.rem_euclid(rhs)`.
237 /// In other words, the result is `self / rhs` rounded to the integer `n`
238 /// such that `self >= n * rhs`.
243 /// let a: f64 = 7.0;
245 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
246 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
247 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
248 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
250 #[must_use = "method returns a new number and does not mutate the original value"]
252 #[stable(feature = "euclidean_division", since = "1.38.0")]
253 pub fn div_euclid(self, rhs: f64) -> f64 {
254 let q = (self / rhs).trunc();
255 if self % rhs < 0.0 {
256 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
261 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
263 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
264 /// most cases. However, due to a floating point round-off error it can
265 /// result in `r == rhs.abs()`, violating the mathematical definition, if
266 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
267 /// This result is not an element of the function's codomain, but it is the
268 /// closest floating point number in the real numbers and thus fulfills the
269 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
275 /// let a: f64 = 7.0;
277 /// assert_eq!(a.rem_euclid(b), 3.0);
278 /// assert_eq!((-a).rem_euclid(b), 1.0);
279 /// assert_eq!(a.rem_euclid(-b), 3.0);
280 /// assert_eq!((-a).rem_euclid(-b), 1.0);
281 /// // limitation due to round-off error
282 /// assert!((-std::f64::EPSILON).rem_euclid(3.0) != 0.0);
284 #[must_use = "method returns a new number and does not mutate the original value"]
286 #[stable(feature = "euclidean_division", since = "1.38.0")]
287 pub fn rem_euclid(self, rhs: f64) -> f64 {
296 /// Raises a number to an integer power.
298 /// Using this function is generally faster than using `powf`
304 /// let abs_difference = (x.powi(2) - (x * x)).abs();
306 /// assert!(abs_difference < 1e-10);
308 #[must_use = "method returns a new number and does not mutate the original value"]
309 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn powi(self, n: i32) -> f64 {
312 unsafe { intrinsics::powif64(self, n) }
315 /// Raises a number to a floating point power.
321 /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
323 /// assert!(abs_difference < 1e-10);
325 #[must_use = "method returns a new number and does not mutate the original value"]
326 #[stable(feature = "rust1", since = "1.0.0")]
328 pub fn powf(self, n: f64) -> f64 {
329 unsafe { intrinsics::powf64(self, n) }
332 /// Takes the square root of a number.
334 /// Returns NaN if `self` is a negative number.
339 /// let positive = 4.0_f64;
340 /// let negative = -4.0_f64;
342 /// let abs_difference = (positive.sqrt() - 2.0).abs();
344 /// assert!(abs_difference < 1e-10);
345 /// assert!(negative.sqrt().is_nan());
347 #[must_use = "method returns a new number and does not mutate the original value"]
348 #[stable(feature = "rust1", since = "1.0.0")]
350 pub fn sqrt(self) -> f64 {
354 unsafe { intrinsics::sqrtf64(self) }
358 /// Returns `e^(self)`, (the exponential function).
363 /// let one = 1.0_f64;
365 /// let e = one.exp();
367 /// // ln(e) - 1 == 0
368 /// let abs_difference = (e.ln() - 1.0).abs();
370 /// assert!(abs_difference < 1e-10);
372 #[must_use = "method returns a new number and does not mutate the original value"]
373 #[stable(feature = "rust1", since = "1.0.0")]
375 pub fn exp(self) -> f64 {
376 unsafe { intrinsics::expf64(self) }
379 /// Returns `2^(self)`.
387 /// let abs_difference = (f.exp2() - 4.0).abs();
389 /// assert!(abs_difference < 1e-10);
391 #[must_use = "method returns a new number and does not mutate the original value"]
392 #[stable(feature = "rust1", since = "1.0.0")]
394 pub fn exp2(self) -> f64 {
395 unsafe { intrinsics::exp2f64(self) }
398 /// Returns the natural logarithm of the number.
403 /// let one = 1.0_f64;
405 /// let e = one.exp();
407 /// // ln(e) - 1 == 0
408 /// let abs_difference = (e.ln() - 1.0).abs();
410 /// assert!(abs_difference < 1e-10);
412 #[must_use = "method returns a new number and does not mutate the original value"]
413 #[stable(feature = "rust1", since = "1.0.0")]
415 pub fn ln(self) -> f64 {
416 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
419 /// Returns the logarithm of the number with respect to an arbitrary base.
421 /// The result may not be correctly rounded owing to implementation details;
422 /// `self.log2()` can produce more accurate results for base 2, and
423 /// `self.log10()` can produce more accurate results for base 10.
428 /// let twenty_five = 25.0_f64;
430 /// // log5(25) - 2 == 0
431 /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
433 /// assert!(abs_difference < 1e-10);
435 #[must_use = "method returns a new number and does not mutate the original value"]
436 #[stable(feature = "rust1", since = "1.0.0")]
438 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
440 /// Returns the base 2 logarithm of the number.
445 /// let four = 4.0_f64;
447 /// // log2(4) - 2 == 0
448 /// let abs_difference = (four.log2() - 2.0).abs();
450 /// assert!(abs_difference < 1e-10);
452 #[must_use = "method returns a new number and does not mutate the original value"]
453 #[stable(feature = "rust1", since = "1.0.0")]
455 pub fn log2(self) -> f64 {
456 self.log_wrapper(|n| {
457 #[cfg(target_os = "android")]
458 return crate::sys::android::log2f64(n);
459 #[cfg(not(target_os = "android"))]
460 return unsafe { intrinsics::log2f64(n) };
464 /// Returns the base 10 logarithm of the number.
469 /// let hundred = 100.0_f64;
471 /// // log10(100) - 2 == 0
472 /// let abs_difference = (hundred.log10() - 2.0).abs();
474 /// assert!(abs_difference < 1e-10);
476 #[must_use = "method returns a new number and does not mutate the original value"]
477 #[stable(feature = "rust1", since = "1.0.0")]
479 pub fn log10(self) -> f64 {
480 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
483 /// The positive difference of two numbers.
485 /// * If `self <= other`: `0:0`
486 /// * Else: `self - other`
492 /// let y = -3.0_f64;
494 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
495 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
497 /// assert!(abs_difference_x < 1e-10);
498 /// assert!(abs_difference_y < 1e-10);
500 #[must_use = "method returns a new number and does not mutate the original value"]
501 #[stable(feature = "rust1", since = "1.0.0")]
503 #[rustc_deprecated(since = "1.10.0",
504 reason = "you probably meant `(self - other).abs()`: \
505 this operation is `(self - other).max(0.0)` \
506 except that `abs_sub` also propagates NaNs (also \
507 known as `fdim` in C). If you truly need the positive \
508 difference, consider using that expression or the C function \
509 `fdim`, depending on how you wish to handle NaN (please consider \
510 filing an issue describing your use-case too).")]
511 pub fn abs_sub(self, other: f64) -> f64 {
512 unsafe { cmath::fdim(self, other) }
515 /// Takes the cubic root of a number.
522 /// // x^(1/3) - 2 == 0
523 /// let abs_difference = (x.cbrt() - 2.0).abs();
525 /// assert!(abs_difference < 1e-10);
527 #[must_use = "method returns a new number and does not mutate the original value"]
528 #[stable(feature = "rust1", since = "1.0.0")]
530 pub fn cbrt(self) -> f64 {
531 unsafe { cmath::cbrt(self) }
534 /// Calculates the length of the hypotenuse of a right-angle triangle given
535 /// legs of length `x` and `y`.
543 /// // sqrt(x^2 + y^2)
544 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
546 /// assert!(abs_difference < 1e-10);
548 #[must_use = "method returns a new number and does not mutate the original value"]
549 #[stable(feature = "rust1", since = "1.0.0")]
551 pub fn hypot(self, other: f64) -> f64 {
552 unsafe { cmath::hypot(self, other) }
555 /// Computes the sine of a number (in radians).
562 /// let x = f64::consts::FRAC_PI_2;
564 /// let abs_difference = (x.sin() - 1.0).abs();
566 /// assert!(abs_difference < 1e-10);
568 #[must_use = "method returns a new number and does not mutate the original value"]
569 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn sin(self) -> f64 {
572 unsafe { intrinsics::sinf64(self) }
575 /// Computes the cosine of a number (in radians).
582 /// let x = 2.0 * f64::consts::PI;
584 /// let abs_difference = (x.cos() - 1.0).abs();
586 /// assert!(abs_difference < 1e-10);
588 #[must_use = "method returns a new number and does not mutate the original value"]
589 #[stable(feature = "rust1", since = "1.0.0")]
591 pub fn cos(self) -> f64 {
592 unsafe { intrinsics::cosf64(self) }
595 /// Computes the tangent of a number (in radians).
602 /// let x = f64::consts::FRAC_PI_4;
603 /// let abs_difference = (x.tan() - 1.0).abs();
605 /// assert!(abs_difference < 1e-14);
607 #[must_use = "method returns a new number and does not mutate the original value"]
608 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn tan(self) -> f64 {
611 unsafe { cmath::tan(self) }
614 /// Computes the arcsine of a number. Return value is in radians in
615 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
623 /// let f = f64::consts::FRAC_PI_2;
625 /// // asin(sin(pi/2))
626 /// let abs_difference = (f.sin().asin() - f64::consts::FRAC_PI_2).abs();
628 /// assert!(abs_difference < 1e-10);
630 #[must_use = "method returns a new number and does not mutate the original value"]
631 #[stable(feature = "rust1", since = "1.0.0")]
633 pub fn asin(self) -> f64 {
634 unsafe { cmath::asin(self) }
637 /// Computes the arccosine of a number. Return value is in radians in
638 /// the range [0, pi] or NaN if the number is outside the range
646 /// let f = f64::consts::FRAC_PI_4;
648 /// // acos(cos(pi/4))
649 /// let abs_difference = (f.cos().acos() - f64::consts::FRAC_PI_4).abs();
651 /// assert!(abs_difference < 1e-10);
653 #[must_use = "method returns a new number and does not mutate the original value"]
654 #[stable(feature = "rust1", since = "1.0.0")]
656 pub fn acos(self) -> f64 {
657 unsafe { cmath::acos(self) }
660 /// Computes the arctangent of a number. Return value is in radians in the
661 /// range [-pi/2, pi/2];
669 /// let abs_difference = (f.tan().atan() - 1.0).abs();
671 /// assert!(abs_difference < 1e-10);
673 #[must_use = "method returns a new number and does not mutate the original value"]
674 #[stable(feature = "rust1", since = "1.0.0")]
676 pub fn atan(self) -> f64 {
677 unsafe { cmath::atan(self) }
680 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
682 /// * `x = 0`, `y = 0`: `0`
683 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
684 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
685 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
692 /// // Positive angles measured counter-clockwise
693 /// // from positive x axis
694 /// // -pi/4 radians (45 deg clockwise)
695 /// let x1 = 3.0_f64;
696 /// let y1 = -3.0_f64;
698 /// // 3pi/4 radians (135 deg counter-clockwise)
699 /// let x2 = -3.0_f64;
700 /// let y2 = 3.0_f64;
702 /// let abs_difference_1 = (y1.atan2(x1) - (-f64::consts::FRAC_PI_4)).abs();
703 /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * f64::consts::FRAC_PI_4)).abs();
705 /// assert!(abs_difference_1 < 1e-10);
706 /// assert!(abs_difference_2 < 1e-10);
708 #[must_use = "method returns a new number and does not mutate the original value"]
709 #[stable(feature = "rust1", since = "1.0.0")]
711 pub fn atan2(self, other: f64) -> f64 {
712 unsafe { cmath::atan2(self, other) }
715 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
716 /// `(sin(x), cos(x))`.
723 /// let x = f64::consts::FRAC_PI_4;
724 /// let f = x.sin_cos();
726 /// let abs_difference_0 = (f.0 - x.sin()).abs();
727 /// let abs_difference_1 = (f.1 - x.cos()).abs();
729 /// assert!(abs_difference_0 < 1e-10);
730 /// assert!(abs_difference_1 < 1e-10);
732 #[stable(feature = "rust1", since = "1.0.0")]
734 pub fn sin_cos(self) -> (f64, f64) {
735 (self.sin(), self.cos())
738 /// Returns `e^(self) - 1` in a way that is accurate even if the
739 /// number is close to zero.
747 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
749 /// assert!(abs_difference < 1e-10);
751 #[must_use = "method returns a new number and does not mutate the original value"]
752 #[stable(feature = "rust1", since = "1.0.0")]
754 pub fn exp_m1(self) -> f64 {
755 unsafe { cmath::expm1(self) }
758 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
759 /// the operations were performed separately.
766 /// let x = f64::consts::E - 1.0;
768 /// // ln(1 + (e - 1)) == ln(e) == 1
769 /// let abs_difference = (x.ln_1p() - 1.0).abs();
771 /// assert!(abs_difference < 1e-10);
773 #[must_use = "method returns a new number and does not mutate the original value"]
774 #[stable(feature = "rust1", since = "1.0.0")]
776 pub fn ln_1p(self) -> f64 {
777 unsafe { cmath::log1p(self) }
780 /// Hyperbolic sine function.
787 /// let e = f64::consts::E;
790 /// let f = x.sinh();
791 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
792 /// let g = ((e * e) - 1.0) / (2.0 * e);
793 /// let abs_difference = (f - g).abs();
795 /// assert!(abs_difference < 1e-10);
797 #[must_use = "method returns a new number and does not mutate the original value"]
798 #[stable(feature = "rust1", since = "1.0.0")]
800 pub fn sinh(self) -> f64 {
801 unsafe { cmath::sinh(self) }
804 /// Hyperbolic cosine function.
811 /// let e = f64::consts::E;
813 /// let f = x.cosh();
814 /// // Solving cosh() at 1 gives this result
815 /// let g = ((e * e) + 1.0) / (2.0 * e);
816 /// let abs_difference = (f - g).abs();
819 /// assert!(abs_difference < 1.0e-10);
821 #[must_use = "method returns a new number and does not mutate the original value"]
822 #[stable(feature = "rust1", since = "1.0.0")]
824 pub fn cosh(self) -> f64 {
825 unsafe { cmath::cosh(self) }
828 /// Hyperbolic tangent function.
835 /// let e = f64::consts::E;
838 /// let f = x.tanh();
839 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
840 /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
841 /// let abs_difference = (f - g).abs();
843 /// assert!(abs_difference < 1.0e-10);
845 #[must_use = "method returns a new number and does not mutate the original value"]
846 #[stable(feature = "rust1", since = "1.0.0")]
848 pub fn tanh(self) -> f64 {
849 unsafe { cmath::tanh(self) }
852 /// Inverse hyperbolic sine function.
858 /// let f = x.sinh().asinh();
860 /// let abs_difference = (f - x).abs();
862 /// assert!(abs_difference < 1.0e-10);
864 #[must_use = "method returns a new number and does not mutate the original value"]
865 #[stable(feature = "rust1", since = "1.0.0")]
867 pub fn asinh(self) -> f64 {
868 if self == NEG_INFINITY {
871 (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
875 /// Inverse hyperbolic cosine function.
881 /// let f = x.cosh().acosh();
883 /// let abs_difference = (f - x).abs();
885 /// assert!(abs_difference < 1.0e-10);
887 #[must_use = "method returns a new number and does not mutate the original value"]
888 #[stable(feature = "rust1", since = "1.0.0")]
890 pub fn acosh(self) -> f64 {
894 (self + ((self * self) - 1.0).sqrt()).ln()
898 /// Inverse hyperbolic tangent function.
905 /// let e = f64::consts::E;
906 /// let f = e.tanh().atanh();
908 /// let abs_difference = (f - e).abs();
910 /// assert!(abs_difference < 1.0e-10);
912 #[must_use = "method returns a new number and does not mutate the original value"]
913 #[stable(feature = "rust1", since = "1.0.0")]
915 pub fn atanh(self) -> f64 {
916 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
919 /// Restrict a value to a certain interval unless it is NaN.
921 /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
922 /// less than `min`. Otherwise this returns `self`.
924 /// Not that this function returns NaN if the initial value was NaN as
929 /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
934 /// #![feature(clamp)]
935 /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
936 /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
937 /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
938 /// assert!((std::f64::NAN).clamp(-2.0, 1.0).is_nan());
940 #[must_use = "method returns a new number and does not mutate the original value"]
941 #[unstable(feature = "clamp", issue = "44095")]
943 pub fn clamp(self, min: f64, max: f64) -> f64 {
946 if x < min { x = min; }
947 if x > max { x = max; }
951 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
952 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
954 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
955 if !cfg!(target_os = "solaris") {
958 if self.is_finite() {
961 } else if self == 0.0 {
962 NEG_INFINITY // log(0) = -Inf
966 } else if self.is_nan() {
967 self // log(NaN) = NaN
968 } else if self > 0.0 {
969 self // log(Inf) = Inf
971 NAN // log(-Inf) = NaN
982 use crate::num::FpCategory as Fp;
986 test_num(10f64, 2f64);
991 assert_eq!(NAN.min(2.0), 2.0);
992 assert_eq!(2.0f64.min(NAN), 2.0);
997 assert_eq!(NAN.max(2.0), 2.0);
998 assert_eq!(2.0f64.max(NAN), 2.0);
1004 assert!(nan.is_nan());
1005 assert!(!nan.is_infinite());
1006 assert!(!nan.is_finite());
1007 assert!(!nan.is_normal());
1008 assert!(nan.is_sign_positive());
1009 assert!(!nan.is_sign_negative());
1010 assert_eq!(Fp::Nan, nan.classify());
1014 fn test_infinity() {
1015 let inf: f64 = INFINITY;
1016 assert!(inf.is_infinite());
1017 assert!(!inf.is_finite());
1018 assert!(inf.is_sign_positive());
1019 assert!(!inf.is_sign_negative());
1020 assert!(!inf.is_nan());
1021 assert!(!inf.is_normal());
1022 assert_eq!(Fp::Infinite, inf.classify());
1026 fn test_neg_infinity() {
1027 let neg_inf: f64 = NEG_INFINITY;
1028 assert!(neg_inf.is_infinite());
1029 assert!(!neg_inf.is_finite());
1030 assert!(!neg_inf.is_sign_positive());
1031 assert!(neg_inf.is_sign_negative());
1032 assert!(!neg_inf.is_nan());
1033 assert!(!neg_inf.is_normal());
1034 assert_eq!(Fp::Infinite, neg_inf.classify());
1039 let zero: f64 = 0.0f64;
1040 assert_eq!(0.0, zero);
1041 assert!(!zero.is_infinite());
1042 assert!(zero.is_finite());
1043 assert!(zero.is_sign_positive());
1044 assert!(!zero.is_sign_negative());
1045 assert!(!zero.is_nan());
1046 assert!(!zero.is_normal());
1047 assert_eq!(Fp::Zero, zero.classify());
1051 fn test_neg_zero() {
1052 let neg_zero: f64 = -0.0;
1053 assert_eq!(0.0, neg_zero);
1054 assert!(!neg_zero.is_infinite());
1055 assert!(neg_zero.is_finite());
1056 assert!(!neg_zero.is_sign_positive());
1057 assert!(neg_zero.is_sign_negative());
1058 assert!(!neg_zero.is_nan());
1059 assert!(!neg_zero.is_normal());
1060 assert_eq!(Fp::Zero, neg_zero.classify());
1063 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1066 let one: f64 = 1.0f64;
1067 assert_eq!(1.0, one);
1068 assert!(!one.is_infinite());
1069 assert!(one.is_finite());
1070 assert!(one.is_sign_positive());
1071 assert!(!one.is_sign_negative());
1072 assert!(!one.is_nan());
1073 assert!(one.is_normal());
1074 assert_eq!(Fp::Normal, one.classify());
1080 let inf: f64 = INFINITY;
1081 let neg_inf: f64 = NEG_INFINITY;
1082 assert!(nan.is_nan());
1083 assert!(!0.0f64.is_nan());
1084 assert!(!5.3f64.is_nan());
1085 assert!(!(-10.732f64).is_nan());
1086 assert!(!inf.is_nan());
1087 assert!(!neg_inf.is_nan());
1091 fn test_is_infinite() {
1093 let inf: f64 = INFINITY;
1094 let neg_inf: f64 = NEG_INFINITY;
1095 assert!(!nan.is_infinite());
1096 assert!(inf.is_infinite());
1097 assert!(neg_inf.is_infinite());
1098 assert!(!0.0f64.is_infinite());
1099 assert!(!42.8f64.is_infinite());
1100 assert!(!(-109.2f64).is_infinite());
1104 fn test_is_finite() {
1106 let inf: f64 = INFINITY;
1107 let neg_inf: f64 = NEG_INFINITY;
1108 assert!(!nan.is_finite());
1109 assert!(!inf.is_finite());
1110 assert!(!neg_inf.is_finite());
1111 assert!(0.0f64.is_finite());
1112 assert!(42.8f64.is_finite());
1113 assert!((-109.2f64).is_finite());
1116 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1118 fn test_is_normal() {
1120 let inf: f64 = INFINITY;
1121 let neg_inf: f64 = NEG_INFINITY;
1122 let zero: f64 = 0.0f64;
1123 let neg_zero: f64 = -0.0;
1124 assert!(!nan.is_normal());
1125 assert!(!inf.is_normal());
1126 assert!(!neg_inf.is_normal());
1127 assert!(!zero.is_normal());
1128 assert!(!neg_zero.is_normal());
1129 assert!(1f64.is_normal());
1130 assert!(1e-307f64.is_normal());
1131 assert!(!1e-308f64.is_normal());
1134 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1136 fn test_classify() {
1138 let inf: f64 = INFINITY;
1139 let neg_inf: f64 = NEG_INFINITY;
1140 let zero: f64 = 0.0f64;
1141 let neg_zero: f64 = -0.0;
1142 assert_eq!(nan.classify(), Fp::Nan);
1143 assert_eq!(inf.classify(), Fp::Infinite);
1144 assert_eq!(neg_inf.classify(), Fp::Infinite);
1145 assert_eq!(zero.classify(), Fp::Zero);
1146 assert_eq!(neg_zero.classify(), Fp::Zero);
1147 assert_eq!(1e-307f64.classify(), Fp::Normal);
1148 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1153 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1154 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1155 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1156 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1157 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1158 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1159 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1160 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1161 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1162 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1167 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1168 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1169 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1170 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1171 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1172 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1173 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1174 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1175 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1176 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1181 assert_approx_eq!(1.0f64.round(), 1.0f64);
1182 assert_approx_eq!(1.3f64.round(), 1.0f64);
1183 assert_approx_eq!(1.5f64.round(), 2.0f64);
1184 assert_approx_eq!(1.7f64.round(), 2.0f64);
1185 assert_approx_eq!(0.0f64.round(), 0.0f64);
1186 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1187 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1188 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1189 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1190 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1195 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1196 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1197 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1198 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1199 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1200 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1201 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1202 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1203 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1204 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1209 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1210 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1211 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1212 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1213 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1214 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1215 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1216 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1217 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1218 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1223 assert_eq!(INFINITY.abs(), INFINITY);
1224 assert_eq!(1f64.abs(), 1f64);
1225 assert_eq!(0f64.abs(), 0f64);
1226 assert_eq!((-0f64).abs(), 0f64);
1227 assert_eq!((-1f64).abs(), 1f64);
1228 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1229 assert_eq!((1f64 / NEG_INFINITY).abs(), 0f64);
1230 assert!(NAN.abs().is_nan());
1235 assert_eq!(INFINITY.signum(), 1f64);
1236 assert_eq!(1f64.signum(), 1f64);
1237 assert_eq!(0f64.signum(), 1f64);
1238 assert_eq!((-0f64).signum(), -1f64);
1239 assert_eq!((-1f64).signum(), -1f64);
1240 assert_eq!(NEG_INFINITY.signum(), -1f64);
1241 assert_eq!((1f64 / NEG_INFINITY).signum(), -1f64);
1242 assert!(NAN.signum().is_nan());
1246 fn test_is_sign_positive() {
1247 assert!(INFINITY.is_sign_positive());
1248 assert!(1f64.is_sign_positive());
1249 assert!(0f64.is_sign_positive());
1250 assert!(!(-0f64).is_sign_positive());
1251 assert!(!(-1f64).is_sign_positive());
1252 assert!(!NEG_INFINITY.is_sign_positive());
1253 assert!(!(1f64 / NEG_INFINITY).is_sign_positive());
1254 assert!(NAN.is_sign_positive());
1255 assert!(!(-NAN).is_sign_positive());
1259 fn test_is_sign_negative() {
1260 assert!(!INFINITY.is_sign_negative());
1261 assert!(!1f64.is_sign_negative());
1262 assert!(!0f64.is_sign_negative());
1263 assert!((-0f64).is_sign_negative());
1264 assert!((-1f64).is_sign_negative());
1265 assert!(NEG_INFINITY.is_sign_negative());
1266 assert!((1f64 / NEG_INFINITY).is_sign_negative());
1267 assert!(!NAN.is_sign_negative());
1268 assert!((-NAN).is_sign_negative());
1274 let inf: f64 = INFINITY;
1275 let neg_inf: f64 = NEG_INFINITY;
1276 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1277 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1278 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1279 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1280 assert!(nan.mul_add(7.8, 9.0).is_nan());
1281 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1282 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1283 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1284 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1290 let inf: f64 = INFINITY;
1291 let neg_inf: f64 = NEG_INFINITY;
1292 assert_eq!(1.0f64.recip(), 1.0);
1293 assert_eq!(2.0f64.recip(), 0.5);
1294 assert_eq!((-0.4f64).recip(), -2.5);
1295 assert_eq!(0.0f64.recip(), inf);
1296 assert!(nan.recip().is_nan());
1297 assert_eq!(inf.recip(), 0.0);
1298 assert_eq!(neg_inf.recip(), 0.0);
1304 let inf: f64 = INFINITY;
1305 let neg_inf: f64 = NEG_INFINITY;
1306 assert_eq!(1.0f64.powi(1), 1.0);
1307 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1308 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1309 assert_eq!(8.3f64.powi(0), 1.0);
1310 assert!(nan.powi(2).is_nan());
1311 assert_eq!(inf.powi(3), inf);
1312 assert_eq!(neg_inf.powi(2), inf);
1318 let inf: f64 = INFINITY;
1319 let neg_inf: f64 = NEG_INFINITY;
1320 assert_eq!(1.0f64.powf(1.0), 1.0);
1321 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1322 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1323 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1324 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1325 assert_eq!(8.3f64.powf(0.0), 1.0);
1326 assert!(nan.powf(2.0).is_nan());
1327 assert_eq!(inf.powf(2.0), inf);
1328 assert_eq!(neg_inf.powf(3.0), neg_inf);
1332 fn test_sqrt_domain() {
1333 assert!(NAN.sqrt().is_nan());
1334 assert!(NEG_INFINITY.sqrt().is_nan());
1335 assert!((-1.0f64).sqrt().is_nan());
1336 assert_eq!((-0.0f64).sqrt(), -0.0);
1337 assert_eq!(0.0f64.sqrt(), 0.0);
1338 assert_eq!(1.0f64.sqrt(), 1.0);
1339 assert_eq!(INFINITY.sqrt(), INFINITY);
1344 assert_eq!(1.0, 0.0f64.exp());
1345 assert_approx_eq!(2.718282, 1.0f64.exp());
1346 assert_approx_eq!(148.413159, 5.0f64.exp());
1348 let inf: f64 = INFINITY;
1349 let neg_inf: f64 = NEG_INFINITY;
1351 assert_eq!(inf, inf.exp());
1352 assert_eq!(0.0, neg_inf.exp());
1353 assert!(nan.exp().is_nan());
1358 assert_eq!(32.0, 5.0f64.exp2());
1359 assert_eq!(1.0, 0.0f64.exp2());
1361 let inf: f64 = INFINITY;
1362 let neg_inf: f64 = NEG_INFINITY;
1364 assert_eq!(inf, inf.exp2());
1365 assert_eq!(0.0, neg_inf.exp2());
1366 assert!(nan.exp2().is_nan());
1372 let inf: f64 = INFINITY;
1373 let neg_inf: f64 = NEG_INFINITY;
1374 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1375 assert!(nan.ln().is_nan());
1376 assert_eq!(inf.ln(), inf);
1377 assert!(neg_inf.ln().is_nan());
1378 assert!((-2.3f64).ln().is_nan());
1379 assert_eq!((-0.0f64).ln(), neg_inf);
1380 assert_eq!(0.0f64.ln(), neg_inf);
1381 assert_approx_eq!(4.0f64.ln(), 1.386294);
1387 let inf: f64 = INFINITY;
1388 let neg_inf: f64 = NEG_INFINITY;
1389 assert_eq!(10.0f64.log(10.0), 1.0);
1390 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1391 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1392 assert!(1.0f64.log(1.0).is_nan());
1393 assert!(1.0f64.log(-13.9).is_nan());
1394 assert!(nan.log(2.3).is_nan());
1395 assert_eq!(inf.log(10.0), inf);
1396 assert!(neg_inf.log(8.8).is_nan());
1397 assert!((-2.3f64).log(0.1).is_nan());
1398 assert_eq!((-0.0f64).log(2.0), neg_inf);
1399 assert_eq!(0.0f64.log(7.0), neg_inf);
1405 let inf: f64 = INFINITY;
1406 let neg_inf: f64 = NEG_INFINITY;
1407 assert_approx_eq!(10.0f64.log2(), 3.321928);
1408 assert_approx_eq!(2.3f64.log2(), 1.201634);
1409 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1410 assert!(nan.log2().is_nan());
1411 assert_eq!(inf.log2(), inf);
1412 assert!(neg_inf.log2().is_nan());
1413 assert!((-2.3f64).log2().is_nan());
1414 assert_eq!((-0.0f64).log2(), neg_inf);
1415 assert_eq!(0.0f64.log2(), neg_inf);
1421 let inf: f64 = INFINITY;
1422 let neg_inf: f64 = NEG_INFINITY;
1423 assert_eq!(10.0f64.log10(), 1.0);
1424 assert_approx_eq!(2.3f64.log10(), 0.361728);
1425 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1426 assert_eq!(1.0f64.log10(), 0.0);
1427 assert!(nan.log10().is_nan());
1428 assert_eq!(inf.log10(), inf);
1429 assert!(neg_inf.log10().is_nan());
1430 assert!((-2.3f64).log10().is_nan());
1431 assert_eq!((-0.0f64).log10(), neg_inf);
1432 assert_eq!(0.0f64.log10(), neg_inf);
1436 fn test_to_degrees() {
1437 let pi: f64 = consts::PI;
1439 let inf: f64 = INFINITY;
1440 let neg_inf: f64 = NEG_INFINITY;
1441 assert_eq!(0.0f64.to_degrees(), 0.0);
1442 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1443 assert_eq!(pi.to_degrees(), 180.0);
1444 assert!(nan.to_degrees().is_nan());
1445 assert_eq!(inf.to_degrees(), inf);
1446 assert_eq!(neg_inf.to_degrees(), neg_inf);
1450 fn test_to_radians() {
1451 let pi: f64 = consts::PI;
1453 let inf: f64 = INFINITY;
1454 let neg_inf: f64 = NEG_INFINITY;
1455 assert_eq!(0.0f64.to_radians(), 0.0);
1456 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1457 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1458 assert_eq!(180.0f64.to_radians(), pi);
1459 assert!(nan.to_radians().is_nan());
1460 assert_eq!(inf.to_radians(), inf);
1461 assert_eq!(neg_inf.to_radians(), neg_inf);
1466 assert_eq!(0.0f64.asinh(), 0.0f64);
1467 assert_eq!((-0.0f64).asinh(), -0.0f64);
1469 let inf: f64 = INFINITY;
1470 let neg_inf: f64 = NEG_INFINITY;
1472 assert_eq!(inf.asinh(), inf);
1473 assert_eq!(neg_inf.asinh(), neg_inf);
1474 assert!(nan.asinh().is_nan());
1475 assert!((-0.0f64).asinh().is_sign_negative());
1477 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1478 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1483 assert_eq!(1.0f64.acosh(), 0.0f64);
1484 assert!(0.999f64.acosh().is_nan());
1486 let inf: f64 = INFINITY;
1487 let neg_inf: f64 = NEG_INFINITY;
1489 assert_eq!(inf.acosh(), inf);
1490 assert!(neg_inf.acosh().is_nan());
1491 assert!(nan.acosh().is_nan());
1492 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1493 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1498 assert_eq!(0.0f64.atanh(), 0.0f64);
1499 assert_eq!((-0.0f64).atanh(), -0.0f64);
1501 let inf: f64 = INFINITY;
1502 let neg_inf: f64 = NEG_INFINITY;
1504 assert_eq!(1.0f64.atanh(), inf);
1505 assert_eq!((-1.0f64).atanh(), neg_inf);
1506 assert!(2f64.atanh().atanh().is_nan());
1507 assert!((-2f64).atanh().atanh().is_nan());
1508 assert!(inf.atanh().is_nan());
1509 assert!(neg_inf.atanh().is_nan());
1510 assert!(nan.atanh().is_nan());
1511 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1512 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1516 fn test_real_consts() {
1518 let pi: f64 = consts::PI;
1519 let frac_pi_2: f64 = consts::FRAC_PI_2;
1520 let frac_pi_3: f64 = consts::FRAC_PI_3;
1521 let frac_pi_4: f64 = consts::FRAC_PI_4;
1522 let frac_pi_6: f64 = consts::FRAC_PI_6;
1523 let frac_pi_8: f64 = consts::FRAC_PI_8;
1524 let frac_1_pi: f64 = consts::FRAC_1_PI;
1525 let frac_2_pi: f64 = consts::FRAC_2_PI;
1526 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1527 let sqrt2: f64 = consts::SQRT_2;
1528 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1529 let e: f64 = consts::E;
1530 let log2_e: f64 = consts::LOG2_E;
1531 let log10_e: f64 = consts::LOG10_E;
1532 let ln_2: f64 = consts::LN_2;
1533 let ln_10: f64 = consts::LN_10;
1535 assert_approx_eq!(frac_pi_2, pi / 2f64);
1536 assert_approx_eq!(frac_pi_3, pi / 3f64);
1537 assert_approx_eq!(frac_pi_4, pi / 4f64);
1538 assert_approx_eq!(frac_pi_6, pi / 6f64);
1539 assert_approx_eq!(frac_pi_8, pi / 8f64);
1540 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1541 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1542 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1543 assert_approx_eq!(sqrt2, 2f64.sqrt());
1544 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1545 assert_approx_eq!(log2_e, e.log2());
1546 assert_approx_eq!(log10_e, e.log10());
1547 assert_approx_eq!(ln_2, 2f64.ln());
1548 assert_approx_eq!(ln_10, 10f64.ln());
1552 fn test_float_bits_conv() {
1553 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1554 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1555 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1556 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1557 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1558 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1559 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1560 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1562 // Check that NaNs roundtrip their bits regardless of signalingness
1563 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1564 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1565 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1566 assert!(f64::from_bits(masked_nan1).is_nan());
1567 assert!(f64::from_bits(masked_nan2).is_nan());
1569 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1570 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
1575 fn test_clamp_min_greater_than_max() {
1576 let _ = 1.0f64.clamp(3.0, 1.0);
1581 fn test_clamp_min_is_nan() {
1582 let _ = 1.0f64.clamp(NAN, 1.0);
1587 fn test_clamp_max_is_nan() {
1588 let _ = 1.0f64.clamp(3.0, NAN);