Re: [Rd] Standalone Mathlib, C++ and ISNAN()

From: Bill Northcott <>
Date: Thu 06 Jan 2005 - 06:26:54 EST

On 06/01/2005, at 3:09 AM, Thomas Lumley wrote:
>> If building against a full R library, one might possible use R_IsNaN
>> instead but this function is not included in libRmath v2.0 and the
>> function R_IsNaNorNA which was used in libRmath v1.9 no longer exists
> Yes, but in your standalone C or C++ code you can just use isnan()
> from math.h directly.

As I explained in the last message:
>> In R2.0 ISNAN() is defined using isnan thus (isnan(x)!=0). isnan()
>> is in math.h and this works perfectly for C code. However, in C++
>> the header cmath (included by iostream and others) undefs isnan().
>> So on platforms for which isnan() is a macro, the code breaks at
>> compile time with 'isnan() undeclared'.
> The reason that R doesn't do that is twofold. In the past we tried to
> support computers that didn't use IEEE 754 arithmetic, which meant
> that isnan might not exist. Also, IIRC we found that isnan() doesn't
> use 1 for TRUE on all platforms -- sometimes it uses -1 or something
> else -- and code had assumed 1. That's why the strange-looking
> isnan(x)!=0 is there.

There is nothing strange about this. The spec for isnan says it is zero if and only if the object is not a nan. Any other value indicates a nan.
> It would be worth noting somewhere that ISNAN breaks when some C++
> headers are included on Mac OS X, and I will do that.

The point I am trying to make is that it is not just MacOS X. The g++ cmath header undefs isnan so it will break with gcc on any platform which has a macro for isnan. HP-UX is one. Also the C++ gurus insist that the ISO C++ standard requires cmath to zap the macros, which implies it will happen with other ISO compliant C++ compilers.

There is a function R_IsNaN. I have not checked, but I presume it just calls isnan. This should work because it will not be zapped by the C++ headers, because it is in a different (C?) compilation unit. However, it is currently not part of the standalone Rmath.h/libRmath. It would be good if it could be included in the standalone build as was its predecessor R_IsNaNorNA.

That way, we would have a workaround by using R_IsNaN.

Bill Northcott mailing list Received on Thu Jan 06 06:23:26 2005

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:02:29 EST