title: How to write a pow() function to handle pow(2, 48) without getting infinity (overflow) in C++?
body: <pre><code>#include &lt;iostream&gt;
  
  long double abs(long double num){
          if (num&gt;=0)
                  return num;
          else
                  return -num;
  }
  
  long double pow(long double val, long double given_exp){
  
          long double res = val;
          long double exp = abs(given_exp);
  
          for (; exp &gt; 1; exp--){
                  res *= res;
          }
  
          if (given_exp &gt; 0)
                  return res;
          else
                  return 1/res;
  }
  
  int main(){
          std::cout &lt;&lt; pow(2, 1) &lt;&lt; std::endl;
          std::cout &lt;&lt; pow(2, -1) &lt;&lt; std::endl;
          std::cout &lt;&lt; pow(2, 48) &lt;&lt; std::endl;
  }
  </code></pre>
  <p>This is the second exercise in Lippman's 2nd edition.
  Right now, the last line prints inf. How do I modify this function to prevent overflow?</p>