f64

Primitive Type f64 

1.0.0
Expand description

A 64-bit floating-point type (specifically, the “binary64” type defined in IEEE 754-2008).

This type is very similar to f32, but has increased precision by using twice as many bits. Please see the documentation for f32 or Wikipedia on double-precision values for more information.

See also the std::f64::consts module.

Implementations§

Source§

impl f64

1.43.0 · Source

pub const RADIX: u32 = 2u32

The radix or base of the internal representation of f64.

1.43.0 · Source

pub const MANTISSA_DIGITS: u32 = 53u32

Number of significant digits in base 2.

Note that the size of the mantissa in the bitwise representation is one smaller than this since the leading 1 is not stored explicitly.

1.43.0 · Source

pub const DIGITS: u32 = 15u32

Approximate number of significant digits in base 10.

This is the maximum x such that any decimal number with x significant digits can be converted to f64 and back without loss.

Equal to floor(log10 2MANTISSA_DIGITS − 1).

1.43.0 · Source

pub const EPSILON: f64 = 2.2204460492503131E-16f64

Machine epsilon value for f64.

This is the difference between 1.0 and the next larger representable number.

Equal to 21 − MANTISSA_DIGITS.

1.43.0 · Source

pub const MIN: f64 = -1.7976931348623157E+308f64

Smallest finite f64 value.

Equal to −MAX.

1.43.0 · Source

pub const MIN_POSITIVE: f64 = 2.2250738585072014E-308f64

Smallest positive normal f64 value.

Equal to 2MIN_EXP − 1.

1.43.0 · Source

pub const MAX: f64 = 1.7976931348623157E+308f64

Largest finite f64 value.

Equal to (1 − 2MANTISSA_DIGITS) 2MAX_EXP.

1.43.0 · Source

pub const MIN_EXP: i32 = -1_021i32

One greater than the minimum possible normal power of 2 exponent for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).

This corresponds to the exact minimum possible normal power of 2 exponent for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). In other words, all normal numbers representable by this type are greater than or equal to 0.5 × 2MIN_EXP.

1.43.0 · Source

pub const MAX_EXP: i32 = 1_024i32

One greater than the maximum possible power of 2 exponent for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).

This corresponds to the exact maximum possible power of 2 exponent for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). In other words, all numbers representable by this type are strictly less than 2MAX_EXP.

1.43.0 · Source

pub const MIN_10_EXP: i32 = -307i32

Minimum x for which 10x is normal.

Equal to ceil(log10 MIN_POSITIVE).

1.43.0 · Source

pub const MAX_10_EXP: i32 = 308i32

Maximum x for which 10x is normal.

Equal to floor(log10 MAX).

1.43.0 · Source

pub const NAN: f64 = NaN_f64

Not a Number (NaN).

Note that IEEE 754 doesn’t define just a single NaN value; a plethora of bit patterns are considered to be NaN. Furthermore, the standard makes a difference between a “signaling” and a “quiet” NaN, and allows inspecting its “payload” (the unspecified bits in the bit pattern) and its sign. See the specification of NaN bit patterns for more info.

This constant is guaranteed to be a quiet NaN (on targets that follow the Rust assumptions that the quiet/signaling bit being set to 1 indicates a quiet NaN). Beyond that, nothing is guaranteed about the specific bit pattern chosen here: both payload and sign are arbitrary. The concrete bit pattern may change across Rust versions and target platforms.

1.43.0 · Source

pub const INFINITY: f64 = +Inf_f64

Infinity (∞).

1.43.0 · Source

pub const NEG_INFINITY: f64 = -Inf_f64

Negative infinity (−∞).

1.20.0 (const: 1.83.0) · Source

pub const fn to_bits(self) -> u64

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);
1.20.0 (const: 1.83.0) · Source

pub const fn from_bits(v: u64) -> 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 endianness 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 favors 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 signaling-ness (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
let v = f64::from_bits(0x4029000000000000);
assert_eq!(v, 12.5);
1.40.0 (const: 1.83.0) · Source

pub const fn to_le_bytes(self) -> [u8; 8]

Returns the memory representation of this floating point number as a byte array in little-endian byte order.

See from_bits for some discussion of the portability of this operation (there are almost no issues).

§Examples
let bytes = 12.5f64.to_le_bytes();
assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
1.40.0 (const: 1.83.0) · Source

pub const fn from_le_bytes(bytes: [u8; 8]) -> Self

Creates a floating point value from its representation as a byte array in little endian.

See from_bits for some discussion of the portability of this operation (there are almost no issues).

§Examples
let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
assert_eq!(value, 12.5);

Trait Implementations§

1.0.0 (const: unstable) · Source§

impl Add<&f64> for &f64

Source§

type Output = <f64 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &f64) -> <f64 as Add<f64>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add<&f64> for f64

Source§

type Output = <f64 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &f64) -> <f64 as Add<f64>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add<f64> for &f64

Source§

type Output = <f64 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> <f64 as Add<f64>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add for f64

