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 #[stable(feature = "rust1", since = "1.0.0")]
45 pub fn floor(self) -> f64 {
46 unsafe { intrinsics::floorf64(self) }
49 /// Returns the smallest integer greater than or equal to a number.
57 /// assert_eq!(f.ceil(), 4.0);
58 /// assert_eq!(g.ceil(), 4.0);
60 #[stable(feature = "rust1", since = "1.0.0")]
62 pub fn ceil(self) -> f64 {
63 unsafe { intrinsics::ceilf64(self) }
66 /// Returns the nearest integer to a number. Round half-way cases away from
75 /// assert_eq!(f.round(), 3.0);
76 /// assert_eq!(g.round(), -3.0);
78 #[stable(feature = "rust1", since = "1.0.0")]
80 pub fn round(self) -> f64 {
81 unsafe { intrinsics::roundf64(self) }
84 /// Returns the integer part of a number.
93 /// assert_eq!(f.trunc(), 3.0);
94 /// assert_eq!(g.trunc(), 3.0);
95 /// assert_eq!(h.trunc(), -3.0);
97 #[stable(feature = "rust1", since = "1.0.0")]
99 pub fn trunc(self) -> f64 {
100 unsafe { intrinsics::truncf64(self) }
103 /// Returns the fractional part of a number.
109 /// let y = -3.5_f64;
110 /// let abs_difference_x = (x.fract() - 0.5).abs();
111 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
113 /// assert!(abs_difference_x < 1e-10);
114 /// assert!(abs_difference_y < 1e-10);
116 #[stable(feature = "rust1", since = "1.0.0")]
118 pub fn fract(self) -> f64 { self - self.trunc() }
120 /// Computes the absolute value of `self`. Returns `NAN` if the
129 /// let y = -3.5_f64;
131 /// let abs_difference_x = (x.abs() - x).abs();
132 /// let abs_difference_y = (y.abs() - (-y)).abs();
134 /// assert!(abs_difference_x < 1e-10);
135 /// assert!(abs_difference_y < 1e-10);
137 /// assert!(f64::NAN.abs().is_nan());
139 #[stable(feature = "rust1", since = "1.0.0")]
141 pub fn abs(self) -> f64 {
142 unsafe { intrinsics::fabsf64(self) }
145 /// Returns a number that represents the sign of `self`.
147 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
148 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
149 /// - `NAN` if the number is `NAN`
158 /// assert_eq!(f.signum(), 1.0);
159 /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
161 /// assert!(f64::NAN.signum().is_nan());
163 #[stable(feature = "rust1", since = "1.0.0")]
165 pub fn signum(self) -> f64 {
169 unsafe { intrinsics::copysignf64(1.0, self) }
173 /// Returns a number composed of the magnitude of `self` and the sign of
176 /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
177 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
183 /// #![feature(copysign)]
188 /// assert_eq!(f.copysign(0.42), 3.5_f64);
189 /// assert_eq!(f.copysign(-0.42), -3.5_f64);
190 /// assert_eq!((-f).copysign(0.42), 3.5_f64);
191 /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
193 /// assert!(f64::NAN.copysign(1.0).is_nan());
197 #[unstable(feature="copysign", issue="55169")]
198 pub fn copysign(self, y: f64) -> f64 {
199 unsafe { intrinsics::copysignf64(self, y) }
202 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
203 /// error, yielding a more accurate result than an unfused multiply-add.
205 /// Using `mul_add` can be more performant than an unfused multiply-add if
206 /// the target architecture has a dedicated `fma` CPU instruction.
211 /// let m = 10.0_f64;
213 /// let b = 60.0_f64;
216 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
218 /// assert!(abs_difference < 1e-10);
220 #[stable(feature = "rust1", since = "1.0.0")]
222 pub fn mul_add(self, a: f64, b: f64) -> f64 {
223 unsafe { intrinsics::fmaf64(self, a, b) }
226 /// Calculates Euclidean division, the matching method for `rem_euclid`.
228 /// This computes the integer `n` such that
229 /// `self = n * rhs + self.rem_euclid(rhs)`.
230 /// In other words, the result is `self / rhs` rounded to the integer `n`
231 /// such that `self >= n * rhs`.
236 /// #![feature(euclidean_division)]
237 /// let a: f64 = 7.0;
239 /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
240 /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
241 /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
242 /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
245 #[unstable(feature = "euclidean_division", issue = "49048")]
246 pub fn div_euclid(self, rhs: f64) -> f64 {
247 let q = (self / rhs).trunc();
248 if self % rhs < 0.0 {
249 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
254 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
256 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
257 /// most cases. However, due to a floating point round-off error it can
258 /// result in `r == rhs.abs()`, violating the mathematical definition, if
259 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
260 /// This result is not an element of the function's codomain, but it is the
261 /// closest floating point number in the real numbers and thus fulfills the
262 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
268 /// #![feature(euclidean_division)]
269 /// let a: f64 = 7.0;
271 /// assert_eq!(a.rem_euclid(b), 3.0);
272 /// assert_eq!((-a).rem_euclid(b), 1.0);
273 /// assert_eq!(a.rem_euclid(-b), 3.0);
274 /// assert_eq!((-a).rem_euclid(-b), 1.0);
275 /// // limitation due to round-off error
276 /// assert!((-std::f64::EPSILON).rem_euclid(3.0) != 0.0);
279 #[unstable(feature = "euclidean_division", issue = "49048")]
280 pub fn rem_euclid(self, rhs: f64) -> f64 {
289 /// Raises a number to an integer power.
291 /// Using this function is generally faster than using `powf`
297 /// let abs_difference = (x.powi(2) - x*x).abs();
299 /// assert!(abs_difference < 1e-10);
301 #[stable(feature = "rust1", since = "1.0.0")]
303 pub fn powi(self, n: i32) -> f64 {
304 unsafe { intrinsics::powif64(self, n) }
307 /// Raises a number to a floating point power.
313 /// let abs_difference = (x.powf(2.0) - x*x).abs();
315 /// assert!(abs_difference < 1e-10);
317 #[stable(feature = "rust1", since = "1.0.0")]
319 pub fn powf(self, n: f64) -> f64 {
320 unsafe { intrinsics::powf64(self, n) }
323 /// Takes the square root of a number.
325 /// Returns NaN if `self` is a negative number.
330 /// let positive = 4.0_f64;
331 /// let negative = -4.0_f64;
333 /// let abs_difference = (positive.sqrt() - 2.0).abs();
335 /// assert!(abs_difference < 1e-10);
336 /// assert!(negative.sqrt().is_nan());
338 #[stable(feature = "rust1", since = "1.0.0")]
340 pub fn sqrt(self) -> f64 {
344 unsafe { intrinsics::sqrtf64(self) }
348 /// Returns `e^(self)`, (the exponential function).
353 /// let one = 1.0_f64;
355 /// let e = one.exp();
357 /// // ln(e) - 1 == 0
358 /// let abs_difference = (e.ln() - 1.0).abs();
360 /// assert!(abs_difference < 1e-10);
362 #[stable(feature = "rust1", since = "1.0.0")]
364 pub fn exp(self) -> f64 {
365 unsafe { intrinsics::expf64(self) }
368 /// Returns `2^(self)`.
376 /// let abs_difference = (f.exp2() - 4.0).abs();
378 /// assert!(abs_difference < 1e-10);
380 #[stable(feature = "rust1", since = "1.0.0")]
382 pub fn exp2(self) -> f64 {
383 unsafe { intrinsics::exp2f64(self) }
386 /// Returns the natural logarithm of the number.
391 /// let one = 1.0_f64;
393 /// let e = one.exp();
395 /// // ln(e) - 1 == 0
396 /// let abs_difference = (e.ln() - 1.0).abs();
398 /// assert!(abs_difference < 1e-10);
400 #[stable(feature = "rust1", since = "1.0.0")]
402 pub fn ln(self) -> f64 {
403 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
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.0_f64;
417 /// // log5(5) - 1 == 0
418 /// let abs_difference = (five.log(5.0) - 1.0).abs();
420 /// assert!(abs_difference < 1e-10);
422 #[stable(feature = "rust1", since = "1.0.0")]
424 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
426 /// Returns the base 2 logarithm of the number.
431 /// let two = 2.0_f64;
433 /// // log2(2) - 1 == 0
434 /// let abs_difference = (two.log2() - 1.0).abs();
436 /// assert!(abs_difference < 1e-10);
438 #[stable(feature = "rust1", since = "1.0.0")]
440 pub fn log2(self) -> f64 {
441 self.log_wrapper(|n| {
442 #[cfg(target_os = "android")]
443 return crate::sys::android::log2f64(n);
444 #[cfg(not(target_os = "android"))]
445 return unsafe { intrinsics::log2f64(n) };
449 /// Returns the base 10 logarithm of the number.
454 /// let ten = 10.0_f64;
456 /// // log10(10) - 1 == 0
457 /// let abs_difference = (ten.log10() - 1.0).abs();
459 /// assert!(abs_difference < 1e-10);
461 #[stable(feature = "rust1", since = "1.0.0")]
463 pub fn log10(self) -> f64 {
464 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
467 /// The positive difference of two numbers.
469 /// * If `self <= other`: `0:0`
470 /// * Else: `self - other`
476 /// let y = -3.0_f64;
478 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
479 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
481 /// assert!(abs_difference_x < 1e-10);
482 /// assert!(abs_difference_y < 1e-10);
484 #[stable(feature = "rust1", since = "1.0.0")]
486 #[rustc_deprecated(since = "1.10.0",
487 reason = "you probably meant `(self - other).abs()`: \
488 this operation is `(self - other).max(0.0)` \
489 except that `abs_sub` also propagates NaNs (also \
490 known as `fdim` in C). If you truly need the positive \
491 difference, consider using that expression or the C function \
492 `fdim`, depending on how you wish to handle NaN (please consider \
493 filing an issue describing your use-case too).")]
494 pub fn abs_sub(self, other: f64) -> f64 {
495 unsafe { cmath::fdim(self, other) }
498 /// Takes the cubic root of a number.
505 /// // x^(1/3) - 2 == 0
506 /// let abs_difference = (x.cbrt() - 2.0).abs();
508 /// assert!(abs_difference < 1e-10);
510 #[stable(feature = "rust1", since = "1.0.0")]
512 pub fn cbrt(self) -> f64 {
513 unsafe { cmath::cbrt(self) }
516 /// Calculates the length of the hypotenuse of a right-angle triangle given
517 /// legs of length `x` and `y`.
525 /// // sqrt(x^2 + y^2)
526 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
528 /// assert!(abs_difference < 1e-10);
530 #[stable(feature = "rust1", since = "1.0.0")]
532 pub fn hypot(self, other: f64) -> f64 {
533 unsafe { cmath::hypot(self, other) }
536 /// Computes the sine of a number (in radians).
543 /// let x = f64::consts::PI/2.0;
545 /// let abs_difference = (x.sin() - 1.0).abs();
547 /// assert!(abs_difference < 1e-10);
549 #[stable(feature = "rust1", since = "1.0.0")]
551 pub fn sin(self) -> f64 {
552 unsafe { intrinsics::sinf64(self) }
555 /// Computes the cosine of a number (in radians).
562 /// let x = 2.0*f64::consts::PI;
564 /// let abs_difference = (x.cos() - 1.0).abs();
566 /// assert!(abs_difference < 1e-10);
568 #[stable(feature = "rust1", since = "1.0.0")]
570 pub fn cos(self) -> f64 {
571 unsafe { intrinsics::cosf64(self) }
574 /// Computes the tangent of a number (in radians).
581 /// let x = f64::consts::PI/4.0;
582 /// let abs_difference = (x.tan() - 1.0).abs();
584 /// assert!(abs_difference < 1e-14);
586 #[stable(feature = "rust1", since = "1.0.0")]
588 pub fn tan(self) -> f64 {
589 unsafe { cmath::tan(self) }
592 /// Computes the arcsine of a number. Return value is in radians in
593 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
601 /// let f = f64::consts::PI / 2.0;
603 /// // asin(sin(pi/2))
604 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
606 /// assert!(abs_difference < 1e-10);
608 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn asin(self) -> f64 {
611 unsafe { cmath::asin(self) }
614 /// Computes the arccosine of a number. Return value is in radians in
615 /// the range [0, pi] or NaN if the number is outside the range
623 /// let f = f64::consts::PI / 4.0;
625 /// // acos(cos(pi/4))
626 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
628 /// assert!(abs_difference < 1e-10);
630 #[stable(feature = "rust1", since = "1.0.0")]
632 pub fn acos(self) -> f64 {
633 unsafe { cmath::acos(self) }
636 /// Computes the arctangent of a number. Return value is in radians in the
637 /// range [-pi/2, pi/2];
645 /// let abs_difference = (f.tan().atan() - 1.0).abs();
647 /// assert!(abs_difference < 1e-10);
649 #[stable(feature = "rust1", since = "1.0.0")]
651 pub fn atan(self) -> f64 {
652 unsafe { cmath::atan(self) }
655 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
657 /// * `x = 0`, `y = 0`: `0`
658 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
659 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
660 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
667 /// let pi = f64::consts::PI;
668 /// // Positive angles measured counter-clockwise
669 /// // from positive x axis
670 /// // -pi/4 radians (45 deg clockwise)
671 /// let x1 = 3.0_f64;
672 /// let y1 = -3.0_f64;
674 /// // 3pi/4 radians (135 deg counter-clockwise)
675 /// let x2 = -3.0_f64;
676 /// let y2 = 3.0_f64;
678 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
679 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
681 /// assert!(abs_difference_1 < 1e-10);
682 /// assert!(abs_difference_2 < 1e-10);
684 #[stable(feature = "rust1", since = "1.0.0")]
686 pub fn atan2(self, other: f64) -> f64 {
687 unsafe { cmath::atan2(self, other) }
690 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
691 /// `(sin(x), cos(x))`.
698 /// let x = f64::consts::PI/4.0;
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 < 1e-10);
705 /// assert!(abs_difference_1 < 1e-10);
707 #[stable(feature = "rust1", since = "1.0.0")]
709 pub fn sin_cos(self) -> (f64, f64) {
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() - 6.0).abs();
724 /// assert!(abs_difference < 1e-10);
726 #[stable(feature = "rust1", since = "1.0.0")]
728 pub fn exp_m1(self) -> f64 {
729 unsafe { cmath::expm1(self) }
732 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
733 /// the operations were performed separately.
740 /// let x = f64::consts::E - 1.0;
742 /// // ln(1 + (e - 1)) == ln(e) == 1
743 /// let abs_difference = (x.ln_1p() - 1.0).abs();
745 /// assert!(abs_difference < 1e-10);
747 #[stable(feature = "rust1", since = "1.0.0")]
749 pub fn ln_1p(self) -> f64 {
750 unsafe { cmath::log1p(self) }
753 /// Hyperbolic sine function.
760 /// let e = f64::consts::E;
763 /// let f = x.sinh();
764 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
765 /// let g = (e*e - 1.0)/(2.0*e);
766 /// let abs_difference = (f - g).abs();
768 /// assert!(abs_difference < 1e-10);
770 #[stable(feature = "rust1", since = "1.0.0")]
772 pub fn sinh(self) -> f64 {
773 unsafe { cmath::sinh(self) }
776 /// Hyperbolic cosine function.
783 /// let e = f64::consts::E;
785 /// let f = x.cosh();
786 /// // Solving cosh() at 1 gives this result
787 /// let g = (e*e + 1.0)/(2.0*e);
788 /// let abs_difference = (f - g).abs();
791 /// assert!(abs_difference < 1.0e-10);
793 #[stable(feature = "rust1", since = "1.0.0")]
795 pub fn cosh(self) -> f64 {
796 unsafe { cmath::cosh(self) }
799 /// Hyperbolic tangent function.
806 /// let e = f64::consts::E;
809 /// let f = x.tanh();
810 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
811 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
812 /// let abs_difference = (f - g).abs();
814 /// assert!(abs_difference < 1.0e-10);
816 #[stable(feature = "rust1", since = "1.0.0")]
818 pub fn tanh(self) -> f64 {
819 unsafe { cmath::tanh(self) }
822 /// Inverse hyperbolic sine function.
828 /// let f = x.sinh().asinh();
830 /// let abs_difference = (f - x).abs();
832 /// assert!(abs_difference < 1.0e-10);
834 #[stable(feature = "rust1", since = "1.0.0")]
836 pub fn asinh(self) -> f64 {
837 if self == NEG_INFINITY {
840 (self + ((self * self) + 1.0).sqrt()).ln()
844 /// Inverse hyperbolic cosine function.
850 /// let f = x.cosh().acosh();
852 /// let abs_difference = (f - x).abs();
854 /// assert!(abs_difference < 1.0e-10);
856 #[stable(feature = "rust1", since = "1.0.0")]
858 pub fn acosh(self) -> f64 {
861 x => (x + ((x * x) - 1.0).sqrt()).ln(),
865 /// Inverse hyperbolic tangent function.
872 /// let e = f64::consts::E;
873 /// let f = e.tanh().atanh();
875 /// let abs_difference = (f - e).abs();
877 /// assert!(abs_difference < 1.0e-10);
879 #[stable(feature = "rust1", since = "1.0.0")]
881 pub fn atanh(self) -> f64 {
882 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
885 /// Returns max if self is greater than max, and min if self is less than min.
886 /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
891 /// #![feature(clamp)]
892 /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
893 /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
894 /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
895 /// assert!((std::f64::NAN).clamp(-2.0f64, 1.0f64).is_nan());
897 #[unstable(feature = "clamp", issue = "44095")]
899 pub fn clamp(self, min: f64, max: f64) -> f64 {
902 if x < min { x = min; }
903 if x > max { x = max; }
907 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
908 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
910 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
911 if !cfg!(target_os = "solaris") {
914 if self.is_finite() {
917 } else if self == 0.0 {
918 NEG_INFINITY // log(0) = -Inf
922 } else if self.is_nan() {
923 self // log(NaN) = NaN
924 } else if self > 0.0 {
925 self // log(Inf) = Inf
927 NAN // log(-Inf) = NaN
938 use crate::num::FpCategory as Fp;
942 test_num(10f64, 2f64);
947 assert_eq!(NAN.min(2.0), 2.0);
948 assert_eq!(2.0f64.min(NAN), 2.0);
953 assert_eq!(NAN.max(2.0), 2.0);
954 assert_eq!(2.0f64.max(NAN), 2.0);
960 assert!(nan.is_nan());
961 assert!(!nan.is_infinite());
962 assert!(!nan.is_finite());
963 assert!(!nan.is_normal());
964 assert!(nan.is_sign_positive());
965 assert!(!nan.is_sign_negative());
966 assert_eq!(Fp::Nan, nan.classify());
971 let inf: f64 = INFINITY;
972 assert!(inf.is_infinite());
973 assert!(!inf.is_finite());
974 assert!(inf.is_sign_positive());
975 assert!(!inf.is_sign_negative());
976 assert!(!inf.is_nan());
977 assert!(!inf.is_normal());
978 assert_eq!(Fp::Infinite, inf.classify());
982 fn test_neg_infinity() {
983 let neg_inf: f64 = NEG_INFINITY;
984 assert!(neg_inf.is_infinite());
985 assert!(!neg_inf.is_finite());
986 assert!(!neg_inf.is_sign_positive());
987 assert!(neg_inf.is_sign_negative());
988 assert!(!neg_inf.is_nan());
989 assert!(!neg_inf.is_normal());
990 assert_eq!(Fp::Infinite, neg_inf.classify());
995 let zero: f64 = 0.0f64;
996 assert_eq!(0.0, zero);
997 assert!(!zero.is_infinite());
998 assert!(zero.is_finite());
999 assert!(zero.is_sign_positive());
1000 assert!(!zero.is_sign_negative());
1001 assert!(!zero.is_nan());
1002 assert!(!zero.is_normal());
1003 assert_eq!(Fp::Zero, zero.classify());
1007 fn test_neg_zero() {
1008 let neg_zero: f64 = -0.0;
1009 assert_eq!(0.0, neg_zero);
1010 assert!(!neg_zero.is_infinite());
1011 assert!(neg_zero.is_finite());
1012 assert!(!neg_zero.is_sign_positive());
1013 assert!(neg_zero.is_sign_negative());
1014 assert!(!neg_zero.is_nan());
1015 assert!(!neg_zero.is_normal());
1016 assert_eq!(Fp::Zero, neg_zero.classify());
1019 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1022 let one: f64 = 1.0f64;
1023 assert_eq!(1.0, one);
1024 assert!(!one.is_infinite());
1025 assert!(one.is_finite());
1026 assert!(one.is_sign_positive());
1027 assert!(!one.is_sign_negative());
1028 assert!(!one.is_nan());
1029 assert!(one.is_normal());
1030 assert_eq!(Fp::Normal, one.classify());
1036 let inf: f64 = INFINITY;
1037 let neg_inf: f64 = NEG_INFINITY;
1038 assert!(nan.is_nan());
1039 assert!(!0.0f64.is_nan());
1040 assert!(!5.3f64.is_nan());
1041 assert!(!(-10.732f64).is_nan());
1042 assert!(!inf.is_nan());
1043 assert!(!neg_inf.is_nan());
1047 fn test_is_infinite() {
1049 let inf: f64 = INFINITY;
1050 let neg_inf: f64 = NEG_INFINITY;
1051 assert!(!nan.is_infinite());
1052 assert!(inf.is_infinite());
1053 assert!(neg_inf.is_infinite());
1054 assert!(!0.0f64.is_infinite());
1055 assert!(!42.8f64.is_infinite());
1056 assert!(!(-109.2f64).is_infinite());
1060 fn test_is_finite() {
1062 let inf: f64 = INFINITY;
1063 let neg_inf: f64 = NEG_INFINITY;
1064 assert!(!nan.is_finite());
1065 assert!(!inf.is_finite());
1066 assert!(!neg_inf.is_finite());
1067 assert!(0.0f64.is_finite());
1068 assert!(42.8f64.is_finite());
1069 assert!((-109.2f64).is_finite());
1072 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1074 fn test_is_normal() {
1076 let inf: f64 = INFINITY;
1077 let neg_inf: f64 = NEG_INFINITY;
1078 let zero: f64 = 0.0f64;
1079 let neg_zero: f64 = -0.0;
1080 assert!(!nan.is_normal());
1081 assert!(!inf.is_normal());
1082 assert!(!neg_inf.is_normal());
1083 assert!(!zero.is_normal());
1084 assert!(!neg_zero.is_normal());
1085 assert!(1f64.is_normal());
1086 assert!(1e-307f64.is_normal());
1087 assert!(!1e-308f64.is_normal());
1090 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1092 fn test_classify() {
1094 let inf: f64 = INFINITY;
1095 let neg_inf: f64 = NEG_INFINITY;
1096 let zero: f64 = 0.0f64;
1097 let neg_zero: f64 = -0.0;
1098 assert_eq!(nan.classify(), Fp::Nan);
1099 assert_eq!(inf.classify(), Fp::Infinite);
1100 assert_eq!(neg_inf.classify(), Fp::Infinite);
1101 assert_eq!(zero.classify(), Fp::Zero);
1102 assert_eq!(neg_zero.classify(), Fp::Zero);
1103 assert_eq!(1e-307f64.classify(), Fp::Normal);
1104 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1109 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1110 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1111 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1112 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1113 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1114 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1115 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1116 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1117 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1118 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1123 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1124 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1125 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1126 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1127 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1128 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1129 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1130 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1131 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1132 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1137 assert_approx_eq!(1.0f64.round(), 1.0f64);
1138 assert_approx_eq!(1.3f64.round(), 1.0f64);
1139 assert_approx_eq!(1.5f64.round(), 2.0f64);
1140 assert_approx_eq!(1.7f64.round(), 2.0f64);
1141 assert_approx_eq!(0.0f64.round(), 0.0f64);
1142 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1143 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1144 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1145 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1146 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1151 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1152 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1153 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1154 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1155 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1156 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1157 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1158 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1159 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1160 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1165 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1166 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1167 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1168 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1169 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1170 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1171 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1172 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1173 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1174 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1179 assert_eq!(INFINITY.abs(), INFINITY);
1180 assert_eq!(1f64.abs(), 1f64);
1181 assert_eq!(0f64.abs(), 0f64);
1182 assert_eq!((-0f64).abs(), 0f64);
1183 assert_eq!((-1f64).abs(), 1f64);
1184 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1185 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1186 assert!(NAN.abs().is_nan());
1191 assert_eq!(INFINITY.signum(), 1f64);
1192 assert_eq!(1f64.signum(), 1f64);
1193 assert_eq!(0f64.signum(), 1f64);
1194 assert_eq!((-0f64).signum(), -1f64);
1195 assert_eq!((-1f64).signum(), -1f64);
1196 assert_eq!(NEG_INFINITY.signum(), -1f64);
1197 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1198 assert!(NAN.signum().is_nan());
1202 fn test_is_sign_positive() {
1203 assert!(INFINITY.is_sign_positive());
1204 assert!(1f64.is_sign_positive());
1205 assert!(0f64.is_sign_positive());
1206 assert!(!(-0f64).is_sign_positive());
1207 assert!(!(-1f64).is_sign_positive());
1208 assert!(!NEG_INFINITY.is_sign_positive());
1209 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1210 assert!(NAN.is_sign_positive());
1211 assert!(!(-NAN).is_sign_positive());
1215 fn test_is_sign_negative() {
1216 assert!(!INFINITY.is_sign_negative());
1217 assert!(!1f64.is_sign_negative());
1218 assert!(!0f64.is_sign_negative());
1219 assert!((-0f64).is_sign_negative());
1220 assert!((-1f64).is_sign_negative());
1221 assert!(NEG_INFINITY.is_sign_negative());
1222 assert!((1f64/NEG_INFINITY).is_sign_negative());
1223 assert!(!NAN.is_sign_negative());
1224 assert!((-NAN).is_sign_negative());
1230 let inf: f64 = INFINITY;
1231 let neg_inf: f64 = NEG_INFINITY;
1232 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1233 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1234 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1235 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1236 assert!(nan.mul_add(7.8, 9.0).is_nan());
1237 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1238 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1239 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1240 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1246 let inf: f64 = INFINITY;
1247 let neg_inf: f64 = NEG_INFINITY;
1248 assert_eq!(1.0f64.recip(), 1.0);
1249 assert_eq!(2.0f64.recip(), 0.5);
1250 assert_eq!((-0.4f64).recip(), -2.5);
1251 assert_eq!(0.0f64.recip(), inf);
1252 assert!(nan.recip().is_nan());
1253 assert_eq!(inf.recip(), 0.0);
1254 assert_eq!(neg_inf.recip(), 0.0);
1260 let inf: f64 = INFINITY;
1261 let neg_inf: f64 = NEG_INFINITY;
1262 assert_eq!(1.0f64.powi(1), 1.0);
1263 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1264 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1265 assert_eq!(8.3f64.powi(0), 1.0);
1266 assert!(nan.powi(2).is_nan());
1267 assert_eq!(inf.powi(3), inf);
1268 assert_eq!(neg_inf.powi(2), inf);
1274 let inf: f64 = INFINITY;
1275 let neg_inf: f64 = NEG_INFINITY;
1276 assert_eq!(1.0f64.powf(1.0), 1.0);
1277 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1278 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1279 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1280 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1281 assert_eq!(8.3f64.powf(0.0), 1.0);
1282 assert!(nan.powf(2.0).is_nan());
1283 assert_eq!(inf.powf(2.0), inf);
1284 assert_eq!(neg_inf.powf(3.0), neg_inf);
1288 fn test_sqrt_domain() {
1289 assert!(NAN.sqrt().is_nan());
1290 assert!(NEG_INFINITY.sqrt().is_nan());
1291 assert!((-1.0f64).sqrt().is_nan());
1292 assert_eq!((-0.0f64).sqrt(), -0.0);
1293 assert_eq!(0.0f64.sqrt(), 0.0);
1294 assert_eq!(1.0f64.sqrt(), 1.0);
1295 assert_eq!(INFINITY.sqrt(), INFINITY);
1300 assert_eq!(1.0, 0.0f64.exp());
1301 assert_approx_eq!(2.718282, 1.0f64.exp());
1302 assert_approx_eq!(148.413159, 5.0f64.exp());
1304 let inf: f64 = INFINITY;
1305 let neg_inf: f64 = NEG_INFINITY;
1307 assert_eq!(inf, inf.exp());
1308 assert_eq!(0.0, neg_inf.exp());
1309 assert!(nan.exp().is_nan());
1314 assert_eq!(32.0, 5.0f64.exp2());
1315 assert_eq!(1.0, 0.0f64.exp2());
1317 let inf: f64 = INFINITY;
1318 let neg_inf: f64 = NEG_INFINITY;
1320 assert_eq!(inf, inf.exp2());
1321 assert_eq!(0.0, neg_inf.exp2());
1322 assert!(nan.exp2().is_nan());
1328 let inf: f64 = INFINITY;
1329 let neg_inf: f64 = NEG_INFINITY;
1330 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1331 assert!(nan.ln().is_nan());
1332 assert_eq!(inf.ln(), inf);
1333 assert!(neg_inf.ln().is_nan());
1334 assert!((-2.3f64).ln().is_nan());
1335 assert_eq!((-0.0f64).ln(), neg_inf);
1336 assert_eq!(0.0f64.ln(), neg_inf);
1337 assert_approx_eq!(4.0f64.ln(), 1.386294);
1343 let inf: f64 = INFINITY;
1344 let neg_inf: f64 = NEG_INFINITY;
1345 assert_eq!(10.0f64.log(10.0), 1.0);
1346 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1347 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1348 assert!(1.0f64.log(1.0).is_nan());
1349 assert!(1.0f64.log(-13.9).is_nan());
1350 assert!(nan.log(2.3).is_nan());
1351 assert_eq!(inf.log(10.0), inf);
1352 assert!(neg_inf.log(8.8).is_nan());
1353 assert!((-2.3f64).log(0.1).is_nan());
1354 assert_eq!((-0.0f64).log(2.0), neg_inf);
1355 assert_eq!(0.0f64.log(7.0), neg_inf);
1361 let inf: f64 = INFINITY;
1362 let neg_inf: f64 = NEG_INFINITY;
1363 assert_approx_eq!(10.0f64.log2(), 3.321928);
1364 assert_approx_eq!(2.3f64.log2(), 1.201634);
1365 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1366 assert!(nan.log2().is_nan());
1367 assert_eq!(inf.log2(), inf);
1368 assert!(neg_inf.log2().is_nan());
1369 assert!((-2.3f64).log2().is_nan());
1370 assert_eq!((-0.0f64).log2(), neg_inf);
1371 assert_eq!(0.0f64.log2(), neg_inf);
1377 let inf: f64 = INFINITY;
1378 let neg_inf: f64 = NEG_INFINITY;
1379 assert_eq!(10.0f64.log10(), 1.0);
1380 assert_approx_eq!(2.3f64.log10(), 0.361728);
1381 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1382 assert_eq!(1.0f64.log10(), 0.0);
1383 assert!(nan.log10().is_nan());
1384 assert_eq!(inf.log10(), inf);
1385 assert!(neg_inf.log10().is_nan());
1386 assert!((-2.3f64).log10().is_nan());
1387 assert_eq!((-0.0f64).log10(), neg_inf);
1388 assert_eq!(0.0f64.log10(), neg_inf);
1392 fn test_to_degrees() {
1393 let pi: f64 = consts::PI;
1395 let inf: f64 = INFINITY;
1396 let neg_inf: f64 = NEG_INFINITY;
1397 assert_eq!(0.0f64.to_degrees(), 0.0);
1398 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1399 assert_eq!(pi.to_degrees(), 180.0);
1400 assert!(nan.to_degrees().is_nan());
1401 assert_eq!(inf.to_degrees(), inf);
1402 assert_eq!(neg_inf.to_degrees(), neg_inf);
1406 fn test_to_radians() {
1407 let pi: f64 = consts::PI;
1409 let inf: f64 = INFINITY;
1410 let neg_inf: f64 = NEG_INFINITY;
1411 assert_eq!(0.0f64.to_radians(), 0.0);
1412 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1413 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1414 assert_eq!(180.0f64.to_radians(), pi);
1415 assert!(nan.to_radians().is_nan());
1416 assert_eq!(inf.to_radians(), inf);
1417 assert_eq!(neg_inf.to_radians(), neg_inf);
1422 assert_eq!(0.0f64.asinh(), 0.0f64);
1423 assert_eq!((-0.0f64).asinh(), -0.0f64);
1425 let inf: f64 = INFINITY;
1426 let neg_inf: f64 = NEG_INFINITY;
1428 assert_eq!(inf.asinh(), inf);
1429 assert_eq!(neg_inf.asinh(), neg_inf);
1430 assert!(nan.asinh().is_nan());
1431 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1432 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1437 assert_eq!(1.0f64.acosh(), 0.0f64);
1438 assert!(0.999f64.acosh().is_nan());
1440 let inf: f64 = INFINITY;
1441 let neg_inf: f64 = NEG_INFINITY;
1443 assert_eq!(inf.acosh(), inf);
1444 assert!(neg_inf.acosh().is_nan());
1445 assert!(nan.acosh().is_nan());
1446 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1447 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1452 assert_eq!(0.0f64.atanh(), 0.0f64);
1453 assert_eq!((-0.0f64).atanh(), -0.0f64);
1455 let inf: f64 = INFINITY;
1456 let neg_inf: f64 = NEG_INFINITY;
1458 assert_eq!(1.0f64.atanh(), inf);
1459 assert_eq!((-1.0f64).atanh(), neg_inf);
1460 assert!(2f64.atanh().atanh().is_nan());
1461 assert!((-2f64).atanh().atanh().is_nan());
1462 assert!(inf.atanh().is_nan());
1463 assert!(neg_inf.atanh().is_nan());
1464 assert!(nan.atanh().is_nan());
1465 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1466 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1470 fn test_real_consts() {
1472 let pi: f64 = consts::PI;
1473 let frac_pi_2: f64 = consts::FRAC_PI_2;
1474 let frac_pi_3: f64 = consts::FRAC_PI_3;
1475 let frac_pi_4: f64 = consts::FRAC_PI_4;
1476 let frac_pi_6: f64 = consts::FRAC_PI_6;
1477 let frac_pi_8: f64 = consts::FRAC_PI_8;
1478 let frac_1_pi: f64 = consts::FRAC_1_PI;
1479 let frac_2_pi: f64 = consts::FRAC_2_PI;
1480 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1481 let sqrt2: f64 = consts::SQRT_2;
1482 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1483 let e: f64 = consts::E;
1484 let log2_e: f64 = consts::LOG2_E;
1485 let log10_e: f64 = consts::LOG10_E;
1486 let ln_2: f64 = consts::LN_2;
1487 let ln_10: f64 = consts::LN_10;
1489 assert_approx_eq!(frac_pi_2, pi / 2f64);
1490 assert_approx_eq!(frac_pi_3, pi / 3f64);
1491 assert_approx_eq!(frac_pi_4, pi / 4f64);
1492 assert_approx_eq!(frac_pi_6, pi / 6f64);
1493 assert_approx_eq!(frac_pi_8, pi / 8f64);
1494 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1495 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1496 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1497 assert_approx_eq!(sqrt2, 2f64.sqrt());
1498 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1499 assert_approx_eq!(log2_e, e.log2());
1500 assert_approx_eq!(log10_e, e.log10());
1501 assert_approx_eq!(ln_2, 2f64.ln());
1502 assert_approx_eq!(ln_10, 10f64.ln());
1506 fn test_float_bits_conv() {
1507 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1508 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1509 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1510 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1511 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1512 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1513 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1514 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1516 // Check that NaNs roundtrip their bits regardless of signalingness
1517 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1518 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1519 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1520 assert!(f64::from_bits(masked_nan1).is_nan());
1521 assert!(f64::from_bits(masked_nan2).is_nan());
1523 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1524 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
1529 fn test_clamp_min_greater_than_max() {
1530 1.0f64.clamp(3.0, 1.0);
1535 fn test_clamp_min_is_nan() {
1536 1.0f64.clamp(NAN, 1.0);
1541 fn test_clamp_max_is_nan() {
1542 1.0f64.clamp(3.0, NAN);