liveBook · Manning

64-bit floating-point number

As explained in Rust in Action: Systems programming concepts and techniques

A double-precision floating-point type is a floating-point type represented with the precision associated with Rust's f64 type.

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)

Operator

Rust syntax

Example

Less than (<)

<

1.0 < 2.0

Greater than (>)

>

2.0 > 1.0

Equal to (=)

==

1.0 == 1.0

Unequal to (≠)

!=

1.0 != 2.0

Equal to or less than (≤)

<=

1.0 <= 2.0

Equal to greater than or (≥)

>=

2.0 >= 1.0

Use the operator table to review comparisons such as less-than, greater-than, equality, and inequality for numeric values. section 2.3.3

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains binary approximation, partial equivalence, and bit-level differences through runnable comparisons.
Examples
Uses 0.1 + 0.2 versus 0.3, hexadecimal bit patterns, and EPSILON-based comparison.
What it assumes
Assumes basic numeric expressions and comparisons while developing awareness of floating-point representation hazards.
What you can do after
Evaluate floating-point comparisons and diagnose surprising results using differences, special values, and bit patterns.
How it connects
Connects floating-point representation with IEEE 754, binary arithmetic, PartialEq, NaN, and signed zero.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.