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 shift_2pi(long double x);
- long double old_s21_atan(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;
- // t = s21_PI_POPOLAM;
- // long double test_tan = s21_tan(t);
- // long double orig_tan = tanl(t);
- // printf("tan: %Lf\n", s21_tan(t));
- // printf("orig tan: %Lf\n", tan(t));
- // printf("difference: %.40Lf\n", test_tan - orig_tan);
- // t = 1.570796;
- // t = s21_PI_POPOLAM;
- t = 3;
- double test_exp = s21_exp(t);
- double orig_exp = exp(t);
- printf("exp: %f\n", test_exp);
- printf("orig exp: %f\n", orig_exp);
- printf("difference: %.40f\n", test_exp - orig_exp);
- // 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 = -1;
- // double n = -501;
- // long double x = powl(t, n);
- // long double y = s21_pow(t, n);
- // 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 = 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);
- // printf("%.40Lf\n", s21_sqrt(2));
- // printf("%.40f\n", sqrt(2));
- 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 {
- long double dif = 1;
- long double res2;
- int k = 0;
- while (dif > PREC) {
- double current = 1;
- for (int j = 1; j <= k; ++j) {
- current *= x;
- current /= j;
- }
- res += current;
- if (k > 0) dif = s21_fabs(res - res2);
- res2 = res;
- k++;
- }
- }
- 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;
- long double dif = 1;
- long double res2;
- int k = 0;
- while (dif > PREC) {
- long double current = 1;
- for (int j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- current /= (j);
- }
- res += s21_pow(-1, k) * current;
- if (k > 0) dif = s21_fabs(res - res2);
- res2 = res;
- k++;
- }
- return res * f_minus;
- }
- long double s21_cos(double x) {
- x = shift_2pi(x);
- return s21_sin(s21_PI_POPOLAM - x);
- }
- 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++;
- }
- res = s21_log(x) + counter * s21_LOG_10;
- } else if (x < 0.1) {
- int counter = 0;
- while (x < 0.1) {
- x *= 10;
- counter++;
- }
- res = s21_log(x) - counter * s21_LOG_10;
- } else {
- x -= 1;
- long double dif = 1;
- long double res2 = x;
- int k = 1;
- while (dif > PREC) {
- long double current = 1;
- for (int j = 1; j <= k; ++j) {
- current *= x;
- }
- current /= k;
- res += pow(-1, k + 1) * current;
- if (k > 1) dif = fabs(res - res2);
- res2 = res;
- k++;
- }
- }
- 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;
- 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) {
- long double res = 0;
- int flag_minus = 1;
- if (base < 0 && s21_fabs(exp - (int)exp) > 0.000001) {
- res = 0 / 0.0;
- } else {
- res = s21_exp(exp * s21_log(s21_fabs(base)));
- }
- if (base < 0 && s21_abs((int)exp % 2) == 1) {
- flag_minus = -1;
- }
- // printf("%d\n", (int)exp % 2);
- return flag_minus * res;
- }
- 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 / s21_sqrt(2)) {
- res = s21_PI_POPOLAM - s21_asin(s21_sqrt(1 - x * x));
- } else {
- long double dif = 1;
- long double res2 = x;
- int k = 0;
- while (dif > PREC) {
- 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;
- }
- res += current;
- if (k > 0) dif = s21_fabs(res - res2);
- res2 = res;
- k++;
- }
- }
- 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 / s21_sqrt(1 + x * x));
- }
- return f_minus * res;
- }
- long double old_s21_atan(double x) {
- long double res = 0;
- long double dif = 1;
- long double res2 = x;
- int k = 0;
- while (dif > PREC) {
- long double current = 1;
- int j;
- for (j = 1; j <= 2 * k + 1; ++j) {
- current *= x;
- }
- if (j != 0) {
- current /= (j - 1);
- }
- res += s21_pow(-1, k) * current;
- if (k > 0) dif = s21_fabs(res - res2);
- res2 = res;
- k++;
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement