- 1

how can i write taylor series in c++?

i'm beginner so please write with common algorithms .

14th Dec 2020, 8:51 PM
alireza
alireza - avatar
3 Answers
+ 2
Do you just want the formula displayed?
15th Dec 2020, 4:12 AM
Sonic
Sonic - avatar
+ 1
if you want to approximate a function try this for example exp(x) = 1 + x/1! + x^2/2! + x^3/3! ... #include <iostream> #include <math.h> #include <iomanip> #define _MAX 20 /* if you use a numbr > 20 the variable will overflow because of factorial! */ uint64_t fact(uint64_t x) { if (x == 1) return 1; return x * fact(x - 1); } int main(int argc, char *argv[]) { long double e = 1, x = 1.6; for (uint64_t k = 1; k < _MAX; k++) e += pow(x, k) / fact(k); std::cout << std::setprecision(_MAX) << "exp(x) = " << e; }
14th Dec 2020, 9:51 PM
Alpha Zero
Alpha Zero - avatar