aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/f64.rs
diff options
context:
space:
mode:
authorFenrir <[email protected]>2018-01-21 14:06:28 -0700
committerFenrirWolf <[email protected]>2018-01-21 19:16:33 -0700
commit23be3f4885688e5e0011005e2295c75168854c0a (patch)
treedd0850f9c73c489e114a761d5c0757f3dbec3a65 /ctr-std/src/f64.rs
parentUpdate CI for Rust nightly-2017-12-01 + other fixes (diff)
downloadarchived-ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.tar.xz
archived-ctru-rs-23be3f4885688e5e0011005e2295c75168854c0a.zip
Recreate ctr-std from latest nightly
Diffstat (limited to 'ctr-std/src/f64.rs')
-rw-r--r--ctr-std/src/f64.rs347
1 files changed, 118 insertions, 229 deletions
diff --git a/ctr-std/src/f64.rs b/ctr-std/src/f64.rs
index 55b85cd..e4eea74 100644
--- a/ctr-std/src/f64.rs
+++ b/ctr-std/src/f64.rs
@@ -8,9 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//! The 64-bit floating point type.
+//! This module provides constants which are specific to the implementation
+//! of the `f64` floating point data type.
//!
-//! *[See also the `f64` primitive type](../primitive.f64.html).*
+//! Mathematically significant numbers are provided in the `consts` sub-module.
+//!
+//! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
#![stable(feature = "rust1", since = "1.0.0")]
#![allow(missing_docs)]
@@ -20,9 +23,9 @@ use core::num;
#[cfg(not(test))]
use intrinsics;
#[cfg(not(test))]
-use libc::c_int;
-#[cfg(not(test))]
use num::FpCategory;
+#[cfg(not(test))]
+use sys::cmath;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
@@ -35,55 +38,6 @@ pub use core::f64::{MIN, MIN_POSITIVE, MAX};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::f64::consts;
-#[allow(dead_code)]
-mod cmath {
- use libc::{c_double, c_int};
-
- #[link_name = "m"]
- extern {
- pub fn acos(n: c_double) -> c_double;
- pub fn asin(n: c_double) -> c_double;
- pub fn atan(n: c_double) -> c_double;
- pub fn atan2(a: c_double, b: c_double) -> c_double;
- pub fn cbrt(n: c_double) -> c_double;
- pub fn cosh(n: c_double) -> c_double;
- pub fn erf(n: c_double) -> c_double;
- pub fn erfc(n: c_double) -> c_double;
- pub fn expm1(n: c_double) -> c_double;
- pub fn fdim(a: c_double, b: c_double) -> c_double;
- pub fn fmax(a: c_double, b: c_double) -> c_double;
- pub fn fmin(a: c_double, b: c_double) -> c_double;
- pub fn fmod(a: c_double, b: c_double) -> c_double;
- pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
- pub fn ilogb(n: c_double) -> c_int;
- pub fn ldexp(x: c_double, n: c_int) -> c_double;
- pub fn logb(n: c_double) -> c_double;
- pub fn log1p(n: c_double) -> c_double;
- pub fn nextafter(x: c_double, y: c_double) -> c_double;
- pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
- pub fn sinh(n: c_double) -> c_double;
- pub fn tan(n: c_double) -> c_double;
- pub fn tanh(n: c_double) -> c_double;
- pub fn tgamma(n: c_double) -> c_double;
-
- // These are commonly only available for doubles
-
- pub fn j0(n: c_double) -> c_double;
- pub fn j1(n: c_double) -> c_double;
- pub fn jn(i: c_int, n: c_double) -> c_double;
-
- pub fn y0(n: c_double) -> c_double;
- pub fn y1(n: c_double) -> c_double;
- pub fn yn(i: c_int, n: c_double) -> c_double;
-
- #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
- pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
-
- #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
- pub fn hypot(x: c_double, y: c_double) -> c_double;
- }
-}
-
#[cfg(not(test))]
#[lang = "f64"]
impl f64 {
@@ -303,21 +257,15 @@ impl f64 {
#[inline]
pub fn signum(self) -> f64 { num::Float::signum(self) }
- /// Returns `true` if `self`'s sign bit is positive, including
- /// `+0.0` and `INFINITY`.
+ /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
+ /// positive sign bit and positive infinity.
///
/// ```
- /// use std::f64;
- ///
- /// let nan: f64 = f64::NAN;
- ///
/// let f = 7.0_f64;
/// let g = -7.0_f64;
///
/// assert!(f.is_sign_positive());
/// assert!(!g.is_sign_positive());
- /// // Requires both tests to determine if is `NaN`
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -328,21 +276,15 @@ impl f64 {
#[inline]
pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
- /// Returns `true` if `self`'s sign is negative, including `-0.0`
- /// and `NEG_INFINITY`.
+ /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
+ /// negative sign bit and negative infinity.
///
/// ```
- /// use std::f64;
- ///
- /// let nan = f64::NAN;
- ///
/// let f = 7.0_f64;
/// let g = -7.0_f64;
///
/// assert!(!f.is_sign_negative());
/// assert!(g.is_sign_negative());
- /// // Requires both tests to determine if is `NaN`.
- /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -490,18 +432,17 @@ impl f64 {
/// Returns the logarithm of the number with respect to an arbitrary base.
///
- /// ```
- /// let ten = 10.0_f64;
- /// let two = 2.0_f64;
+ /// The result may not be correctly rounded owing to implementation details;
+ /// `self.log2()` can produce more accurate results for base 2, and
+ /// `self.log10()` can produce more accurate results for base 10.
///
- /// // log10(10) - 1 == 0
- /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
+ /// ```
+ /// let five = 5.0_f64;
///
- /// // log2(2) - 1 == 0
- /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
+ /// // log5(5) - 1 == 0
+ /// let abs_difference = (five.log(5.0) - 1.0).abs();
///
- /// assert!(abs_difference_10 < 1e-10);
- /// assert!(abs_difference_2 < 1e-10);
+ /// assert!(abs_difference < 1e-10);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -574,84 +515,6 @@ impl f64 {
#[inline]
pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
- /// Constructs a floating point number of `x*2^exp`.
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// // 3*2^2 - 12 == 0
- /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
- ///
- /// assert!(abs_difference < 1e-10);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "pending integer conventions",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn ldexp(x: f64, exp: isize) -> f64 {
- unsafe { cmath::ldexp(x, exp as c_int) }
- }
-
- /// Breaks the number into a normalized fraction and a base-2 exponent,
- /// satisfying:
- ///
- /// * `self = x * 2^exp`
- /// * `0.5 <= abs(x) < 1.0`
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// let x = 4.0_f64;
- ///
- /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
- /// let f = x.frexp();
- /// let abs_difference_0 = (f.0 - 0.5).abs();
- /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
- ///
- /// assert!(abs_difference_0 < 1e-10);
- /// assert!(abs_difference_1 < 1e-10);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "pending integer conventions",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn frexp(self) -> (f64, isize) {
- unsafe {
- let mut exp = 0;
- let x = cmath::frexp(self, &mut exp);
- (x, exp as isize)
- }
- }
-
- /// Returns the next representable floating-point value in the direction of
- /// `other`.
- ///
- /// ```
- /// #![feature(float_extras)]
- ///
- /// let x = 1.0f64;
- ///
- /// let abs_diff = (x.next_after(2.0) - 1.0000000000000002220446049250313_f64).abs();
- ///
- /// assert!(abs_diff < 1e-10);
- /// ```
- #[unstable(feature = "float_extras",
- reason = "unsure about its place in the world",
- issue = "27752")]
- #[rustc_deprecated(since = "1.11.0",
- reason = "never really came to fruition and easily \
- implementable outside the standard library")]
- #[inline]
- pub fn next_after(self, other: f64) -> f64 {
- unsafe { cmath::nextafter(self, other) }
- }
-
/// Returns the maximum of the two numbers.
///
/// ```
@@ -665,7 +528,7 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn max(self, other: f64) -> f64 {
- unsafe { cmath::fmax(self, other) }
+ num::Float::max(self, other)
}
/// Returns the minimum of the two numbers.
@@ -681,7 +544,7 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn min(self, other: f64) -> f64 {
- unsafe { cmath::fmin(self, other) }
+ num::Float::min(self, other)
}
/// The positive difference of two numbers.
@@ -1086,6 +949,74 @@ impl f64 {
}
}
}
+
+ /// Raw transmutation to `u64`.
+ ///
+ /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
+ ///
+ /// See `from_bits` for some discussion of the portability of this operation
+ /// (there are almost no issues).
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
+ /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
+ ///
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn to_bits(self) -> u64 {
+ unsafe { ::mem::transmute(self) }
+ }
+
+ /// Raw transmutation from `u64`.
+ ///
+ /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
+ /// It turns out this is incredibly portable, for two reasons:
+ ///
+ /// * Floats and Ints have the same endianess on all supported platforms.
+ /// * IEEE-754 very precisely specifies the bit layout of floats.
+ ///
+ /// However there is one caveat: prior to the 2008 version of IEEE-754, how
+ /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
+ /// (notably x86 and ARM) picked the interpretation that was ultimately
+ /// standardized in 2008, but some didn't (notably MIPS). As a result, all
+ /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
+ ///
+ /// Rather than trying to preserve signaling-ness cross-platform, this
+ /// implementation favours preserving the exact bits. This means that
+ /// any payloads encoded in NaNs will be preserved even if the result of
+ /// this method is sent over the network from an x86 machine to a MIPS one.
+ ///
+ /// If the results of this method are only manipulated by the same
+ /// architecture that produced them, then there is no portability concern.
+ ///
+ /// If the input isn't NaN, then there is no portability concern.
+ ///
+ /// If you don't care about signalingness (very likely), then there is no
+ /// portability concern.
+ ///
+ /// Note that this function is distinct from `as` casting, which attempts to
+ /// preserve the *numeric* value, and not the bitwise value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::f64;
+ /// let v = f64::from_bits(0x4029000000000000);
+ /// let difference = (v - 12.5).abs();
+ /// assert!(difference <= 1e-5);
+ /// ```
+ #[stable(feature = "float_bits_conv", since = "1.20.0")]
+ #[inline]
+ pub fn from_bits(v: u64) -> Self {
+ // It turns out the safety issues with sNaN were overblown! Hooray!
+ unsafe { ::mem::transmute(v) }
+ }
}
#[cfg(test)]
@@ -1119,7 +1050,7 @@ mod tests {
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(!nan.is_normal());
- assert!(!nan.is_sign_positive());
+ assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify());
}
@@ -1174,6 +1105,7 @@ mod tests {
assert_eq!(Fp::Zero, neg_zero.classify());
}
+ #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
#[test]
fn test_one() {
let one: f64 = 1.0f64;
@@ -1226,6 +1158,7 @@ mod tests {
assert!((-109.2f64).is_finite());
}
+ #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
#[test]
fn test_is_normal() {
let nan: f64 = NAN;
@@ -1243,6 +1176,7 @@ mod tests {
assert!(!1e-308f64.is_normal());
}
+ #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
#[test]
fn test_classify() {
let nan: f64 = NAN;
@@ -1260,23 +1194,6 @@ mod tests {
}
#[test]
- #[allow(deprecated)]
- fn test_integer_decode() {
- assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
- assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
- assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
- assert_eq!(0f64.integer_decode(), (0, -1075, 1));
- assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
- assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
- assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
-
- // Ignore the "sign" (quiet / signalling flag) of NAN.
- // It can vary between runtime operations and LLVM folding.
- let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
- assert_eq!((nan_m, nan_e), (6755399441055744, 972));
- }
-
- #[test]
fn test_floor() {
assert_approx_eq!(1.0f64.floor(), 1.0f64);
assert_approx_eq!(1.3f64.floor(), 1.0f64);
@@ -1379,7 +1296,8 @@ mod tests {
assert!(!(-1f64).is_sign_positive());
assert!(!NEG_INFINITY.is_sign_positive());
assert!(!(1f64/NEG_INFINITY).is_sign_positive());
- assert!(!NAN.is_sign_positive());
+ assert!(NAN.is_sign_positive());
+ assert!(!(-NAN).is_sign_positive());
}
#[test]
@@ -1392,6 +1310,7 @@ mod tests {
assert!(NEG_INFINITY.is_sign_negative());
assert!((1f64/NEG_INFINITY).is_sign_negative());
assert!(!NAN.is_sign_negative());
+ assert!((-NAN).is_sign_negative());
}
#[test]
@@ -1588,58 +1507,6 @@ mod tests {
}
#[test]
- #[allow(deprecated)]
- fn test_ldexp() {
- let f1 = 2.0f64.powi(-123);
- let f2 = 2.0f64.powi(-111);
- let f3 = 1.75 * 2.0f64.powi(-12);
- assert_eq!(f64::ldexp(1f64, -123), f1);
- assert_eq!(f64::ldexp(1f64, -111), f2);
- assert_eq!(f64::ldexp(1.75f64, -12), f3);
-
- assert_eq!(f64::ldexp(0f64, -123), 0f64);
- assert_eq!(f64::ldexp(-0f64, -123), -0f64);
-
- let inf: f64 = INFINITY;
- let neg_inf: f64 = NEG_INFINITY;
- let nan: f64 = NAN;
- assert_eq!(f64::ldexp(inf, -123), inf);
- assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
- assert!(f64::ldexp(nan, -123).is_nan());
- }
-
- #[test]
- #[allow(deprecated)]
- fn test_frexp() {
- let f1 = 2.0f64.powi(-123);
- let f2 = 2.0f64.powi(-111);
- let f3 = 1.75 * 2.0f64.powi(-123);
- let (x1, exp1) = f1.frexp();
- let (x2, exp2) = f2.frexp();
- let (x3, exp3) = f3.frexp();
- assert_eq!((x1, exp1), (0.5f64, -122));
- assert_eq!((x2, exp2), (0.5f64, -110));
- assert_eq!((x3, exp3), (0.875f64, -122));
- assert_eq!(f64::ldexp(x1, exp1), f1);
- assert_eq!(f64::ldexp(x2, exp2), f2);
- assert_eq!(f64::ldexp(x3, exp3), f3);
-
- assert_eq!(0f64.frexp(), (0f64, 0));
- assert_eq!((-0f64).frexp(), (-0f64, 0));
- }
-
- #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
- #[allow(deprecated)]
- fn test_frexp_nowin() {
- let inf: f64 = INFINITY;
- let neg_inf: f64 = NEG_INFINITY;
- let nan: f64 = NAN;
- assert_eq!(match inf.frexp() { (x, _) => x }, inf);
- assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
- assert!(match nan.frexp() { (x, _) => x.is_nan() })
- }
-
- #[test]
fn test_asinh() {
assert_eq!(0.0f64.asinh(), 0.0f64);
assert_eq!((-0.0f64).asinh(), -0.0f64);
@@ -1723,4 +1590,26 @@ mod tests {
assert_approx_eq!(ln_2, 2f64.ln());
assert_approx_eq!(ln_10, 10f64.ln());
}
+
+ #[test]
+ fn test_float_bits_conv() {
+ assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
+ assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
+ assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
+ assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
+ assert_approx_eq!(f64::from_bits(0x3ff0000000000000), 1.0);
+ assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
+ assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
+ assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
+
+ // Check that NaNs roundtrip their bits regardless of signalingness
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
+ let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
+ let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
+ assert!(f64::from_bits(masked_nan1).is_nan());
+ assert!(f64::from_bits(masked_nan2).is_nan());
+
+ assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
+ assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
+ }
}