vivado hls学习笔记-测试文件输出打印

文章介绍了C++中使用ap_int处理整数和浮点数的格式化输出,包括八进制、十进制、十六进制以及科学计数法,并展示了如何将数据写入文本文件以保持精度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 数据直接输出到观察窗口

/*
 * Copyright 1986-2022 Xilinx, Inc. All Rights Reserved.
 * Copyright 2022-2023 Advanced Micro Devices, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <ap_int.h> 

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
using namespace std;


int main() {
    // 前缀0表示八进制 前缀0x表示十六进制 不带前缀表示十进制
    ap_int<16> a = 123;
    double pi = 22.0/7.0;

    // setbase(n) 设置整数为n进制(n=8,10,16)
    // oct 八进制 dec 十进制 hex 十六进制
    // setiosflags(ios::showbase) 显示进制的前缀
    // 数值默认十进制显示输出
    std::cout << a << std::endl;
    std::cout << "oct: " << std::showbase << std::setbase(8) << a << " " << std::oct << a << std::endl;
    std::cout << "dec: " << std::showbase << std::setbase(10) << a << " " << std::dec << a << std::endl;
    std::cout << "hex: " << std::showbase << std::setbase(16) << a << " " << std::hex << a << std::endl;
	
	std::cout << "2 hex: " << bitset<64> (a) << endl ;
	// 数据使用to_string(2/8/16).c_str();

	std::cout << "oct: "  <<a.to_string(2).c_str()   << " \n"  ;
	std::cout << "dec: "  <<a.to_string(8).c_str()   << "\n " ;
	std::cout << "hex: "  <<a.to_string(16).c_str()  << "\n " ;
	
	ap_fixed<8,4> varf = -1.5 ;
	cout << "-hex xiao shu : " << hex << varf  << endl;
	cout << "-c_str xiao shu : " << varf.to_string(16).c_str() << endl;
	
    // setprecision(n) 设置浮点数的有效数字为n位
    // 有效位数默认是6位,即setprecision(6),即小数点前面和小数点后面加起来的位数为6个有效数字(注意会四舍五入) 测试
    std::cout << pi << std::endl;
	//  fixed固定数据位宽 
    std::cout << std::setprecision(3) << fixed << pi << std::endl;

    // setfill(n) 设置字符填充,c可以是字符常或字符变量
    // setw(n) 设置字段宽度为n位, 若是实际宽度大于被设置的,则setw函数此时失效, 只针对其后的第一个输出项有效
    // setiosflags(ios::left) 输出左对齐
    // setiosflags(ios::right) 输出右对齐 默认右对齐
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << pi << std::endl;
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::right << pi << std::endl;
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::left << pi << std::endl;

    // setiosflags(ios::fixed) 设置浮点数以固定的小数位数显示
    std::cout << std::fixed << std::setprecision(12) << pi << std::endl;

    // setiosflags(ios::scientific)  设置浮点数以科学计数法表示 科学计数法输出E与十六进制输出默认是以小写的,要换成大写需添加uppercase
    std::cout << std::scientific << std::setprecision(12) << pi << std::endl;
    std::cout << std::scientific << std::uppercase << std::setprecision(12) << pi << std::endl;

    // resetiosflags() 终止已经设置的输出格式状态,在括号中应指定内容
    std::cout << std::setiosflags(std::ios::scientific) << std::setprecision(12) << pi << "   " << std::resetiosflags(std::ios::scientific) << pi << std::endl;
    // 
    return 0;
}

以上可用于测试文件中的数据输出推荐使用data.to_string(2/8/10/16).c_str()来输出数据,能够更好的保证浮点数精度。

2.写入文本进行比较

将数据写入txt文件操作函数

#define N 15 
void WriteFileVec(char *fp,  ap_int<16> InBuf[N])
{
	int i ;
	int j ;
	ofstream fp_strmo(fp);
	if(!fp_strmo.is_open()){
		cerr<<"Error!\n The file is not able to open!\n";
	}
	else{
		for(i=0;i<N;i++){
			fp_strmo << InBuf[i] << " " <<InBuf[i].to_string(16).c_str()<< '\n' ;
		}
	}
	fp_strmo.close();
	cout <<setw(60) << setfill('_') <<'_'<< '\n';
	cout <<"Data has been successfully stored to target file!"<< '\n';
	cout << setw(60) <<  setfill('_') <<'_'<< '\n';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值