# [Rd] What should dnorm(0, 0, -Inf) return?

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sun Dec 8 12:11:50 CET 2019

```Yes, that looks like a bug and an easily fixable one too.

However, I spy another issue: Why do we check the !R_FINITE(x) && mu == x before checking for sd < 0 ? The difference is whether we

return ML_NAN;
or
ML_ERR_return_NAN;

but surely negative sd should always be an error?

I'd be inclined to do

if (sigma < 0) ML_ERR_return_NAN;
if(!R_FINITE(sigma)) return R_D__0;
if(!R_FINITE(x) && mu == x) return ML_NAN;/* x-mu is NaN */
if (sigma == 0)
return (x == mu) ? ML_POSINF : R_D__0;
x = (x - mu) / sigma;

(Ping Martin...)

-pd

> On 7 Dec 2019, at 23:40 , Wang Jiefei <szwjf08 using gmail.com> wrote:
>
> Good question, I cannot speak for R's developers but I would like to
> provide some information on the problem. Here are the first few lines of
> the dnorm function located at src\nmath\dnorm.c:
>
> ```
> double dnorm4(double x, double mu, double sigma, int give_log)
> {
> #ifdef IEEE_754
>    if (ISNAN(x) || ISNAN(mu) || ISNAN(sigma))
> return x + mu + sigma;
> #endif
>    if(!R_FINITE(sigma)) return R_D__0;
>    if(!R_FINITE(x) && mu == x) return ML_NAN;/* x-mu is NaN */
>    if (sigma <= 0) {
>    if (sigma < 0) ML_ERR_return_NAN;
>        /* sigma == 0 */
>        return (x == mu) ? ML_POSINF : R_D__0;
>    }
>    ....
> }
> ```
>
> You can clearly see where the problem is. I think either the document or
> the code needs a modification.
>
> Best,
> Jiefei
>
> On Sat, Dec 7, 2019 at 5:05 PM Weigand, Stephen D. via R-devel <
> r-devel using r-project.org> wrote:
>
>> Hi,
>> Apropos of a recent Inf question, I've previously wondered if dnorm "does
>> the right thing" with
>>
>>  dnorm(0, 0, -Inf)
>>
>> which gives zero. Should that be zero or NaN (or NA)?
>>
>> The help says "'sd < 0' is an error and returns 'NaN'" and since -Inf < 0
>> is TRUE, then... is this a bug?
>>
>> Thank you,
>> Stephen
>> Rochester, MN USA
>>
>> ______________________________________________
>> R-devel using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

```