title: How to test for -1.#IND (indeterminate) in Lua?
body: <p><strong>Irrelavent justification for the question</strong>:</p>
  
  <p>i'm getting an error calling Lua <code>format</code>:</p>
  
  <blockquote>
    <p>integer overflow attempting to store -1.#IND</p>
  </blockquote>
  
  <p>The variable's <code>type(n)</code> really <em>is</em> a <code>number</code>, and i can <code>format</code> it as a string (i.e. <code>%s</code>), but it's not a number, e.g.:</p>
  
  <pre><code>print(string.format("value=%s, type=%s", n, type(n)));
  </code></pre>
  
  <p>for the <code>NaN</code> value returns:</p>
  
  <blockquote>
    <p>value=-1.#IND, type=number</p>
  </blockquote>
  
  <p>i want to fix this, but i have no idea who is generating this <code>NaN</code> (Lua has no debugger).</p>
  
  <p>So i'm left with having to throw a lot of <code>asserts</code> all over the code until i can pin down to the source of this intermittent <code>NaN</code> value. </p>
  
  <p>But i can't find any condition that traps it, and Lua doesn't have <code>isnan(x)</code>.</p>
  
  <p><strong>Question</strong>:</p>
  
  <p>How can i test a number for <code>-1.#IND</code> in Lua?</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>i tried:</p>
  
  <pre><code>if (n ~= n) then
     print(string.format("NaN: value=%s, type=%s", n, type(n)));
  else
     print(string.format("value=%s, type=%s", n, type(n)));
  end;
  </code></pre>
  
  <p>and it prints</p>
  
  <blockquote>
    <p>value=-1.#IND, number</p>
  </blockquote>
  
  <p><strong>Update Two</strong>: Just in case i missed something, my <em>actual</em> code is:</p>
  
  <pre><code>    if (oldValue ~= oldValue) then
          print(string.format("Is NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
      else
          print(string.format("Is not NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
      end;
  </code></pre>
  
  <p>And the faulty value outputs:</p>
  
  <blockquote>
    <p>Is not NaN: labelNumber=4, formatString="%d", oldValue=-1.#IND (number)</p>
  </blockquote>
  
  <h2>Update Three</h2>
  
  <p>Still trying to solve this problem, i just noticed the absurdadity of reality:</p>
  
  <pre><code>function isnan(x)
     if type(x) ~= "number" then
         return false; --only a number can not be a number
     end;
  
     ...
  end;
  </code></pre>