A
A
Artem2016-04-25 01:53:06
C++ / C#
Artem, 2016-04-25 01:53:06

Why does printf output #INF0 when adding 1500 numbers less than 1?

YP - C.
Wrote this loop:

int i,n;
   double t_i,rnd,sum;
sum=0.0;
n=1500;
   for (i=0;i<n;i++){
        rnd = (rand()%1001)/1000.0; // от 0 до 1
        t_i = (-1.0)*log(rnd)/5.333; // не превышает единицы
        sum=t_i+sum;
        printf("rnd: %.5f, log(rnd): %.5f, t_i: %.5f, sum: %.5f\n\n",rnd,log(rnd),t_i,sum);
   }

The sum variable, for reasons I don't understand, produces 1.#INF0.
The variable t_i, by definition, cannot be greater than one or less than zero, therefore, the maximum value is sum=1500, the minimum is 0. Then why?
Here is a piece of the output:
rnd: 0.56000, log(rnd): -0.57982, t_i: 0.10872, sum: 1.#INF0
rnd: 0.42200, log(rnd): -0.86275, t_i: 0.16178, sum: 1.#INF0
rnd: 0.17900, log( rnd): -1.72037, t_i: 0.32259, sum: 1.#INF0
rnd: 0.28600, log(rnd): -1.25176, t_i: 0.23472, sum: 1.#INF0
rnd: 0.02500, log(rnd): -3.68888, t_i: 0.69171, sum: 1.#INF0
rnd: 0.13000, log(rnd): -2.04022, t_i: 0.38257, sum: 1.#INF0
rnd: 0.74900, log(rnd): -0.28902, t_i: 0.05419, sum: 1.#INF0
rnd: 0.30100, log(rnd): -1.20065, t_i: 0.22514, sum: 1.#INF0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Schullz, 2016-04-25
@ber_enot

At some iteration, the following happens:

rnd = 0;
t_i = 1.#INF0
sum=1.#INF0

This is because log(0) tends to minus infinity

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question