Type ::std::f64

Overview

The primitive float type.

Methods

fn is_nan(self) -> bool

Returns true if this value is NaN.

Examples

let nan = f64::NAN;
let f = 7.0_f64;

assert!(nan.is_nan());
assert!(!f.is_nan());

Returns true if this value is positive infinity or negative infinity, and false otherwise.

Examples

let f = 7.0f64;
let inf = f64::INFINITY;
let neg_inf = f64::NEG_INFINITY;
let nan = f64::NAN;

assert!(!f.is_infinite());
assert!(!nan.is_infinite());

assert!(inf.is_infinite());
assert!(neg_inf.is_infinite());

Returns true if this number is neither infinite nor NaN.

Examples

let f = 7.0f64;
let inf = f64::INFINITY;
let neg_inf = f64::NEG_INFINITY;
let nan = f64::NAN;

assert!(f.is_finite());

assert!(!nan.is_finite());
assert!(!inf.is_finite());
assert!(!neg_inf.is_finite());

Returns true if the number is subnormal.

Examples

let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308_f64
let max = f64::MAX;
let lower_than_min = 1.0e-308_f64;
let zero = 0.0_f64;

assert!(!min.is_subnormal());
assert!(!max.is_subnormal());

assert!(!zero.is_subnormal());
assert!(!f64::NAN.is_subnormal());
assert!(!f64::INFINITY.is_subnormal());
// Values between `0` and `min` are Subnormal.
assert!(lower_than_min.is_subnormal());

Returns true if the number is neither zero, infinite, subnormal, or NaN.

Examples

let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
let max = f64::MAX;
let lower_than_min = 1.0e-308_f64;
let zero = 0.0f64;

assert!(min.is_normal());
assert!(max.is_normal());

assert!(!zero.is_normal());
assert!(!f64::NAN.is_normal());
assert!(!f64::INFINITY.is_normal());
// Values between `0` and `min` are Subnormal.
assert!(!lower_than_min.is_normal());
fn max(self, other: f64) -> f64

Returns the maximum of the two numbers, ignoring NaN.

If one of the arguments is NaN, then the other argument is returned. This follows the IEEE 754-2008 semantics for maxNum, except for handling of signaling NaNs; this function handles all NaNs the same way and avoids maxNum's problems with associativity. This also matches the behavior of libm’s fmax.

Examples

let x = 1.0_f64;
let y = 2.0_f64;

assert_eq!(x.max(y), y);
fn min(self, other: f64) -> f64

Returns the minimum of the two numbers, ignoring NaN.

If one of the arguments is NaN, then the other argument is returned. This follows the IEEE 754-2008 semantics for minNum, except for handling of signaling NaNs; this function handles all NaNs the same way and avoids minNum's problems with associativity. This also matches the behavior of libm’s fmin.

Examples

let x = 1.0_f64;
let y = 2.0_f64;

assert_eq!(x.min(y), x);
fn abs(self) -> f64

Computes the absolute value of self.

Examples

let x = 3.5_f64;
let y = -3.5_f64;

let abs_difference_x = (x.abs() - x).abs();
let abs_difference_y = (y.abs() - (-y)).abs();

assert!(abs_difference_x < 1e-10);
assert!(abs_difference_y < 1e-10);

assert!(f64::NAN.abs().is_nan());
fn powf(self, other: f64) -> f64

Raises a number to a floating point power.

Examples

let x = 2.0_f64;
let abs_difference = (x.powf(2.0) - (x * x)).abs();

assert!(abs_difference < 1e-10);
fn powi(self, other: i64) -> f64

Raises a number to an integer power.

Using this function is generally faster than using powf. It might have a different sequence of rounding operations than powf, so the results are not guaranteed to agree.

Examples

let x = 2.0_f64;
let abs_difference = (x.powi(2) - (x * x)).abs();

assert!(abs_difference < 1e-10);
fn to<i64>(self) -> i64

Convert a float to a an integer.

Examples

let n = 7.0_f64.to::<i64>();
assert_eq!(n, 7);

Protocols

protocol partial_eq
if value == b { }

Test two floats for partial equality.

Examples

assert!(5.0 == 5.0);
assert!(5.0 != 10.0);
assert!(10.0 != 5.0);
assert!(10.0 != f64::NAN);
assert!(f64::NAN != f64::NAN);
protocol eq
if value == b { }

Test two floats for total equality.

Examples

use std::ops::eq;

assert_eq!(eq(5.0, 5.0), true);
assert_eq!(eq(5.0, 10.0), false);
assert_eq!(eq(10.0, 5.0), false);
protocol partial_cmp
if value < b { }

Perform a partial ordered comparison between two floats.

Examples

use std::cmp::Ordering;
use std::ops::partial_cmp;

assert_eq!(partial_cmp(5.0, 10.0), Some(Ordering::Less));
assert_eq!(partial_cmp(10.0, 5.0), Some(Ordering::Greater));
assert_eq!(partial_cmp(5.0, 5.0), Some(Ordering::Equal));
assert_eq!(partial_cmp(5.0, f64::NAN), None);
protocol cmp
if value < b { }

Perform a partial ordered comparison between two floats.

Examples

use std::cmp::Ordering;
use std::ops::cmp;

assert_eq!(cmp(5.0, 10.0), Ordering::Less);
assert_eq!(cmp(10.0, 5.0), Ordering::Greater);
assert_eq!(cmp(5.0, 5.0), Ordering::Equal);