Addition problems

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Apr 1 17:17:48 EDT 2011


Op 2011-04-01 16:04, vm schreef:
> I'm analyzing some data in python. I have stumbled across unusual problem:
> 
> I have the function...
> 
> def fun1(params_used_below_except_lk_and_lk2):
>     lk = 0.0
>     lk2 = 0.0
>     for raw_data, hist, freq in raw_data_hist_list:
>         lk2 = lk2 + fun2(some_constants_and_params_from_this_scope)
>         q = fun2(same_args_as_before)
>         lk = lk + q
>     print lk, lk2
> 
> For some set of input parameters, This function with specific input 
> prints out the same constant twice (as expected):
> 
> 15377.7424582 15377.7424582 (twice the same value)
> 
> If I comment out lines q = fun2.... and lk = lk + q, fun1, with the same 
> parameters, prints:
> 
> 0.0 3.3469936856
> 
> First value is expected, but I cannot understand how come the second 
> value is different in these two cases!

Let's have a look at your source:

> fun1 source:
> 
> def process_measurements_beta_lk(x, raw_data_hist_list):
>     lk = 0.0
>     lk2 = 0.0
>     (p,q,loc,scale) = x
>     for raw_data, hist, freq in raw_data_hist_list:
>         lk2 =   lk2 + get_beta_func_fit_quality_hist_lk('beta',
> (p,q,loc,scale), raw_data, hist)
> #        q = get_beta_func_fit_quality_hist_lk('beta',(p,q,loc,scale), 
> raw_data, hist)
> # 	lk = lk + q
>     print lk, lk2
>     retrun lk

The lie with "lk2 = " uses the value of q. In the version with commented
lines, that value is never changed, while it is changed each iteration
in the version with the lines uncommented.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven



More information about the Python-list mailing list