For large n
your program has undefined behavior.
You are calculating the factorial of 2n
(so 200) in factorial
. 200!
is, according to Wolfram Alpha:
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
For comparison, the typical largest value that a long long int
can hold is
9223372036854775807
(which is assuming it is 64-bit)
Clearly you will not be able to fit 200!
into that. When you overflow a signed integer variable your program will have undefined behavior. That means that there will be no guarantee how it will behave.
But even if you change the variable type to be unsigned, not much will change. The program won't have undefined behavior anymore, but the factorial
will not actually hold the correct value. Instead it will keep wrapping around back to zero.
Even if you change factorial
to be type double
, this will probably not be enough with at typical double
implementation to hold this value. Your platform might have a long double
type that is larger than double
and able to hold this value.
You will have similar problems with pow(x, power)
if x
is not close to 1
.
As mentioned in the answer by @idclev463035818 the Taylor series, if evaluated straightforwardly, is numerically very ill-behaved and can not really be used practically in this form for large n
.