sin
Alexmegawin
c_cpp
2 years ago
915 B
4
Indexable
Never
#include <iostream> using namespace std; float power(float a, int b) { float num = a; if (b == 0) { return 1; } for(int i = 1; i < b; i++) { a = a * num; } return a; } int factorial(int a) { int num = 1; for (int i = 1; i <= a; i++) { num = num * i; } return num; } float rad(int degree) { float rad; rad = (degree * 3.14) / 180; return rad; } float sin(int degree, float eps) { float sin_ = 0; float _sin = 0; int n = 1; int k = 0; while(true) { _sin = power(-1, k) * (power(rad(degree), 2 * k + 1) / factorial(2 * k + 1)); sin_ += _sin; if (_sin < eps && _sin > -eps) { return sin_; } k++; } return sin_; } int main() { float eps = 0.01; cout << sin(30, eps); } //for (int k = 0; k < 5; k++) //{ // Sin += power(-1, k) * (power(rad(degree), 2 * k + 1) / factorial(2 * k + 1)); // //}