[R] swap_tail macro in pnorm.c

From: Adam B. Thompson <tngamer_at_gmail.com>
Date: Mon, 14 Jul 2008 10:39:57 -0400


Looking over the swap_tail macro in pnorm.c, the comment inside the code indicates it is to swap the values of '*cum' and '*ccum'. According to the code below that is taken from the source file, the swap functionality is dependent on what values are provided for 'x' and 'lower'.

#define swap_tail      \
    if (x > 0.) {/* swap  ccum <--> cum */      \
        temp = *cum; if(lower) *cum = *ccum; *ccum = temp;      \

It only swaps the corresponding values if and only if 'x' is greater than zero and 'lower' is "true" (non-zero). The following examples will show the actual behavior:

Input 1 Input 2
-------- --------
x: 1 x: 1
lower: 0 lower: 2
*cum: 1 *cum: 1
*ccum: 2 *ccum: 2

Result 1 Result 2
-------- --------
temp: 1 temp: 1
*cum: 1 *cum: 2
*ccum: 1 *ccum: 1

According to the code, Input 1 causes '*cum' to retain its value while '*ccum' loses its own and takes on that of '*cum', performing a half-swap. Input 2 performs the full swap, resulting in '*cum' taking on the original value of '*ccum' while '*ccum' takes on the original value of '*cum'.

Since the logic following the 'lower' conditional statement is not surrounded in braces, this causes the second statement (*ccum = temp;) to be executed even if 'lower' evaluates to "false" (zero). Is this an appropriate result for the macro since this does not actually result in a swapped pair of values, as indicated in the above example (Input 1)? Or should this only result in swapped values if and only if 'x' and 'lower' are greater than zero and non-zero, respectively?

Thank you for your assistance.

Adam B. Thompson


	[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 14 Jul 2008 - 14:54:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Jul 2008 - 15:01:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive