【问题标题】:1.#QNAN error C++1.#QNAN错误C++
【发布时间】:2011-06-04 19:19:27
【问题描述】:

我是编程新手,正在尝试编写新程序。在检查我的程序时,它返回错误代码 1.#QNAN。我尝试隔离变量并研究答案,但找不到任何解决方案。

我的代码:

 // This is a program to decide what culvert should be properly used for a specific installation
// using given measurements and data
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
// initializing functions 
double slope_function();
double cbasin();
// initializing classes: Subdivisions specs

//intitializing global variables
double edge_road =0;
double up_stream  =0;
double down_stream =0;
double tbm =0.0;

//double culv_length =0;
double slope = 0.0 ;
char street_name[1001];
int min_culv = 15;
double up_strm_culv =0;
double dwn_strm_culv =0;


int main (int nNumberofArgs, char* pszArgs[])
{
    cout<< "This program will allow the surveyor to double check their calculations\n";
    cout << "in deciding what size, type, and requirements are allowed for the\n";
    cout << "installation of culverts in Terrebonne Parish.\n\n";



// begin input
   cout << "what is the name of the street\nwhere the culverts will be installed: ";
   cin.getline (street_name,1000);
   cout << endl;
   cout << "What is the Benchmark: ";
   cin >> tbm;
   cout << endl;
   cout << "What is the elevation of the edge of the road: ";
   cin >> edge_road;
   cout << endl;
   cout << "What is the up-stream culvert size: ";
   cin >> up_strm_culv;
   cout << endl;
   cout << "What is the culverts up-stream inverted elevation: ";
   cin >> up_stream;
   cout << endl;
   cout << "What is the down-stream culvert size: ";
   cin >> dwn_strm_culv;
   cout << endl;
   cout << "What is the culverts down-stream inverted elevation: ";
   cin >> down_stream;
   cout << endl;
   cout << "What is the length of culvert requested: ";
   cin >> culv_length;


   cout << "Your slope is : "; 
   cout << slope_function();
   cout << endl;
   cout << street_name;
   cout << endl;
   cout << cbasin();


    cout << endl;
  // wait until user is ready before terminating program
  // to allow the user to see the program results 
  system ("pause");
  return 0;
}  

// slope function 
double slope_function()
{
    double riseoverrun = 0.0;
    slope = (up_stream - down_stream)/ culv_length;
    return slope;
}


// Catch Basin function
double cbasin ( )
{
    double cb = 0;
    cb = culv_length / 60;
    cout << endl;
    cout << "You need ";
   cout << cb;
   cout << " catch basins for this job.";
   cout << endl;
}

【问题讨论】:

  • 您链接的代码无法编译。但即使它运行了,你也可能将 0 除以 0,这是不允许的。
  • 我已经给出了一个 NAN 的描述,但你的实际错误是一个返回双精度但没有返回语句的函数。

标签: c++ error-handling


【解决方案1】:

1#QNAN 是“安静 NAN”的字符串表示形式。 “NAN”是“非数字”,仅适用于浮点数和双精度数。

NAN 实际上对于表示“空”值非常有用(而不是选择一些真正的数字并希望获得最好的结果,您不需要该数字的自然含义)。

如果运算“无效”(例如取负数的对数),某些数学运算可能会返回 NAN。

您可以使用 C++ 生成 QNAN

double d = std::numeric_limits<double>::quiet_NaN();

NAN 上的任何比较操作(==、

(您代码中的实际错误似乎是一个返回双精度但没有返回语句的函数。

【讨论】:

  • 判断一个值是否为 NAN 的好函数是:template&lt;typename VAL&gt; bool is_ieee_nan(const VAL&amp; v) { return !(v == v); }
  • 你从哪里得到!=总是返回true?
  • 听起来是个很奇怪而且没用的功能。如果检测不到 NAN,那有什么好处呢?
  • @Jorgen:NaN 本身不是 C++ 特性。这是一种 IEEE 浮点算术功能,在 C++ 和其他几种语言中都有体现。主要价值是您可以以对数学家有意义的方式发出数学运算失败的信号。您可以检测到它们。这才是重点。但是您也可以在数学运算中使用它们(结果始终为 NaN:1/0+5*4-6 是 NaN,因为“1/0”步骤)并在您阅读最终结果时确定失败。
  • IEEE 标准是 NaN 不等于任何东西,包括 NaN。你可以写成NaN != NaN。这是唯一具有此行为的数字,因此 v != v 仅在 v 为 NaN 时为真。不过要小心,因为其他“等价”语句不起作用,例如 0.3 != (0.1 + 0.2),但这是因为 0.1 + 0.2 是计算得出的数字,而 0.3 不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多