c – 可怕的数字来自无处

前端之家收集整理的这篇文章主要介绍了c – 可怕的数字来自无处前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
无处不在,我为这个功能得到了很大的成果……它应该很简单,但我现在看不到它.
double prob_calculator_t::pimpl_t::B_full_term() const
{
    double result = 0.0;
    for (uint32_t j=0,j_end=U; j<j_end; j++)
    {
        uint32_t inhabited_columns = doc->row_sums[j];
        // DEBUG
        cout << "inhabited_columns: " << inhabited_columns << endl;
        cout << "log_of_sum[j]: " << log_of_sum[j] << endl;
        cout << "sum_of_log[j]: " << sum_of_log[j] << endl;
        // end DEBUG
        result += ( -inhabited_columns * log( log_of_sum[j] ) + sum_of_log[ j ] );
        cout << "result: " << result << endl;
    }
    return result;
}

跟踪在哪里:

inhabited_columns: 1
log_of_sum[j]: 110.56
sum_of_log[j]: -2.81341
result: 2.02102e+10
inhabited_columns: 42
log_of_sum[j]: 110.56
sum_of_log[j]: -143.064
result: 4.04204e+10

谢谢您的帮助!

解决方法

inhabited_columns是无符号的,我看到一个一元 – 就在它之前:-inhabited_columns.

(注意,一元 – 具有非常高的运算符优先级;高于*等).

这就是你的问题所在!引用Mike Seymour的回答:

When you negate it,the result is still unsigned; the value is reduced
modulo 232 to give a large positive value.

一个修复就是写

-(inhabited_columns * log(log_of_sum[j]))

因为那时否定将以浮点进行

原文链接:https://www.f2er.com/c/112457.html

猜你在找的C&C++相关文章