[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gsl] gsl_max and nested random variables
From: |
Frank Reininghaus |
Subject: |
Re: [Bug-gsl] gsl_max and nested random variables |
Date: |
Mon, 9 Jun 2008 13:05:32 +0200 |
Hi,
I'm attaching a reduced testcase that can be compiled. I'm getting a
negative result in the first iteration.
The reason appears to be that the macro GSL_MAX is defined like this in
gsl/gsl_math.h:
#define GSL_MAX(a,b) ((a) > (b) ? (a) : (b))
The preprocessor therefore translates the line
double max = GSL_MAX (0.0, gsl_ran_gaussian (r, 2));
into
double max = (0.0 > gsl_ran_gaussian (r, 2) ? 0.0 : gsl_ran_gaussian (r,
2));
Because gsl_ran_gaussian () does not always return the same value for the
same input arguments, it is possible that the first function call yields
something positive, so the ( ? : ) expression decides not to return 0.0, but
the result of a second call to the function, which might be negative.
I can't see any obvious way to fix this right now (except for replacing the
macro by a function call). Maybe one could say in the documentation that one
should use the workaround
double random = gsl_ran_gaussian (r, 2);
double max = GSL_MAX (0.0, random);
in cases like this.
Cheers,
Frank
2008/6/9 K Okamoto <address@hidden>:
> Hello,
>
> I searched gsl_max in the bug archives to no avail so I am guessing
> this is the first time? In any event I found GSL_MAX to occasionally
> give the wrong answer. Here's the code:
>
testcase.c
Description: Text Data