Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace myfuncs {
- const double PI2 = M_PI * 2;
- // calculate sin using taylor series
- // D(y) = R
- double sin(double x) {
- while (abs(x) > PI2) {
- x += x > 0 ? -PI2 : PI2;
- }
- double result = 0;
- double term = x;
- int i = 1;
- while (term != 0) {
- result += term;
- term *= -1 * x * x / (2 * i * (2 * i + 1));
- i++;
- }
- return result;
- }
- // calculate cos using taylor series
- // D(y) = R
- double cos(double x) {
- while (abs(x) > PI2) {
- x += x > 0 ? -PI2 : PI2;
- }
- double result = 0;
- double term = 1;
- int i = 1;
- while (term != 0) {
- result += term;
- term *= -1 * x * x / (2 * i * (2 * i - 1));
- i++;
- }
- return result;
- }
- // calculate pow
- double pow(double x, double y) {
- double result = 1;
- if (y == 0) {
- return 1;
- }
- if (y < 0) {
- x = 1 / x;
- y = -y;
- }
- while (y > 0) {
- result *= x;
- y--;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement