title: C++: Lorentz Factor Equation
body: <p>I am entirely new to C++. I took a class in Python, but would rather continue learning in a language that is more useful. But I am redoing several of my assignments from that class in C++ just to help me get going.</p>
  
  <p>This particular problem is to write code that would solve for the Lorentz Factor with the input of a fraction of the speed of light (velocity/c) which should be between 0 and 1. I am sure there is a very simple thing that I am doing wrong; something I may not be familiar with coming from Python. But help would be appreciated. I keep getting the answer "nan". Does it have something to do with my type declarations? From my understanding, since I'm working with decimals, I should use float right?</p>
  
  <p>Here's the Lorentz equation (but keep in mind, my code is accepting v/c as one number):</p>
  
  <p><a href="https://i.stack.imgur.com/EPiyB.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EPiyB.gif" alt="http://spiff.rit.edu/classes/phys200/lectures/ke_rel/eqn_gamma.gif"></a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  
  using namespace std;
  
  float lorentz_factor (float v) {
      float answer = 1 / sqrt(1 - exp(v));
      return answer;
  }
  
  int main() {
  
      float v;
  
      cout&lt;&lt;"Please enter a number between 0 and 1";
      cin&gt;&gt; v;
      while (!((v &lt; 1) &amp;&amp; (v &gt; 0))) { // "v" should be entered as a fraction of     the speed of light.
          cout&lt;&lt;"Try again: ";
          cin&gt;&gt;v;                     // and only accepted if it is between 0 and 1
  }
      float factor = v;
      cin.ignore();
      cout&lt;&lt;"The lorentz_factor is: "&lt;&lt; lorentz_factor (factor) &lt;&lt; "\n";
      cin.get();
  }  
  </code></pre>
  
  <p>Help me on this.</p>