title: Why does Rust only use 16 significant digits for f64 equality checks?
body: <p>I have the following Rust code:</p>
  <pre><code>use std::f64::consts as f64;
  
  fn main() {
      println!(&quot;Checking f64 PI...&quot;);
      // f64::PI definition: https://github.com/rust-lang/rust/blob/e1fc9ff4a794fb069d670dded1a66f05c86f3555/library/core/src/num/f64.rs#L240
      println!(&quot;Definition: pub const PI: f64 = 3.14159265358979323846264338327950288_f64;&quot;);
      println!(&quot;Print it:                       {:.35}&quot;, f64::PI);
      println!(&quot;Different after 16 significant digits ----------|                         &quot;);
      println!(&quot;##############################################################################&quot;);
      println!(&quot;Question 1: Why do the digits differ after 16 significant digits when printed?&quot;);
      println!(&quot;##############################################################################&quot;);
  
      println!(&quot;PERFORM ASSERTIONS...&quot;); 
      assert_eq!(f64::PI, 3.14159265358979323846264338327950288_f64); // 36 significant digits definition
      assert_eq!(f64::PI, 3.141592653589793_f64); // 16 significant digits (less then the 36 in definition)
      // compares up to here -------------|
      assert_eq!(f64::PI, 3.14159265358979300000000000000000000_f64); // 36 significant digits (16 used in equality comparison)
      assert_ne!(f64::PI, 3.14159265358979_f64); // 15 significant digits (not equal)
  
      println!(&quot;PERFORM EQUALITY CHECK...&quot;); 
      if 3.14159265358979323846264338327950288_f64 == 3.14159265358979300000000000000000000_f64 {
          println!(&quot;BAD: floats considered equal even when they differ past 16 significant digits&quot;);
          println!(&quot;######################################################################&quot;);
          println!(&quot;Question 2: Why does equality checking use only 16 significant digits?&quot;);
          println!(&quot;They are defined using 36 significant digits so why can't we perform&quot;);
          println!(&quot;an equality check with this accuracy?&quot;);
          println!(&quot;######################################################################&quot;);
      } else {
          println!(&quot;GOOD: floats considered different when they differ past 16 significant digits&quot;);
          println!(&quot;NOTE: This block won't execute :(&quot;);
      }
  }
  </code></pre>
  <p>I understand floating point arithmetic can be tricky but wanting to know if the trickiness also affects printing and performing equality checks on f64's. Here is the output from the above code:</p>
  <pre><code>Checking f64 PI...
  Definition: pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
  Print it:                       3.14159265358979311599796346854418516
  Different after 16 significant digits ----------|                         
  ##############################################################################
  Question 1: Why do the digits differ after 16 significant digits when printed?
  ##############################################################################
  PERFORM ASSERTIONS...
  PERFORM EQUALITY CHECK...
  BAD: floats considered equal even when they differ past 16 significant digits
  ######################################################################
  Question 2: Why does equality checking use only 16 significant digits?
  They are defined using 36 significant digits so why can't we perform
  an equality check with this accuracy?
  ######################################################################
  </code></pre>