🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.leda
Article #14

Re: X^Y In C++

#14
From: Paul Evans
Date: Wed, 02 Aug 1995 15:11
25 lines
1225 bytes
In article <DC9nCI.HH9@inesc.pt>, pjn@novabase.pt (Paulo Jorge Cardiga Nunes) writes:

|> I'm trying to built a simple(?) routine to calculate a number powered to
|> other. (X^Y)
|> Well, it's simple, you may think, but... Y is not an integer. It's a real!!
|> So as X, but problems are not in X, but in Y!!!

template< class T > 
T toThePowerOf( T x, T y)
{
  T lnX = log( x );  // compute ln(x)
  T ylnX = y * lnX;  // compute y*ln(x)     == ln(x^y)
  T xToY = exp(ylnX) // compute e^(ln(x^y)) == x^y

  return xToY;
}

This will work for any type T that has log(), operator*() and exp() 
functions defined for it.  This includes complex numbers (!) aswell 
as reals (be they user-defined concrete data types or built-in types
such as double and float).  

Hope this helps,
Pablo
Technical Details
Message-ID: <1995Aug2.151156.9704@janix.mfr.dec.com>
Path: arc1-ca.pugleaf.net!obelix.pcs.dec.com!pevans
References: <DC9nCI.HH9@inesc.pt>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.c++.leda
Path: arc1-ca.pugleaf.net!obelix.pcs.dec.com!pevans
From: pevans@obelix.pcs.dec.com (Paul Evans)
Subject: Re: X^Y In C++
Message-ID: <1995Aug2.151156.9704@janix.mfr.dec.com>
Sender: news@janix.mfr.dec.com (SDSC USENET News System)
Reply-To: pevans@obelix.pcs.dec.com (Paul Evans)
Organization: Digital Equipment Corporation
References:  <DC9nCI.HH9@inesc.pt>
Date: Wed, 2 Aug 1995 15:11:56 GMT