auto merge of #10136 : hatahet/rust/master, r=alexcrichton

Fixes #10077
This commit is contained in:
bors 2013-10-29 12:02:59 -07:00
commit 67d7be0ff1
8 changed files with 169 additions and 175 deletions

View File

@ -491,7 +491,7 @@ types.
use std::f64;
use std::num::atan;
fn angle(vector: (f64, f64)) -> f64 {
let pi = f64::consts::pi;
let pi = f64::consts::PI;
match vector {
(0.0, y) if y < 0.0 => 1.5 * pi,
(0.0, y) => 0.5 * pi,
@ -689,7 +689,7 @@ use std::f64;
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
fn area(sh: Shape) -> f64 {
match sh {
Circle(_, size) => f64::consts::pi * size * size,
Circle(_, size) => f64::consts::PI * size * size,
Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
}
}
@ -725,7 +725,7 @@ enum Shape {
}
fn area(sh: Shape) -> f64 {
match sh {
Circle { radius: radius, _ } => f64::consts::pi * square(radius),
Circle { radius: radius, _ } => f64::consts::PI * square(radius),
Rectangle { top_left: top_left, bottom_right: bottom_right } => {
(bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
}
@ -1699,10 +1699,10 @@ impl Circle {
To call such a method, just prefix it with the type name and a double colon:
~~~~
use std::f64::consts::pi;
use std::f64::consts::PI;
struct Circle { radius: f64 }
impl Circle {
fn new(area: f64) -> Circle { Circle { radius: (area / pi).sqrt() } }
fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
}
let c = Circle::new(42.5);
~~~~
@ -1977,13 +1977,13 @@ name and a double colon. The compiler uses type inference to decide which
implementation to use.
~~~~
use std::f64::consts::pi;
use std::f64::consts::PI;
trait Shape { fn new(area: f64) -> Self; }
struct Circle { radius: f64 }
struct Square { length: f64 }
impl Shape for Circle {
fn new(area: f64) -> Circle { Circle { radius: (area / pi).sqrt() } }
fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
}
impl Shape for Square {
fn new(area: f64) -> Square { Square { length: (area).sqrt() } }
@ -2157,17 +2157,17 @@ trait Circle : Shape { fn radius(&self) -> f64; }
Now, we can implement `Circle` on a type only if we also implement `Shape`.
~~~~
use std::f64::consts::pi;
use std::f64::consts::PI;
# trait Shape { fn area(&self) -> f64; }
# trait Circle : Shape { fn radius(&self) -> f64; }
# struct Point { x: f64, y: f64 }
# fn square(x: f64) -> f64 { x * x }
struct CircleStruct { center: Point, radius: f64 }
impl Circle for CircleStruct {
fn radius(&self) -> f64 { (self.area() / pi).sqrt() }
fn radius(&self) -> f64 { (self.area() / PI).sqrt() }
}
impl Shape for CircleStruct {
fn area(&self) -> f64 { pi * square(self.radius) }
fn area(&self) -> f64 { PI * square(self.radius) }
}
~~~~
@ -2192,13 +2192,13 @@ fn radius_times_area<T: Circle>(c: T) -> f64 {
Likewise, supertrait methods may also be called on trait objects.
~~~ {.xfail-test}
use std::f64::consts::pi;
use std::f64::consts::PI;
# trait Shape { fn area(&self) -> f64; }
# trait Circle : Shape { fn radius(&self) -> f64; }
# struct Point { x: f64, y: f64 }
# struct CircleStruct { center: Point, radius: f64 }
# impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / pi).sqrt() } }
# impl Shape for CircleStruct { fn area(&self) -> f64 { pi * square(self.radius) } }
# impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / PI).sqrt() } }
# impl Shape for CircleStruct { fn area(&self) -> f64 { PI * square(self.radius) } }
let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};
let mycircle: @Circle = concrete as @Circle;

View File

@ -10,8 +10,6 @@
//! Operations and constants for `f32`
#[allow(missing_doc)];
#[allow(non_uppercase_statics)];
#[allow(non_uppercase_pattern_statics)];
use default::Default;
use libc::c_int;
@ -112,11 +110,11 @@ delegate!(
// These are not defined inside consts:: for consistency with
// the integer types
pub static NaN: f32 = 0.0_f32/0.0_f32;
pub static NAN: f32 = 0.0_f32/0.0_f32;
pub static infinity: f32 = 1.0_f32/0.0_f32;
pub static INFINITY: f32 = 1.0_f32/0.0_f32;
pub static neg_infinity: f32 = -1.0_f32/0.0_f32;
pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
// FIXME (#1999): replace the predicates below with llvm intrinsics or
// calls to the libmath macros in the rust runtime for performance.
@ -128,43 +126,43 @@ pub mod consts {
// FIXME (requires Issue #1433 to fix): replace with mathematical
// staticants from cmath.
/// Archimedes' constant
pub static pi: f32 = 3.14159265358979323846264338327950288_f32;
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
/// pi/2.0
pub static frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
/// pi/4.0
pub static frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
/// 1.0/pi
pub static frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
/// 2.0/pi
pub static frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
/// 2.0/sqrt(pi)
pub static frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
/// sqrt(2.0)
pub static sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
/// 1.0/sqrt(2.0)
pub static frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
/// Euler's number
pub static e: f32 = 2.71828182845904523536028747135266250_f32;
pub static E: f32 = 2.71828182845904523536028747135266250_f32;
/// log2(e)
pub static log2_e: f32 = 1.44269504088896340735992468100189214_f32;
pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
/// log10(e)
pub static log10_e: f32 = 0.434294481903251827651128918916605082_f32;
pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
/// ln(2.0)
pub static ln_2: f32 = 0.693147180559945309417232121458176568_f32;
pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32;
/// ln(10.0)
pub static ln_10: f32 = 2.30258509299404568401799145468436421_f32;
pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
}
impl Num for f32 {}
@ -204,7 +202,7 @@ impl Ord for f32 {
}
impl Orderable for f32 {
/// Returns `NaN` if either of the numbers are `NaN`.
/// Returns `NAN` if either of the numbers are `NAN`.
#[inline]
fn min(&self, other: &f32) -> f32 {
match () {
@ -215,7 +213,7 @@ impl Orderable for f32 {
}
}
/// Returns `NaN` if either of the numbers are `NaN`.
/// Returns `NAN` if either of the numbers are `NAN`.
#[inline]
fn max(&self, other: &f32) -> f32 {
match () {
@ -227,7 +225,7 @@ impl Orderable for f32 {
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
/// If any of the numbers are `NAN` then `NAN` is returned.
#[inline]
fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
match () {
@ -295,7 +293,7 @@ impl Neg<f32> for f32 {
}
impl Signed for f32 {
/// Computes the absolute value. Returns `NaN` if the number is `NaN`.
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f32 { abs(*self) }
@ -309,30 +307,30 @@ impl Signed for f32 {
///
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `infinity`
/// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
/// - `NaN` if the number is NaN
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
///
#[inline]
fn signum(&self) -> f32 {
if self.is_nan() { NaN } else { copysign(1.0, *self) }
if self.is_nan() { NAN } else { copysign(1.0, *self) }
}
/// Returns `true` if the number is positive, including `+0.0` and `infinity`
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Round for f32 {
/// Round half-way cases toward `neg_infinity`
/// Round half-way cases toward `NEG_INFINITY`
#[inline]
fn floor(&self) -> f32 { floor(*self) }
/// Round half-way cases toward `infinity`
/// Round half-way cases toward `INFINITY`
#[inline]
fn ceil(&self) -> f32 { ceil(*self) }
@ -449,13 +447,13 @@ impl Hyperbolic for f32 {
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
/// - `NaN` if `self` is `NaN`
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f32 {
match *self {
neg_infinity => neg_infinity,
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
@ -466,8 +464,8 @@ impl Hyperbolic for f32 {
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `infinity` if `self` is `infinity`
/// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f32 {
@ -484,10 +482,10 @@ impl Hyperbolic for f32 {
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `infinity` if `self` is `1.0`
/// - `neg_infinity` if `self` is `-1.0`
/// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `infinity` and `neg_infinity`)
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f32 {
@ -821,7 +819,7 @@ impl num::ToStrRadix for f32 {
let (r, special) = strconv::float_to_str_common(
*self, rdx, true, strconv::SignNeg, strconv::DigAll);
if special { fail!("number has a special value, \
try to_str_radix_special() if those are expected") }
try to_str_radix_special() if those are expected") }
r
}
}
@ -850,7 +848,7 @@ impl num::ToStrRadix for f32 {
///
/// # Return value
///
/// `none` if the string did not represent a valid number. Otherwise,
/// `None` if the string did not represent a valid number. Otherwise,
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
///
#[inline]
@ -884,7 +882,7 @@ impl FromStr for f32 {
///
/// # Return value
///
/// `none` if the string did not represent a valid number. Otherwise,
/// `None` if the string did not represent a valid number. Otherwise,
/// `Some(n)` where `n` is the floating-point number represented by `num`.
///
#[inline]
@ -911,7 +909,7 @@ impl num::FromStrRadix for f32 {
///
/// # Return value
///
/// `none` if the string did not represent a valid number. Otherwise,
/// `None` if the string did not represent a valid number. Otherwise,
/// `Some(n)` where `n` is the floating-point number represented by `num`.
///
#[inline]
@ -1122,14 +1120,14 @@ mod tests {
#[test]
pub fn test_abs() {
assert_eq!(infinity.abs(), infinity);
assert_eq!(INFINITY.abs(), INFINITY);
assert_eq!(1f32.abs(), 1f32);
assert_eq!(0f32.abs(), 0f32);
assert_eq!((-0f32).abs(), 0f32);
assert_eq!((-1f32).abs(), 1f32);
assert_eq!(neg_infinity.abs(), infinity);
assert_eq!((1f32/neg_infinity).abs(), 0f32);
assert!(NaN.abs().is_nan());
assert_eq!(NEG_INFINITY.abs(), INFINITY);
assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
assert!(NAN.abs().is_nan());
}
#[test]
@ -1138,52 +1136,52 @@ mod tests {
assert_eq!(1f32.abs_sub(&1f32), 0f32);
assert_eq!(1f32.abs_sub(&0f32), 1f32);
assert_eq!(1f32.abs_sub(&-1f32), 2f32);
assert_eq!(neg_infinity.abs_sub(&0f32), 0f32);
assert_eq!(infinity.abs_sub(&1f32), infinity);
assert_eq!(0f32.abs_sub(&neg_infinity), infinity);
assert_eq!(0f32.abs_sub(&infinity), 0f32);
assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
}
#[test] #[ignore(cfg(windows))] // FIXME #8663
fn test_abs_sub_nowin() {
assert!(NaN.abs_sub(&-1f32).is_nan());
assert!(1f32.abs_sub(&NaN).is_nan());
assert!(NAN.abs_sub(&-1f32).is_nan());
assert!(1f32.abs_sub(&NAN).is_nan());
}
#[test]
fn test_signum() {
assert_eq!(infinity.signum(), 1f32);
assert_eq!(INFINITY.signum(), 1f32);
assert_eq!(1f32.signum(), 1f32);
assert_eq!(0f32.signum(), 1f32);
assert_eq!((-0f32).signum(), -1f32);
assert_eq!((-1f32).signum(), -1f32);
assert_eq!(neg_infinity.signum(), -1f32);
assert_eq!((1f32/neg_infinity).signum(), -1f32);
assert!(NaN.signum().is_nan());
assert_eq!(NEG_INFINITY.signum(), -1f32);
assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
assert!(NAN.signum().is_nan());
}
#[test]
fn test_is_positive() {
assert!(infinity.is_positive());
assert!(INFINITY.is_positive());
assert!(1f32.is_positive());
assert!(0f32.is_positive());
assert!(!(-0f32).is_positive());
assert!(!(-1f32).is_positive());
assert!(!neg_infinity.is_positive());
assert!(!(1f32/neg_infinity).is_positive());
assert!(!NaN.is_positive());
assert!(!NEG_INFINITY.is_positive());
assert!(!(1f32/NEG_INFINITY).is_positive());
assert!(!NAN.is_positive());
}
#[test]
fn test_is_negative() {
assert!(!infinity.is_negative());
assert!(!INFINITY.is_negative());
assert!(!1f32.is_negative());
assert!(!0f32.is_negative());
assert!((-0f32).is_negative());
assert!((-1f32).is_negative());
assert!(neg_infinity.is_negative());
assert!((1f32/neg_infinity).is_negative());
assert!(!NaN.is_negative());
assert!(NEG_INFINITY.is_negative());
assert!((1f32/NEG_INFINITY).is_negative());
assert!(!NAN.is_negative());
}
#[test]

View File

@ -11,8 +11,6 @@
//! Operations and constants for `f64`
#[allow(missing_doc)];
#[allow(non_uppercase_statics)];
#[allow(non_uppercase_pattern_statics)];
use default::Default;
use libc::c_int;
@ -122,27 +120,27 @@ delegate!(
// These are not defined inside consts:: for consistency with
// the integer types
pub static radix: uint = 2u;
pub static RADIX: uint = 2u;
pub static mantissa_digits: uint = 53u;
pub static digits: uint = 15u;
pub static MANTISSA_DIGITS: uint = 53u;
pub static DIGITS: uint = 15u;
pub static epsilon: f64 = 2.2204460492503131e-16_f64;
pub static EPSILON: f64 = 2.2204460492503131e-16_f64;
pub static min_value: f64 = 2.2250738585072014e-308_f64;
pub static max_value: f64 = 1.7976931348623157e+308_f64;
pub static MIN_VALUE: f64 = 2.2250738585072014e-308_f64;
pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;
pub static min_exp: int = -1021;
pub static max_exp: int = 1024;
pub static MIN_EXP: int = -1021;
pub static MAX_EXP: int = 1024;
pub static min_10_exp: int = -307;
pub static max_10_exp: int = 308;
pub static MIN_10_EXP: int = -307;
pub static MAX_10_EXP: int = 308;
pub static NaN: f64 = 0.0_f64/0.0_f64;
pub static NAN: f64 = 0.0_f64/0.0_f64;
pub static infinity: f64 = 1.0_f64/0.0_f64;
pub static INFINITY: f64 = 1.0_f64/0.0_f64;
pub static neg_infinity: f64 = -1.0_f64/0.0_f64;
pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
// FIXME (#1999): add is_normal, is_subnormal, and fpclassify
@ -151,43 +149,43 @@ pub mod consts {
// FIXME (requires Issue #1433 to fix): replace with mathematical
// constants from cmath.
/// Archimedes' constant
pub static pi: f64 = 3.14159265358979323846264338327950288_f64;
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
/// pi/2.0
pub static frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
pub static FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
/// pi/4.0
pub static frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
pub static FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
/// 1.0/pi
pub static frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
pub static FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
/// 2.0/pi
pub static frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
pub static FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
/// 2.0/sqrt(pi)
pub static frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
pub static FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64;
/// sqrt(2.0)
pub static sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
pub static SQRT2: f64 = 1.41421356237309504880168872420969808_f64;
/// 1.0/sqrt(2.0)
pub static frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
pub static FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64;
/// Euler's number
pub static e: f64 = 2.71828182845904523536028747135266250_f64;
pub static E: f64 = 2.71828182845904523536028747135266250_f64;
/// log2(e)
pub static log2_e: f64 = 1.44269504088896340735992468100189214_f64;
pub static LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
/// log10(e)
pub static log10_e: f64 = 0.434294481903251827651128918916605082_f64;
pub static LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
/// ln(2.0)
pub static ln_2: f64 = 0.693147180559945309417232121458176568_f64;
pub static LN_2: f64 = 0.693147180559945309417232121458176568_f64;
/// ln(10.0)
pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64;
pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
}
impl Num for f64 {}
@ -227,7 +225,7 @@ impl Ord for f64 {
}
impl Orderable for f64 {
/// Returns `NaN` if either of the numbers are `NaN`.
/// Returns `NAN` if either of the numbers are `NAN`.
#[inline]
fn min(&self, other: &f64) -> f64 {
match () {
@ -238,7 +236,7 @@ impl Orderable for f64 {
}
}
/// Returns `NaN` if either of the numbers are `NaN`.
/// Returns `NAN` if either of the numbers are `NAN`.
#[inline]
fn max(&self, other: &f64) -> f64 {
match () {
@ -250,7 +248,7 @@ impl Orderable for f64 {
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
/// If any of the numbers are `NAN` then `NAN` is returned.
#[inline]
fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
match () {
@ -313,7 +311,7 @@ impl Neg<f64> for f64 {
}
impl Signed for f64 {
/// Computes the absolute value. Returns `NaN` if the number is `NaN`.
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f64 { abs(*self) }
@ -327,30 +325,30 @@ impl Signed for f64 {
///
/// # Returns
///
/// - `1.0` if the number is positive, `+0.0` or `infinity`
/// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
/// - `NaN` if the number is NaN
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
///
#[inline]
fn signum(&self) -> f64 {
if self.is_nan() { NaN } else { copysign(1.0, *self) }
if self.is_nan() { NAN } else { copysign(1.0, *self) }
}
/// Returns `true` if the number is positive, including `+0.0` and `infinity`
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
/// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
}
impl Round for f64 {
/// Round half-way cases toward `neg_infinity`
/// Round half-way cases toward `NEG_INFINITY`
#[inline]
fn floor(&self) -> f64 { floor(*self) }
/// Round half-way cases toward `infinity`
/// Round half-way cases toward `INFINITY`
#[inline]
fn ceil(&self) -> f64 { ceil(*self) }
@ -467,13 +465,13 @@ impl Hyperbolic for f64 {
/// # Returns
///
/// - on success, the inverse hyperbolic sine of `self` will be returned
/// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
/// - `NaN` if `self` is `NaN`
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
/// - `NAN` if `self` is `NAN`
///
#[inline]
fn asinh(&self) -> f64 {
match *self {
neg_infinity => neg_infinity,
NEG_INFINITY => NEG_INFINITY,
x => (x + ((x * x) + 1.0).sqrt()).ln(),
}
}
@ -484,8 +482,8 @@ impl Hyperbolic for f64 {
/// # Returns
///
/// - on success, the inverse hyperbolic cosine of `self` will be returned
/// - `infinity` if `self` is `infinity`
/// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
/// - `INFINITY` if `self` is `INFINITY`
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
///
#[inline]
fn acosh(&self) -> f64 {
@ -502,10 +500,10 @@ impl Hyperbolic for f64 {
///
/// - on success, the inverse hyperbolic tangent of `self` will be returned
/// - `self` if `self` is `0.0` or `-0.0`
/// - `infinity` if `self` is `1.0`
/// - `neg_infinity` if `self` is `-1.0`
/// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `infinity` and `neg_infinity`)
/// - `INFINITY` if `self` is `1.0`
/// - `NEG_INFINITY` if `self` is `-1.0`
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
/// (including `INFINITY` and `NEG_INFINITY`)
///
#[inline]
fn atanh(&self) -> f64 {
@ -861,7 +859,7 @@ impl num::ToStrRadix for f64 {
///
/// # Failure
///
/// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
/// Fails if called on a special value like `inf`, `-inf` or `NAN` due to
/// possible misinterpretation of the result at higher bases. If those values
/// are expected, use `to_str_radix_special()` instead.
#[inline]
@ -898,7 +896,7 @@ impl num::ToStrRadix for f64 {
///
/// # Return value
///
/// `none` if the string did not represent a valid number. Otherwise,
/// `None` if the string did not represent a valid number. Otherwise,
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
///
#[inline]
@ -959,7 +957,7 @@ impl num::FromStrRadix for f64 {
///
/// # Return value
///
/// `none` if the string did not represent a valid number. Otherwise,
/// `None` if the string did not represent a valid number. Otherwise,
/// `Some(n)` where `n` is the floating-point number represented by `num`.
///
#[inline]
@ -1173,14 +1171,14 @@ mod tests {
#[test]
pub fn test_abs() {
assert_eq!(infinity.abs(), infinity);
assert_eq!(INFINITY.abs(), INFINITY);
assert_eq!(1f64.abs(), 1f64);
assert_eq!(0f64.abs(), 0f64);
assert_eq!((-0f64).abs(), 0f64);
assert_eq!((-1f64).abs(), 1f64);
assert_eq!(neg_infinity.abs(), infinity);
assert_eq!((1f64/neg_infinity).abs(), 0f64);
assert!(NaN.abs().is_nan());
assert_eq!(NEG_INFINITY.abs(), INFINITY);
assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
assert!(NAN.abs().is_nan());
}
#[test]
@ -1189,52 +1187,52 @@ mod tests {
assert_eq!(1f64.abs_sub(&1f64), 0f64);
assert_eq!(1f64.abs_sub(&0f64), 1f64);
assert_eq!(1f64.abs_sub(&-1f64), 2f64);
assert_eq!(neg_infinity.abs_sub(&0f64), 0f64);
assert_eq!(infinity.abs_sub(&1f64), infinity);
assert_eq!(0f64.abs_sub(&neg_infinity), infinity);
assert_eq!(0f64.abs_sub(&infinity), 0f64);
assert_eq!(NEG_INFINITY.abs_sub(&0f64), 0f64);
assert_eq!(INFINITY.abs_sub(&1f64), INFINITY);
assert_eq!(0f64.abs_sub(&NEG_INFINITY), INFINITY);
assert_eq!(0f64.abs_sub(&INFINITY), 0f64);
}
#[test] #[ignore(cfg(windows))] // FIXME #8663
fn test_abs_sub_nowin() {
assert!(NaN.abs_sub(&-1f64).is_nan());
assert!(1f64.abs_sub(&NaN).is_nan());
assert!(NAN.abs_sub(&-1f64).is_nan());
assert!(1f64.abs_sub(&NAN).is_nan());
}
#[test]
fn test_signum() {
assert_eq!(infinity.signum(), 1f64);
assert_eq!(INFINITY.signum(), 1f64);
assert_eq!(1f64.signum(), 1f64);
assert_eq!(0f64.signum(), 1f64);
assert_eq!((-0f64).signum(), -1f64);
assert_eq!((-1f64).signum(), -1f64);
assert_eq!(neg_infinity.signum(), -1f64);
assert_eq!((1f64/neg_infinity).signum(), -1f64);
assert!(NaN.signum().is_nan());
assert_eq!(NEG_INFINITY.signum(), -1f64);
assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
assert!(NAN.signum().is_nan());
}
#[test]
fn test_is_positive() {
assert!(infinity.is_positive());
assert!(INFINITY.is_positive());
assert!(1f64.is_positive());
assert!(0f64.is_positive());
assert!(!(-0f64).is_positive());
assert!(!(-1f64).is_positive());
assert!(!neg_infinity.is_positive());
assert!(!(1f64/neg_infinity).is_positive());
assert!(!NaN.is_positive());
assert!(!NEG_INFINITY.is_positive());
assert!(!(1f64/NEG_INFINITY).is_positive());
assert!(!NAN.is_positive());
}
#[test]
fn test_is_negative() {
assert!(!infinity.is_negative());
assert!(!INFINITY.is_negative());
assert!(!1f64.is_negative());
assert!(!0f64.is_negative());
assert!((-0f64).is_negative());
assert!((-1f64).is_negative());
assert!(neg_infinity.is_negative());
assert!((1f64/neg_infinity).is_negative());
assert!(!NaN.is_negative());
assert!(NEG_INFINITY.is_negative());
assert!((1f64/NEG_INFINITY).is_negative());
assert!(!NAN.is_negative());
}
#[test]

View File

@ -86,9 +86,9 @@ pub trait Signed: Num
/// Returns the sign of the number.
///
/// For float, f32, f64:
/// - `1.0` if the number is positive, `+0.0` or `infinity`
/// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
/// - `NaN` if the number is `NaN`
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is `NAN`
///
/// For int:
/// - `0` if the number is zero

View File

@ -16,7 +16,7 @@ fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
fn random_gradient<R:Rng>(r: &mut R) -> Vec2 {
let v = 2.0 * f64::consts::pi * r.gen();
let v = 2.0 * f64::consts::PI * r.gen();
Vec2 {
x: v.cos() as f32,
y: v.sin() as f32,

View File

@ -1,18 +1,16 @@
#[allow(non_uppercase_pattern_statics)];
// Matching against NaN should result in a warning
use std::f64::NaN;
use std::f64::NAN;
fn main() {
let x = NaN;
let x = NAN;
match x {
NaN => {},
NAN => {},
_ => {},
};
//~^^^ WARNING unmatchable NaN in pattern, use the is_nan method in a guard instead
match [x, 1.0] {
[NaN, _] => {},
[NAN, _] => {},
_ => {},
};
//~^^^ WARNING unmatchable NaN in pattern, use the is_nan method in a guard instead

View File

@ -61,31 +61,31 @@ pub fn main() {
assert!((powif64(23.2f64, 2i32).approx_eq(&538.24f64)));
assert!((sinf32(0f32).approx_eq(&0f32)));
assert!((sinf64(f64::consts::pi / 2f64).approx_eq(&1f64)));
assert!((sinf64(f64::consts::PI / 2f64).approx_eq(&1f64)));
assert!((cosf32(0f32).approx_eq(&1f32)));
assert!((cosf64(f64::consts::pi * 2f64).approx_eq(&1f64)));
assert!((cosf64(f64::consts::PI * 2f64).approx_eq(&1f64)));
assert!((powf32(25f32, -2f32).approx_eq(&0.0016f32)));
assert!((powf64(400f64, 0.5f64).approx_eq(&20f64)));
assert!((fabsf32(expf32(1f32) - f32::consts::e).approx_eq(&0f32)));
assert!((expf64(1f64).approx_eq(&f64::consts::e)));
assert!((fabsf32(expf32(1f32) - f32::consts::E).approx_eq(&0f32)));
assert!((expf64(1f64).approx_eq(&f64::consts::E)));
assert!((exp2f32(10f32).approx_eq(&1024f32)));
assert!((exp2f64(50f64).approx_eq(&1125899906842624f64)));
assert!((fabsf32(logf32(f32::consts::e) - 1f32).approx_eq(&0f32)));
assert!((fabsf32(logf32(f32::consts::E) - 1f32).approx_eq(&0f32)));
assert!((logf64(1f64).approx_eq(&0f64)));
assert!((log10f32(10f32).approx_eq(&1f32)));
assert!((log10f64(f64::consts::e).approx_eq(&f64::consts::log10_e)));
assert!((log10f64(f64::consts::E).approx_eq(&f64::consts::LOG10_E)));
assert!((log2f32(8f32).approx_eq(&3f32)));
assert!((log2f64(f64::consts::e).approx_eq(&f64::consts::log2_e)));
assert!((log2f64(f64::consts::E).approx_eq(&f64::consts::LOG2_E)));
assert!((fmaf32(1.0f32, 2.0f32, 5.0f32).approx_eq(&7.0f32)));
assert!((fmaf64(0.0f64, -2.0f64, f64::consts::e).approx_eq(&f64::consts::e)));
assert!((fmaf64(0.0f64, -2.0f64, f64::consts::E).approx_eq(&f64::consts::E)));
assert!((fabsf32(-1.0f32).approx_eq(&1.0f32)));
assert!((fabsf64(34.2f64).approx_eq(&34.2f64)));

View File

@ -27,7 +27,7 @@ pub enum Shape {
impl Shape {
pub fn area(&self, sh: Shape) -> f64 {
match sh {
Circle(_, size) => f64::consts::pi * size * size,
Circle(_, size) => f64::consts::PI * size * size,
Rectangle(Point {x, y}, Point {x: x2, y: y2}) => (x2 - x) * (y2 - y)
}
}