0

I want to use isfinite function in my C++ code.

This function is available in the default math.h but not in the default version(-std=gnu++98) of cmath.

So if I include math.h and make sure cmath is not included, then isfinite is available.

If any of other header files, like valarray includes cmath, then isfinite is gone.

C++11 in GCC 4.3 is experimental so I don't want to turn it on.

Is there a way to use C99 math.h in C++98 code?

I found this related question on testing NaN, and the non-C++11 solutions seems very ugly.

EDIT

As is pointed by @old_mountain in the comment, when cmath is used, isfinite is still available but need to be called by std::isfinite, using the std namespace.

Community
  • 1
  • 1
user3528438
  • 2,737
  • 2
  • 23
  • 42
  • 1
    Are you getting errors using `cmath` and `std::isfinite` ? Apparently it runs fine with [g++4.3.6](http://melpon.org/wandbox/permlink/fFGmUSXLyMYxmQpx). If you use just `isfinite` (without namespace) you'll get an error – hlscalon Oct 06 '15 at 18:52
  • is boos or `std::tr1` not an option? – NathanOliver Oct 06 '15 at 18:56

2 Answers2

1

Include <cmath> and use std::isfinite with std namespace.

It should work fine (g++4.3.6)

hlscalon
  • 7,304
  • 4
  • 33
  • 40
  • Is that standard compliant? isfinite is marked as C++11 so it is possibly a bug that GCC allow using that without std=c++11 flag – CoffeDeveloper Oct 06 '15 at 19:06
0

Create the function yourself! According to cppreference that function is available as part of C++11 standard, so I'm not sure is portable using std::isfinite with GCC 4.3.X

isfinite.cpp

#include <math.h>

bool myIsFinite(float arg){
    return isfinite(arg)!=0;
}


bool myIsFinite(double arg){
    return isfinite(arg)!=0;
}

isfinite.hpp

bool myIsFinite(float arg);
bool myIsFinite(double arg);


If you still want to call a function called "isfinite" (I do not suggest that):

isfinite.cpp

bool myIsFinite(float arg);
bool myIsFinite(double arg);

bool isfinite(float arg){
    return myIsFinite(arg);
}


bool isfinite(double arg){
    return myIsFinite(arg);
}

#include <math.h>

bool myIsFinite(float arg){
    return isfinite(arg)!=0;
}


bool myIsFinite(double arg){
    return isfinite(arg)!=0;
}

isfinite.hpp

bool isfinite(float arg);
bool isfinite(double arg);

WARNING

You will not be able to do things like "single translation unit" using this file. So you have to exclude "isfinite.cpp" from any single compilation unit and compile it apart alone.

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • I tried something similar. I wrote a `extern "C"` wrapper to a c version and compiled it in C99 mode. I did some research and found out using `.h` C headers in C++ code isn't good practice. – user3528438 Oct 06 '15 at 19:10
  • this is not C, but C++ code. The only downside is that C use a lot of macros that pollute namespace, but as you see the inclusion of `math.h` is done in a small tiny C++ file wich prevent any namespace pollution to code including `isfinite.hpp`. And as you see I don't used "extern" (wich alter the name mangling, wich could be another issue to deal with, but again I resolved it by using C++ and hiding behind it all C code ;) ) – CoffeDeveloper Oct 06 '15 at 19:12