Skip to content
Snippets Groups Projects
Commit 0c4edf84 authored by 이장원's avatar 이장원
Browse files

8.4

parent 2b8c6c5e
No related branches found
No related tags found
No related merge requests found
#pragma once
typedef int fx_s1516;
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define P2_16 65536.0f
#define e M_E
#define fx_s1516_PI M_PI
#define fx_s1516_INVERSE_PI 1/M_PI
#define degree_to_radian(a) ((a) * M_PI / 180)
#define fx_to_double(a) ((double)(a) / P2_16)
#define double_to_fx(a) ((a) * P2_16)
#define fx_s1516_add(a, b) double_to_fx((a) + (b))
#define fx_s1516_sub(a, b) double_to_fx((a) - (b))
#define fx_s1516_mul(a, b) double_to_fx((a) * (b) * P2_16)
#define fx_s1516_div(a, b) double_to_fx((a) / (b) / P2_16)
#define fx_s1516_sin(a) double_to_fx(sin(a * P2_16) / (P2_16))
#define fx_s1516_sqrt(a) double_to_fx(sqrt(a) / sqrt(P2_16))
#define fx_s1516_pow(a, b) double_to_fx(pow(a, (double)b) * pow(P2_16, (double)b - 1))
#define fx_s1516_log(a, b) double_to_fx(baselog(a * P2_16, b) / (P2_16))
double baselog(double a, double base)
{
return log(a) / log(base);
}
\ No newline at end of file
#include "fx_s1516_double.h"
/*
make first prototype fx_sxxyy_double.c with double and math.h
define macro fx_sxxyy_double.h program test.c
*/
int main()
{
fx_s1516 a = 131072;
fx_s1516 b = 65536;
double fa = fx_to_double(a);
double fb = fx_to_double(b);
printf("a : %d b : %d fa : %lf fb : %lf\noriginal a : %lf original b : %lf\n\n", a, b, fa, fb, double_to_fx(fa), double_to_fx(fb));
printf("add : %-15d %lf\n", a + b, fx_s1516_add(fa, fb));
printf("sub : %-15d %lf\n", a - b, fx_s1516_sub(fa, fb));
printf("mul : %-15lld %lf\n", (long long)a * b, fx_s1516_mul(fa, fb));
printf("div : %-15lf %lf\n\n", (double)a / b, fx_s1516_div(fa, fb));
printf("sin : %-15lf %lf\n", sin(degree_to_radian(a)), fx_s1516_sin(degree_to_radian(fa)));
printf("sqrt : %-15lf %lf\n", sqrt(a), fx_s1516_sqrt(fa));
printf("sqrt : %-15lf %lf\n", sqrt(b), fx_s1516_sqrt(fb));
printf("pow : %-15lld %lf\n", (long long)pow(a, 2), fx_s1516_pow(fa, 2));
printf("log : %-15lf %lf\n\n", baselog(a, e), fx_s1516_log(fa, e));
printf("pi : %f\n", fx_s1516_pi);
printf("inverse pi : %f\n", fx_s1516_inverse_pi);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment