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

Computes the arccosine of a number.

Return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1].

Examples

let f = std::f64::consts::FRAC_PI_4;

// acos(cos(pi/4))
let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
assert!(abs_difference < 1e-10);
fn asin(self) -> f64

Computes the arcsine of a number.

Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].

Examples

let f = std::f64::consts::FRAC_PI_2;

// asin(sin(pi/2))
let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
assert!(abs_difference < 1e-7);
fn atan(self) -> f64

Computes the arctangent of a number.

Return value is in radians in the range [-pi/2, pi/2];

Examples

let f = 1.0;

// atan(tan(1))
let abs_difference = (f.tan().atan() - 1.0).abs();
assert!(abs_difference < 1e-10);
fn atan2(self, other: f64) -> f64

Computes the four quadrant arctangent of self (y) and other (x) in radians.

  • x = 0, y = 0: 0
  • x >= 0: arctan(y/x) -> [-pi/2, pi/2]
  • y >= 0: arctan(y/x) + pi -> (pi/2, pi]
  • y < 0: arctan(y/x) - pi -> (-pi, -pi/2)

Examples

// Positive angles measured counter-clockwise
// from positive x axis
// -pi/4 radians (45 deg clockwise)
let x1 = 3.0;
let y1 = -3.0;

// 3pi/4 radians (135 deg counter-clockwise)
let x2 = -3.0;
let y2 = 3.0;

let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();

assert!(abs_difference_1 < 1e-10);
assert!(abs_difference_2 < 1e-10);
fn cbrt(self) -> f64

Returns the cube root of a number.

Examples

let x = 8.0_f64;

// x^(1/3) - 2 == 0
let abs_difference = (x.cbrt() - 2.0).abs();
assert!(abs_difference < 1e-10);
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 clamp(self, min: f64, max: f64) -> f64

Restrict a value to a certain interval unless it is NaN.

Returns max if self is greater than max, and min if self is less than min. Otherwise this returns self.

Note that this function returns NaN if the initial value was NaN as well.

Panics

Panics if min > max, min is NaN, or max is NaN.

Examples

assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
fn cos(self) -> f64

Computes the cosine of a number (in radians).

Examples

let x = 2.0 * std::f64::consts::PI;

let abs_difference = (x.cos() - 1.0).abs();
assert!(abs_difference < 1e-10);
fn div_euclid(self, rhs: f64) -> f64

Calculates Euclidean division, the matching method for rem_euclid.

This computes the integer n such that self = n * rhs + self.rem_euclid(rhs). In other words, the result is self / rhs rounded to the integer n such that self >= n * rhs.

Examples

let a = 7.0;
let b = 4.0;
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
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
fn exp(self) -> f64

Returns e^(self), (the exponential function).

Examples

let one = 1.0_f64;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();
assert!(abs_difference < 1e-10);
fn exp2(self) -> f64

Returns 2^(self).

Examples

let f = 2.0_f64;

// 2^2 - 4 == 0
let abs_difference = (f.exp2() - 4.0).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 ln(self) -> f64

Returns the natural logarithm of the number.

This returns NaN when the number is negative, and negative infinity when number is zero.

Examples

let one = 1.0;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();
assert!(abs_difference < 1e-10);
fn log(self, base: f64) -> f64

Returns the logarithm of the number with respect to an arbitrary base.

This returns NaN when the number is negative, and negative infinity when number is zero.

The result might not be correctly rounded owing to implementation details; self.log2() can produce more accurate results for base 2, and self.log10() can produce more accurate results for base 10.

Examples

let twenty_five = 25.0_f64;

// log5(25) - 2 == 0
let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
assert!(abs_difference < 1e-10);
fn log10(self) -> f64

Returns the base 10 logarithm of the number.

This returns NaN when the number is negative, and negative infinity when number is zero.

Examples

let hundred = 100.0_f64;

// log10(100) - 2 == 0
let abs_difference = (hundred.log10() - 2.0).abs();
assert!(abs_difference < 1e-10);
fn log2(self) -> f64

Returns the base 2 logarithm of the number.

This returns NaN when the number is negative, and negative infinity when number is zero.

Examples

let four = 4.0_f64;

// log2(4) - 2 == 0
let abs_difference = (four.log2() - 2.0).abs();

assert!(abs_difference < 1e-10);

Non-positive values:

assert_eq!(0_f64.log2(), f64::NEG_INFINITY);
assert!((-42_f64).log2().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 rem_euclid(self, rhs: f64) -> f64

Computes the least nonnegative remainder of self (mod rhs).

In particular, the return value r satisfies 0.0 <= r < rhs.abs() in most cases. However, due to a floating point round-off error it can result in r == rhs.abs(), violating the mathematical definition, if self is much smaller than rhs.abs() in magnitude and self < 0.0. This result is not an element of the function’s codomain, but it is the closest floating point number in the real numbers and thus fulfills the property self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs) approximately.

Examples

let a = 7.0;
let b = 4.0;
assert_eq!(a.rem_euclid(b), 3.0);
assert_eq!((-a).rem_euclid(b), 1.0);
assert_eq!(a.rem_euclid(-b), 3.0);
assert_eq!((-a).rem_euclid(-b), 1.0);
// limitation due to round-off error
assert!((-f64::EPSILON).rem_euclid(3.0) != 0.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 sin(self) -> f64

Computes the sine of a number (in radians).

Examples

let x = std::f64::consts::FRAC_PI_2;

let abs_difference = (x.sin() - 1.0).abs();
assert!(abs_difference < 1e-10);
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 tan(self) -> f64

Computes the tangent of a number (in radians).

Examples

let x = std::f64::consts::FRAC_PI_4;
let abs_difference = (x.tan() - 1.0).abs();
assert!(abs_difference < 1e-14);
fn to<i64>(self) -> i64

Convert a float to a an integer.

Examples

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

Converts radians to degrees.

Examples

let abs_difference = (std::f64::consts::PI.to_degrees() - 180.0).abs();
assert!(abs_difference < 1e-10);

Converts degrees to radians.

Examples

let abs_difference = (180.0.to_radians() - std::f64::consts::PI).abs();
assert!(abs_difference < 1e-10);

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.