Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horrible number comes from nowhere

Tags:

c++

Out of nowhere I get quite a big result for this function... It should be very simple, but I can't see it now.

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;
} 

and where is the trace:

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

Thanks for the help!

like image 283
dsign Avatar asked Oct 01 '13 12:10

dsign


People also ask

What is Pennywise phone number?

A telephone number serves as an address for switching telephone calls using a system of destination code routing. Telephone numbers are entered or dialed by a calling party on the originating telephone set, which transmits the sequence of digits in the process of signaling to a telephone exchange.

What is a cursed number?

The curse of 39, also referred to as triakontenneaphobia, is the fear of the number 39. In some parts of Afghanistan, the number is considered to be cursed or a badge of shame as it is purportedly linked with curse, shame, and prostitution.

Why can't I call back a number that called me?

That's because the number you see is not the caller's actual number. The caller doesn't want people to call back and has forged the number the caller ID shows.

How do you call someone from a private number?

You can call privately by entering *67 before you dial a number, which hides your phone number and name. You can also make a call private by turning off caller ID for all outgoing calls using the settings on your iPhone or Android. By default, everyone you call sees your name and phone number thanks to caller ID.


2 Answers

inhabited_columns is unsigned and I see a unary - just before it: -inhabited_columns.

(Note that unary - has a really high operator precedence; higher than * etc).

That is where your problem is! To quote Mike Seymour's answer:

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

One fix would be to write

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

as then the negation will be carried out in floating point

like image 192
Bathsheba Avatar answered Oct 02 '22 20:10

Bathsheba


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

You should change it to a sufficiently large signed type (maybe int32_t, if you're not going to have more than a couple of billion columns), or perhaps double since you're about to use it in double-precision arithmetic.

like image 31
Mike Seymour Avatar answered Oct 02 '22 18:10

Mike Seymour