Skip to content

add min_pos_value constant for floats #13735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ pub static DIGITS: uint = 6u;

pub static EPSILON: f32 = 1.19209290e-07_f32;

/// Minimum normalized f32 value
pub static MIN_VALUE: f32 = 1.17549435e-38_f32;
/// Maximum f32 value
/// Smallest finite f32 value
pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a different but still reasonable name for this? It may be confusing for someone who's used to C's weird definition and tries to use this constant without looking at the value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't shock me since Rust tries to fix the C/C++ legacy in various domains... plus I would expect a newcomer to at least lookup the function name/doc on rustdoc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++11 introduced numeric_limits::lowest, but that's an ugly backwards compatibility workaround. So long as we document this, I think it is good to get this right from the start.

/// Smallest positive, normalized f32 value
pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
/// Largest finite f32 value
pub static MAX_VALUE: f32 = 3.40282347e+38_f32;

pub static MIN_EXP: int = -125;
Expand All @@ -90,8 +92,9 @@ pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
pub mod consts {
// FIXME: replace with mathematical constants from cmath.

// FIXME(#11621): These constants should be deprecated once CTFE is
// implemented in favour of calling their respective functions in `Float`.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.

/// Archimedes' constant
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
Expand Down Expand Up @@ -342,6 +345,9 @@ impl Float for f32 {
#[inline]
fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }

#[inline]
fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }

/// Constructs a floating point number by multiplying `x` by 2 raised to the
/// power of `exp`
#[inline]
Expand Down
35 changes: 21 additions & 14 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ mod cmath {
}
}

// FIXME(#11621): These constants should be deprecated once CTFE is implemented
// in favour of calling their respective functions in `Bounded` and `Float`.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective
// members of `Bounded` and `Float`.

pub static RADIX: uint = 2u;

Expand All @@ -83,9 +84,11 @@ pub static DIGITS: uint = 15u;

pub static EPSILON: f64 = 2.2204460492503131e-16_f64;

/// Minimum normalized f64 value
pub static MIN_VALUE: f64 = 2.2250738585072014e-308_f64;
/// Maximum f64 value
/// Smallest finite f64 value
pub static MIN_VALUE: f64 = -1.7976931348623157e+308_f64;
/// Smallest positive, normalized f64 value
pub static MIN_POS_VALUE: f64 = 2.2250738585072014e-308_f64;
/// Largest finite f64 value
pub static MAX_VALUE: f64 = 1.7976931348623157e+308_f64;

pub static MIN_EXP: int = -1021;
Expand All @@ -104,8 +107,9 @@ pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
pub mod consts {
// FIXME: replace with mathematical constants from cmath.

// FIXME(#11621): These constants should be deprecated once CTFE is
// implemented in favour of calling their respective functions in `Float`.
// FIXME(#5527): These constants should be deprecated once associated
// constants are implemented in favour of referencing the respective members
// of `Float`.

/// Archimedes' constant
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
Expand Down Expand Up @@ -330,25 +334,28 @@ impl Float for f64 {
}

#[inline]
fn mantissa_digits(_: Option<f64>) -> uint { 53 }
fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS }

#[inline]
fn digits(_: Option<f64>) -> uint { 15 }
fn digits(_: Option<f64>) -> uint { DIGITS }

#[inline]
fn epsilon() -> f64 { 2.2204460492503131e-16 }
fn epsilon() -> f64 { EPSILON }

#[inline]
fn min_exp(_: Option<f64>) -> int { -1021 }
fn min_exp(_: Option<f64>) -> int { MIN_EXP }

#[inline]
fn max_exp(_: Option<f64>) -> int { 1024 }
fn max_exp(_: Option<f64>) -> int { MAX_EXP }

#[inline]
fn min_10_exp(_: Option<f64>) -> int { -307 }
fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP }

#[inline]
fn max_10_exp(_: Option<f64>) -> int { 308 }
fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }

#[inline]
fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }

/// Constructs a floating point number by multiplying `x` by 2 raised to the
/// power of `exp`
Expand Down
8 changes: 8 additions & 0 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent
fn min_value() -> Self;
/// returns the largest finite number this type can represent
fn max_value() -> Self;
}

Expand Down Expand Up @@ -356,6 +358,8 @@ pub trait Float: Signed + Primitive {
/// Returns the category that this number falls into.
fn classify(self) -> FPCategory;

// FIXME (#5527): These should be associated constants

/// Returns the number of binary digits of mantissa that this type supports.
fn mantissa_digits(unused_self: Option<Self>) -> uint;
/// Returns the number of base-10 digits of precision that this type supports.
Expand All @@ -370,6 +374,8 @@ pub trait Float: Signed + Primitive {
fn min_10_exp(unused_self: Option<Self>) -> int;
/// Returns the maximum base-10 exponent that this type can represent.
fn max_10_exp(unused_self: Option<Self>) -> int;
/// Returns the smallest normalized positive number that this type can represent.
fn min_pos_value(unused_self: Option<Self>) -> Self;

/// Constructs a floating point number created by multiplying `x` by 2
/// raised to the power of `exp`.
Expand Down Expand Up @@ -434,6 +440,8 @@ pub trait Float: Signed + Primitive {
/// legs of length `x` and `y`.
fn hypot(self, other: Self) -> Self;

// FIXME (#5527): These should be associated constants

/// Archimedes' constant.
fn pi() -> Self;
/// 2.0 * pi.
Expand Down