title: Why EPSILON is used in comparing two floating point numbers
body: <p>I'm googling about how to find if a number <code>y</code> is power of <code>x</code> and came across this <a href="https://leetcode.com/articles/power-of-three/" rel="nofollow noreferrer">link</a></p>
  
  <blockquote>
    <p><strong>Java</strong></p>
  
  <pre><code>public class Solution {
  public boolean isPowerOfThree(int n) {
      return (Math.log10(n) / Math.log10(3)) % 1 == 0;
  } } 
  </code></pre>
    
    <p><strong>Common pitfalls</strong></p>
    
    <p>This solution is problematic because we start using doubles, which
    means we are subject to precision errors. This means, we should never
    use <code>==</code> when comparing doubles. That is because the result of
    <code>Math.log10(n) / Math.log10(3)</code> could be <code>5.0000001</code> or <code>4.9999999</code>. This
    effect can be observed by using the function <code>Math.log()</code> instead of
    <code>Math.log10()</code>.</p>
    
    <p>In order to fix that, we need to compare the result against an
    <code>epsilon</code>.</p>
    
    <p><strong>Java</strong></p>
    
    <p><code>return (Math.log(n) / Math.log(3) + epsilon) % 1 &lt;= 2 * epsilon;</code></p>
  </blockquote>
  
  <p>There I didn't understand <code>return (Math.log(n) / Math.log(3) + epsilon) % 1 &lt;= 2 * epsilon;</code></p>
  
  <p>What is the meaning of that line?</p>
  
  <p>Why <strong><code>EPSILON</code></strong> is used while comparing floating points?</p>