IEEE754数据生成 (附C++代码)

版权声明:——转载请留言询问—— https://blog.csdn.net/weixin_44344462/article/details/88172871

说明

由于手头的FPGA项目需要生成三角函数的表,所以有如下程序。
代码是很久以前写的,因为方向比较冷,觉得还是有一些价值,就搬过来了。

代码

#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;


int main() {
	// 打开文件
    ofstream fotan("xxx.txt");
    const float pi=3.1415926;

    for(float f=0; f < pi/2; f=f+0.0001){
        //float m=f;
        const float m=cos(f);
        unsigned char *p, ch;
        int i;
        p = (unsigned char *) (&m);
        i = sizeof(m);
        
        for(i = sizeof(m) - 1; i >= 0; --i) {
            ch = *(p + i);
            int j;
          
            for(j=0; j < 8; ++j) {
                if( (ch<<j) & 0x80) fotan<<1;
                else fotan<<0;
            }
            
        }
        
        fotan<<"\n";
    }
    fotan.close();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44344462/article/details/88172871