【问题标题】:SIGFPE, Arithmetic exception with well-defined divisionSIGFPE,具有明确除法的算术异常
【发布时间】:2020-09-18 06:28:05
【问题描述】:

我有以下一段 c++ 代码:

void update(const int step, const int total) const
{
   double s = static_cast<double>(step);

   double t = static_cast<double>(total);

   std::cout << s/t <<"------\n";

   // etc...
}

我正在使用激活了 -fp-trap=all 标志的 intel c++ 编译器。通过 gdb 运行代码时出现以下错误:

Program received signal SIGFPE, Arithmetic exception.
0x000000000040ee07 in NilDa::progressBar::update (this=0x7fffffffbc9c, step=1, total=60000) at /home/d2d/dev/NilDa/sources/utils/progressBar.h:69
69  std::cout << s/t <<"------\n";

我真的不明白发生了什么。分工似乎很明确。

【问题讨论】:

  • 输入是 step=1,total=60000(由 gdb 报告)因此 s=1.0, t=60000.0
  • 此外,我们不知道何时何地调用此代码。一个简单的main 程序怎么样,只需将 1.0 除以 60000.0,看看这个简单的情况是否有问题。
  • @cigien 这些值在 gdb 输出中可见;) Dante:您使用哪个编译器?
  • PS 如果您使用的是 intel C++ 编译器,那么在这里快速查找将解决您的问题 (Enables or disables the IEEE trap for invalid operation.):software.intel.com/content/www/us/en/develop/documentation/…

标签: c++ sigfpe


【解决方案1】:

我假设您使用的是英特尔 C++ 编译器(我不知道有任何其他编译器具有这样的标志)。 如果是这样,您可以在这里查看:https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-trap-qfp-trap.html#

如该文档中所述,参数 all 继承了不精确结果的陷阱 ([no]inexact)

为不精确的结果启用或禁用 IEEE 陷阱。

由于 1/60000 不能用浮点数表示,因此结果不准确 (1.66667e-05)。

【讨论】:

  • @WolvernDEV 正如问题中所报告的,我正在使用英特尔 c++ 编译器。我明白了 1/6000 的近似值,但对于双倍来说也是如此吗?我没有使用浮点数,而是双精度数。
  • 好吧,不管是双浮点数还是什么,1/60000 都是周期性结果 (1/6 * 1e-5)。 1/6 不能表示为任何浮点数或定点数;)
猜你喜欢
  • 2011-10-08
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2021-12-27
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多