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
180 /// another, or `NAN` if the number is `NAN`.
182 /// Equal to `self` if the sign of `self` and `y` are the same, otherwise
188 /// #![feature(copysign)]
193 /// assert_eq!(f.copysign(0.42), 3.5_f64);
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);
198 /// assert!(f64::NAN.copysign(1.0).is_nan());
201 #[unstable(feature="copysign", issue="0")]
202 pub fn copysign(self, y: f64) -> f64 {
203 unsafe { intrinsics::copysignf64(self, y) }
206 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
207 /// error, yielding a more accurate result than an unfused multiply-add.
209 /// Using `mul_add` can be more performant than an unfused multiply-add if
210 /// the target architecture has a dedicated `fma` CPU instruction.
215 /// let m = 10.0_f64;
217 /// let b = 60.0_f64;
220 /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
222 /// assert!(abs_difference < 1e-10);
224 #[stable(feature = "rust1", since = "1.0.0")]
226 pub fn mul_add(self, a: f64, b: f64) -> f64 {
227 unsafe { intrinsics::fmaf64(self, a, b) }
230 /// Calculates Euclidean division, the matching method for `mod_euc`.
232 /// This computes the integer `n` such that
233 /// `self = n * rhs + self.mod_euc(rhs)`.
234 /// In other words, the result is `self / rhs` rounded to the integer `n`
235 /// such that `self >= n * rhs`.
240 /// #![feature(euclidean_division)]
241 /// let a: f64 = 7.0;
243 /// assert_eq!(a.div_euc(b), 1.0); // 7.0 > 4.0 * 1.0
244 /// assert_eq!((-a).div_euc(b), -2.0); // -7.0 >= 4.0 * -2.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
249 #[unstable(feature = "euclidean_division", issue = "49048")]
250 pub fn div_euc(self, rhs: f64) -> f64 {
251 let q = (self / rhs).trunc();
252 if self % rhs < 0.0 {
253 return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
258 /// Calculates the Euclidean modulo (self mod rhs), which is never negative.
260 /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
261 /// most cases. However, due to a floating point round-off error it can
262 /// result in `r == rhs.abs()`, violating the mathematical definition, if
263 /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
264 /// This result is not an element of the function's codomain, but it is the
265 /// closest floating point number in the real numbers and thus fulfills the
266 /// property `self == self.div_euc(rhs) * rhs + self.mod_euc(rhs)`
272 /// #![feature(euclidean_division)]
273 /// let a: f64 = 7.0;
275 /// assert_eq!(a.mod_euc(b), 3.0);
276 /// assert_eq!((-a).mod_euc(b), 1.0);
277 /// assert_eq!(a.mod_euc(-b), 3.0);
278 /// assert_eq!((-a).mod_euc(-b), 1.0);
279 /// // limitation due to round-off error
280 /// assert!((-std::f64::EPSILON).mod_euc(3.0) != 0.0);
283 #[unstable(feature = "euclidean_division", issue = "49048")]
284 pub fn mod_euc(self, rhs: f64) -> f64 {
293 /// Raises a number to an integer power.
295 /// Using this function is generally faster than using `powf`
301 /// let abs_difference = (x.powi(2) - x*x).abs();
303 /// assert!(abs_difference < 1e-10);
305 #[stable(feature = "rust1", since = "1.0.0")]
307 pub fn powi(self, n: i32) -> f64 {
308 unsafe { intrinsics::powif64(self, n) }
311 /// Raises a number to a floating point power.
317 /// let abs_difference = (x.powf(2.0) - x*x).abs();
319 /// assert!(abs_difference < 1e-10);
321 #[stable(feature = "rust1", since = "1.0.0")]
323 pub fn powf(self, n: f64) -> f64 {
324 unsafe { intrinsics::powf64(self, n) }
327 /// Takes the square root of a number.
329 /// Returns NaN if `self` is a negative number.
334 /// let positive = 4.0_f64;
335 /// let negative = -4.0_f64;
337 /// let abs_difference = (positive.sqrt() - 2.0).abs();
339 /// assert!(abs_difference < 1e-10);
340 /// assert!(negative.sqrt().is_nan());
342 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn sqrt(self) -> f64 {
348 unsafe { intrinsics::sqrtf64(self) }
352 /// Returns `e^(self)`, (the exponential function).
357 /// let one = 1.0_f64;
359 /// let e = one.exp();
361 /// // ln(e) - 1 == 0
362 /// let abs_difference = (e.ln() - 1.0).abs();
364 /// assert!(abs_difference < 1e-10);
366 #[stable(feature = "rust1", since = "1.0.0")]
368 pub fn exp(self) -> f64 {
369 unsafe { intrinsics::expf64(self) }
372 /// Returns `2^(self)`.
380 /// let abs_difference = (f.exp2() - 4.0).abs();
382 /// assert!(abs_difference < 1e-10);
384 #[stable(feature = "rust1", since = "1.0.0")]
386 pub fn exp2(self) -> f64 {
387 unsafe { intrinsics::exp2f64(self) }
390 /// Returns the natural logarithm of the number.
395 /// let one = 1.0_f64;
397 /// let e = one.exp();
399 /// // ln(e) - 1 == 0
400 /// let abs_difference = (e.ln() - 1.0).abs();
402 /// assert!(abs_difference < 1e-10);
404 #[stable(feature = "rust1", since = "1.0.0")]
406 pub fn ln(self) -> f64 {
407 self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
410 /// Returns the logarithm of the number with respect to an arbitrary base.
412 /// The result may not be correctly rounded owing to implementation details;
413 /// `self.log2()` can produce more accurate results for base 2, and
414 /// `self.log10()` can produce more accurate results for base 10.
419 /// let five = 5.0_f64;
421 /// // log5(5) - 1 == 0
422 /// let abs_difference = (five.log(5.0) - 1.0).abs();
424 /// assert!(abs_difference < 1e-10);
426 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
430 /// Returns the base 2 logarithm of the number.
435 /// let two = 2.0_f64;
437 /// // log2(2) - 1 == 0
438 /// let abs_difference = (two.log2() - 1.0).abs();
440 /// assert!(abs_difference < 1e-10);
442 #[stable(feature = "rust1", since = "1.0.0")]
444 pub fn log2(self) -> f64 {
445 self.log_wrapper(|n| {
446 #[cfg(target_os = "android")]
447 return ::sys::android::log2f64(n);
448 #[cfg(not(target_os = "android"))]
449 return unsafe { intrinsics::log2f64(n) };
453 /// Returns the base 10 logarithm of the number.
458 /// let ten = 10.0_f64;
460 /// // log10(10) - 1 == 0
461 /// let abs_difference = (ten.log10() - 1.0).abs();
463 /// assert!(abs_difference < 1e-10);
465 #[stable(feature = "rust1", since = "1.0.0")]
467 pub fn log10(self) -> f64 {
468 self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
471 /// The positive difference of two numbers.
473 /// * If `self <= other`: `0:0`
474 /// * Else: `self - other`
480 /// let y = -3.0_f64;
482 /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
483 /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
485 /// assert!(abs_difference_x < 1e-10);
486 /// assert!(abs_difference_y < 1e-10);
488 #[stable(feature = "rust1", since = "1.0.0")]
490 #[rustc_deprecated(since = "1.10.0",
491 reason = "you probably meant `(self - other).abs()`: \
492 this operation is `(self - other).max(0.0)` (also \
493 known as `fdim` in C). If you truly need the positive \
494 difference, consider using that expression or the C function \
495 `fdim`, depending on how you wish to handle NaN (please consider \
496 filing an issue describing your use-case too).")]
497 pub fn abs_sub(self, other: f64) -> f64 {
498 unsafe { cmath::fdim(self, other) }
501 /// Takes the cubic root of a number.
508 /// // x^(1/3) - 2 == 0
509 /// let abs_difference = (x.cbrt() - 2.0).abs();
511 /// assert!(abs_difference < 1e-10);
513 #[stable(feature = "rust1", since = "1.0.0")]
515 pub fn cbrt(self) -> f64 {
516 unsafe { cmath::cbrt(self) }
519 /// Calculates the length of the hypotenuse of a right-angle triangle given
520 /// legs of length `x` and `y`.
528 /// // sqrt(x^2 + y^2)
529 /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
531 /// assert!(abs_difference < 1e-10);
533 #[stable(feature = "rust1", since = "1.0.0")]
535 pub fn hypot(self, other: f64) -> f64 {
536 unsafe { cmath::hypot(self, other) }
539 /// Computes the sine of a number (in radians).
546 /// let x = f64::consts::PI/2.0;
548 /// let abs_difference = (x.sin() - 1.0).abs();
550 /// assert!(abs_difference < 1e-10);
552 #[stable(feature = "rust1", since = "1.0.0")]
554 pub fn sin(self) -> f64 {
555 unsafe { intrinsics::sinf64(self) }
558 /// Computes the cosine of a number (in radians).
565 /// let x = 2.0*f64::consts::PI;
567 /// let abs_difference = (x.cos() - 1.0).abs();
569 /// assert!(abs_difference < 1e-10);
571 #[stable(feature = "rust1", since = "1.0.0")]
573 pub fn cos(self) -> f64 {
574 unsafe { intrinsics::cosf64(self) }
577 /// Computes the tangent of a number (in radians).
584 /// let x = f64::consts::PI/4.0;
585 /// let abs_difference = (x.tan() - 1.0).abs();
587 /// assert!(abs_difference < 1e-14);
589 #[stable(feature = "rust1", since = "1.0.0")]
591 pub fn tan(self) -> f64 {
592 unsafe { cmath::tan(self) }
595 /// Computes the arcsine of a number. Return value is in radians in
596 /// the range [-pi/2, pi/2] or NaN if the number is outside the range
604 /// let f = f64::consts::PI / 2.0;
606 /// // asin(sin(pi/2))
607 /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
609 /// assert!(abs_difference < 1e-10);
611 #[stable(feature = "rust1", since = "1.0.0")]
613 pub fn asin(self) -> f64 {
614 unsafe { cmath::asin(self) }
617 /// Computes the arccosine of a number. Return value is in radians in
618 /// the range [0, pi] or NaN if the number is outside the range
626 /// let f = f64::consts::PI / 4.0;
628 /// // acos(cos(pi/4))
629 /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
631 /// assert!(abs_difference < 1e-10);
633 #[stable(feature = "rust1", since = "1.0.0")]
635 pub fn acos(self) -> f64 {
636 unsafe { cmath::acos(self) }
639 /// Computes the arctangent of a number. Return value is in radians in the
640 /// range [-pi/2, pi/2];
648 /// let abs_difference = (f.tan().atan() - 1.0).abs();
650 /// assert!(abs_difference < 1e-10);
652 #[stable(feature = "rust1", since = "1.0.0")]
654 pub fn atan(self) -> f64 {
655 unsafe { cmath::atan(self) }
658 /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
660 /// * `x = 0`, `y = 0`: `0`
661 /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
662 /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
663 /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
670 /// let pi = f64::consts::PI;
671 /// // Positive angles measured counter-clockwise
672 /// // from positive x axis
673 /// // -pi/4 radians (45 deg clockwise)
674 /// let x1 = 3.0_f64;
675 /// let y1 = -3.0_f64;
677 /// // 3pi/4 radians (135 deg counter-clockwise)
678 /// let x2 = -3.0_f64;
679 /// let y2 = 3.0_f64;
681 /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
682 /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
684 /// assert!(abs_difference_1 < 1e-10);
685 /// assert!(abs_difference_2 < 1e-10);
687 #[stable(feature = "rust1", since = "1.0.0")]
689 pub fn atan2(self, other: f64) -> f64 {
690 unsafe { cmath::atan2(self, other) }
693 /// Simultaneously computes the sine and cosine of the number, `x`. Returns
694 /// `(sin(x), cos(x))`.
701 /// let x = f64::consts::PI/4.0;
702 /// let f = x.sin_cos();
704 /// let abs_difference_0 = (f.0 - x.sin()).abs();
705 /// let abs_difference_1 = (f.1 - x.cos()).abs();
707 /// assert!(abs_difference_0 < 1e-10);
708 /// assert!(abs_difference_1 < 1e-10);
710 #[stable(feature = "rust1", since = "1.0.0")]
712 pub fn sin_cos(self) -> (f64, f64) {
713 (self.sin(), self.cos())
716 /// Returns `e^(self) - 1` in a way that is accurate even if the
717 /// number is close to zero.
725 /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
727 /// assert!(abs_difference < 1e-10);
729 #[stable(feature = "rust1", since = "1.0.0")]
731 pub fn exp_m1(self) -> f64 {
732 unsafe { cmath::expm1(self) }
735 /// Returns `ln(1+n)` (natural logarithm) more accurately than if
736 /// the operations were performed separately.
743 /// let x = f64::consts::E - 1.0;
745 /// // ln(1 + (e - 1)) == ln(e) == 1
746 /// let abs_difference = (x.ln_1p() - 1.0).abs();
748 /// assert!(abs_difference < 1e-10);
750 #[stable(feature = "rust1", since = "1.0.0")]
752 pub fn ln_1p(self) -> f64 {
753 unsafe { cmath::log1p(self) }
756 /// Hyperbolic sine function.
763 /// let e = f64::consts::E;
766 /// let f = x.sinh();
767 /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
768 /// let g = (e*e - 1.0)/(2.0*e);
769 /// let abs_difference = (f - g).abs();
771 /// assert!(abs_difference < 1e-10);
773 #[stable(feature = "rust1", since = "1.0.0")]
775 pub fn sinh(self) -> f64 {
776 unsafe { cmath::sinh(self) }
779 /// Hyperbolic cosine function.
786 /// let e = f64::consts::E;
788 /// let f = x.cosh();
789 /// // Solving cosh() at 1 gives this result
790 /// let g = (e*e + 1.0)/(2.0*e);
791 /// let abs_difference = (f - g).abs();
794 /// assert!(abs_difference < 1.0e-10);
796 #[stable(feature = "rust1", since = "1.0.0")]
798 pub fn cosh(self) -> f64 {
799 unsafe { cmath::cosh(self) }
802 /// Hyperbolic tangent function.
809 /// let e = f64::consts::E;
812 /// let f = x.tanh();
813 /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
814 /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
815 /// let abs_difference = (f - g).abs();
817 /// assert!(abs_difference < 1.0e-10);
819 #[stable(feature = "rust1", since = "1.0.0")]
821 pub fn tanh(self) -> f64 {
822 unsafe { cmath::tanh(self) }
825 /// Inverse hyperbolic sine function.
831 /// let f = x.sinh().asinh();
833 /// let abs_difference = (f - x).abs();
835 /// assert!(abs_difference < 1.0e-10);
837 #[stable(feature = "rust1", since = "1.0.0")]
839 pub fn asinh(self) -> f64 {
840 if self == NEG_INFINITY {
843 (self + ((self * self) + 1.0).sqrt()).ln()
847 /// Inverse hyperbolic cosine function.
853 /// let f = x.cosh().acosh();
855 /// let abs_difference = (f - x).abs();
857 /// assert!(abs_difference < 1.0e-10);
859 #[stable(feature = "rust1", since = "1.0.0")]
861 pub fn acosh(self) -> f64 {
864 x => (x + ((x * x) - 1.0).sqrt()).ln(),
868 /// Inverse hyperbolic tangent function.
875 /// let e = f64::consts::E;
876 /// let f = e.tanh().atanh();
878 /// let abs_difference = (f - e).abs();
880 /// assert!(abs_difference < 1.0e-10);
882 #[stable(feature = "rust1", since = "1.0.0")]
884 pub fn atanh(self) -> f64 {
885 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
888 // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
889 // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
891 fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
892 if !cfg!(target_os = "solaris") {
895 if self.is_finite() {
898 } else if self == 0.0 {
899 NEG_INFINITY // log(0) = -Inf
903 } else if self.is_nan() {
904 self // log(NaN) = NaN
905 } else if self > 0.0 {
906 self // log(Inf) = Inf
908 NAN // log(-Inf) = NaN
919 use num::FpCategory as Fp;
923 test_num(10f64, 2f64);
928 assert_eq!(NAN.min(2.0), 2.0);
929 assert_eq!(2.0f64.min(NAN), 2.0);
934 assert_eq!(NAN.max(2.0), 2.0);
935 assert_eq!(2.0f64.max(NAN), 2.0);
941 assert!(nan.is_nan());
942 assert!(!nan.is_infinite());
943 assert!(!nan.is_finite());
944 assert!(!nan.is_normal());
945 assert!(nan.is_sign_positive());
946 assert!(!nan.is_sign_negative());
947 assert_eq!(Fp::Nan, nan.classify());
952 let inf: f64 = INFINITY;
953 assert!(inf.is_infinite());
954 assert!(!inf.is_finite());
955 assert!(inf.is_sign_positive());
956 assert!(!inf.is_sign_negative());
957 assert!(!inf.is_nan());
958 assert!(!inf.is_normal());
959 assert_eq!(Fp::Infinite, inf.classify());
963 fn test_neg_infinity() {
964 let neg_inf: f64 = NEG_INFINITY;
965 assert!(neg_inf.is_infinite());
966 assert!(!neg_inf.is_finite());
967 assert!(!neg_inf.is_sign_positive());
968 assert!(neg_inf.is_sign_negative());
969 assert!(!neg_inf.is_nan());
970 assert!(!neg_inf.is_normal());
971 assert_eq!(Fp::Infinite, neg_inf.classify());
976 let zero: f64 = 0.0f64;
977 assert_eq!(0.0, zero);
978 assert!(!zero.is_infinite());
979 assert!(zero.is_finite());
980 assert!(zero.is_sign_positive());
981 assert!(!zero.is_sign_negative());
982 assert!(!zero.is_nan());
983 assert!(!zero.is_normal());
984 assert_eq!(Fp::Zero, zero.classify());
989 let neg_zero: f64 = -0.0;
990 assert_eq!(0.0, neg_zero);
991 assert!(!neg_zero.is_infinite());
992 assert!(neg_zero.is_finite());
993 assert!(!neg_zero.is_sign_positive());
994 assert!(neg_zero.is_sign_negative());
995 assert!(!neg_zero.is_nan());
996 assert!(!neg_zero.is_normal());
997 assert_eq!(Fp::Zero, neg_zero.classify());
1000 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1003 let one: f64 = 1.0f64;
1004 assert_eq!(1.0, one);
1005 assert!(!one.is_infinite());
1006 assert!(one.is_finite());
1007 assert!(one.is_sign_positive());
1008 assert!(!one.is_sign_negative());
1009 assert!(!one.is_nan());
1010 assert!(one.is_normal());
1011 assert_eq!(Fp::Normal, one.classify());
1017 let inf: f64 = INFINITY;
1018 let neg_inf: f64 = NEG_INFINITY;
1019 assert!(nan.is_nan());
1020 assert!(!0.0f64.is_nan());
1021 assert!(!5.3f64.is_nan());
1022 assert!(!(-10.732f64).is_nan());
1023 assert!(!inf.is_nan());
1024 assert!(!neg_inf.is_nan());
1028 fn test_is_infinite() {
1030 let inf: f64 = INFINITY;
1031 let neg_inf: f64 = NEG_INFINITY;
1032 assert!(!nan.is_infinite());
1033 assert!(inf.is_infinite());
1034 assert!(neg_inf.is_infinite());
1035 assert!(!0.0f64.is_infinite());
1036 assert!(!42.8f64.is_infinite());
1037 assert!(!(-109.2f64).is_infinite());
1041 fn test_is_finite() {
1043 let inf: f64 = INFINITY;
1044 let neg_inf: f64 = NEG_INFINITY;
1045 assert!(!nan.is_finite());
1046 assert!(!inf.is_finite());
1047 assert!(!neg_inf.is_finite());
1048 assert!(0.0f64.is_finite());
1049 assert!(42.8f64.is_finite());
1050 assert!((-109.2f64).is_finite());
1053 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1055 fn test_is_normal() {
1057 let inf: f64 = INFINITY;
1058 let neg_inf: f64 = NEG_INFINITY;
1059 let zero: f64 = 0.0f64;
1060 let neg_zero: f64 = -0.0;
1061 assert!(!nan.is_normal());
1062 assert!(!inf.is_normal());
1063 assert!(!neg_inf.is_normal());
1064 assert!(!zero.is_normal());
1065 assert!(!neg_zero.is_normal());
1066 assert!(1f64.is_normal());
1067 assert!(1e-307f64.is_normal());
1068 assert!(!1e-308f64.is_normal());
1071 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
1073 fn test_classify() {
1075 let inf: f64 = INFINITY;
1076 let neg_inf: f64 = NEG_INFINITY;
1077 let zero: f64 = 0.0f64;
1078 let neg_zero: f64 = -0.0;
1079 assert_eq!(nan.classify(), Fp::Nan);
1080 assert_eq!(inf.classify(), Fp::Infinite);
1081 assert_eq!(neg_inf.classify(), Fp::Infinite);
1082 assert_eq!(zero.classify(), Fp::Zero);
1083 assert_eq!(neg_zero.classify(), Fp::Zero);
1084 assert_eq!(1e-307f64.classify(), Fp::Normal);
1085 assert_eq!(1e-308f64.classify(), Fp::Subnormal);
1090 assert_approx_eq!(1.0f64.floor(), 1.0f64);
1091 assert_approx_eq!(1.3f64.floor(), 1.0f64);
1092 assert_approx_eq!(1.5f64.floor(), 1.0f64);
1093 assert_approx_eq!(1.7f64.floor(), 1.0f64);
1094 assert_approx_eq!(0.0f64.floor(), 0.0f64);
1095 assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1096 assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1097 assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1098 assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1099 assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1104 assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1105 assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1106 assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1107 assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1108 assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1109 assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1110 assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1111 assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1112 assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1113 assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1118 assert_approx_eq!(1.0f64.round(), 1.0f64);
1119 assert_approx_eq!(1.3f64.round(), 1.0f64);
1120 assert_approx_eq!(1.5f64.round(), 2.0f64);
1121 assert_approx_eq!(1.7f64.round(), 2.0f64);
1122 assert_approx_eq!(0.0f64.round(), 0.0f64);
1123 assert_approx_eq!((-0.0f64).round(), -0.0f64);
1124 assert_approx_eq!((-1.0f64).round(), -1.0f64);
1125 assert_approx_eq!((-1.3f64).round(), -1.0f64);
1126 assert_approx_eq!((-1.5f64).round(), -2.0f64);
1127 assert_approx_eq!((-1.7f64).round(), -2.0f64);
1132 assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1133 assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1134 assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1135 assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1136 assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1137 assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1138 assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1139 assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1140 assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1141 assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1146 assert_approx_eq!(1.0f64.fract(), 0.0f64);
1147 assert_approx_eq!(1.3f64.fract(), 0.3f64);
1148 assert_approx_eq!(1.5f64.fract(), 0.5f64);
1149 assert_approx_eq!(1.7f64.fract(), 0.7f64);
1150 assert_approx_eq!(0.0f64.fract(), 0.0f64);
1151 assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1152 assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1153 assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1154 assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1155 assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1160 assert_eq!(INFINITY.abs(), INFINITY);
1161 assert_eq!(1f64.abs(), 1f64);
1162 assert_eq!(0f64.abs(), 0f64);
1163 assert_eq!((-0f64).abs(), 0f64);
1164 assert_eq!((-1f64).abs(), 1f64);
1165 assert_eq!(NEG_INFINITY.abs(), INFINITY);
1166 assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1167 assert!(NAN.abs().is_nan());
1172 assert_eq!(INFINITY.signum(), 1f64);
1173 assert_eq!(1f64.signum(), 1f64);
1174 assert_eq!(0f64.signum(), 1f64);
1175 assert_eq!((-0f64).signum(), -1f64);
1176 assert_eq!((-1f64).signum(), -1f64);
1177 assert_eq!(NEG_INFINITY.signum(), -1f64);
1178 assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1179 assert!(NAN.signum().is_nan());
1183 fn test_is_sign_positive() {
1184 assert!(INFINITY.is_sign_positive());
1185 assert!(1f64.is_sign_positive());
1186 assert!(0f64.is_sign_positive());
1187 assert!(!(-0f64).is_sign_positive());
1188 assert!(!(-1f64).is_sign_positive());
1189 assert!(!NEG_INFINITY.is_sign_positive());
1190 assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1191 assert!(NAN.is_sign_positive());
1192 assert!(!(-NAN).is_sign_positive());
1196 fn test_is_sign_negative() {
1197 assert!(!INFINITY.is_sign_negative());
1198 assert!(!1f64.is_sign_negative());
1199 assert!(!0f64.is_sign_negative());
1200 assert!((-0f64).is_sign_negative());
1201 assert!((-1f64).is_sign_negative());
1202 assert!(NEG_INFINITY.is_sign_negative());
1203 assert!((1f64/NEG_INFINITY).is_sign_negative());
1204 assert!(!NAN.is_sign_negative());
1205 assert!((-NAN).is_sign_negative());
1211 let inf: f64 = INFINITY;
1212 let neg_inf: f64 = NEG_INFINITY;
1213 assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
1214 assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
1215 assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
1216 assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
1217 assert!(nan.mul_add(7.8, 9.0).is_nan());
1218 assert_eq!(inf.mul_add(7.8, 9.0), inf);
1219 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1220 assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
1221 assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
1227 let inf: f64 = INFINITY;
1228 let neg_inf: f64 = NEG_INFINITY;
1229 assert_eq!(1.0f64.recip(), 1.0);
1230 assert_eq!(2.0f64.recip(), 0.5);
1231 assert_eq!((-0.4f64).recip(), -2.5);
1232 assert_eq!(0.0f64.recip(), inf);
1233 assert!(nan.recip().is_nan());
1234 assert_eq!(inf.recip(), 0.0);
1235 assert_eq!(neg_inf.recip(), 0.0);
1241 let inf: f64 = INFINITY;
1242 let neg_inf: f64 = NEG_INFINITY;
1243 assert_eq!(1.0f64.powi(1), 1.0);
1244 assert_approx_eq!((-3.1f64).powi(2), 9.61);
1245 assert_approx_eq!(5.9f64.powi(-2), 0.028727);
1246 assert_eq!(8.3f64.powi(0), 1.0);
1247 assert!(nan.powi(2).is_nan());
1248 assert_eq!(inf.powi(3), inf);
1249 assert_eq!(neg_inf.powi(2), inf);
1255 let inf: f64 = INFINITY;
1256 let neg_inf: f64 = NEG_INFINITY;
1257 assert_eq!(1.0f64.powf(1.0), 1.0);
1258 assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
1259 assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
1260 assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
1261 assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
1262 assert_eq!(8.3f64.powf(0.0), 1.0);
1263 assert!(nan.powf(2.0).is_nan());
1264 assert_eq!(inf.powf(2.0), inf);
1265 assert_eq!(neg_inf.powf(3.0), neg_inf);
1269 fn test_sqrt_domain() {
1270 assert!(NAN.sqrt().is_nan());
1271 assert!(NEG_INFINITY.sqrt().is_nan());
1272 assert!((-1.0f64).sqrt().is_nan());
1273 assert_eq!((-0.0f64).sqrt(), -0.0);
1274 assert_eq!(0.0f64.sqrt(), 0.0);
1275 assert_eq!(1.0f64.sqrt(), 1.0);
1276 assert_eq!(INFINITY.sqrt(), INFINITY);
1281 assert_eq!(1.0, 0.0f64.exp());
1282 assert_approx_eq!(2.718282, 1.0f64.exp());
1283 assert_approx_eq!(148.413159, 5.0f64.exp());
1285 let inf: f64 = INFINITY;
1286 let neg_inf: f64 = NEG_INFINITY;
1288 assert_eq!(inf, inf.exp());
1289 assert_eq!(0.0, neg_inf.exp());
1290 assert!(nan.exp().is_nan());
1295 assert_eq!(32.0, 5.0f64.exp2());
1296 assert_eq!(1.0, 0.0f64.exp2());
1298 let inf: f64 = INFINITY;
1299 let neg_inf: f64 = NEG_INFINITY;
1301 assert_eq!(inf, inf.exp2());
1302 assert_eq!(0.0, neg_inf.exp2());
1303 assert!(nan.exp2().is_nan());
1309 let inf: f64 = INFINITY;
1310 let neg_inf: f64 = NEG_INFINITY;
1311 assert_approx_eq!(1.0f64.exp().ln(), 1.0);
1312 assert!(nan.ln().is_nan());
1313 assert_eq!(inf.ln(), inf);
1314 assert!(neg_inf.ln().is_nan());
1315 assert!((-2.3f64).ln().is_nan());
1316 assert_eq!((-0.0f64).ln(), neg_inf);
1317 assert_eq!(0.0f64.ln(), neg_inf);
1318 assert_approx_eq!(4.0f64.ln(), 1.386294);
1324 let inf: f64 = INFINITY;
1325 let neg_inf: f64 = NEG_INFINITY;
1326 assert_eq!(10.0f64.log(10.0), 1.0);
1327 assert_approx_eq!(2.3f64.log(3.5), 0.664858);
1328 assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
1329 assert!(1.0f64.log(1.0).is_nan());
1330 assert!(1.0f64.log(-13.9).is_nan());
1331 assert!(nan.log(2.3).is_nan());
1332 assert_eq!(inf.log(10.0), inf);
1333 assert!(neg_inf.log(8.8).is_nan());
1334 assert!((-2.3f64).log(0.1).is_nan());
1335 assert_eq!((-0.0f64).log(2.0), neg_inf);
1336 assert_eq!(0.0f64.log(7.0), neg_inf);
1342 let inf: f64 = INFINITY;
1343 let neg_inf: f64 = NEG_INFINITY;
1344 assert_approx_eq!(10.0f64.log2(), 3.321928);
1345 assert_approx_eq!(2.3f64.log2(), 1.201634);
1346 assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
1347 assert!(nan.log2().is_nan());
1348 assert_eq!(inf.log2(), inf);
1349 assert!(neg_inf.log2().is_nan());
1350 assert!((-2.3f64).log2().is_nan());
1351 assert_eq!((-0.0f64).log2(), neg_inf);
1352 assert_eq!(0.0f64.log2(), neg_inf);
1358 let inf: f64 = INFINITY;
1359 let neg_inf: f64 = NEG_INFINITY;
1360 assert_eq!(10.0f64.log10(), 1.0);
1361 assert_approx_eq!(2.3f64.log10(), 0.361728);
1362 assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
1363 assert_eq!(1.0f64.log10(), 0.0);
1364 assert!(nan.log10().is_nan());
1365 assert_eq!(inf.log10(), inf);
1366 assert!(neg_inf.log10().is_nan());
1367 assert!((-2.3f64).log10().is_nan());
1368 assert_eq!((-0.0f64).log10(), neg_inf);
1369 assert_eq!(0.0f64.log10(), neg_inf);
1373 fn test_to_degrees() {
1374 let pi: f64 = consts::PI;
1376 let inf: f64 = INFINITY;
1377 let neg_inf: f64 = NEG_INFINITY;
1378 assert_eq!(0.0f64.to_degrees(), 0.0);
1379 assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
1380 assert_eq!(pi.to_degrees(), 180.0);
1381 assert!(nan.to_degrees().is_nan());
1382 assert_eq!(inf.to_degrees(), inf);
1383 assert_eq!(neg_inf.to_degrees(), neg_inf);
1387 fn test_to_radians() {
1388 let pi: f64 = consts::PI;
1390 let inf: f64 = INFINITY;
1391 let neg_inf: f64 = NEG_INFINITY;
1392 assert_eq!(0.0f64.to_radians(), 0.0);
1393 assert_approx_eq!(154.6f64.to_radians(), 2.698279);
1394 assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
1395 assert_eq!(180.0f64.to_radians(), pi);
1396 assert!(nan.to_radians().is_nan());
1397 assert_eq!(inf.to_radians(), inf);
1398 assert_eq!(neg_inf.to_radians(), neg_inf);
1403 assert_eq!(0.0f64.asinh(), 0.0f64);
1404 assert_eq!((-0.0f64).asinh(), -0.0f64);
1406 let inf: f64 = INFINITY;
1407 let neg_inf: f64 = NEG_INFINITY;
1409 assert_eq!(inf.asinh(), inf);
1410 assert_eq!(neg_inf.asinh(), neg_inf);
1411 assert!(nan.asinh().is_nan());
1412 assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1413 assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1418 assert_eq!(1.0f64.acosh(), 0.0f64);
1419 assert!(0.999f64.acosh().is_nan());
1421 let inf: f64 = INFINITY;
1422 let neg_inf: f64 = NEG_INFINITY;
1424 assert_eq!(inf.acosh(), inf);
1425 assert!(neg_inf.acosh().is_nan());
1426 assert!(nan.acosh().is_nan());
1427 assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1428 assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1433 assert_eq!(0.0f64.atanh(), 0.0f64);
1434 assert_eq!((-0.0f64).atanh(), -0.0f64);
1436 let inf: f64 = INFINITY;
1437 let neg_inf: f64 = NEG_INFINITY;
1439 assert_eq!(1.0f64.atanh(), inf);
1440 assert_eq!((-1.0f64).atanh(), neg_inf);
1441 assert!(2f64.atanh().atanh().is_nan());
1442 assert!((-2f64).atanh().atanh().is_nan());
1443 assert!(inf.atanh().is_nan());
1444 assert!(neg_inf.atanh().is_nan());
1445 assert!(nan.atanh().is_nan());
1446 assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1447 assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1451 fn test_real_consts() {
1453 let pi: f64 = consts::PI;
1454 let frac_pi_2: f64 = consts::FRAC_PI_2;
1455 let frac_pi_3: f64 = consts::FRAC_PI_3;
1456 let frac_pi_4: f64 = consts::FRAC_PI_4;
1457 let frac_pi_6: f64 = consts::FRAC_PI_6;
1458 let frac_pi_8: f64 = consts::FRAC_PI_8;
1459 let frac_1_pi: f64 = consts::FRAC_1_PI;
1460 let frac_2_pi: f64 = consts::FRAC_2_PI;
1461 let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
1462 let sqrt2: f64 = consts::SQRT_2;
1463 let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
1464 let e: f64 = consts::E;
1465 let log2_e: f64 = consts::LOG2_E;
1466 let log10_e: f64 = consts::LOG10_E;
1467 let ln_2: f64 = consts::LN_2;
1468 let ln_10: f64 = consts::LN_10;
1470 assert_approx_eq!(frac_pi_2, pi / 2f64);
1471 assert_approx_eq!(frac_pi_3, pi / 3f64);
1472 assert_approx_eq!(frac_pi_4, pi / 4f64);
1473 assert_approx_eq!(frac_pi_6, pi / 6f64);
1474 assert_approx_eq!(frac_pi_8, pi / 8f64);
1475 assert_approx_eq!(frac_1_pi, 1f64 / pi);
1476 assert_approx_eq!(frac_2_pi, 2f64 / pi);
1477 assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
1478 assert_approx_eq!(sqrt2, 2f64.sqrt());
1479 assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
1480 assert_approx_eq!(log2_e, e.log2());
1481 assert_approx_eq!(log10_e, e.log10());
1482 assert_approx_eq!(ln_2, 2f64.ln());
1483 assert_approx_eq!(ln_10, 10f64.ln());
1487 fn test_float_bits_conv() {
1488 assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
1489 assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
1490 assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
1491 assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
1492 assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
1493 assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
1494 assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
1495 assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1497 // Check that NaNs roundtrip their bits regardless of signalingness
1498 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1499 let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1500 let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1501 assert!(f64::from_bits(masked_nan1).is_nan());
1502 assert!(f64::from_bits(masked_nan2).is_nan());
1504 assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1505 assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);