我是c++编程的初学者,给我的任务是在c++中实现定点数学算法。在这里,我尝试实现一个函数isnan(),如果数字不是-a- number,则返回true,否则将返回false。
测试文件
#include "fixed_point_header.h"
int main()
{
fp::fixed_point<long long int, 63> a=fp::fixed_point<long long int, 63>::positive_infinity(); // will assign positive infinity value to a from an function from header
fp::fixed_point<long long int, 63> b=fp::fixed_point<long long int, 63>::negative_infinity(); // will assign positive infinity value to b from an function from header
float nan=fp::fixed_point<long long int, 63>::isnan(a,b);
printf( "fixed point nan value == %f\n", float (nan));
} 在头文件中,我想做的有点像下面显示的代码,如果正负无穷大的值相加,isnan函数应该返回1或者0。
头文件
#include fixed_point_header
static fp::fixed_point<FP, I, F> isnan (fp::fixed_point<FP, I, F> x,fp::fixed_point<FP, I, F> y){
/*if ( x + y ) happens, ie. x and y are infinities
{
should return 1; }
else {
should return 0; }
} */有谁能告诉我该怎么做吗?或者如何解决这个范例
发布于 2013-05-13 21:39:16
我正在尝试实现一个函数isnan(),如果数字不是-
,它将返回true,否则将返回false。
这很简单;定义一个保留值来表示nan (就像您对无穷大所做的那样),并与之进行比较:
bool isnan(fixed_point x) {
return x == fixed_point::nan();
}如果将正负无穷大的值相加,则
函数应该返回1否则为0
加法运算符负责检查输入并在适当的情况下返回nan:
fixed_point operator+(fixed_point x, fixed_point y) {
if (x == fixed_point::nan() || y == fixed_point::nan()) {
return nan;
}
if (x == fixed_point::positive_infinity()) {
return y == fixed_point::negative_infinity() ? fixed_point::nan() : x;
}
// and so on
}那么main中的测试就变成了:
bool nan = fixed_point::isnan(a+b);https://stackoverflow.com/questions/16522916
复制相似问题