欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

HLS Lesson6-数据类型转换-3. 浮点数据类型

最编程 2024-07-27 07:19:41
...

与c语言类似。

double vf2(5.0);

float vf3(5.0f);//单精度浮点型需要加上后缀f

还有数学库hls_math.h

隐式数据类型转换:(promotion,扩展)

ap_uint<8> res;

ap_int<4> v1=-4;

ap_uint<4> v2=4;

res =v1; //进行了扩展,有符号位扩展res = 252

res =v2; //进行了扩展,无符号位扩展res = 4

conversion(转换,可能导致数据错误)

ap_int<4> v5=3;

ap_uint<4> v6=3;

ap_int<2> res2;

res2=v5; //只取了低两位,11,对于有符号数,则为-1

res2=v6; //只取了低两位,11,对于有符号数,则为-1

ap_fixed<4,2> v1=1.25;

ap_fixed<3,2,AP_RND> v3 = v1; //1.5,精度有所损失

显式类型转换:

(1)通过()进行转换

(2)采用函数的方式

 ap_uint<3> i3 = 4;
 ap_uint<4> i4 = 10;
 ap_ufixed<6, 4> i5 = i4 / i3;
 cout << "This value of i5 : t" << i5 << endl;
 ap_ufixed<6, 4> i6 = (ap_ufixed<6, 4>)i4 / i3;
 cout << "This value of i6 : t" << i6 << endl;
 ap_ufixed<6, 4> i7 = (ap_ufixed<6, 4>)(i4) / i3;
 cout << "This value of i7 : t" << i7 << endl; 

获取变量类型信息:typeid();需要加入头文件:#include<typeinfo>