f64 is Rust’s double-precision floating-point type. It represents decimal-looking values using IEEE 754 binary floating-point, so some values are approximations rather than exact quantities. section 5.8 section 2.3.3
Use f64 with these floating-point hazards in mind:
- Avoid testing floating-point values for exact equality; calculations such as 0.1 + 0.2 may not produce the same bit pattern as 0.3. section 2.3.3
- Compare results using an acceptable difference when approximate equality is appropriate. section 2.3.3
- Remember that NaN and signed zero have unusual comparison behavior: NaN is not equal to itself, while -0 equals 0. section 5.8
Inspecting f32 and f64 results
A comparison program adds 0.1 and 0.2, then prints the underlying bit patterns. The f32 addition matches its 0.3 value in the example, but the f64 addition differs by one bit and causes an equality assertion to fail.
Table 2.3 Mathematical operators supported by Rust’s numeric types (view table figure)
Use the operator table to review comparisons such as less-than, greater-than, equality, and inequality for numeric values. section 2.3.3
