sequence points and evaluation order



I've been playing with splint, which returns the following
warning for the code below:

statlib.c: (in function log_norm_pdf)
statlib.c(1054,31): Expression has undefined behavior (left operand uses errno,
modified by right operand): (log(x) - mu) * (log(x) - mu)
Code has unspecified behavior. Order of evaluation of function parameters or
subexpressions is not defined, so if a value is used and modified in
different places not separated by a sequence point constraining evaluation
order, then the result of the expression is unspecified.

How can the offending expression be written so that the order of
evaluation is properly specified? Can t2num be assigned the value
of any single expression equivalent to ((log(x)-mu) ^ 2) * -1.0?


/* Return log normal probability density at x.
mu=a_mean(ln(X)); sigma=s_stdev(ln(X)) (output OK) */
double log_norm_pdf(double x, double mu, double sigma)
{
double t1, t2num, t2den;

if(x <= 0.0) return 0.0;

t1 = 1.0 / (sqrt(2.0 * PI) * sigma * x);
t2num = -((log(x) - mu) * (log(x) - mu)); /* problem here */
t2den = (2.0 * (sigma * sigma));

return t1 * exp(t2num / t2den);
}

BTW: this function has been rigorously tested.
.



Relevant Pages

  • Re: pre, post increment standard behaviour, and friend function declaration
    ... There are a number of sequence points (before each function ... >>evaluation of the function parameters. ... > it might be undefined behavior, or merely unspecified behavior. ...
    (comp.lang.cpp)
  • Re: sequence points and evaluation order
    ... Code has unspecified behavior. ... Order of evaluation of function ... What splint is trying to tell you is that the value of errno is ... no undefined behavior here (there is a sequence point between any ...
    (comp.lang.c)
  • Re: C FAQ 3.1
    ... I am convinced we have undefined behavior because ... > favour left to right evaluation of arguments, on another ot might be right ... With OOE processors and write-back memory caching, ... Should C compilers be forced to insert memory ...
    (comp.lang.c)
  • Re: Does this program exhibit undefined behaviour or output?
    ... thearg got progname ... Yes, the order of evaluation of arguments is not specified, so at the ... the order of argument evaluation without invoking undefined behavior. ...
    (comp.lang.c)
  • Re: C FAQ 3.1
    ... >> ccwork wrote: ... I am convinced we have undefined behavior because ... optimisation. ... favour left to right evaluation of arguments, on another ot might be right ...
    (comp.lang.c)

Loading