diff --git a/fx_s2308_longlong.c b/fx_s2308_longlong.c index c274a357a768bb8ee22e430335ff316b1b260881..a242072a100dec4cab8b67999d209efff2d02d48 100644 --- a/fx_s2308_longlong.c +++ b/fx_s2308_longlong.c @@ -16,7 +16,7 @@ const fx_s2308 fx_s2308_SinTable[92] = }; -fx_s2308 fx_s2308_sine(fx_s2308 fa) +fx_s2308 fx_s2308_sind(fx_s2308 fa) { int sign = 1; fixed32 ret0, diff; @@ -37,5 +37,5 @@ fx_s2308 fx_s2308_sine(fx_s2308 fa) idx = fa>>8; ret0 = fx_s2308_SinTable[idx]; diff = fx_s2308_SinTable[idx+1]-ret0; - return ( sign *( ret0 + ((diff*(fa&0xFFFF))>>8))); + return ( sign *( ret0 + ((diff*(fa&0x00FF))>>8))); } diff --git a/fx_s2308_longlong.h b/fx_s2308_longlong.h index e8ad94b259d314b9fabfa857610deca8d7c46e01..7f19587fc404930f375f8822937f9774afb94611 100644 --- a/fx_s2308_longlong.h +++ b/fx_s2308_longlong.h @@ -7,12 +7,12 @@ #define fx_2308_l_mul3(a,b) (int)(((a)*(b)) >> 8) #define fx_2308_l_mul4(a,b) (int)(((a>>4)*(b>>4))) -#define fx_s2308_l_div1 (int)((long long)(a)/(b)) -#define fx_s2308_l_div2 (int)((a<<8)/(b)) +#define fx_s2308_l_div1(a,b) (int)((long long)(a)/(b)) +#define fx_s2308_l_div2(a,b) (int)((a<<8)/(b)) typedef int fx_s2308; typedef long fixed32; -fx_s2308 fx32_sind(fx_s2308 fa); +fx_s2308 fx_s2308_sind(fx_s2308 fa); diff --git a/test.c b/test.c index 39514a4876a697c74d69196cd52efcaecdc3139a..fa2e63bcf44e2d94a5d9c73d3a045315f3baffd1 100644 --- a/test.c +++ b/test.c @@ -13,26 +13,26 @@ int main() { scanf("%lf %lf", &a, &b); f_a=DOUBLE_TO_FX(a); f_b=DOUBLE_TO_FX(b); - printf("\n====== Calculation =======\n"); - printf("\t 1. Add: Exact answer: %lf, Fixed Point Result: %lf\n", a+b, FX_TO_DOUBLE(FX_ADD(f_a,f_b))); - printf("\t 2. Subtraction: Exact answer: %lf, Fixed Point Result: %lf\n", a-b, FX_TO_DOUBLE(FX_SUB(f_a,f_b))); - printf("\t 3. Multiplication: Exact answer: %lf, Fixed Point Result: %lf\n", a*b, FX_TO_DOUBLE(FX_MUL(f_a,f_b))); - printf("\t 4. Division: Exact answer: %lf, Fixed Point Result: %lf\n", a/b, FX_TO_DOUBLE(FX_DIV(f_a,f_b))); + printf("\n\t====== Calculation =======\n"); + printf("\n\t 1. Add: Exact answer: %lf, Fixed Point Result: %lf\n", a+b, FX_TO_DOUBLE(FX_ADD(f_a,f_b))); + printf("\n\t 2. Subtraction: Exact answer: %lf, Fixed Point Result: %lf\n", a-b, FX_TO_DOUBLE(FX_SUB(f_a,f_b))); + printf("\n\t 3. Multiplication: Exact answer: %lf, Fixed Point Result: %lf\n", a*b, FX_TO_DOUBLE(FX_MUL(f_a,f_b))); + printf("\n\t 4. Division: Exact answer: %lf, Fixed Point Result: %lf\n", a/b, FX_TO_DOUBLE(FX_DIV(f_a,f_b))); - printf("\n====== Function =======\n"); - printf("\t 1. Sine(%lf): Exact answer: %lf, Fixed Point Result: %lf\n", a, sin(a*(PI/180)), FX_TO_DOUBLE(fx_s2308_double_sine(f_a))); - printf("\t 2. Sqrt(%lf): Exact answer: %lf, Fixed Point Result: %lf\n", a, sqrt(a), FX_TO_DOUBLE(fx_s2308_double_sqrt(f_a))); - printf("\t 3. Power(%lf,2): Exact answer: %lf, Fixed Point Result: %lf\n", a, pow(a,2), FX_TO_DOUBLE(fx_s2308_double_pow(f_a,2))); + printf("\n\t====== Function =======\n"); + printf("\n\t 1. Sine(%lf): Exact answer: %lf, Fixed Point Result: %lf\n", a, sin(a*(PI/180)), FX_TO_DOUBLE(fx_s2308_double_sine(f_a))); + printf("\n\t 2. Sqrt(%lf): Exact answer: %lf, Fixed Point Result: %lf\n", a, sqrt(a), FX_TO_DOUBLE(fx_s2308_double_sqrt(f_a))); + printf("\n\t 3. Power(%lf,2): Exact answer: %lf, Fixed Point Result: %lf\n", a, pow(a,2), FX_TO_DOUBLE(fx_s2308_double_pow(f_a,2))); - printf("\n====== Conversion =======\n"); - printf("\t 1. Fx to Double: fx_s2308 %d is changed into double %lf\n", f_a, FX_TO_DOUBLE(f_a)); - printf("\t 2. Fx to Float: fx_s2308 %d is changed into float %f\n", f_a, FX_TO_FLOAT(f_a)); - printf("\t 3. Fx to Int: fx_s2308 %d is changed into int %d\n", f_a, FX_TO_INT(f_a)); - printf("\t 4. Fx to Short: fx_s2308 %d is changed into short %hd\n", f_a, FX_TO_SHORT(f_a)); + printf("\n\t====== Conversion =======\n"); + printf("\n\t 1. Fx to Double: fx_s2308 %d is changed into double %lf\n", f_a, FX_TO_DOUBLE(f_a)); + printf("\n\t 2. Fx to Float: fx_s2308 %d is changed into float %f\n", f_a, FX_TO_FLOAT(f_a)); + printf("\n\t 3. Fx to Int: fx_s2308 %d is changed into int %d\n", f_a, FX_TO_INT(f_a)); + printf("\n\t 4. Fx to Short: fx_s2308 %d is changed into short %hd\n", f_a, FX_TO_SHORT(f_a)); - printf("\n====== Constant =======\n"); - printf("\t 1. PI: PI to fx_2308: %d\n",FX_PI); - printf("\t 2. 1/PI: 1/PI to fx_2308: %d\n",FX_INV_PI); + printf("\n\t====== Constant =======\n"); + printf("\n\t 1. PI: PI to fx_2308: %d\n",FX_PI); + printf("\n\t 2. 1/PI: 1/PI to fx_2308: %d\n",FX_INV_PI); printf("\n======STEP 2: fx_s2308 long long Test=======\n"); @@ -48,18 +48,28 @@ int main() { fb=DOUBLE_TO_FX(db); fc=fx_2308_l_mul(fa,fb); + printf("\n\t====== Multiplication =======\n"); + printf("\n\tmul1: Exact answer = %f func result = %f, error = %f\n", da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); + fc=fx_2308_l_mul2(fa,fb); + printf("\n\tmul2: Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); + fc=fx_2308_l_mul3(fa,fb); + printf("\n\tmul3: Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); + fc=fx_2308_l_mul4(fa,fb); + printf("\n\tmul4: Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); + + printf("\n\t====== Division =======\n"); + fc=fx_s2308_l_div1(fa,fb); + printf("\n\t\tdiv1: Exact answer = %f func result = %f, error = %f\n",da/db, FX_TO_DOUBLE(fc), da/db-FX_TO_DOUBLE(fc)); + fc=fx_s2308_l_div2(fa,fb); + printf("\n\tdiv2: Exact answer = %f func result = %f, error = %f\n",da/db, FX_TO_DOUBLE(fc), da/db-FX_TO_DOUBLE(fc)); + + printf("\n\t====== Sine =======\n"); + printf("\n\t fx_s2308 long long sine 30 = %lf", FX_TO_DOUBLE(fx_s2308_sind(DOUBLE_TO_FX(30)))); + + - printf("Exact answer = %f func result = %f, error = %f\n", da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); - fc=fx_2308_l_mul2(fa,fb); - - printf("Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); - - fc=fx_2308_l_mul3(fa,fb); - printf("Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc)); - fc=fx_2308_l_mul4(fa,fb); - printf("Exact answer = %f func result = %f, error = %f\n",da*db, FX_TO_DOUBLE(fc), da*db-FX_TO_DOUBLE(fc));