x = 0.25
N = 5
Series Cosine(x) = 0.968912421711
C++ cos(x) = 0.968912421711
Series sine(x) = 0.247403959255
C++ sin(x) = 0.247403959255
Series Tangent(x) = 0.255341921221
C++ tan(x) = 0.255341921221
Series sin(2x) = 0.479425538604
C++ sin(2x) = 0.479425538604
C++ 2sin(x)cos(x) = 0.479425538604
End app ? y = yes = n
x = 0.5
N = 5
Series Cosine(x) = 0.877582561890
C++ cos(x) = 0.877582561890
Series sine(x) = 0.479425538604
C++ sin(x) = 0.479425538604
Series Tangent(x) = 0.546302489844
C++ tan(x) = 0.546302489844
Series sin(2x) = 0.841470984807
C++ sin(2x) = 0.841470984808
C++ 2sin(x)cos(x) = 0.841470984808
End app ? y = yes = y
x = 0.75
N = 5
Series Cosine(x) = 0.731688868808
C++ cos(x) = 0.731688868874
Series sine(x) = 0.681638760020
C++ sin(x) = 0.681638760023
Series Tangent(x) = 0.931596460023
C++ tan(x) = 0.931596459944
Series sin(2x) = 0.997494986509
C++ sin(2x) = 0.997494986604
C++ 2sin(x)cos(x) = 0.997494986604
End app ? y = yes = n
x = 1.00
N = 5
Series Cosine(x) = 0.540302303792
C++ cos(x) = 0.540302305868
Series sine(x) = 0.841470984648
C++ sin(x) = 0.841470984808
Series Tangent(x) = 1.557407730344
C++ tan(x) = 1.557407724655
Series sin(2x) = 0.909297423159
C++ sin(2x) = 0.909297426826
C++ 2sin(x)cos(x) = 0.909297426826
End app ? y = yes = n
x = 1.25
N = 5
Series Cosine(x) = 0.315322332275
C++ cos(x) = 0.315322362395
Series sine(x) = 0.948984616456
C++ sin(x) = 0.948984619356
Series Tangent(x) = 3.009569952151
C++ tan(x) = 3.009569673863
Series sin(2x) = 0.598472085108
C++ sin(2x) = 0.598472144104
C++ 2sin(x)cos(x) = 0.598472144104
End app ? y = yes = n
x = 1.50
N = 5
Series Cosine(x) = 0.070736934117
C++ cos(x) = 0.070737201668
Series sine(x) = 0.997494955682
C++ sin(x) = 0.997494986604
Series Tangent(x) = 14.101472846329
C++ tan(x) = 14.101419947172
Series sin(2x) = 0.141119469924
C++ sin(2x) = 0.141120008060
C++ 2sin(x)cos(x) = 0.141120008060
End app ? y = yes = y
// DivMulPowerSeries.cpp (c) Tuesday, April 8, 2025
// by James Pate Williams, Jr.
// https://math.libretexts.org/Bookshelves/Calculus/Calculus_(OpenStax)/10%3A_Power_Series/10.02%3A_Properties_of_Power_Series
// https://en.wikipedia.org/wiki/Formal_power_series
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
std::vector<double> Multiplication(
std::vector<double> c,
std::vector<double> d,
int N) {
std::vector<double> e(N + 1);
for (int n = 0; n <= N; n++) {
double sum = 0.0;
for (int k = 0; k <= n; k++) {
sum += c[k] * d[n - k];
}
e[n] = sum;
}
return e;
}
std::vector<double> Division(
std::vector<double> a,
std::vector<double> b,
int N) {
double a0 = a[0];
std::vector<double> c(N + 1);
for (int n = 0; n <= N; n++) {
double sum = 0.0;
for (int k = 1; k <= n; k++) {
sum += a[k] * c[n - k];
}
c[n] = (b[n] - sum) / a0;
}
return c;
}
double Factorial(int n) {
double nf = 1.0;
for (int i = 2; i <= n; i++) {
nf *= i;
}
return nf;
}
std::vector<double> Cosine(double x, int N, double& fx) {
std::vector<double> series(N + 1);
fx = 0.0;
for (int n = 0; n <= N; n++) {
int argument = 2 * n;
double coeff = pow(-1, n) / Factorial(argument);
series[n] = coeff;
fx += coeff * pow(x, argument);
}
return series;
}
std::vector<double> Sine(double x, int N, double& fx) {
std::vector<double> series(N + 1);
fx = 0.0;
for (int n = 0; n <= N; n++) {
int argument = 2 * n + 1;
double coeff = pow(-1, n) / Factorial(argument);
series[n] = coeff;
fx += coeff * pow(x, argument);
}
return series;
}
std::vector<double> Tangent(double x, int N, double& fx) {
double fc = 0.0, fs = 0.0;
std::vector<double> seriesC = Cosine(x, N, fc);
std::vector<double> seriesS = Sine(x, N, fs);
std::vector<double> seriesT = Division(seriesS, seriesC, N);
fx = fs / fc;
return seriesT;
}
std::vector<double> Sine2x(double x, int N, double& fx) {
double fc = 0.0, fs = 0.0;
std::vector<double> seriesC = Cosine(x, N, fc);
std::vector<double> seriesS = Sine(x, N, fs);
std::vector<double> series2 = Multiplication(seriesS, seriesC, N);
fx = 2.0 * fs * fc;
return series2;
}
int main()
{
while (true) {
char line[128] = { };
std::cout << "x = ";
std::cin.getline(line, 127);
std::string str1(line);
double x = std::stod(str1);
std::cout << "N = ";
std::cin.getline(line, 127);
std::string str2(line);
int N = std::stoi(str2);
double cx = 0.0, sx = 0.0, tx = 0.0, xx = 0.0;
std::vector<double> cSeries = Cosine(x, N, cx);
std::vector<double> sSeries = Sine(x, N, sx);
std::vector<double> tSeries = Tangent(x, N, tx);
std::vector<double> xSeries = Sine2x(x, N, xx);
std::cout << std::fixed << std::setprecision(12);
std::cout << "Series Cosine(x) = " << cx << std::endl;
std::cout << "C++ cos(x) = " << cos(x) << std::endl;
std::cout << "Series sine(x) = " << sx << std::endl;
std::cout << "C++ sin(x) = " << sin(x) << std::endl;
std::cout << "Series Tangent(x) = " << tx << std::endl;
std::cout << "C++ tan(x) = " << tan(x) << std::endl;
std::cout << "Series sin(2x) = " << xx << std::endl;
std::cout << "C++ sin(2x) = " << sin(x + x) << std::endl;
std::cout << "C++ 2sin(x)cos(x) = " << 2.0 * sin(x) * cos(x);
std::cout << std::endl;
std::cout << "End app ? y = yes = ";
std::cin.getline(line, 127);
if (line[0] == 'Y' || line[0] == 'y') {
break;
}
}
return 0;
}
Author: jamespatewilliamsjr
My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.
View all posts by jamespatewilliamsjr