📄 math.c
字号:
/************************************************************
Copyright (C), 2007 by SEED Electronic Technology LTD.
FileName: math.c
Author: Ya.X Version : V1.0 Date:2007-09-20
Description: 简单数学计算应用程序实验
*************************************************************/
#include <stdio.h>
#define Qx 4
#define Qy 4
#define Qz 4
/*in the fixed example,the default value of Q is 4.*/
/*The range of date is [-2048,2047.9375]*/
void fixed_add(int x, int y)
{
long temp=0;
int z;
temp = y << (Qx-Qy);
temp += x;
if ( Qx >= Qz )
{
z = temp>>(Qx-Qz);
}
else
{
z = temp<<(Qz-Qx);
}
printf("the result of fixed_add was %i !\n",z);
}
void fixed_sub(int x, int y)
{
long temp;
int z;
temp = y<<(Qx-Qy);
temp = x - temp;
if ( Qx >= Qz )
{
z = temp>>(Qx-Qz);
}
else
{
z = temp<<(Qz-Qx);
}
printf("the result of fixed_sub was %i !\n", z);
}
void fixed_mul(int x, int y)
{
long temp;
int z;
temp = (long)x;
z = (temp*y)>>(Qx + Qy - Qz);
printf("the result of fixed_sub was %i !\n", z);
}
void fixed_div(int x,int y)
{
long temp;
int z;
temp = (long)x;
z = (temp<<(Qz-Qx+Qy))/y;
printf("the result of fixed_div was %i !\n", z);
}
void float_add(double x,double y)
{
double z;
z = x;
z += y;
printf("the result of float_add was %10.3f !\n", z);
}
void float_sub(double x,double y)
{
double z;
z = x;
z -= y;
printf("the result of float_sub was %10.3f !\n", z);
}
void float_mul(double x,double y)
{
double z;
z = x;
z *= y;
printf("the result of float_mul was %e !\n", z);
}
void float_div(double x,double y)
{
double z;
z = x;
z /= y;
printf("the result of float_div was %e !\n", z);
}
void float_fixed(double x)
{
int i;
int n;
for( i=0; i < Qx; i++ )
{
x *= 2;
}
n = (int)x;
printf("the result of float_fixed was %i !\n", n);
}
void fixed_float(int x)
{
int i;
double n;
n = x;
for( i=0; i < Qx; i++)
{
n /= 2;
}
printf("the result of fixed_float was %e !\n", n);
}
void main()
{
fixed_add(1556,40);
fixed_sub(334,222);
fixed_mul(188,188);
fixed_div(188,18);
float_add(2.5e3,1.2e3);
float_sub(2.0e2,3.0e2);
float_mul(2.0e2,3.0e2);
float_div(2.0e4,2.0e2);
float_fixed(9.735e1);
fixed_float(1557);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -