diff --git a/fx_3132.c b/fx_3132.c index 3de2d91e31df6719a6944a956a05eba7928e7749..5eba89908c7a426f24424b9ca4de62137cc28b7b 100644 --- a/fx_3132.c +++ b/fx_3132.c @@ -1,6 +1,6 @@ #include <stdio.h> #include <math.h> -#include "fx_3132.h" +#include "fx_s3132.h" fx_3132 sine(fx_3132 a) { return double_to_fx(sin(fx_to_double(a))); @@ -17,4 +17,4 @@ fx_3132 pi() { }; fx_3132 piReverse() { return double_to_fx((fx_to_double(1 / fx_PI))); -}; \ No newline at end of file +}; diff --git a/fx_s3132.h b/fx_s3132.h new file mode 100644 index 0000000000000000000000000000000000000000..ac849a4f4c1366aa120c30608d75b410ce5ad392 --- /dev/null +++ b/fx_s3132.h @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <math.h> +#define P2_2to32 4294967296.0 +#define PI 3.141592653 +#define fx_to_double(a) (a/P2_2to32) +#define double_to_fx(a) (long long)(a*P2_2to32) +#define fx_add(a, b) ((a)+(b)) +#define fx_sub(a, b) ((a)-(b)) +#define fx_mul(a, b) double_to_fx(fx_to_double (a) * fx_to_double (b)) +#define fx_mul2(a, b) (double)a * (double)b / P2_2to32 +#define fx_div(a, b) double_to_fx(fx_to_double (a) / fx_to_double (b)) +#define fx_sin(a) double_to_fx(sin(fx_to_double(a))) +#define fx_sqrt(a) double_to_fx(sqrt(fx_to_double(a))) +#define fx_power(a, b) double_to_fx(pow(fx_to_double(a), fx_to_double(b))) +#define fx_log10(a) double_to_fx(log10(fx_to_double(a))) +#define fx_ln(a) double_to_fx(log(fx_to_double(a))) +#define fx_PI() (double_to_fx(PI)) +#define fx_PI_Reverse() (double_to_fx((1/PI))) + +typedef long long fx_3132; diff --git a/fx_s3132_double.h b/fx_s3132_double.h deleted file mode 100644 index fd2de9531a2c45080ee7a7d5b63feeab8a6d8c4b..0000000000000000000000000000000000000000 --- a/fx_s3132_double.h +++ /dev/null @@ -1,31 +0,0 @@ -#define P2_2to32 4294967296.0 -#define fx_to_double(a) a/P2_2to32 -#define double_to_fx(a) (long long)(a*P2_2to32) -#define fx_add(a, b) ((a)+(b)) -#define fx_sub(a, b) ((a)-(b)) -#define fx_mul(a, b) double_to_fx(fx_to_double (a) * fx_to_double (b)) -#define fx_mul2(a, b) (double)a * (double)b / P2_2to32 -#define fx_div(a, b) double_to_fx(fx_to_double (a) / fx_to_double (b)) -#define fx_PI double_to_fx(3.141592653) - -typedef long long fx_3132; - -#include <stdio.h> -#include <math.h> - -fx_3132 sine(fx_3132 a) { - return double_to_fx(sin(fx_to_double(a))); -}; -fx_3132 sqrt1(fx_3132 a) { - return double_to_fx(sqrt(fx_to_double(a))); -}; -fx_3132 power(fx_3132 a, fx_3132 b) { - return double_to_fx(pow(fx_to_double(a), fx_to_double(b))); -}; - -fx_3132 pi() { - return double_to_fx(fx_to_double(fx_PI)); -}; -fx_3132 piReverse() { - return double_to_fx((fx_to_double(1 / fx_PI))); -}; \ No newline at end of file diff --git a/test.c b/test.c index 32a706728e6ab7335df7340b90617d20271d06a8..70e1d48b1c0ef8e27424bbbae35906b838787967 100644 --- a/test.c +++ b/test.c @@ -1,11 +1,12 @@ #include <stdio.h> -#include "fx_s3132_double.h" +#include "fx_s3132.h" int main() { int choose; long long num1, num2; + double dnum1; int a; - + while (1) { printf("\nChoose the function\n"); printf("1. Operation 2. Function 3. Conversion 4. Constant 5. End\n"); @@ -38,7 +39,7 @@ int main() { } else if (choose == 2) { printf("Choose the function.\n"); - printf("1. Sine 2. Sqrt 3. Power\n"); + printf("1. Sine 2. Sqrt 3. Power 4. log10 5. ln\n"); fflush(stdout); scanf("%d", &a); @@ -47,14 +48,14 @@ int main() { fflush(stdout); scanf("%lld", &num1); - printf("Result: %lld\n", sine(num1)); + printf("Result: %lld\n", fx_sin(num1)); } else if (a == 2) { printf("Input 1 fixed point number.\n"); fflush(stdout); scanf("%lld", &num1); - printf("Result: %lld\n", sqrt1(num1)); + printf("Result: %lld\n", fx_sqrt(num1)); } else if (a == 3) { printf("Input 2 fixed point number.\n"); @@ -62,7 +63,21 @@ int main() { scanf("%lld", &num1); scanf("%lld", &num2); - printf("Result: %lld\n", power(num1, num2)); + printf("Result: %lld\n", fx_power(num1, num2)); + } + else if (a == 4) { + printf("Input 1 fixed point number.\n"); + fflush(stdout); + scanf("%lld", &num1); + + printf("Result: %lld\n", fx_log10(num1)); + } + else if (a == 5) { + printf("Input 1 fixed point number.\n"); + fflush(stdout); + scanf("%lld", &num1); + + printf("Result: %lld\n", fx_ln(num1)); } } else if (choose == 3) { @@ -71,15 +86,17 @@ int main() { fflush(stdout); scanf("%d", &a); - printf("Input 1 fixed point number.\n"); - fflush(stdout); - scanf("%lld", &num1); - if (a == 1) { - printf("Result: %lld\n", fx_to_double(num1)); + printf("Input 1 fixed point number.\n"); + fflush(stdout); + scanf("%lld", &num1); + printf("Result: %lf\n", fx_to_double(num1)); } else if (a == 2) { - printf("Result: %lld\n", double_to_fx(num1)); + printf("Input 1 double number.\n"); + fflush(stdout); + scanf("%lf", &dnum1); + printf("Result: %lld\n", double_to_fx(dnum1)); } } else if (choose == 4) { @@ -89,10 +106,10 @@ int main() { scanf("%d", &a); if (a == 1) { - printf("Result: %lld\n", pi()); + printf("Result: %lld\n", fx_PI()); } else if (a == 2) { - printf("Result: %lld\n", piReverse()); + printf("Result: %lld\n", fx_PI_Reverse()); } } else if (choose == 5) {