简体   繁体   English

奇怪的浮点异常

[英]strange floating point exception

I've got a floating point exception in huge application after some changes. 经过一些更改,我在大型应用程序中有一个浮点异常。 I tried to comment my changes and found that FPE happens when I enable one simple function call. 我试图评论我的更改,发现启用一个简单的函数调用时会发生FPE。

api::getMaxSize();

which simply returns value. 它只是返回值。 looks like this 看起来像这样

int api::getMaxSize() { return 536870912; };

This is static member function. 这是静态成员函数。 When I move this to header file everything works fine. 当我将其移至头文件时,一切正常。 I'm confused, what can be the reson? 我很困惑,那会是什么? Looks like API is in another module and linked as dynamic library, but how can this cause a problem? 看起来API在另一个模块中,并作为动态库链接,但是这怎么会引起问题呢?

added 添加

There is function maxBox() which is template and implemented in api.h header file. 函数maxBox()是模板,并在api.h头文件中实现。 This function calls getMaxSize() 此函数调用getMaxSize()

template <typename T>
static rectangle<T> maxBox()
{
    return rectangle<T>(
        getMinSize(), getMinSize(),
        getMaxSize(), getMaxSize()
    );
}

here is the calling code 这是调用代码

if (!api::maxBox<double>().contains(box * scale)) { /* api::getMaxSize(); */ }

If I enable getMaxSize() call the program starts throwing FPE, but getMaxSize() is actually never called. 如果启用了getMaxSize()调用,程序将开始抛出FPE,但实际上从未调用过getMaxSize()。

added Found FPE in box * scale , can't understand why it was working without getMaxSize() call, but however the problem is solved. box * scale 添加了 Found FPE,无法理解为什么没有调用getMaxSize()就可以正常工作,但是解决了问题。 Thanks to everybody. 感谢大家。

Thanks in advance. 提前致谢。

Floating point exceptions (actually signals ) are raised for different reasons. 浮点异常(实际上是信号 )因不同的原因而产生。 The main ones are: 主要的是:

  • you divide an integer by zero 您将整数除以零
  • an operation on signed integers overflows (unsigned integers must wrap around silently in C and C++). 对有符号整数进行的操作会溢出(无符号整数必须在C和C ++中以静默方式环绕)。

As you can see, they have nothing to do with floating point numbers ! 如您所见,它们与浮点数无关! The name is historical and cannot be changed without breaking a lot of source code (there is a SIGFPE constant in <signal.h> ). 该名称是历史名称,在不破坏大量源代码的情况下不能更改(在<signal.h>有一个SIGFPE常量)。

It can be here that GetMaxSize returns a value which is not representable by a int . GetMaxSize可能在这里返回一个int无法表示的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM