1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module provides constants which are specific to the implementation
12 //! of the `f64` floating point data type.
14 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
16 //! Mathematically significant numbers are provided in the `consts` sub-module.
18 #![stable(feature = "rust1", since = "1.0.0")]
19 #![allow(missing_docs)]
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f64::{MIN, MIN_POSITIVE, MAX};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f64::consts;
38 #[lang = "f64_runtime"]
40 /// Returns the largest integer less than or equal to a number.
48 /// assert_eq!(f.floor(), 3.0);
49 /// assert_eq!(g.floor(), 3.0);
51 #[stable(feature = "rust1", since = "1.0.0")]
53 pub fn floor(self) -> f64 {
54 unsafe { intrinsics::floorf64(self) }
57 /// Returns the smallest integer greater than or equal to a number.
65 /// assert_eq!(f.ceil(), 4.0);
66 /// assert_eq!(g.ceil(), 4.0);
68 #[stable(feature = "rust1", since = "1.0.0")]
70 pub fn ceil(self) -> f64 {
71 unsafe { intrinsics::ceilf64(self) }
74 /// Returns the nearest integer to a number. Round half-way cases away from
83 /// assert_eq!(f.round(), 3.0);
84 /// assert_eq!(g.round(), -3.0);
86 #[stable(feature = "rust1", since = "1.0.0")]
88 pub fn round(self) -> f64 {
89 unsafe { intrinsics::roundf64(self) }
92 /// Returns the integer part of a number.
100 /// assert_eq!(f.trunc(), 3.0);
101 /// assert_eq!(g.trunc(), -3.0);
103 #[stable(feature = "rust1", since = "1.0.0")]
105 pub fn trunc(self) -> f64 {
106 unsafe { intrinsics::truncf64(self) }
109 /// Returns the fractional part of a number.
115 /// let y = -3.5_f64;
116 /// let abs_difference_x = (x.fract() - 0.5).abs();
117 /// let abs_difference_y = (y.fract() - (-0.5)).abs();
119 /// assert!(abs_difference_x < 1e-10);
120 /// assert!(abs_difference_y < 1e-10);
122 #[stable(feature = "rust1", since = "1.0.0")]
124 pub fn fract(self) -> f64 { self - self.trunc() }
126 /// Computes the absolute value of `self`. Returns `NAN` if the
135 /// let y = -3.5_f64;
137 /// let abs_difference_x = (x.abs() - x).abs();
138 /// let abs_difference_y = (y.abs() - (-y)).abs();
140 /// assert!(abs_difference_x < 1e-10);
141 /// assert!(abs_difference_y < 1e-10);
143 /// assert!(f64::NAN.abs().is_nan());
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 #[stable(feature = "rust1", since = "1.0.0")]
171 pub fn signum(self) -> f64 {
175 unsafe { intrinsics::copysignf64(1.0, self) }
179 /// Returns a number composed of the magnitude of `self` and the sign of
182 /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
183 /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
189 /// #![feature(copysign)]
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());
203 #[unstable(feature="copysign", issue="55169")]
204 pub fn copysign(self, y: f64) -> f64 {
205 unsafe { intrinsics::copysignf64(self, y) }
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 #[stable(feature = "rust1", since = "1.0.0")]
228 pub fn mul_add(self, a: f64, b: f64) -> f64 {
229 unsafe { intrinsics::fmaf64(self, a, b) }
232 /// Calculates Euclidean division, the matching method for `rem_euclid`.
234 /// This computes the integer `n` such that
235 /// `self = n * rhs + self.rem_euclid(rhs)`.
236 /// In other words, the result is `self / rhs` rounded to the integer `n`
237 /// such that `self >= n * rhs`.
242 /// #![feature(euclidean_division)]
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
251 #[unstable(feature = "euclidean_division", issue = "49048")]
252 pub fn div_euclid(self, rhs: f64) -> f64 {
253 let q = (self / rhs).trunc();
254 if self % rhs < 0.0 {
255 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
260 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
262 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
263 /// most cases. However, due to a floating point round-off error it can
264 /// result in `r == rhs.abs()`, violating the mathematical definition, if
265 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
266 /// This result is not an element of the function's codomain, but it is the
267 /// closest floating point number in the real numbers and thus fulfills the
268 /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
274 /// #![feature(euclidean_division)]
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);
285 #[unstable(feature = "euclidean_division", issue = "49048")]
286 pub fn rem_euclid(self, rhs: f64) -> f64 {
295 /// Raises a number to an integer power.
297 /// Using this function is generally faster than using `powf`
303 /// let abs_difference = (x.powi(2) - x*x).abs();
305 /// assert!(abs_difference < 1e-10);
307 #[stable(feature = "rust1", since = "1.0.0")]
309 pub fn powi(self, n: i32) -> f64 {
310 unsafe { intrinsics::powif64(self, n) }
313 /// Raises a number to a floating point power.
319 /// let abs_difference = (x.powf(2.0) - x*x).abs();
321 /// assert!(abs_difference < 1e-10);
323 #[stable(feature = "rust1", since = "1.0.0")]
325 pub fn powf(self, n: f64) -> f64 {
326 unsafe { intrinsics::powf64(self, n) }
329 /// Takes the square root of a number.
331 /// Returns NaN if `self` is a negative number.
336 /// let positive = 4.0_f64;
337 /// let negative = -4.0_f64;
339 /// let abs_difference = (positive.sqrt() - 2.0).abs();
341 /// assert!(abs_difference < 1e-10);
342 /// assert!(negative.sqrt().is_nan());
344 #[stable(feature = "rust1", since = "1.0.0")]
346 pub fn sqrt(self) -> f64 {
350 unsafe { intrinsics::sqrtf64(self) }
354 /// Returns `e^(self)`, (the exponential function).
359 /// let one = 1.0_f64;
361 /// let e = one.exp();
363 /// // ln(e) - 1 == 0
364 /// let abs_difference = (e.ln() - 1.0).abs();
366 /// assert!(abs_difference < 1e-10);
368 #[stable(feature = "rust1", since = "1.0.0")]
370 pub fn exp(self) -> f64 {
371 unsafe { intrinsics::expf64(self) }
374 /// Returns `2^(self)`.
382 /// let abs_difference = (f.exp2() - 4.0).abs();
384 /// assert!(abs_difference < 1e-10);
386 #[stable(feature = "rust1", since = "1.0.0")]
388 pub fn exp2(self) -> f64 {
389 unsafe { intrinsics::exp2f64(self) }
392 /// Returns the natural logarithm of the number.
397 /// let one = 1.0_f64;
399 /// let e = one.exp();
401 /// // ln(e) - 1 == 0
402 /// let abs_difference = (e.ln() - 1.0).abs();
404 /// assert!(abs_difference < 1e-10);
406 #[stable(feature = "rust1", since = "1.0.0")]
408 pub fn ln(self) -> f64 {
409 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
412 /// Returns the logarithm of the number with respect to an arbitrary base.
414 /// The result may not be correctly rounded owing to implementation details;
415 /// `self.log2()` can produce more accurate results for base 2, and
416 /// `self.log10()` can produce more accurate results for base 10.
421 /// let five = 5.0_f64;
423 /// // log5(5) - 1 == 0
424 /// let abs_difference = (five.log(5.0) - 1.0).abs();
426 /// assert!(abs_difference < 1e-10);
428 #[stable(feature = "rust1", since = "1.0.0")]
430 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
432 /// Returns the base 2 logarithm of the number.
437 /// let two = 2.0_f64;
439 /// // log2(2) - 1 == 0
440 /// let abs_difference = (two.log2() - 1.0).abs();
442 /// assert!(abs_difference < 1e-10);
444 #[stable(feature = "rust1", since = "1.0.0")]
446 pub fn log2(self) -> f64 {
447 self.log_wrapper(|n| {
448 #[cfg(target_os = "android")]
449 return ::sys::android::log2f64(n);
450 #[cfg(not(target_os = "android"))]
451 return unsafe { intrinsics::log2f64(n) };
455 /// Returns the base 10 logarithm of the number.
460 /// let ten = 10.0_f64;
462 /// // log10(10) - 1 == 0
463 /// let abs_difference = (ten.log10() - 1.0).abs();
465 /// assert!(abs_difference < 1e-10);
467 #[stable(feature = "rust1", since = "1.0.0")]
469 pub fn log10(self) -> f64 {
470 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
473 /// The positive difference of two numbers.
475 /// * If `self <= other`: `0:0`
476 /// * Else: `self - other`
482 /// let y = -3.0_f64;
484 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
485 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
487 /// assert!(abs_difference_x < 1e-10);
488 /// assert!(abs_difference_y < 1e-10);
490 #[stable(feature = "rust1", since = "1.0.0")]
492 #[rustc_deprecated(since = "1.10.0",
493 reason = "you probably meant `(self - other).abs()`: \
494 this operation is `(self - other).max(0.0)` \
495 except that `abs_sub` also propagates NaNs (also \
496 known as `fdim` in C). If you truly need the positive \
497 difference, consider using that expression or the C function \
498 `fdim`, depending on how you wish to handle NaN (please consider \
499 filing an issue describing your use-case too).")]
500 pub fn abs_sub(self, other: f64) -> f64 {
501 unsafe { cmath::fdim(self, other) }
504 /// Takes 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 < 1e-10);
516 #[stable(feature = "rust1", since = "1.0.0")]
518 pub fn cbrt(self) -> f64 {
519 unsafe { cmath::cbrt(self) }
522 /// Calculates the length of the hypotenuse of a right-angle triangle given
523 /// legs of length `x` and `y`.
531 /// // sqrt(x^2 + y^2)
532 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
534 /// assert!(abs_difference < 1e-10);
536 #[stable(feature = "rust1", since = "1.0.0")]
538 pub fn hypot(self, other: f64) -> f64 {
539 unsafe { cmath::hypot(self, other) }
542 /// Computes the sine of a number (in radians).
549 /// let x = f64::consts::PI/2.0;
551 /// let abs_difference = (x.sin() - 1.0).abs();
553 /// assert!(abs_difference < 1e-10);
555 #[stable(feature = "rust1", since = "1.0.0")]
557 pub fn sin(self) -> f64 {
558 unsafe { intrinsics::sinf64(self) }
561 /// Computes the cosine of a number (in radians).
568 /// let x = 2.0*f64::consts::PI;
570 /// let abs_difference = (x.cos() - 1.0).abs();
572 /// assert!(abs_difference < 1e-10);
574 #[stable(feature = "rust1", since = "1.0.0")]
576 pub fn cos(self) -> f64 {
577 unsafe { intrinsics::cosf64(self) }
580 /// Computes the tangent of a number (in radians).
587 /// let x = f64::consts::PI/4.0;
588 /// let abs_difference = (x.tan() - 1.0).abs();
590 /// assert!(abs_difference < 1e-14);
592 #[stable(feature = "rust1", since = "1.0.0")]
594 pub fn tan(self) -> f64 {
595 unsafe { cmath::tan(self) }
598 /// Computes the arcsine of a number. Return value is in radians in
599 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
607 /// let f = f64::consts::PI / 2.0;
609 /// // asin(sin(pi/2))
610 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
612 /// assert!(abs_difference < 1e-10);
614 #[stable(feature = "rust1", since = "1.0.0")]
616 pub fn asin(self) -> f64 {
617 unsafe { cmath::asin(self) }
620 /// Computes the arccosine of a number. Return value is in radians in
621 /// the range [0, pi] or NaN if the number is outside the range
629 /// let f = f64::consts::PI / 4.0;
631 /// // acos(cos(pi/4))
632 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
634 /// assert!(abs_difference < 1e-10);
636 #[stable(feature = "rust1", since = "1.0.0")]
638 pub fn acos(self) -> f64 {
639 unsafe { cmath::acos(self) }
642 /// Computes the arctangent of a number. Return value is in radians in the
643 /// range [-pi/2, pi/2];
651 /// let abs_difference = (f.tan().atan() - 1.0).abs();
653 /// assert!(abs_difference < 1e-10);
655 #[stable(feature = "rust1", since = "1.0.0")]
657 pub fn atan(self) -> f64 {
658 unsafe { cmath::atan(self) }
661 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
663 /// * `x = 0`, `y = 0`: `0`
664 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
665 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
666 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
673 /// let pi = f64::consts::PI;
674 /// // Positive angles measured counter-clockwise
675 /// // from positive x axis
676 /// // -pi/4 radians (45 deg clockwise)
677 /// let x1 = 3.0_f64;
678 /// let y1 = -3.0_f64;
680 /// // 3pi/4 radians (135 deg counter-clockwise)
681 /// let x2 = -3.0_f64;
682 /// let y2 = 3.0_f64;
684 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
685 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
687 /// assert!(abs_difference_1 < 1e-10);
688 /// assert!(abs_difference_2 < 1e-10);
690 #[stable(feature = "rust1", since = "1.0.0")]
692 pub fn atan2(self, other: f64) -> f64 {
693 unsafe { cmath::atan2(self, other) }
696 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
697 /// `(sin(x), cos(x))`.
704 /// let x = f64::consts::PI/4.0;
705 /// let f = x.sin_cos();
707 /// let abs_difference_0 = (f.0 - x.sin()).abs();
708 /// let abs_difference_1 = (f.1 - x.cos()).abs();
710 /// assert!(abs_difference_0 < 1e-10);
711 /// assert!(abs_difference_1 < 1e-10);
713 #[stable(feature = "rust1", since = "1.0.0")]
715 pub fn sin_cos(self) -> (f64, f64) {
716 (self.sin(), self.cos())
719 /// Returns `e^(self) - 1` in a way that is accurate even if the
720 /// number is close to zero.
728 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
730 /// assert!(abs_difference < 1e-10);
732 #[stable(feature = "rust1", since = "1.0.0")]
734 pub fn exp_m1(self) -> f64 {
735 unsafe { cmath::expm1(self) }
738 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
739 /// the operations were performed separately.
746 /// let x = f64::consts::E - 1.0;
748 /// // ln(1 + (e - 1)) == ln(e) == 1
749 /// let abs_difference = (x.ln_1p() - 1.0).abs();
751 /// assert!(abs_difference < 1e-10);
753 #[stable(feature = "rust1", since = "1.0.0")]
755 pub fn ln_1p(self) -> f64 {
756 unsafe { cmath::log1p(self) }
759 /// Hyperbolic sine function.
766 /// let e = f64::consts::E;
769 /// let f = x.sinh();
770 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
771 /// let g = (e*e - 1.0)/(2.0*e);
772 /// let abs_difference = (f - g).abs();
774 /// assert!(abs_difference < 1e-10);
776 #[stable(feature = "rust1", since = "1.0.0")]
778 pub fn sinh(self) -> f64 {
779 unsafe { cmath::sinh(self) }
782 /// Hyperbolic cosine function.
789 /// let e = f64::consts::E;
791 /// let f = x.cosh();
792 /// // Solving cosh() at 1 gives this result
793 /// let g = (e*e + 1.0)/(2.0*e);
794 /// let abs_difference = (f - g).abs();
797 /// assert!(abs_difference < 1.0e-10);
799 #[stable(feature = "rust1", since = "1.0.0")]
801 pub fn cosh(self) -> f64 {
802 unsafe { cmath::cosh(self) }
805 /// Hyperbolic tangent function.
812 /// let e = f64::consts::E;
815 /// let f = x.tanh();
816 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
817 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
818 /// let abs_difference = (f - g).abs();
820 /// assert!(abs_difference < 1.0e-10);
822 #[stable(feature = "rust1", since = "1.0.0")]
824 pub fn tanh(self) -> f64 {
825 unsafe { cmath::tanh(self) }
828 /// Inverse hyperbolic sine function.
834 /// let f = x.sinh().asinh();
836 /// let abs_difference = (f - x).abs();
838 /// assert!(abs_difference < 1.0e-10);
840 #[stable(feature = "rust1", since = "1.0.0")]
842 pub fn asinh(self) -> f64 {
843 if self == NEG_INFINITY {
846 (self + ((self * self) + 1.0).sqrt()).ln()
850 /// Inverse hyperbolic cosine function.
856 /// let f = x.cosh().acosh();
858 /// let abs_difference = (f - x).abs();
860 /// assert!(abs_difference < 1.0e-10);
862 #[stable(feature = "rust1", since = "1.0.0")]
864 pub fn acosh(self) -> f64 {
867 x => (x + ((x * x) - 1.0).sqrt()).ln(),
871 /// Inverse hyperbolic tangent function.
878 /// let e = f64::consts::E;
879 /// let f = e.tanh().atanh();
881 /// let abs_difference = (f - e).abs();
883 /// assert!(abs_difference < 1.0e-10);
885 #[stable(feature = "rust1", since = "1.0.0")]
887 pub fn atanh(self) -> f64 {
888 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
891 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
892 // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
894 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
895 if !cfg!(target_os = "solaris") {
898 if self.is_finite() {
901 } else if self == 0.0 {
902 NEG_INFINITY // log(0) = -Inf
906 } else if self.is_nan() {
907 self // log(NaN) = NaN
908 } else if self > 0.0 {
909 self // log(Inf) = Inf
911 NAN // log(-Inf) = NaN
922 use num::FpCategory as Fp;
926 test_num(10f64, 2f64);
931 assert_eq!(NAN.min(2.0), 2.0);
932 assert_eq!(2.0f64.min(NAN), 2.0);
937 assert_eq!(NAN.max(2.0), 2.0);
938 assert_eq!(2.0f64.max(NAN), 2.0);
944 assert!(nan.is_nan());
945 assert!(!nan.is_infinite());
946 assert!(!nan.is_finite());
947 assert!(!nan.is_normal());
948 assert!(nan.is_sign_positive());
949 assert!(!nan.is_sign_negative());
950 assert_eq!(Fp::Nan, nan.classify());
955 let inf: f64 = INFINITY;
956 assert!(inf.is_infinite());
957 assert!(!inf.is_finite());
958 assert!(inf.is_sign_positive());
959 assert!(!inf.is_sign_negative());
960 assert!(!inf.is_nan());
961 assert!(!inf.is_normal());
962 assert_eq!(Fp::Infinite, inf.classify());
966 fn test_neg_infinity() {
967 let neg_inf: f64 = NEG_INFINITY;
968 assert!(neg_inf.is_infinite());
969 assert!(!neg_inf.is_finite());
970 assert!(!neg_inf.is_sign_positive());
971 assert!(neg_inf.is_sign_negative());
972 assert!(!neg_inf.is_nan());
973 assert!(!neg_inf.is_normal());
974 assert_eq!(Fp::Infinite, neg_inf.classify());
979 let zero: f64 = 0.0f64;
980 assert_eq!(0.0, zero);
981 assert!(!zero.is_infinite());
982 assert!(zero.is_finite());
983 assert!(zero.is_sign_positive());
984 assert!(!zero.is_sign_negative());
985 assert!(!zero.is_nan());
986 assert!(!zero.is_normal());
987 assert_eq!(Fp::Zero, zero.classify());
992 let neg_zero: f64 = -0.0;
993 assert_eq!(0.0, neg_zero);
994 assert!(!neg_zero.is_infinite());
995 assert!(neg_zero.is_finite());
996 assert!(!neg_zero.is_sign_positive());
997 assert!(neg_zero.is_sign_negative());
998 assert!(!neg_zero.is_nan());
999 assert!(!neg_zero.is_normal());
1000 assert_eq!(Fp::Zero, neg_zero.classify());
1003 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1006 let one: f64 = 1.0f64;
1007 assert_eq!(1.0, one);
1008 assert!(!one.is_infinite());
1009 assert!(one.is_finite());
1010 assert!(one.is_sign_positive());
1011 assert!(!one.is_sign_negative());
1012 assert!(!one.is_nan());
1013 assert!(one.is_normal());
1014 assert_eq!(Fp::Normal, one.classify());
1020 let inf: f64 = INFINITY;
1021 let neg_inf: f64 = NEG_INFINITY;
1022 assert!(nan.is_nan());
1023 assert!(!0.0f64.is_nan());
1024 assert!(!5.3f64.is_nan());
1025 assert!(!(-10.732f64).is_nan());
1026 assert!(!inf.is_nan());
1027 assert!(!neg_inf.is_nan());
1031 fn test_is_infinite() {
1033 let inf: f64 = INFINITY;
1034 let neg_inf: f64 = NEG_INFINITY;
1035 assert!(!nan.is_infinite());
1036 assert!(inf.is_infinite());
1037 assert!(neg_inf.is_infinite());
1038 assert!(!0.0f64.is_infinite());
1039 assert!(!42.8f64.is_infinite());
1040 assert!(!(-109.2f64).is_infinite());
1044 fn test_is_finite() {
1046 let inf: f64 = INFINITY;
1047 let neg_inf: f64 = NEG_INFINITY;
1048 assert!(!nan.is_finite());
1049 assert!(!inf.is_finite());
1050 assert!(!neg_inf.is_finite());
1051 assert!(0.0f64.is_finite());
1052 assert!(42.8f64.is_finite());
1053 assert!((-109.2f64).is_finite());
1056 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1058 fn test_is_normal() {
1060 let inf: f64 = INFINITY;
1061 let neg_inf: f64 = NEG_INFINITY;
1062 let zero: f64 = 0.0f64;
1063 let neg_zero: f64 = -0.0;
1064 assert!(!nan.is_normal());
1065 assert!(!inf.is_normal());
1066 assert!(!neg_inf.is_normal());
1067 assert!(!zero.is_normal());
1068 assert!(!neg_zero.is_normal());
1069 assert!(1f64.is_normal());
1070 assert!(1e-307f64.is_normal());
1071 assert!(!1e-308f64.is_normal());
1074 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1076 fn test_classify() {
1078 let inf: f64 = INFINITY;
1079 let neg_inf: f64 = NEG_INFINITY;
1080 let zero: f64 = 0.0f64;
1081 let neg_zero: f64 = -0.0;
1082 assert_eq!(nan.classify(), Fp::Nan);
1083 assert_eq!(inf.classify(), Fp::Infinite);
1084 assert_eq!(neg_inf.classify(), Fp::Infinite);
1085 assert_eq!(zero.classify(), Fp::Zero);
1086 assert_eq!(neg_zero.classify(), Fp::Zero);
1087 assert_eq!(1e-307f64.classify(), Fp::Normal);
1088 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1093 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1094 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1095 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1096 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1097 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1098 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1099 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1100 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1101 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1102 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1107 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1108 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1109 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1110 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1111 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1112 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1113 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1114 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1115 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1116 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1121 assert_approx_eq!(1.0f64.round(), 1.0f64);
1122 assert_approx_eq!(1.3f64.round(), 1.0f64);
1123 assert_approx_eq!(1.5f64.round(), 2.0f64);
1124 assert_approx_eq!(1.7f64.round(), 2.0f64);
1125 assert_approx_eq!(0.0f64.round(), 0.0f64);
1126 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1127 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1128 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1129 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1130 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1135 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1136 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1137 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1138 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1139 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1140 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1141 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1142 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1143 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1144 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1149 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1150 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1151 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1152 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1153 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1154 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1155 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1156 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1157 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1158 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1163 assert_eq!(INFINITY.abs(), INFINITY);
1164 assert_eq!(1f64.abs(), 1f64);
1165 assert_eq!(0f64.abs(), 0f64);
1166 assert_eq!((-0f64).abs(), 0f64);
1167 assert_eq!((-1f64).abs(), 1f64);
1168 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1169 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1170 assert!(NAN.abs().is_nan());
1175 assert_eq!(INFINITY.signum(), 1f64);
1176 assert_eq!(1f64.signum(), 1f64);
1177 assert_eq!(0f64.signum(), 1f64);
1178 assert_eq!((-0f64).signum(), -1f64);
1179 assert_eq!((-1f64).signum(), -1f64);
1180 assert_eq!(NEG_INFINITY.signum(), -1f64);
1181 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1182 assert!(NAN.signum().is_nan());
1186 fn test_is_sign_positive() {
1187 assert!(INFINITY.is_sign_positive());
1188 assert!(1f64.is_sign_positive());
1189 assert!(0f64.is_sign_positive());
1190 assert!(!(-0f64).is_sign_positive());
1191 assert!(!(-1f64).is_sign_positive());
1192 assert!(!NEG_INFINITY.is_sign_positive());
1193 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1194 assert!(NAN.is_sign_positive());
1195 assert!(!(-NAN).is_sign_positive());
1199 fn test_is_sign_negative() {
1200 assert!(!INFINITY.is_sign_negative());
1201 assert!(!1f64.is_sign_negative());
1202 assert!(!0f64.is_sign_negative());
1203 assert!((-0f64).is_sign_negative());
1204 assert!((-1f64).is_sign_negative());
1205 assert!(NEG_INFINITY.is_sign_negative());
1206 assert!((1f64/NEG_INFINITY).is_sign_negative());
1207 assert!(!NAN.is_sign_negative());
1208 assert!((-NAN).is_sign_negative());
1214 let inf: f64 = INFINITY;
1215 let neg_inf: f64 = NEG_INFINITY;
1216 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1217 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1218 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1219 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1220 assert!(nan.mul_add(7.8, 9.0).is_nan());
1221 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1222 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1223 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1224 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1230 let inf: f64 = INFINITY;
1231 let neg_inf: f64 = NEG_INFINITY;
1232 assert_eq!(1.0f64.recip(), 1.0);
1233 assert_eq!(2.0f64.recip(), 0.5);
1234 assert_eq!((-0.4f64).recip(), -2.5);
1235 assert_eq!(0.0f64.recip(), inf);
1236 assert!(nan.recip().is_nan());
1237 assert_eq!(inf.recip(), 0.0);
1238 assert_eq!(neg_inf.recip(), 0.0);
1244 let inf: f64 = INFINITY;
1245 let neg_inf: f64 = NEG_INFINITY;
1246 assert_eq!(1.0f64.powi(1), 1.0);
1247 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1248 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1249 assert_eq!(8.3f64.powi(0), 1.0);
1250 assert!(nan.powi(2).is_nan());
1251 assert_eq!(inf.powi(3), inf);
1252 assert_eq!(neg_inf.powi(2), inf);
1258 let inf: f64 = INFINITY;
1259 let neg_inf: f64 = NEG_INFINITY;
1260 assert_eq!(1.0f64.powf(1.0), 1.0);
1261 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1262 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1263 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1264 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1265 assert_eq!(8.3f64.powf(0.0), 1.0);
1266 assert!(nan.powf(2.0).is_nan());
1267 assert_eq!(inf.powf(2.0), inf);
1268 assert_eq!(neg_inf.powf(3.0), neg_inf);
1272 fn test_sqrt_domain() {
1273 assert!(NAN.sqrt().is_nan());
1274 assert!(NEG_INFINITY.sqrt().is_nan());
1275 assert!((-1.0f64).sqrt().is_nan());
1276 assert_eq!((-0.0f64).sqrt(), -0.0);
1277 assert_eq!(0.0f64.sqrt(), 0.0);
1278 assert_eq!(1.0f64.sqrt(), 1.0);
1279 assert_eq!(INFINITY.sqrt(), INFINITY);
1284 assert_eq!(1.0, 0.0f64.exp());
1285 assert_approx_eq!(2.718282, 1.0f64.exp());
1286 assert_approx_eq!(148.413159, 5.0f64.exp());
1288 let inf: f64 = INFINITY;
1289 let neg_inf: f64 = NEG_INFINITY;
1291 assert_eq!(inf, inf.exp());
1292 assert_eq!(0.0, neg_inf.exp());
1293 assert!(nan.exp().is_nan());
1298 assert_eq!(32.0, 5.0f64.exp2());
1299 assert_eq!(1.0, 0.0f64.exp2());
1301 let inf: f64 = INFINITY;
1302 let neg_inf: f64 = NEG_INFINITY;
1304 assert_eq!(inf, inf.exp2());
1305 assert_eq!(0.0, neg_inf.exp2());
1306 assert!(nan.exp2().is_nan());
1312 let inf: f64 = INFINITY;
1313 let neg_inf: f64 = NEG_INFINITY;
1314 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1315 assert!(nan.ln().is_nan());
1316 assert_eq!(inf.ln(), inf);
1317 assert!(neg_inf.ln().is_nan());
1318 assert!((-2.3f64).ln().is_nan());
1319 assert_eq!((-0.0f64).ln(), neg_inf);
1320 assert_eq!(0.0f64.ln(), neg_inf);
1321 assert_approx_eq!(4.0f64.ln(), 1.386294);
1327 let inf: f64 = INFINITY;
1328 let neg_inf: f64 = NEG_INFINITY;
1329 assert_eq!(10.0f64.log(10.0), 1.0);
1330 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1331 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1332 assert!(1.0f64.log(1.0).is_nan());
1333 assert!(1.0f64.log(-13.9).is_nan());
1334 assert!(nan.log(2.3).is_nan());
1335 assert_eq!(inf.log(10.0), inf);
1336 assert!(neg_inf.log(8.8).is_nan());
1337 assert!((-2.3f64).log(0.1).is_nan());
1338 assert_eq!((-0.0f64).log(2.0), neg_inf);
1339 assert_eq!(0.0f64.log(7.0), neg_inf);
1345 let inf: f64 = INFINITY;
1346 let neg_inf: f64 = NEG_INFINITY;
1347 assert_approx_eq!(10.0f64.log2(), 3.321928);
1348 assert_approx_eq!(2.3f64.log2(), 1.201634);
1349 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1350 assert!(nan.log2().is_nan());
1351 assert_eq!(inf.log2(), inf);
1352 assert!(neg_inf.log2().is_nan());
1353 assert!((-2.3f64).log2().is_nan());
1354 assert_eq!((-0.0f64).log2(), neg_inf);
1355 assert_eq!(0.0f64.log2(), neg_inf);
1361 let inf: f64 = INFINITY;
1362 let neg_inf: f64 = NEG_INFINITY;
1363 assert_eq!(10.0f64.log10(), 1.0);
1364 assert_approx_eq!(2.3f64.log10(), 0.361728);
1365 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1366 assert_eq!(1.0f64.log10(), 0.0);
1367 assert!(nan.log10().is_nan());
1368 assert_eq!(inf.log10(), inf);
1369 assert!(neg_inf.log10().is_nan());
1370 assert!((-2.3f64).log10().is_nan());
1371 assert_eq!((-0.0f64).log10(), neg_inf);
1372 assert_eq!(0.0f64.log10(), neg_inf);
1376 fn test_to_degrees() {
1377 let pi: f64 = consts::PI;
1379 let inf: f64 = INFINITY;
1380 let neg_inf: f64 = NEG_INFINITY;
1381 assert_eq!(0.0f64.to_degrees(), 0.0);
1382 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1383 assert_eq!(pi.to_degrees(), 180.0);
1384 assert!(nan.to_degrees().is_nan());
1385 assert_eq!(inf.to_degrees(), inf);
1386 assert_eq!(neg_inf.to_degrees(), neg_inf);
1390 fn test_to_radians() {
1391 let pi: f64 = consts::PI;
1393 let inf: f64 = INFINITY;
1394 let neg_inf: f64 = NEG_INFINITY;
1395 assert_eq!(0.0f64.to_radians(), 0.0);
1396 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1397 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1398 assert_eq!(180.0f64.to_radians(), pi);
1399 assert!(nan.to_radians().is_nan());
1400 assert_eq!(inf.to_radians(), inf);
1401 assert_eq!(neg_inf.to_radians(), neg_inf);
1406 assert_eq!(0.0f64.asinh(), 0.0f64);
1407 assert_eq!((-0.0f64).asinh(), -0.0f64);
1409 let inf: f64 = INFINITY;
1410 let neg_inf: f64 = NEG_INFINITY;
1412 assert_eq!(inf.asinh(), inf);
1413 assert_eq!(neg_inf.asinh(), neg_inf);
1414 assert!(nan.asinh().is_nan());
1415 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1416 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1421 assert_eq!(1.0f64.acosh(), 0.0f64);
1422 assert!(0.999f64.acosh().is_nan());
1424 let inf: f64 = INFINITY;
1425 let neg_inf: f64 = NEG_INFINITY;
1427 assert_eq!(inf.acosh(), inf);
1428 assert!(neg_inf.acosh().is_nan());
1429 assert!(nan.acosh().is_nan());
1430 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1431 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1436 assert_eq!(0.0f64.atanh(), 0.0f64);
1437 assert_eq!((-0.0f64).atanh(), -0.0f64);
1439 let inf: f64 = INFINITY;
1440 let neg_inf: f64 = NEG_INFINITY;
1442 assert_eq!(1.0f64.atanh(), inf);
1443 assert_eq!((-1.0f64).atanh(), neg_inf);
1444 assert!(2f64.atanh().atanh().is_nan());
1445 assert!((-2f64).atanh().atanh().is_nan());
1446 assert!(inf.atanh().is_nan());
1447 assert!(neg_inf.atanh().is_nan());
1448 assert!(nan.atanh().is_nan());
1449 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1450 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1454 fn test_real_consts() {
1456 let pi: f64 = consts::PI;
1457 let frac_pi_2: f64 = consts::FRAC_PI_2;
1458 let frac_pi_3: f64 = consts::FRAC_PI_3;
1459 let frac_pi_4: f64 = consts::FRAC_PI_4;
1460 let frac_pi_6: f64 = consts::FRAC_PI_6;
1461 let frac_pi_8: f64 = consts::FRAC_PI_8;
1462 let frac_1_pi: f64 = consts::FRAC_1_PI;
1463 let frac_2_pi: f64 = consts::FRAC_2_PI;
1464 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1465 let sqrt2: f64 = consts::SQRT_2;
1466 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1467 let e: f64 = consts::E;
1468 let log2_e: f64 = consts::LOG2_E;
1469 let log10_e: f64 = consts::LOG10_E;
1470 let ln_2: f64 = consts::LN_2;
1471 let ln_10: f64 = consts::LN_10;
1473 assert_approx_eq!(frac_pi_2, pi / 2f64);
1474 assert_approx_eq!(frac_pi_3, pi / 3f64);
1475 assert_approx_eq!(frac_pi_4, pi / 4f64);
1476 assert_approx_eq!(frac_pi_6, pi / 6f64);
1477 assert_approx_eq!(frac_pi_8, pi / 8f64);
1478 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1479 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1480 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1481 assert_approx_eq!(sqrt2, 2f64.sqrt());
1482 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1483 assert_approx_eq!(log2_e, e.log2());
1484 assert_approx_eq!(log10_e, e.log10());
1485 assert_approx_eq!(ln_2, 2f64.ln());
1486 assert_approx_eq!(ln_10, 10f64.ln());
1490 fn test_float_bits_conv() {
1491 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1492 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1493 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1494 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1495 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1496 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1497 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1498 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1500 // Check that NaNs roundtrip their bits regardless of signalingness
1501 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1502 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1503 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1504 assert!(f64::from_bits(masked_nan1).is_nan());
1505 assert!(f64::from_bits(masked_nan2).is_nan());
1507 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1508 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);