use crate::sys::cmath;
#[stable(feature = "rust1", since = "1.0.0")]
-pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
+pub use core::f64::consts;
#[stable(feature = "rust1", since = "1.0.0")]
-pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
+pub use core::f64::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX};
#[stable(feature = "rust1", since = "1.0.0")]
-pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
+pub use core::f64::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY};
#[stable(feature = "rust1", since = "1.0.0")]
-pub use core::f64::{MIN, MIN_POSITIVE, MAX};
+pub use core::f64::{MAX, MIN, MIN_POSITIVE};
#[stable(feature = "rust1", since = "1.0.0")]
-pub use core::f64::consts;
+pub use core::f64::{MAX_EXP, MIN_10_EXP, MIN_EXP};
#[cfg(not(test))]
#[lang = "f64_runtime"]
/// assert_eq!(g.floor(), 3.0);
/// assert_eq!(h.floor(), -4.0);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn floor(self) -> f64 {
/// assert_eq!(f.ceil(), 4.0);
/// assert_eq!(g.ceil(), 4.0);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn ceil(self) -> f64 {
/// assert_eq!(f.round(), 3.0);
/// assert_eq!(g.round(), -3.0);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn round(self) -> f64 {
/// assert_eq!(g.trunc(), 3.0);
/// assert_eq!(h.trunc(), -3.0);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn trunc(self) -> f64 {
/// # Examples
///
/// ```
- /// let x = 3.5_f64;
- /// let y = -3.5_f64;
- /// let abs_difference_x = (x.fract() - 0.5).abs();
- /// let abs_difference_y = (y.fract() - (-0.5)).abs();
+ /// let x = 3.6_f64;
+ /// let y = -3.6_f64;
+ /// let abs_difference_x = (x.fract() - 0.6).abs();
+ /// let abs_difference_y = (y.fract() - (-0.6)).abs();
///
/// assert!(abs_difference_x < 1e-10);
/// assert!(abs_difference_y < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn fract(self) -> f64 { self - self.trunc() }
+ pub fn fract(self) -> f64 {
+ self - self.trunc()
+ }
/// Computes the absolute value of `self`. Returns `NAN` if the
/// number is `NAN`.
///
/// assert!(f64::NAN.abs().is_nan());
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn abs(self) -> f64 {
///
/// assert!(f64::NAN.signum().is_nan());
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn signum(self) -> f64 {
- if self.is_nan() {
- NAN
- } else {
- 1.0_f64.copysign(self)
- }
+ if self.is_nan() { NAN } else { 1.0_f64.copysign(self) }
}
/// Returns a number composed of the magnitude of `self` and the sign of
///
/// assert!(f64::NAN.copysign(1.0).is_nan());
/// ```
- #[inline]
- #[must_use]
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "copysign", since = "1.35.0")]
+ #[inline]
pub fn copysign(self, sign: f64) -> f64 {
unsafe { intrinsics::copysignf64(self, sign) }
}
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn mul_add(self, a: f64, b: f64) -> f64 {
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
pub fn div_euclid(self, rhs: f64) -> f64 {
/// // limitation due to round-off error
/// assert!((-std::f64::EPSILON).rem_euclid(3.0) != 0.0);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[inline]
#[stable(feature = "euclidean_division", since = "1.38.0")]
pub fn rem_euclid(self, rhs: f64) -> f64 {
let r = self % rhs;
- if r < 0.0 {
- r + rhs.abs()
- } else {
- r
- }
+ if r < 0.0 { r + rhs.abs() } else { r }
}
/// Raises a number to an integer power.
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn powi(self, n: i32) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn powf(self, n: f64) -> f64 {
unsafe { intrinsics::powf64(self, n) }
}
- /// Takes the square root of a number.
+ /// Returns the square root of a number.
///
/// Returns NaN if `self` is a negative number.
///
/// assert!(abs_difference < 1e-10);
/// assert!(negative.sqrt().is_nan());
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn sqrt(self) -> f64 {
- if self < 0.0 {
- NAN
- } else {
- unsafe { intrinsics::sqrtf64(self) }
- }
+ unsafe { intrinsics::sqrtf64(self) }
}
/// Returns `e^(self)`, (the exponential function).
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn exp(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn exp2(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn ln(self) -> f64 {
- self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
+ self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
}
/// Returns the logarithm of the number with respect to an arbitrary base.
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
+ pub fn log(self, base: f64) -> f64 {
+ self.ln() / base.ln()
+ }
/// Returns the base 2 logarithm of the number.
///
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn log2(self) -> f64 {
self.log_wrapper(|n| {
#[cfg(target_os = "android")]
- return crate::sys::android::log2f64(n);
+ return crate::sys::android::log2f64(n);
#[cfg(not(target_os = "android"))]
- return unsafe { intrinsics::log2f64(n) };
+ return unsafe { intrinsics::log2f64(n) };
})
}
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn log10(self) -> f64 {
- self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
+ self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
}
/// The positive difference of two numbers.
/// assert!(abs_difference_x < 1e-10);
/// assert!(abs_difference_y < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
- #[rustc_deprecated(since = "1.10.0",
- reason = "you probably meant `(self - other).abs()`: \
- this operation is `(self - other).max(0.0)` \
- except that `abs_sub` also propagates NaNs (also \
- known as `fdim` in C). If you truly need the positive \
- difference, consider using that expression or the C function \
- `fdim`, depending on how you wish to handle NaN (please consider \
- filing an issue describing your use-case too).")]
+ #[rustc_deprecated(
+ since = "1.10.0",
+ reason = "you probably meant `(self - other).abs()`: \
+ this operation is `(self - other).max(0.0)` \
+ except that `abs_sub` also propagates NaNs (also \
+ known as `fdim` in C). If you truly need the positive \
+ difference, consider using that expression or the C function \
+ `fdim`, depending on how you wish to handle NaN (please consider \
+ filing an issue describing your use-case too)."
+ )]
pub fn abs_sub(self, other: f64) -> f64 {
unsafe { cmath::fdim(self, other) }
}
- /// Takes the cubic root of a number.
+ /// Returns the cubic root of a number.
///
/// # Examples
///
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn cbrt(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn hypot(self, other: f64) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn sin(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn cos(self) -> f64 {
///
/// assert!(abs_difference < 1e-14);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn tan(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asin(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acos(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn atan(self) -> f64 {
/// assert!(abs_difference_1 < 1e-10);
/// assert!(abs_difference_2 < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn atan2(self, other: f64) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn exp_m1(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn ln_1p(self) -> f64 {
///
/// assert!(abs_difference < 1e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn sinh(self) -> f64 {
/// // Same result
/// assert!(abs_difference < 1.0e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn cosh(self) -> f64 {
///
/// assert!(abs_difference < 1.0e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn tanh(self) -> f64 {
///
/// assert!(abs_difference < 1.0e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f64 {
///
/// assert!(abs_difference < 1.0e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f64 {
- if self < 1.0 {
- NAN
- } else {
- (self + ((self * self) - 1.0).sqrt()).ln()
- }
+ if self < 1.0 { NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
}
/// Inverse hyperbolic tangent function.
///
/// assert!(abs_difference < 1.0e-10);
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn atanh(self) -> f64 {
/// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
/// assert!((std::f64::NAN).clamp(-2.0, 1.0).is_nan());
/// ```
+ #[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "clamp", issue = "44095")]
#[inline]
pub fn clamp(self, min: f64, max: f64) -> f64 {
assert!(min <= max);
let mut x = self;
- if x < min { x = min; }
- if x > max { x = max; }
+ if x < min {
+ x = min;
+ }
+ if x > max {
+ x = max;
+ }
x
}
mod tests {
use crate::f64;
use crate::f64::*;
- use crate::num::*;
use crate::num::FpCategory as Fp;
+ use crate::num::*;
#[test]
fn test_num_f64() {
#[test]
#[should_panic]
fn test_clamp_min_greater_than_max() {
- 1.0f64.clamp(3.0, 1.0);
+ let _ = 1.0f64.clamp(3.0, 1.0);
}
#[test]
#[should_panic]
fn test_clamp_min_is_nan() {
- 1.0f64.clamp(NAN, 1.0);
+ let _ = 1.0f64.clamp(NAN, 1.0);
}
#[test]
#[should_panic]
fn test_clamp_max_is_nan() {
- 1.0f64.clamp(3.0, NAN);
+ let _ = 1.0f64.clamp(3.0, NAN);
}
}