[Rd] patch for random.c

Martin Maechler maechler at stat.math.ethz.ch
Mon Mar 3 10:16:45 CET 2008


>>>>> "BAT" == Berwin A Turlach <statba at nus.edu.sg>
>>>>>     on Fri, 29 Feb 2008 18:19:40 +0800 writes:

    BAT> Dear all,
    BAT> while looking for some inspiration of how to organise some code, I
    BAT> studied the code of random.c and noticed that for distributions with
    BAT> 2 or 3 parameters the user is not warned if NAs are created while such
    BAT> a warning is issued for distributions with 1 parameter.  E.g:

    BAT> R version 2.7.0 Under development (unstable) (2008-02-29 r44639)

    BAT> [...]

    >> rexp(2, rate=Inf)
    BAT> [1] NaN NaN
    BAT> Warning message:
    BAT> In rexp(2, rate = Inf) : NAs produced
    >> rnorm(2, mean=Inf)
    BAT> [1] NaN NaN

    BAT> Surprisingly, the code for issuing warnings for distributions with 2 or
    BAT> 3 parameters is essentially there, but does not seem to be used.  The
    BAT> attached patch rectifies this.  With the patch the above command produce
    BAT> the following output:

    >> rexp(2, rate=Inf)
    BAT> [1] NaN NaN
    BAT> Warning message:
    BAT> In rexp(2, rate = Inf) : NAs produced
    >> rnorm(2, mean=Inf)
    BAT> [1] NaN NaN
    BAT> Warning message:
    BAT> In rnorm(2, mean = Inf) : NAs produced

    BAT> Please ignore the patch if the code that was designed to produce the
    BAT> warning had been removed on purpose.  

I cannot imagine a design reason for that.  If there was, it
should have been mentioned as a comment in the C code.

I'll commit your patch (if it passes the checks).

    BAT> BTW, there are other places in the code were NAs can be created but no
    BAT> warning is issued.  E.g:

    >> rexp(2, rate=numeric())
    BAT> [1] NA NA
    >> rnorm(2, mean=numeric())
    BAT> [1] NA NA

    BAT> I wonder whether a warning should be issued in this case too.  

Yes, "should in principle".

If you feel like finding another elegant patch...

Thank you Berwin,
for your contribution!

Martin

    BAT> Best wishes,

    BAT> Berwin

    BAT> Index: src/main/random.c
    BAT> ===================================================================
    BAT> --- src/main/random.c	(revision 44639)
    BAT> +++ src/main/random.c	(working copy)
    BAT> @@ -123,7 +123,7 @@
 
    BAT> #define RAND2(num,name) \
    BAT> case num: \
    BAT> -		random2(name, REAL(a), na, REAL(b), nb, REAL(x), n); \
    BAT> +		naflag = random2(name, REAL(a), na, REAL(b), nb, REAL(x), n); \
    BAT> break
 
    BAT> /* "do_random2" - random sampling from 2 parameter families. */
    BAT> @@ -207,7 +207,7 @@
 
    BAT> #define RAND3(num,name) \
    BAT> case num: \
    BAT> -		random3(name, REAL(a), na, REAL(b), nb, REAL(c), nc, REAL(x), n); \
    BAT> +		naflag = random3(name, REAL(a), na, REAL(b), nb, REAL(c), nc, REAL(x), n); \
    BAT> break
 
 
    BAT> ______________________________________________
    BAT> R-devel at r-project.org mailing list
    BAT> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list