bug-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gsl] gsl_max and nested random variables


From: Stijn van Dongen
Subject: Re: [Bug-gsl] gsl_max and nested random variables
Date: Mon, 9 Jun 2008 12:14:09 +0100
User-agent: Mutt/1.5.9i

   Hi,

On Mon, Jun 09, 2008 at 01:05:32PM +0200, Frank Reininghaus wrote:
> 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.

This is the correct solution (except that 'random' is perhaps not a
great variable name as it is a stdlib function on some systems). What
you describe is simply the way C macros work, and it is common pitfall
to pass function calls to macros.  In the best case, you'll evaluate the
function call twice and get the same result. In the worst case you
evaluate it twice or more and get different results.

It is a common programming style to always write macros and constants in
uppercase. So if you see UPPERCASE(a,b) it should alert you
(henceforth) that UPPERCASE is likely to be a macro and may evaluate
its arguments multiple times.

regards,
Stijn

-- 
Stijn van Dongen                >8< -o)   O<  forename pronunciation: [Stan]
Wellcome Trust Sanger Institute,    /\\   Tel: +44-(0)1223-494785
Hinxton, Cambridge, CB10 1SA, UK   _\_/   http://www.sanger.ac.uk/Users/svd/


-- 
 The Wellcome Trust Sanger Institute is operated by Genome Research 
 Limited, a charity registered in England with number 1021457 and a 
 company registered in England with number 2742969, whose registered 
 office is 215 Euston Road, London, NW1 2BE. 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]