我试图测试某些功能fenv.h,但是,当我编译如下功能LD失败,undefined reference to 'feclearexcept'和undefined reference to 'fetestexcept'。我正在运行针对 uclibc 编译的强化 gentoo,我怀疑这至少在某种程度上是相关的
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
#include <fenv.h>
#pragma STDC FENV_ACCESS on
int main ()
{
feclearexcept (FE_ALL_EXCEPT);
sqrt(-1);
if (fetestexcept(FE_INVALID)) printf ("sqrt(-1) raises FE_INVALID\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
fenv.h在/usr/include. 中有静态和动态库 ( libm.a, libm.so) /usr/lib。我正在编译gcc -o test test.c -lm;有没有人知道为什么链接器找不到相关函数。似乎没有fenv.h相应的库。
更新:这篇十年前的博文似乎暗示 uclibc 不支持 fenv。我无法确定是否仍然是这种情况,但如果是这样,有什么可以做的。 http://uclibc.10924.n7.nabble.com/missing-fenv-h-for-qemu-td2703.html
假设我有一些16字节对齐的结构,只包装3xFloat32数组:
#[repr(C, align(16))]
pub struct Vector(pub [f32; 3]);
Run Code Online (Sandbox Code Playgroud)
现在,我想将其划分为两个实例,如下所示:
use core::arch::x86_64;
let a = Vector([1f32, 2f32, 3f32]);
let b = Vector([4f32, 5f32, 6f32]);
let mut q = Vector([0f32, 0f32, 0ff32]);
unsafe {
let a1 = x86_64::_mm_load_ps(a.0.as_ptr());
let b1 = x86_64::_mm_load_ps(b.0.as_ptr());
let q1 = x86_64::_mm_div_ps(a1, b1);
x86_64::_mm_store_ps(q.0.as_mut_ptr(), q1);
}
Run Code Online (Sandbox Code Playgroud)
它可以进行除法,但是有一个问题:第4个元素包含垃圾,除其他外,垃圾可以发出NaN信号。并且,如果未屏蔽某些例外标志,则将触发SIGFPE。我想以某种方式避免这种情况,而不会完全沉默信号。即我要么只想在第4对元素上使其静音,要么在其中添加一些合理的值。最好,最快的方法是什么?也许总体上有更好的方法?
我没有除以零,我的代码中没有float数据类型,我仍然得到浮点异常.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
unsigned long long int t,n;
cin>>t;
while(t--)
{
cin>>n;
unsigned long long int deno = pow(10,n-1),count=2,sum = 0,f1=1,f2=1;
while(1){
sum = f1+f2;
f1 = f2;
f2 = sum;
count++;
if((int)(sum/deno)>0){
cout<<count<<endl;
break;
}
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所有以前的问题都有类似的问题,除以零,但变量deno永远不会为零n>=2.
以前的研究来自我方:
问题陈述:https://www.hackerrank.com/contests/projecteuler/challenges/euler025/problem
它通过2个测试用例并失败2.所有都是隐藏的测试用例.结果图片
在传递输入150时,我们可以重现错误.细节:
GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
was generated …Run Code Online (Sandbox Code Playgroud) 我喜欢在启用浮点异常的情况下运行我的代码。我在 Linux 下使用:
feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。
我遇到的问题是,有时编译器(我使用 clang8)决定使用 SIMD 指令进行标量除法。好吧,如果这更快,即使对于单个标量,为什么不呢。
但结果是 SIMD 寄存器中未使用的通道可能包含零。
并且在执行 SIMD 除法时,会抛出浮点异常。
这是否意味着如果允许编译器使用 sse/avx 扩展,则根本无法使用浮点异常?
就我而言,这行 C 代码:
float a0, min, a, d;
...
a0 = (min - a) / (d);
Run Code Online (Sandbox Code Playgroud)
...被执行为:
divps %xmm2,%xmm3
Run Code Online (Sandbox Code Playgroud)
然后抛出一个:
Thread 1 "noisetuner" received signal SIGFPE, Arithmetic exception.
Run Code Online (Sandbox Code Playgroud) 当使用具有不同优化选项的 g++-11.1.1 编译时,下面的代码片段的行为是不同的。该代码段启用了浮点异常处理。我不认为算法细节很重要(有点 3D 几何)。它所做的事情自然容易受到执行无效浮点运算的影响,但编写的代码使这些无效运算实际上永远不会发生(例如,if 语句用于确保非零分母)。
#include <array>
#include <cfenv>
#include <csignal>
#include <fstream>
#include <iostream>
#include <limits>
#include <cmath>
#include <vector>
// Vector structure -----------------------------------------------------------
struct Vector
{
double xs[3];
Vector(double x)
{
xs[0] = x;
xs[1] = x;
xs[2] = x;
}
Vector(double x, double y, double z)
{
xs[0] = x;
xs[1] = y;
xs[2] = z;
}
Vector(std::istream& is)
{
is >> xs[0] >> xs[1] >> xs[2];
}
};
// Vector output stream operator ----------------------------------------------
inline …Run Code Online (Sandbox Code Playgroud) 我有一个很长的程序,它由一个头文件和两个源文件组成,在第一个我编写了函数的实现,在第二个(我的主要),我调用并执行它们.虽然,有一次我收到一条错误信息
浮点异常(核心转储)
程序停止了.
正如我所说,有很多行代码,因此,我无法在此发布我的整个源代码,但我会发布最相关的部分,以及发生错误的地方.
当我尝试调用此函数时出现我的错误(下面你可以找到它的实现):
void chest_first(Complex* FFTInput, Complex* IFFTOutput, Complex* HFirst)
{
int i;
for(i = 0; i < 64; i++)
{
HFirst[i].real = FFTInput[i].real / IFFTOutput[i].real;
HFirst[i].imag = FFTInput[i].imag / IFFTOutput[i].imag;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Complex是我定义的类型定义.
typedef struct {
int real, imag;
} Complex;
Run Code Online (Sandbox Code Playgroud)
这是main的部分,调用此函数.
Complex HFirst[64];
if((strcmp(channel, "LS") == 0) || (strcmp(channel, "ls") == 0))
{
if(i == 1)
chest_first(fft_input, ifft_bpsk_output, HFirst);
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
我之前调用了一些其他函数,它将值放到fft_input和ifft_bpsk_output,它们都是64个元素的复杂数组.
c floating-point coredump exception floating-point-exceptions
numpy允许通过适当地使用来处理源自浮点的IEEE-754 异常np.seterr。但是seterr仅支持以下关键字,每个关键字都对应于 IEEE-754 例外:
divide – 除以零的处理。under – 处理浮点溢出。over – 处理浮点溢出。invalid – 无效浮点运算的处理。但是,“不精确”的 IEEE-754 例外没有关键字。如何在 Python 中处理它?
在Delphi 2010和Delphi 2007中,我在WebBrowserBeforeNavigate/WebBrowserDocumentComplete上使用Set8087CW来防止ActiveX内的FPU错误导致我的应用程序瘫痪.
但不知何故,这在Delphi XE2中不起作用,至少在64位模式下是这样.
当点击链接(任何)时,我得到"浮动除以零".(将网站地址或内容初始加载到TWebBrowser中工作正常.)
callstack显示这发生在system32\D3D10Warp.dll内(可能由IE9使用?)以响应TApplication.ProcessMessage(以及两者之间的一些???)
我有一个似乎无法解决的问题.我随机生成数字以确定我的数字是否是相对论素数.
这是给我一个浮点异常的函数:
bool modularExponentiationTest(unsigned long long exponent, unsigned long long modulus)
{
short index = 0;
unsigned long long base;
unsigned long long result;
do
{
result = 1;
base = rand() % exponent; // <--CAUSED BY THIS
while (exponent > 0)
{
if (exponent & 1)
result = (result * base) % modulus;
exponent >>= 1;
base = (base * base) % modulus;
}
if (result != 1)
return false;
}while(++index < 10);
return true;
}
Run Code Online (Sandbox Code Playgroud)
通过执行以下操作,我在不同的函数中实现随机种子:
srand(time(NULL));
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
这是维基百科SIGFPE 页面的第二个例子.
#include <limits.h>
int main(void)
{
volatile int x=INT_MIN;
volatile int y=-1;
x=x/y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它将符号反转为INT_MIN的正数.怎么可能是FPE?