Source§

type Output = f64

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> f64

Performs the + operation. Read more
1.22.0 (const: unstable) · Source§

impl AddAssign<&f64> for f64

Source§

fn add_assign(&mut self, other: &f64)

Performs the += operation. Read more
1.8.0 (const: unstable) · Source§

impl AddAssign for f64

Source§

fn add_assign(&mut self, other: f64)

Performs the += operation. Read more
1.0.0 (const: unstable) · Source§

impl Clone for f64

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)
where Self:,

Performs copy-assignment from source. Read more
1.0.0 (const: unstable) · Source§

impl Default for f64

Source§

fn default() -> f64

Returns the default value of 0.0

1.0.0 (const: unstable) · Source§

impl Div<&f64> for &f64

Source§

type Output = <f64 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &f64) -> <f64 as Div<f64>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div<&f64> for f64

Source§

type Output = <f64 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &f64) -> <f64 as Div<f64>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div<f64> for &f64

Source§

type Output = <f64 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> <f64 as Div<f64>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div for f64

Source§

type Output = f64

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> f64

Performs the / operation. Read more
1.22.0 (const: unstable) · Source§

impl DivAssign<&f64> for f64

Source§

fn div_assign(&mut self, other: &f64)

Performs the /= operation. Read more
1.8.0 (const: unstable) · Source§

impl DivAssign for f64

Source§

fn div_assign(&mut self, other: f64)

Performs the /= operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul<&f64> for &f64

Source§

type Output = <f64 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &f64) -> <f64 as Mul<f64>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul<&f64> for f64

Source§

type Output = <f64 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &f64) -> <f64 as Mul<f64>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul<f64> for &f64

Source§

type Output = <f64 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> <f64 as Mul<f64>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul for f64

Source§

type Output = f64

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> f64

Performs the * operation. Read more
1.22.0 (const: unstable) · Source§

impl MulAssign<&f64> for f64

Source§

fn mul_assign(&mut self, other: &f64)

Performs the *= operation. Read more
1.8.0 (const: unstable) · Source§

impl MulAssign for f64

Source§

fn mul_assign(&mut self, other: f64)

Performs the *= operation. Read more
1.0.0 (const: unstable) · Source§

impl Neg for &f64

Source§

type Output = <f64 as Neg>::Output

The resulting type after applying the - operator.
Source§

fn neg(self) -> <f64 as Neg>::Output

Performs the unary - operation. Read more
1.0.0 (const: unstable) · Source§

impl Neg for f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn neg(self) -> f64

Performs the unary - operation. Read more
1.0.0 (const: unstable) · Source§

impl PartialEq for f64

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Self) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
1.0.0 (const: unstable) · Source§

impl PartialOrd for f64

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &Self) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &Self) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &Self) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Self) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
1.0.0 (const: unstable) · Source§

impl Rem<&f64> for &f64

Source§

type Output = <f64 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: &f64) -> <f64 as Rem<f64>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

impl Rem<&f64> for f64

Source§

type Output = <f64 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: &f64) -> <f64 as Rem<f64>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

impl Rem<f64> for &f64

Source§

type Output = <f64 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: f64) -> <f64 as Rem<f64>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

impl Rem for f64

The remainder from the division of two floats.

The remainder has the same sign as the dividend and is computed as: x - (x / y).trunc() * y.

§Examples

let x: f32 = 50.50;
let y: f32 = 8.125;
let remainder = x - (x / y).trunc() * y;

// The answer to both operations is 1.75
assert_eq!(x % y, remainder);
Source§

type Output = f64

The resulting type after applying the % operator.
Source§

fn rem(self, other: f64) -> f64

Performs the % operation. Read more
1.22.0 (const: unstable) · Source§

impl RemAssign<&f64> for f64

Source§

fn rem_assign(&mut self, other: &f64)

Performs the %= operation. Read more
1.8.0 (const: unstable) · Source§

impl RemAssign for f64

Source§

fn rem_assign(&mut self, other: f64)

Performs the %= operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<&f64> for &f64

Source§

type Output = <f64 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &f64) -> <f64 as Sub<f64>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<&f64> for f64

Source§

type Output = <f64 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &f64) -> <f64 as Sub<f64>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<f64> for &f64

Source§

type Output = <f64 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> <f64 as Sub<f64>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub for f64

Source§

type Output = f64

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> f64

Performs the - operation. Read more
1.22.0 (const: unstable) · Source§

impl SubAssign<&f64> for f64

Source§

fn sub_assign(&mut self, other: &f64)

Performs the -= operation. Read more
1.8.0 (const: unstable) · Source§

impl SubAssign for f64

Source§

fn sub_assign(&mut self, other: f64)

Performs the -= operation. Read more
1.0.0 · Source§

impl Copy for f64

Auto Trait Implementations§

§

impl Freeze for f64

§

impl Send for f64

§

impl Sync for f64

§

impl Unpin for f64

Blanket Implementations§

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.