Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "s21_math.h"
- #include <math.h>
- #include <stdio.h>
- double int_pow(double x, int n);
- long long int factorial(int x);
- double shift_2pi(long double x);
- long double old_s21_atan(double x);
- long double old_s21_log(double x);
- float my_faster_logf(float a);
- long double other_s21_log(double x);
- int main() {
- // long double y = -5.55555;
- // printf("%Lf\n", s21_fabs(y));
- // printf("%f\n\n", fabs(y));
- // printf("%f\n", floor(-4.4));
- // printf("%Lf\n", s21_floor(-4.4));
- // printf("%f", exp(1));
- // long long int test = factorial(25);
- // printf("%lld", test);
- double t = s21_PI * 756745.6534643; // сходится
- t = 5.1945045; // сходится
- t = -s21_PI * 756450.5; // cходится
- t = s21_PI * 756450; // cходится
- t = s21_PI * 75645000; // сходится
- t = s21_PI * 756450000.546; // сходится
- // t = 100000000; // сходится
- // t = 10000000040; // сходится 10 знаков
- // t = s21_PI_POPOLAM + s21_PI_POPOLAM;
- // t = 10000000001.0;
- // t = 10000000002.0;
- // t = 19099100000; // не сходится последний знакЪ
- // t = 1000000000100.0;
- // t = 57.739537;
- // t = 57.73932712;
- // t = 2707.543636;
- // t = 2707.543615;
- // t= 2713.02345678;
- // t = 1000000.645646;
- // t = 2707;
- // t = s21_PI/2;
- // t = -s21_PI/2;
- // t = 710;
- // t = 2*s21_PI;
- // t = s21_PI_11;
- // t = s21_PI_10;
- // t = 2707;
- // printf("%Lf\n", s21_sin(t));
- // printf("%f\n", sin(t));
- // printf("%Lf\n", s21_cos(t));
- // printf("%f\n", cos(t));
- // printf("%Lf\n", s21_log(t));
- // printf("%f\n", log(t));
- // printf("%Lf\n", s21_sqrt(t));
- // printf("%f\n", sqrt(t));
- // double pi_d = 314159265358.979323846264338;
- // printf("%.15f\n", pi_d);
- // long double pi_l = 314159265358.979323846264338;
- // printf("%.15Lf\n", pi_l);
- // printf("%.15f\n", s21_PI_9);
- // long double test_exp = s21_exp(t);
- // printf("%f\n", (double)test_exp);
- // printf("%f", exp(t));
- // long double test_sin = s21_sin(t);
- // printf("our sin = %Lf\n", test_sin);
- // printf("origin sin = %f\n", sin(t));
- // printf("pi = %.40f\n", s21_PI);
- // printf("origin pi = %.40f\n", M_PI);
- // printf("1000000*pi = %.40f\n\n", s21_PI * 1000000);
- // printf("%Lf\n", s21_ceil(5.999999999999999999999));
- // printf("%f\n", ceil(5.999999999999999999));
- // printf("%Lf\n", s21_floor(0));
- // printf("%f\n", floor(0));
- // printf("%f\n", sin(0));
- //t = 5 * s21_PI;
- t=1.570796;
- long double test_tan = s21_tan(t);
- long double orig_tan = tan(t);
- printf("tan: %Lf\n", s21_tan(t));
- printf("orig tan: %f\n", tan(t));
- printf("difference: %.40Lf\n", test_tan - orig_tan);
- // t = -0.567897654;
- // t = -21345;
- // t = 345;
- // t = 0.9;
- // printf("%f\n", asin(t));
- // printf("%Lf\n", s21_asin(t));
- // printf("%f\n", acos(t));
- // printf("%Lf\n", s21_acos(t));
- // t = 0.4;
- // double x = atan(t);
- // double y = s21_atan(t);
- // double z = old_s21_atan(t);
- // printf("original atan: %.40lf\t\n", x);
- // printf("s21 atan: %.40lf\n", y);
- // printf("difference origin and new: %.40lf\t\n", x - y);
- // printf("difference new and old: %.40lf\t\t\n", y - z);
- // printf("difference origin and old: %.40lf\t\n", x - z);
- //t = 2.56789;
- //t=1000000000.0;
- //t = 0.00001;
- // t = 1;
- //t = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0;
- // double or_log = log(t);
- // long double new_log = old_old_s21_log(t);
- // printf("original log = %.40f\n", or_log);
- // printf(" new log = %.40Lf\n", new_log);
- // printf("difference origin and old: %.40Lf\t\n", new_log - or_log);
- // t = (float)M_PI_2;
- // t = (float)s21_PI_POPOLAM;
- // t = s21_PI_6;
- // double or_exp = tan(t);
- // long double new_exp = s21_tan(t);
- // printf("original exp = %.40f\n", or_exp);
- // printf(" new exp = %.40Lf\n", new_exp);
- // printf("difference origin and old: %.40Lf\t\n", new_exp - or_exp);
- // t = 10000000000;
- // double or_sin = sin(t);
- // long double new_sin = s21_sin(t);
- // printf("original sin = %.40f\n", or_sin);
- // printf(" new sin = %.40Lf\n", new_sin);
- // printf("difference origin and old: %.40Lf\t\n", new_sin - or_sin);
- return 0;
- }
- long int s21_abs(int x) {
- if (x < 0) {
- x = x * (-1);
- }
- return x;
- }
- long double s21_fabs(double x) {
- if (x < 0) {
- x = x * (-1);
- }
- return (long double)x;
- }
- long double s21_floor(double x) {
- double t = x;
- x = (long double)(int)x;
- if (x < 0 && (int)t - t != 0) x -= 1;
- return x;
- }
- long double s21_ceil(double x) {
- double t = x;
- x = (long double)(int)x;
- if (x > 0 && (int)t - t != 0) x += 1;
- return x;
- }
- long double s21_fmod(double x, double y) {
- double res1, res2, res;
- res1 = x / y;
- if (x < 0 && y < 0) {
- res2 = s21_abs(res1);
- } else if (x < 0 || y < 0) {
- res2 = (-1) * s21_abs(res1);
- } else {
- res2 = s21_abs(res1);
- }
- res = x - res2 * y;
- if (y == 0) {
- res = 0 / 0.0;
- }
- return res;
- }
- long double s21_exp(double x) {
- long double res = 0;
- // if (x > 5) {
- // res = s21_exp(x-5)*exp(5);
- // }
- if (x<0){
- res = 1/s21_exp((-1)*x);
- } else {
- for (int k = 0; k < 2000; ++k) {
- double current = 1;
- for (int j = 1; j <= k; ++j) {
- current*=x;
- current /= j;
- }
- res += current;
- }
- }
- return res;
- }
- double int_pow(double x, int n) {
- double res = 1;
- for (int i = 0; i < n; i++) {
- res = res * x;
- // printf("%");
- }
- return res;
- }
- long long int factorial(int x) {
- long long int res = 0;
- if (x == 0)
- res = 1;
- else
- res = x * factorial(x - 1);
- return res;
- }
- long double s21_sin(double x) {
- int f_minus = 1;
- if (x < 0) {
- f_minus = -1;
- x = (-1)*x;
- }
- x = shift_2pi(x);
- long double res = 0;
- for (int k = 0; k < 30; k++) {
- // long double current = pow(x, 2 * k + 1);
- long double current = 1;
- for (int j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- current /= (j);
- // printf("%Lf\n", current);
- }
- res += pow(-1, k) * current;
- // printf("%Lf\n", res);
- }
- return res*f_minus;
- }
- long double s21_cos(double x) {
- x = shift_2pi(x);
- return s21_sin(s21_PI_POPOLAM - x);
- }
- long double old_s21_log(double x) {
- double yn = x - 1.0;
- double yn1 = yn;
- double epsilon = 0.000001;
- int counter = 0;
- do {
- yn = yn1;
- // printf("yn: %f\n", yn);
- // printf("exp: %f\n", exp(yn));
- yn1 = yn + 2 * (x - exp(yn)) / (x + exp(yn));
- // printf("yn1: %f\n", yn1);
- counter++;
- } while (s21_fabs(yn - yn1) > epsilon);
- // printf("%d\n", counter);
- return yn1;
- }
- long double s21_log(double x) {
- long double res = 0;
- if (x < 0) {
- res = 0 / 0.0; // nan
- } else if (x == 0) {
- res = -1 / 0.0;
- } else if (x>1) {
- int counter = 0;
- while(x>1) {
- x/=10;
- counter++;
- }
- printf("x =%f\n", x);
- printf("c =%d\n", counter);
- res = s21_log(x)+counter*other_s21_log(10);
- } else if (x < 0.1){
- int counter = 0;
- while(x<0.1) {
- x*=10;
- counter++;
- }
- res = s21_log(x)-counter*other_s21_log(10);
- } else{
- x -= 1;
- for (int k = 1; k < 1000; k++) {
- // printf("k=%d\n", k);
- long double current = 1;
- for (int j = 1; j <= k; ++j) {
- current *= x;
- }
- current /= k;
- res += pow(-1, k + 1) * current;
- }
- }
- return res;
- }
- long double s21_tan(double x) {
- long double res = 0;
- int flag_minus = 1;
- if (x < 0) {
- x = -x;
- flag_minus = -1;
- }
- x = shift_2pi(x);
- while (x > s21_PI)
- x-=s21_PI;
- x = x*flag_minus;
- // for (int k = 1;k<2000000;k++)
- // res+=1/((k-0.5)*(k-0.5)*s21_PI*s21_PI-x*x);
- res = s21_sin(x)/s21_cos(x);
- return res;
- }
- long double s21_sqrt(double x) { return s21_pow(x, 0.5); }
- long double s21_pow(double base, double exp) { return s21_exp(exp * s21_log(base)); }
- double shift_2pi(long double x) {
- int flag_minus = 1;
- if (x < 0) {
- x = -x;
- flag_minus = -1;
- }
- while (x > s21_PI_9)
- x -= s21_PI_9;
- while (x > s21_PI_8)
- x -= s21_PI_8;
- while (x > s21_PI_7)
- x -= s21_PI_7;
- while (x > s21_PI_6)
- x -= s21_PI_6;
- while (x > s21_PI_5)
- x -= s21_PI_5;
- while (x > s21_PI_4)
- x -= s21_PI_4;
- while (x > s21_PI_3)
- x -= s21_PI_3;
- while (x > s21_PI_2)
- x -= s21_PI_2;
- while (x > 2 * s21_PI)
- x -= 2 * s21_PI;
- return flag_minus*x;
- }
- long double s21_asin(double x) {
- int flag_minus = 1;
- if (x<0) {
- x = -x;
- flag_minus = -1;
- }
- long double res = 0;
- if (x > 1)
- res = 0 / 0.0;
- else if (x > 1/sqrt(2)) {
- res = s21_PI_POPOLAM - s21_asin(sqrt(1-x*x));
- }
- else {
- for (int k = 0; k < 3000; k++) {
- // long double current = pow(x, 2 * k + 1);
- long double current = 1;
- for (int j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- if (j % 2 == 0 || j == 2 * k + 1)
- current /= j;
- else
- current *= j;
- // printf("%Lf\n", current);
- }
- // if (k != 0) current /= k;
- res += current;
- // printf("%Lf\n", res);
- }
- }
- return flag_minus*res;
- }
- long double s21_acos(double x) { return s21_PI_POPOLAM - s21_asin(x); }
- long double s21_atan(double x) {
- long double res = 0;
- int f_minus = 1;
- if (x < 0) {
- x *= (-1);
- f_minus = -1;
- }
- if (x < 0.9)
- res = old_s21_atan(x);
- else if (x > 1) {
- res = s21_PI_POPOLAM - s21_atan(1 / x);
- } else {
- res = s21_asin(x / sqrt(1 + x * x));
- }
- return f_minus*res;
- }
- long double old_s21_atan(double x) {
- long double res = 0;
- for (int k = 0; k < 5000; k++) {
- long double current = 1;
- int j;
- for (j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- // printf("current in *=: %Lf\n", current);
- }
- if (j != 0) {
- current /= (j - 1);
- // printf("current in /: %Lf\n", current);
- }
- res += pow(-1, k) * current;
- // printf("res: %Lf\n", res);
- }
- return res;
- }
- long double other_s21_log(double x) {
- x = (x - 1) / (x + 1);
- //printf(" x after proc = %.40f\n", x);
- long double res = 0;
- for (int k = 0; k < 5000; k++) {
- long double current = 1;
- int j;
- for (j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- // printf("current in *=: %Lf\n", current);
- }
- if (j != 0) {
- current /= (j - 1);
- // printf("current in /: %Lf\n", current);
- }
- res += current;
- // printf("res: %Lf\n", res);
- }
- return 2 * res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement