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 sqrt(self) -> f64

Returns the square root of a number.

Returns NaN if self is a negative number other than -0.0.

Examples

let positive = 4.0_f64;
let negative = -4.0_f64;
let negative_zero = -0.0_f64;

let abs_difference = (positive.sqrt() - 2.0).abs();

assert!(abs_difference < 1e-10);
assert!(negative.sqrt().is_nan());
assert!(negative_zero.sqrt() == negative_zero);
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 floor(self) -> f64

Returns the largest integer less than or equal to self.

Examples

let f = 3.7_f64;
let g = 3.0_f64;
let h = -3.7_f64;

assert!(f.floor() == 3.0);
assert!(g.floor() == 3.0);
assert!(h.floor() == -4.0);
fn ceil(self) -> f64

Returns the smallest integer greater than or equal to self.

Examples

let f = 3.01_f64;
let g = 4.0_f64;

assert_eq!(f.ceil(), 4.0);
assert_eq!(g.ceil(), 4.0);
fn round(self) -> f64

Returns the nearest integer to self. If a value is half-way between two integers, round away from 0.0.

Examples

let f = 3.3_f64;
let g = -3.3_f64;
let h = -3.7_f64;
let i = 3.5_f64;
let j = 4.5_f64;

assert_eq!(f.round(), 3.0);
assert_eq!(g.round(), -3.0);
assert_eq!(h.round(), -4.0);
assert_eq!(i.round(), 4.0);
assert_eq!(j.round(), 5.0);
fn to<i64>(self) -> i64

Convert a float to a an integer.

Examples

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

Trait Implementations

impl Clone for f64
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);
impl PartialEq for f64
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for f64
impl PartialOrd for f64
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

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

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

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

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

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

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for f64
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol MAX
$a.max($b)

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);
protocol MIN
$a.min($b)

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);
protocol CLONE
let $out = clone(value)

Clone a f64.

Note that since the type is copy, cloning has the same effect as assigning it.

Examples

let a = 5.0;
let b = a;
let c = a.clone();

a += 1.0;

assert_eq!(a, 6.0);
assert_eq!(b, 5.0);
assert_eq!(c, 5.0);
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 totally 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);
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.