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 one number and the sign of
182 /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
183 /// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
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 `mod_euc`.
234 /// This computes the integer `n` such that
235 /// `self = n * rhs + self.mod_euc(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_euc(b), 1.0); // 7.0 > 4.0 * 1.0
246 /// assert_eq!((-a).div_euc(b), -2.0); // -7.0 >= 4.0 * -2.0
247 /// assert_eq!(a.div_euc(-b), -1.0); // 7.0 >= -4.0 * -1.0
248 /// assert_eq!((-a).div_euc(-b), 2.0); // -7.0 >= -4.0 * 2.0
251 #[unstable(feature = "euclidean_division", issue = "49048")]
252 pub fn div_euc(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 Euclidean modulo (self mod rhs), which is never negative.
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_euc(rhs) * rhs + self.mod_euc(rhs)`
274 /// #![feature(euclidean_division)]
275 /// let a: f64 = 7.0;
277 /// assert_eq!(a.mod_euc(b), 3.0);
278 /// assert_eq!((-a).mod_euc(b), 1.0);
279 /// assert_eq!(a.mod_euc(-b), 3.0);
280 /// assert_eq!((-a).mod_euc(-b), 1.0);
281 /// // limitation due to round-off error
282 /// assert!((-std::f64::EPSILON).mod_euc(3.0) != 0.0);
285 #[unstable(feature = "euclidean_division", issue = "49048")]
286 pub fn mod_euc(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)` (also \
495 known as `fdim` in C). If you truly need the positive \
496 difference, consider using that expression or the C function \
497 `fdim`, depending on how you wish to handle NaN (please consider \
498 filing an issue describing your use-case too).")]
499 pub fn abs_sub(self, other: f64) -> f64 {
500 unsafe { cmath::fdim(self, other) }
503 /// Takes the cubic root of a number.
510 /// // x^(1/3) - 2 == 0
511 /// let abs_difference = (x.cbrt() - 2.0).abs();
513 /// assert!(abs_difference < 1e-10);
515 #[stable(feature = "rust1", since = "1.0.0")]
517 pub fn cbrt(self) -> f64 {
518 unsafe { cmath::cbrt(self) }
521 /// Calculates the length of the hypotenuse of a right-angle triangle given
522 /// legs of length `x` and `y`.
530 /// // sqrt(x^2 + y^2)
531 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
533 /// assert!(abs_difference < 1e-10);
535 #[stable(feature = "rust1", since = "1.0.0")]
537 pub fn hypot(self, other: f64) -> f64 {
538 unsafe { cmath::hypot(self, other) }
541 /// Computes the sine of a number (in radians).
548 /// let x = f64::consts::PI/2.0;
550 /// let abs_difference = (x.sin() - 1.0).abs();
552 /// assert!(abs_difference < 1e-10);
554 #[stable(feature = "rust1", since = "1.0.0")]
556 pub fn sin(self) -> f64 {
557 unsafe { intrinsics::sinf64(self) }
560 /// Computes the cosine of a number (in radians).
567 /// let x = 2.0*f64::consts::PI;
569 /// let abs_difference = (x.cos() - 1.0).abs();
571 /// assert!(abs_difference < 1e-10);
573 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn cos(self) -> f64 {
576 unsafe { intrinsics::cosf64(self) }
579 /// Computes the tangent of a number (in radians).
586 /// let x = f64::consts::PI/4.0;
587 /// let abs_difference = (x.tan() - 1.0).abs();
589 /// assert!(abs_difference < 1e-14);
591 #[stable(feature = "rust1", since = "1.0.0")]
593 pub fn tan(self) -> f64 {
594 unsafe { cmath::tan(self) }
597 /// Computes the arcsine of a number. Return value is in radians in
598 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
606 /// let f = f64::consts::PI / 2.0;
608 /// // asin(sin(pi/2))
609 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
611 /// assert!(abs_difference < 1e-10);
613 #[stable(feature = "rust1", since = "1.0.0")]
615 pub fn asin(self) -> f64 {
616 unsafe { cmath::asin(self) }
619 /// Computes the arccosine of a number. Return value is in radians in
620 /// the range [0, pi] or NaN if the number is outside the range
628 /// let f = f64::consts::PI / 4.0;
630 /// // acos(cos(pi/4))
631 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
633 /// assert!(abs_difference < 1e-10);
635 #[stable(feature = "rust1", since = "1.0.0")]
637 pub fn acos(self) -> f64 {
638 unsafe { cmath::acos(self) }
641 /// Computes the arctangent of a number. Return value is in radians in the
642 /// range [-pi/2, pi/2];
650 /// let abs_difference = (f.tan().atan() - 1.0).abs();
652 /// assert!(abs_difference < 1e-10);
654 #[stable(feature = "rust1", since = "1.0.0")]
656 pub fn atan(self) -> f64 {
657 unsafe { cmath::atan(self) }
660 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
662 /// * `x = 0`, `y = 0`: `0`
663 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
664 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
665 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
672 /// let pi = f64::consts::PI;
673 /// // Positive angles measured counter-clockwise
674 /// // from positive x axis
675 /// // -pi/4 radians (45 deg clockwise)
676 /// let x1 = 3.0_f64;
677 /// let y1 = -3.0_f64;
679 /// // 3pi/4 radians (135 deg counter-clockwise)
680 /// let x2 = -3.0_f64;
681 /// let y2 = 3.0_f64;
683 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
684 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
686 /// assert!(abs_difference_1 < 1e-10);
687 /// assert!(abs_difference_2 < 1e-10);
689 #[stable(feature = "rust1", since = "1.0.0")]
691 pub fn atan2(self, other: f64) -> f64 {
692 unsafe { cmath::atan2(self, other) }
695 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
696 /// `(sin(x), cos(x))`.
703 /// let x = f64::consts::PI/4.0;
704 /// let f = x.sin_cos();
706 /// let abs_difference_0 = (f.0 - x.sin()).abs();
707 /// let abs_difference_1 = (f.1 - x.cos()).abs();
709 /// assert!(abs_difference_0 < 1e-10);
710 /// assert!(abs_difference_1 < 1e-10);
712 #[stable(feature = "rust1", since = "1.0.0")]
714 pub fn sin_cos(self) -> (f64, f64) {
715 (self.sin(), self.cos())
718 /// Returns `e^(self) - 1` in a way that is accurate even if the
719 /// number is close to zero.
727 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
729 /// assert!(abs_difference < 1e-10);
731 #[stable(feature = "rust1", since = "1.0.0")]
733 pub fn exp_m1(self) -> f64 {
734 unsafe { cmath::expm1(self) }
737 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
738 /// the operations were performed separately.
745 /// let x = f64::consts::E - 1.0;
747 /// // ln(1 + (e - 1)) == ln(e) == 1
748 /// let abs_difference = (x.ln_1p() - 1.0).abs();
750 /// assert!(abs_difference < 1e-10);
752 #[stable(feature = "rust1", since = "1.0.0")]
754 pub fn ln_1p(self) -> f64 {
755 unsafe { cmath::log1p(self) }
758 /// Hyperbolic sine function.
765 /// let e = f64::consts::E;
768 /// let f = x.sinh();
769 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
770 /// let g = (e*e - 1.0)/(2.0*e);
771 /// let abs_difference = (f - g).abs();
773 /// assert!(abs_difference < 1e-10);
775 #[stable(feature = "rust1", since = "1.0.0")]
777 pub fn sinh(self) -> f64 {
778 unsafe { cmath::sinh(self) }
781 /// Hyperbolic cosine function.
788 /// let e = f64::consts::E;
790 /// let f = x.cosh();
791 /// // Solving cosh() at 1 gives this result
792 /// let g = (e*e + 1.0)/(2.0*e);
793 /// let abs_difference = (f - g).abs();
796 /// assert!(abs_difference < 1.0e-10);
798 #[stable(feature = "rust1", since = "1.0.0")]
800 pub fn cosh(self) -> f64 {
801 unsafe { cmath::cosh(self) }
804 /// Hyperbolic tangent function.
811 /// let e = f64::consts::E;
814 /// let f = x.tanh();
815 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
816 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
817 /// let abs_difference = (f - g).abs();
819 /// assert!(abs_difference < 1.0e-10);
821 #[stable(feature = "rust1", since = "1.0.0")]
823 pub fn tanh(self) -> f64 {
824 unsafe { cmath::tanh(self) }
827 /// Inverse hyperbolic sine function.
833 /// let f = x.sinh().asinh();
835 /// let abs_difference = (f - x).abs();
837 /// assert!(abs_difference < 1.0e-10);
839 #[stable(feature = "rust1", since = "1.0.0")]
841 pub fn asinh(self) -> f64 {
842 if self == NEG_INFINITY {
845 (self + ((self * self) + 1.0).sqrt()).ln()
849 /// Inverse hyperbolic cosine function.
855 /// let f = x.cosh().acosh();
857 /// let abs_difference = (f - x).abs();
859 /// assert!(abs_difference < 1.0e-10);
861 #[stable(feature = "rust1", since = "1.0.0")]
863 pub fn acosh(self) -> f64 {
866 x => (x + ((x * x) - 1.0).sqrt()).ln(),
870 /// Inverse hyperbolic tangent function.
877 /// let e = f64::consts::E;
878 /// let f = e.tanh().atanh();
880 /// let abs_difference = (f - e).abs();
882 /// assert!(abs_difference < 1.0e-10);
884 #[stable(feature = "rust1", since = "1.0.0")]
886 pub fn atanh(self) -> f64 {
887 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
890 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
891 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
893 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
894 if !cfg!(target_os = "solaris") {
897 if self.is_finite() {
900 } else if self == 0.0 {
901 NEG_INFINITY // log(0) = -Inf
905 } else if self.is_nan() {
906 self // log(NaN) = NaN
907 } else if self > 0.0 {
908 self // log(Inf) = Inf
910 NAN // log(-Inf) = NaN
921 use num::FpCategory as Fp;
925 test_num(10f64, 2f64);
930 assert_eq!(NAN.min(2.0), 2.0);
931 assert_eq!(2.0f64.min(NAN), 2.0);
936 assert_eq!(NAN.max(2.0), 2.0);
937 assert_eq!(2.0f64.max(NAN), 2.0);
943 assert!(nan.is_nan());
944 assert!(!nan.is_infinite());
945 assert!(!nan.is_finite());
946 assert!(!nan.is_normal());
947 assert!(nan.is_sign_positive());
948 assert!(!nan.is_sign_negative());
949 assert_eq!(Fp::Nan, nan.classify());
954 let inf: f64 = INFINITY;
955 assert!(inf.is_infinite());
956 assert!(!inf.is_finite());
957 assert!(inf.is_sign_positive());
958 assert!(!inf.is_sign_negative());
959 assert!(!inf.is_nan());
960 assert!(!inf.is_normal());
961 assert_eq!(Fp::Infinite, inf.classify());
965 fn test_neg_infinity() {
966 let neg_inf: f64 = NEG_INFINITY;
967 assert!(neg_inf.is_infinite());
968 assert!(!neg_inf.is_finite());
969 assert!(!neg_inf.is_sign_positive());
970 assert!(neg_inf.is_sign_negative());
971 assert!(!neg_inf.is_nan());
972 assert!(!neg_inf.is_normal());
973 assert_eq!(Fp::Infinite, neg_inf.classify());
978 let zero: f64 = 0.0f64;
979 assert_eq!(0.0, zero);
980 assert!(!zero.is_infinite());
981 assert!(zero.is_finite());
982 assert!(zero.is_sign_positive());
983 assert!(!zero.is_sign_negative());
984 assert!(!zero.is_nan());
985 assert!(!zero.is_normal());
986 assert_eq!(Fp::Zero, zero.classify());
991 let neg_zero: f64 = -0.0;
992 assert_eq!(0.0, neg_zero);
993 assert!(!neg_zero.is_infinite());
994 assert!(neg_zero.is_finite());
995 assert!(!neg_zero.is_sign_positive());
996 assert!(neg_zero.is_sign_negative());
997 assert!(!neg_zero.is_nan());
998 assert!(!neg_zero.is_normal());
999 assert_eq!(Fp::Zero, neg_zero.classify());
1002 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1005 let one: f64 = 1.0f64;
1006 assert_eq!(1.0, one);
1007 assert!(!one.is_infinite());
1008 assert!(one.is_finite());
1009 assert!(one.is_sign_positive());
1010 assert!(!one.is_sign_negative());
1011 assert!(!one.is_nan());
1012 assert!(one.is_normal());
1013 assert_eq!(Fp::Normal, one.classify());
1019 let inf: f64 = INFINITY;
1020 let neg_inf: f64 = NEG_INFINITY;
1021 assert!(nan.is_nan());
1022 assert!(!0.0f64.is_nan());
1023 assert!(!5.3f64.is_nan());
1024 assert!(!(-10.732f64).is_nan());
1025 assert!(!inf.is_nan());
1026 assert!(!neg_inf.is_nan());
1030 fn test_is_infinite() {
1032 let inf: f64 = INFINITY;
1033 let neg_inf: f64 = NEG_INFINITY;
1034 assert!(!nan.is_infinite());
1035 assert!(inf.is_infinite());
1036 assert!(neg_inf.is_infinite());
1037 assert!(!0.0f64.is_infinite());
1038 assert!(!42.8f64.is_infinite());
1039 assert!(!(-109.2f64).is_infinite());
1043 fn test_is_finite() {
1045 let inf: f64 = INFINITY;
1046 let neg_inf: f64 = NEG_INFINITY;
1047 assert!(!nan.is_finite());
1048 assert!(!inf.is_finite());
1049 assert!(!neg_inf.is_finite());
1050 assert!(0.0f64.is_finite());
1051 assert!(42.8f64.is_finite());
1052 assert!((-109.2f64).is_finite());
1055 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1057 fn test_is_normal() {
1059 let inf: f64 = INFINITY;
1060 let neg_inf: f64 = NEG_INFINITY;
1061 let zero: f64 = 0.0f64;
1062 let neg_zero: f64 = -0.0;
1063 assert!(!nan.is_normal());
1064 assert!(!inf.is_normal());
1065 assert!(!neg_inf.is_normal());
1066 assert!(!zero.is_normal());
1067 assert!(!neg_zero.is_normal());
1068 assert!(1f64.is_normal());
1069 assert!(1e-307f64.is_normal());
1070 assert!(!1e-308f64.is_normal());
1073 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1075 fn test_classify() {
1077 let inf: f64 = INFINITY;
1078 let neg_inf: f64 = NEG_INFINITY;
1079 let zero: f64 = 0.0f64;
1080 let neg_zero: f64 = -0.0;
1081 assert_eq!(nan.classify(), Fp::Nan);
1082 assert_eq!(inf.classify(), Fp::Infinite);
1083 assert_eq!(neg_inf.classify(), Fp::Infinite);
1084 assert_eq!(zero.classify(), Fp::Zero);
1085 assert_eq!(neg_zero.classify(), Fp::Zero);
1086 assert_eq!(1e-307f64.classify(), Fp::Normal);
1087 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1092 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1093 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1094 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1095 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1096 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1097 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1098 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1099 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1100 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1101 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1106 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1107 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1108 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1109 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1110 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1111 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1112 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1113 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1114 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1115 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1120 assert_approx_eq!(1.0f64.round(), 1.0f64);
1121 assert_approx_eq!(1.3f64.round(), 1.0f64);
1122 assert_approx_eq!(1.5f64.round(), 2.0f64);
1123 assert_approx_eq!(1.7f64.round(), 2.0f64);
1124 assert_approx_eq!(0.0f64.round(), 0.0f64);
1125 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1126 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1127 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1128 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1129 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1134 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1135 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1136 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1137 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1138 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1139 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1140 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1141 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1142 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1143 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1148 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1149 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1150 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1151 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1152 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1153 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1154 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1155 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1156 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1157 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1162 assert_eq!(INFINITY.abs(), INFINITY);
1163 assert_eq!(1f64.abs(), 1f64);
1164 assert_eq!(0f64.abs(), 0f64);
1165 assert_eq!((-0f64).abs(), 0f64);
1166 assert_eq!((-1f64).abs(), 1f64);
1167 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1168 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1169 assert!(NAN.abs().is_nan());
1174 assert_eq!(INFINITY.signum(), 1f64);
1175 assert_eq!(1f64.signum(), 1f64);
1176 assert_eq!(0f64.signum(), 1f64);
1177 assert_eq!((-0f64).signum(), -1f64);
1178 assert_eq!((-1f64).signum(), -1f64);
1179 assert_eq!(NEG_INFINITY.signum(), -1f64);
1180 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1181 assert!(NAN.signum().is_nan());
1185 fn test_is_sign_positive() {
1186 assert!(INFINITY.is_sign_positive());
1187 assert!(1f64.is_sign_positive());
1188 assert!(0f64.is_sign_positive());
1189 assert!(!(-0f64).is_sign_positive());
1190 assert!(!(-1f64).is_sign_positive());
1191 assert!(!NEG_INFINITY.is_sign_positive());
1192 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1193 assert!(NAN.is_sign_positive());
1194 assert!(!(-NAN).is_sign_positive());
1198 fn test_is_sign_negative() {
1199 assert!(!INFINITY.is_sign_negative());
1200 assert!(!1f64.is_sign_negative());
1201 assert!(!0f64.is_sign_negative());
1202 assert!((-0f64).is_sign_negative());
1203 assert!((-1f64).is_sign_negative());
1204 assert!(NEG_INFINITY.is_sign_negative());
1205 assert!((1f64/NEG_INFINITY).is_sign_negative());
1206 assert!(!NAN.is_sign_negative());
1207 assert!((-NAN).is_sign_negative());
1213 let inf: f64 = INFINITY;
1214 let neg_inf: f64 = NEG_INFINITY;
1215 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1216 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1217 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1218 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1219 assert!(nan.mul_add(7.8, 9.0).is_nan());
1220 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1221 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1222 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1223 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1229 let inf: f64 = INFINITY;
1230 let neg_inf: f64 = NEG_INFINITY;
1231 assert_eq!(1.0f64.recip(), 1.0);
1232 assert_eq!(2.0f64.recip(), 0.5);
1233 assert_eq!((-0.4f64).recip(), -2.5);
1234 assert_eq!(0.0f64.recip(), inf);
1235 assert!(nan.recip().is_nan());
1236 assert_eq!(inf.recip(), 0.0);
1237 assert_eq!(neg_inf.recip(), 0.0);
1243 let inf: f64 = INFINITY;
1244 let neg_inf: f64 = NEG_INFINITY;
1245 assert_eq!(1.0f64.powi(1), 1.0);
1246 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1247 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1248 assert_eq!(8.3f64.powi(0), 1.0);
1249 assert!(nan.powi(2).is_nan());
1250 assert_eq!(inf.powi(3), inf);
1251 assert_eq!(neg_inf.powi(2), inf);
1257 let inf: f64 = INFINITY;
1258 let neg_inf: f64 = NEG_INFINITY;
1259 assert_eq!(1.0f64.powf(1.0), 1.0);
1260 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1261 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1262 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1263 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1264 assert_eq!(8.3f64.powf(0.0), 1.0);
1265 assert!(nan.powf(2.0).is_nan());
1266 assert_eq!(inf.powf(2.0), inf);
1267 assert_eq!(neg_inf.powf(3.0), neg_inf);
1271 fn test_sqrt_domain() {
1272 assert!(NAN.sqrt().is_nan());
1273 assert!(NEG_INFINITY.sqrt().is_nan());
1274 assert!((-1.0f64).sqrt().is_nan());
1275 assert_eq!((-0.0f64).sqrt(), -0.0);
1276 assert_eq!(0.0f64.sqrt(), 0.0);
1277 assert_eq!(1.0f64.sqrt(), 1.0);
1278 assert_eq!(INFINITY.sqrt(), INFINITY);
1283 assert_eq!(1.0, 0.0f64.exp());
1284 assert_approx_eq!(2.718282, 1.0f64.exp());
1285 assert_approx_eq!(148.413159, 5.0f64.exp());
1287 let inf: f64 = INFINITY;
1288 let neg_inf: f64 = NEG_INFINITY;
1290 assert_eq!(inf, inf.exp());
1291 assert_eq!(0.0, neg_inf.exp());
1292 assert!(nan.exp().is_nan());
1297 assert_eq!(32.0, 5.0f64.exp2());
1298 assert_eq!(1.0, 0.0f64.exp2());
1300 let inf: f64 = INFINITY;
1301 let neg_inf: f64 = NEG_INFINITY;
1303 assert_eq!(inf, inf.exp2());
1304 assert_eq!(0.0, neg_inf.exp2());
1305 assert!(nan.exp2().is_nan());
1311 let inf: f64 = INFINITY;
1312 let neg_inf: f64 = NEG_INFINITY;
1313 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1314 assert!(nan.ln().is_nan());
1315 assert_eq!(inf.ln(), inf);
1316 assert!(neg_inf.ln().is_nan());
1317 assert!((-2.3f64).ln().is_nan());
1318 assert_eq!((-0.0f64).ln(), neg_inf);
1319 assert_eq!(0.0f64.ln(), neg_inf);
1320 assert_approx_eq!(4.0f64.ln(), 1.386294);
1326 let inf: f64 = INFINITY;
1327 let neg_inf: f64 = NEG_INFINITY;
1328 assert_eq!(10.0f64.log(10.0), 1.0);
1329 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1330 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1331 assert!(1.0f64.log(1.0).is_nan());
1332 assert!(1.0f64.log(-13.9).is_nan());
1333 assert!(nan.log(2.3).is_nan());
1334 assert_eq!(inf.log(10.0), inf);
1335 assert!(neg_inf.log(8.8).is_nan());
1336 assert!((-2.3f64).log(0.1).is_nan());
1337 assert_eq!((-0.0f64).log(2.0), neg_inf);
1338 assert_eq!(0.0f64.log(7.0), neg_inf);
1344 let inf: f64 = INFINITY;
1345 let neg_inf: f64 = NEG_INFINITY;
1346 assert_approx_eq!(10.0f64.log2(), 3.321928);
1347 assert_approx_eq!(2.3f64.log2(), 1.201634);
1348 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1349 assert!(nan.log2().is_nan());
1350 assert_eq!(inf.log2(), inf);
1351 assert!(neg_inf.log2().is_nan());
1352 assert!((-2.3f64).log2().is_nan());
1353 assert_eq!((-0.0f64).log2(), neg_inf);
1354 assert_eq!(0.0f64.log2(), neg_inf);
1360 let inf: f64 = INFINITY;
1361 let neg_inf: f64 = NEG_INFINITY;
1362 assert_eq!(10.0f64.log10(), 1.0);
1363 assert_approx_eq!(2.3f64.log10(), 0.361728);
1364 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1365 assert_eq!(1.0f64.log10(), 0.0);
1366 assert!(nan.log10().is_nan());
1367 assert_eq!(inf.log10(), inf);
1368 assert!(neg_inf.log10().is_nan());
1369 assert!((-2.3f64).log10().is_nan());
1370 assert_eq!((-0.0f64).log10(), neg_inf);
1371 assert_eq!(0.0f64.log10(), neg_inf);
1375 fn test_to_degrees() {
1376 let pi: f64 = consts::PI;
1378 let inf: f64 = INFINITY;
1379 let neg_inf: f64 = NEG_INFINITY;
1380 assert_eq!(0.0f64.to_degrees(), 0.0);
1381 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1382 assert_eq!(pi.to_degrees(), 180.0);
1383 assert!(nan.to_degrees().is_nan());
1384 assert_eq!(inf.to_degrees(), inf);
1385 assert_eq!(neg_inf.to_degrees(), neg_inf);
1389 fn test_to_radians() {
1390 let pi: f64 = consts::PI;
1392 let inf: f64 = INFINITY;
1393 let neg_inf: f64 = NEG_INFINITY;
1394 assert_eq!(0.0f64.to_radians(), 0.0);
1395 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1396 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1397 assert_eq!(180.0f64.to_radians(), pi);
1398 assert!(nan.to_radians().is_nan());
1399 assert_eq!(inf.to_radians(), inf);
1400 assert_eq!(neg_inf.to_radians(), neg_inf);
1405 assert_eq!(0.0f64.asinh(), 0.0f64);
1406 assert_eq!((-0.0f64).asinh(), -0.0f64);
1408 let inf: f64 = INFINITY;
1409 let neg_inf: f64 = NEG_INFINITY;
1411 assert_eq!(inf.asinh(), inf);
1412 assert_eq!(neg_inf.asinh(), neg_inf);
1413 assert!(nan.asinh().is_nan());
1414 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1415 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1420 assert_eq!(1.0f64.acosh(), 0.0f64);
1421 assert!(0.999f64.acosh().is_nan());
1423 let inf: f64 = INFINITY;
1424 let neg_inf: f64 = NEG_INFINITY;
1426 assert_eq!(inf.acosh(), inf);
1427 assert!(neg_inf.acosh().is_nan());
1428 assert!(nan.acosh().is_nan());
1429 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1430 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1435 assert_eq!(0.0f64.atanh(), 0.0f64);
1436 assert_eq!((-0.0f64).atanh(), -0.0f64);
1438 let inf: f64 = INFINITY;
1439 let neg_inf: f64 = NEG_INFINITY;
1441 assert_eq!(1.0f64.atanh(), inf);
1442 assert_eq!((-1.0f64).atanh(), neg_inf);
1443 assert!(2f64.atanh().atanh().is_nan());
1444 assert!((-2f64).atanh().atanh().is_nan());
1445 assert!(inf.atanh().is_nan());
1446 assert!(neg_inf.atanh().is_nan());
1447 assert!(nan.atanh().is_nan());
1448 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1449 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1453 fn test_real_consts() {
1455 let pi: f64 = consts::PI;
1456 let frac_pi_2: f64 = consts::FRAC_PI_2;
1457 let frac_pi_3: f64 = consts::FRAC_PI_3;
1458 let frac_pi_4: f64 = consts::FRAC_PI_4;
1459 let frac_pi_6: f64 = consts::FRAC_PI_6;
1460 let frac_pi_8: f64 = consts::FRAC_PI_8;
1461 let frac_1_pi: f64 = consts::FRAC_1_PI;
1462 let frac_2_pi: f64 = consts::FRAC_2_PI;
1463 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1464 let sqrt2: f64 = consts::SQRT_2;
1465 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1466 let e: f64 = consts::E;
1467 let log2_e: f64 = consts::LOG2_E;
1468 let log10_e: f64 = consts::LOG10_E;
1469 let ln_2: f64 = consts::LN_2;
1470 let ln_10: f64 = consts::LN_10;
1472 assert_approx_eq!(frac_pi_2, pi / 2f64);
1473 assert_approx_eq!(frac_pi_3, pi / 3f64);
1474 assert_approx_eq!(frac_pi_4, pi / 4f64);
1475 assert_approx_eq!(frac_pi_6, pi / 6f64);
1476 assert_approx_eq!(frac_pi_8, pi / 8f64);
1477 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1478 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1479 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1480 assert_approx_eq!(sqrt2, 2f64.sqrt());
1481 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1482 assert_approx_eq!(log2_e, e.log2());
1483 assert_approx_eq!(log10_e, e.log10());
1484 assert_approx_eq!(ln_2, 2f64.ln());
1485 assert_approx_eq!(ln_10, 10f64.ln());
1489 fn test_float_bits_conv() {
1490 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1491 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1492 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1493 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1494 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1495 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1496 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1497 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1499 // Check that NaNs roundtrip their bits regardless of signalingness
1500 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1501 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1502 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1503 assert!(f64::from_bits(masked_nan1).is_nan());
1504 assert!(f64::from_bits(masked_nan2).is_nan());
1506 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1507 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);