[Rd] LOGICAL arguments in FORTRAN code
G'day all, I just took over maintenance of the quadprog package from Kurt Hornik and noticed that one of the FORTRAN routines has an argument that is declared to be a LOGICAL. The R code that calls this routine (via the .Fortran interface) passes the argument down wrapped in a call to as.logical(). This was fine (and as documented) under S-Plus 3.4, for which this code was originally developed. However, as far as I know, in R objects of storage mode logical were always supposed to be passed to FORTRAN arguments of type INTEGER; and that is what the current Writing R extension manual states. Thus, given that the port of quadprog existed for quite some time, I am wondering whether it is o.k. to pass R objects with storage mode logical into FORTRAN code to arguments declared as LOGICAL? Or should the FORTRAN code be corrected to declare the argument in question as INTEGER? Cheers, Berwin __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] LOGICAL arguments in FORTRAN code
On Thu, 8 Apr 2010, Berwin A Turlach wrote: G'day all, I just took over maintenance of the quadprog package from Kurt Hornik and noticed that one of the FORTRAN routines has an argument that is declared to be a LOGICAL. The R code that calls this routine (via the .Fortran interface) passes the argument down wrapped in a call to as.logical(). This was fine (and as documented) under S-Plus 3.4, for which this code was originally developed. However, as far as I know, in R objects of storage mode logical were always supposed to be passed to FORTRAN arguments of type INTEGER; and that is what the current Writing R extension manual states. Thus, given that the port of quadprog existed for quite some time, I am wondering whether it is o.k. to pass R objects with storage mode logical into FORTRAN code to arguments declared as LOGICAL? Or should the FORTRAN code be corrected to declare the argument in question as INTEGER? The second to be safe. This is not a question on the S-PLUS/R side but on the Fortran side. A Fortran compiler may or may not use the same storage for integer and logical (and it may depend on compiler flags, although not on the compilers I just checked, gfortran and SunStudio f95). S-PLUS ran on only a few platforms and with specified compilers. Cheers, Berwin __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] LOGICAL arguments in FORTRAN code
G'day Brian, On Thu, 8 Apr 2010 12:40:45 +0100 (BST) Prof Brian Ripley rip...@stats.ox.ac.uk wrote: On Thu, 8 Apr 2010, Berwin A Turlach wrote: [...] Thus, given that the port of quadprog existed for quite some time, I am wondering whether it is o.k. to pass R objects with storage mode logical into FORTRAN code to arguments declared as LOGICAL? Or should the FORTRAN code be corrected to declare the argument in question as INTEGER? The second to be safe. [...] Thanks for the quick and informative response. I will make the necessary changes. BTW, can I assume that if R passes down TRUE to the FORTRAN routine the corresponding integer argument will be set to 1, and that it will be set to zero if FALSE is passed down? Likewise, can I assume that if at the end of the FORTRAN routine the integer holds a value of zero, then FALSE is passed back to R and if the integer holds any other value then TRUE is passed back? I don't remember ever reading any documentation about this; and most documentation that I would search is not at hand but back on the bookshelves of my office. Cheers, Berwin __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] LOGICAL arguments in FORTRAN code
On Thu, 8 Apr 2010, Berwin A Turlach wrote: G'day Brian, On Thu, 8 Apr 2010 12:40:45 +0100 (BST) Prof Brian Ripley rip...@stats.ox.ac.uk wrote: On Thu, 8 Apr 2010, Berwin A Turlach wrote: [...] Thus, given that the port of quadprog existed for quite some time, I am wondering whether it is o.k. to pass R objects with storage mode logical into FORTRAN code to arguments declared as LOGICAL? Or should the FORTRAN code be corrected to declare the argument in question as INTEGER? The second to be safe. [...] Thanks for the quick and informative response. I will make the necessary changes. BTW, can I assume that if R passes down TRUE to the FORTRAN routine the corresponding integer argument will be set to 1, and that it will be set to zero if FALSE is passed down? Likewise, can I assume that if at the end of the FORTRAN routine the integer holds a value of zero, then FALSE is passed back to R and if the integer holds any other value then TRUE is passed back? I don't remember ever reading any documentation about this; and most documentation that I would search is not at hand but back on the bookshelves of my office. NA_INTEGER is a possible value in both directions. So R may send 0, 1 or (if NAOK=TRUE) -2^31, and will copy back any value (but as a logical in R, it is intended that any value other than 0 and NA_LOGICAL is true). Brian Ripley Cheers, Berwin -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] LOGICAL arguments in FORTRAN code
-Original Message- From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org] On Behalf Of Prof Brian Ripley Sent: Thursday, April 08, 2010 4:41 AM To: Berwin A Turlach Cc: r-devel Subject: Re: [Rd] LOGICAL arguments in FORTRAN code On Thu, 8 Apr 2010, Berwin A Turlach wrote: G'day all, I just took over maintenance of the quadprog package from Kurt Hornik and noticed that one of the FORTRAN routines has an argument that is declared to be a LOGICAL. The R code that calls this routine (via the .Fortran interface) passes the argument down wrapped in a call to as.logical(). This was fine (and as documented) under S-Plus 3.4, for which this code was originally developed. However, as far as I know, in R objects of storage mode logical were always supposed to be passed to FORTRAN arguments of type INTEGER; and that is what the current Writing R extension manual states. Thus, given that the port of quadprog existed for quite some time, I am wondering whether it is o.k. to pass R objects with storage mode logical into FORTRAN code to arguments declared as LOGICAL? Or should the FORTRAN code be corrected to declare the argument in question as INTEGER? The second to be safe. This is not a question on the S-PLUS/R side but on the Fortran side. A Fortran compiler may or may not use the same storage for integer and logical (and it may depend on compiler flags, although not on the compilers I just checked, gfortran and SunStudio f95). S-PLUS ran on only a few platforms and with specified compilers. I agree that avoiding LOGICAL's in arguments to Fortran subroutines is a good idea. Fortran compilers are prone to do weird things when encoding LOGICAL values. E.g., I vaguely recall that Apollo's compiler (c. 1990) encoded .true. as 0 (all zeros) and .false. as -1 (all 1's) and HP's compiler of the same era looked only at the 8th bit from the left (no matter if you had a 1, 2, or 4 byte LOGICAL), 0 was .false. and 1 was .true. S+'s .Fortran() took care of these but you couldn't safely call Fortran code with such arguments from C. Avoid character arguments for similar reasons. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Cheers, Berwin __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel