2012-12-24 128 views
0

我在程序中使用libresample。经过一段时间(大约50分钟)后,它在一个工作站的lib函数lrsFilterUD()中崩溃。C乘法或加法浮点结果NaN

float lrsFilterUD(float Imp[], /* impulse response */ 
       float ImpD[], /* impulse response deltas */ 
       UWORD Nwing, /* len of one wing of filter */ 
       BOOL Interp, /* Interpolate coefs using deltas? */ 
       float *Xp, /* Current sample */ 
       double Ph, /* Phase */ 
       int Inc, /* increment (1 for right wing or -1 for left) */ 
       double dhb) 
{ 
    float a; 
    float *Hp, *Hdp, *End; 
    float v, t; 
    double Ho; 

    v = 0.0; /* The output value */ 
    Ho = Ph*dhb; 
    End = &Imp[Nwing]; 
    if (Inc == 1)  /* If doing right wing...    */ 
    {      /* ...drop extra coeff, so when Ph is */ 
     End--;   /* 0.5, we don't do too many mult's */ 
     if (Ph == 0)  /* If the phase is zero...   */ 
     Ho += dhb;  /* ...then we've already skipped the */ 
    }       /* first sample, so we must also */ 
         /* skip ahead in Imp[] and ImpD[] */ 

    if (Interp) 
     while ((Hp = &Imp[(int)Ho]) < End) { 
     t = *Hp;  /* Get IR sample */ 
     Hdp = &ImpD[(int)Ho]; /* get interp bits from diff table*/ 
     a = Ho - floor(Ho);  /* a is logically between 0 and 1 */ 
     t += (*Hdp)*a; /* t is now interp'd filter coeff */ 
     t *= *Xp;  /* Mult coeff by input sample */ 
     v += t;   /* The filter output */ 
     Ho += dhb;  /* IR step */ 
     Xp += Inc;  /* Input signal step. NO CHECK ON BOUNDS */ 
     } 
    else 
     while ((Hp = &Imp[(int)Ho]) < End) { 
     dprintf("while begin: Hp = %p, *Hp = %a, (int)Ho = %d, Imp[(int)Ho] = %a, &Imp[(int)Ho] = %p", Hp, *Hp, (int)Ho, Imp[(int)Ho], &Imp[(int)Ho]); 
     t = *Hp;  /* Get IR sample */ 
     dprintf("before t = %a, *Xp = %a, Xp = %p", t, *Xp, Xp); 
     t *= *Xp;  /* Mult coeff by input sample */ 
     dprintf("after2 t = %a, v = %a", t, v); 
     v += t;   /* The filter output */ 
     dprintf("v = %a", v); 
     Ho += dhb;  /* IR step */ 
     Xp += Inc;  /* Input signal step. NO CHECK ON BOUNDS */ 
     } 

    return v; 
} 

我登录,* XP,XP之前和相乘后的T值:

while begin: Hp = 0xaf5daa8, *Hp = -0.009034, (int)Ho = 16384, Imp[(int)Ho] = -0.009034, &Imp[(int)Ho] = 0xaf5daa8 
before multiplication t = -0.009034, *Xp = 0.000000, Xp = 0xaebe9b8 
after multiplication t = nan 

此代码运行多次,也有相同的T和XP值坠毁前:

before multiplication t = -0.009034, *Xp = 0.000000, Xp = 0xaebe9c8 
after multiplication t = -0.000000, v = 282.423676 

或添加另一种情况:

before addition t = -460.799988, v = 0.000000 
after addition v = nan 

什么可能导致南?这是在Linux上用gcc 4.1.2编译的。

更新:将变量打印为%a。结果:

//t = 0x1.2806bap+2 
//Hp = 0xb3bb870 
t = *Hp; 
//t = nan 

更新2:没有这样的问题,如果代码被编译ICPC。那么是否有编译器的具体问题?

+0

这种代码格式很烦人。 – 2012-12-24 13:08:36

+0

请发布您从中获取日志条目的实际代码。另外,确保'Ho'没有超出范围。 (什么类型是“Ho”?) –

+0

通过记录添加了整个函数的代码。 – Alex

回答

5

显然,-0.009034•0.000000不应该产生NaN。因此,问题中提供的代码和数据不是实际计算的准确表示,或者计算实现有缺陷。

如果我们假设硬件和基本运算的实现是没有缺陷的,那么一些可能性,调查包括:

  • t*Xp日志记录失败立即乘法之前登录的t*Xp正确的价值观或在乘法后立即得到正确的值t
  • t*Xp的值的显示不正确。例如,用于显示*Xp的格式显示“0.000000”,尽管*Xp具有其他值,例如NaN。
  • Xp指向某处不恰当,导致*Xp不可靠(例如,由外部操作更改)。
  • 显示的代码不准确。例如,它来自旧的来源,已被更改,或者它是新来源,但是之前编译的代码正在执行。

注:使用浮点对象调试,你应该打印用的格式,如“%F”,尤其不能与数字编号的默认值。您应该使用“%a”打印,该打印使用十六进制表示法输出浮点值的确切值。在许多情况下,您也可以使用“%.99g”,前提是您的C实现提供了将浮点值转换为小数的良好转换。

+0

将变量值添加为要发布的“%a”。 – Alex

+0

我今天遇到的另一个原因是这个警告(不是海报代码,而是我自己的):“隐式声明函数'fabs'”,因为我忘记了包含math.h –

1

Wiki,有三种操作,可以返回NaN的如下:

1. Operations with a NaN as at least one operand. 
2. Indeterminate forms 
     The divisions 0/0 and ±∞/±∞ 
     The multiplications 0×±∞ and ±∞×0 
     The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions 
     The standard has alternative functions for powers: 
     The standard pow function and the integer exponent pown function define 0pow(0), 1pow(∞), 
     and ∞pow(0) as 1. 
     The powr function defines all three indeterminate forms as invalid operations and 
     so returns NaN. 
3. Real operations with complex results, for example: 
     The square root of a negative number. 
     The logarithm of a negative number 
     The inverse sine or cosine of a number that is less than −1 or greater than +1. 

现在,这可以帮助你解决你自己的问题。

+0

我看到了,但我的情况没有人。 – Alex

+0

请确保因为您的算术运算对值't','* Xp','Xp'在'0'和'FLT_MAX'之间执行得很好。 –

0

您必须打印每个计算结果的子结果 - 或使用isnan()函数在常规地点检查并追踪其来源。这可能是一些“坏”的数学,或者你首先在垃圾中喂食(未初始化的变量可能是NaN)

3

有第五可能性埃里克Postpischil的,否则优秀的答案没有提及:

  • 乘法在的x87寄存器执行,以及浮点栈溢出发生了起因到可能不相关的早期操作在你的程序执行中。当处理器处于这种故障状态时,在x87寄存器上执行的全部计算产生NaN结果。

这两个最常见的原因是调用返回浮点结果的函数,该结果在范围中没有原型(有很多调用约定,这会导致调用者无法将结果从FP中弹出堆栈)和不正确的手写(可能是内联)程序集。

失败只发生在一段时间之后的事实为这种可能性提供了一些证据;如果有一个很少使用的代码路径泄漏了浮点堆栈的一个元素,那么在失败清单出现之前需要使用一些次数,这可能会让它直到现在才能通知它。

要诊断或排除这种可能性,您需要查看浮点状态寄存器(FPSR)的位6(SF)。根据您使用的编译器,检查FPSR的确切方法可能会有所不同。