Rust Reference ยท Primitive Types
Rust f32 and f64 types
Reference for Rust's f32 and f64 floating-point types: literals, precision, constants, sqrt, powi, floor, round, mul_add, trig methods and formatting.
Rust has two floating-point types that follow the IEEE 754 standard: f32 (single precision, about 7 significant decimal digits) and f64 (double precision, about 15 to 16 digits). A literal such as 2.5 defaults to f64 unless the context asks for f32. Floats are Copy, but they implement only PartialEq and PartialOrd, not Eq, Ord or Hash, because NaN breaks the rules for those traits.
Most of the useful operations are inherent methods (x.sqrt(), x.floor()) and constants live in std::f64::consts (PI, E, SQRT_2...) and as associated items (f64::EPSILON, f64::MAX).
let a = 2.5; // f64 let b: f32 = 1.5e3; // 1500.0 let c = 7_f64 / 2.0; // 3.5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
let v: f64 = 1.0; let w = 1e-3_f64; let m = v.mul_add(2.0, 3.0); // v * 2.0 + 3.0 with one rounding let r = v.total_cmp(&w); // total ordering, see NaN page
| Item | Description |
|---|---|
sqrt, cbrt, powi(i32), powf(f), exp, ln, log10, log2, log(base) |
Roots, powers and logarithms. sqrt of a negative number is NaN. |
floor, ceil, round, trunc, fract, round_ties_even |
Rounding. round rounds half away from zero. |
abs, signum, copysign, min, max, clamp, rem_euclid, div_euclid |
Sign, bounds and modular arithmetic. |
sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh |
Trigonometry in radians. |
to_radians, to_degrees, hypot, mul_add |
Conversion and combined operations. |
to_bits, from_bits, to_be_bytes, from_le_bytes |
Raw IEEE 754 representation. |
is_nan, is_finite, is_infinite, is_sign_negative |
Classification. |
EPSILON, MAX, MIN, MIN_POSITIVE, NAN, INFINITY, NEG_INFINITY, DIGITS, MANTISSA_DIGITS |
Constants. |
No panics from arithmetic: division by zero gives inf or NaN, overflow gives inf. Only conversions such as as are defined to saturate.
Usage notes
- Never compare computed floats with
==.0.1 + 0.2 != 0.3. Use an absolute or relative tolerance, as in the example. EPSILONis the gap above 1.0, not a universal tolerance. For large values it is smaller than the rounding error.min/maxignoreNaN: if one argument isNaNthe other is returned.f32for graphics and memory-heavy arrays,f64as the default. Conversionsf32tof64are lossless (f64::from), the other way rounds withas f32.powiis faster thanpowffor integer exponents but its last-digit result may vary slightly between platforms.- Formatting:
{:.2}fixes decimals,{:e}uses scientific notation,{:?}shows a whole number with a decimal point (1.0), while{}prints it as1. - Sorting a
Vec<f64>needssort_by(|a, b| a.total_cmp(b))orpartial_cmp(..).unwrap(); plainsort()does not compile. - Not
Hash/Eq: use the bits (to_bits) or a wrapper type for map keys.