Looking for the "isfinite" function in C/C++?

L

Luna Moon

Hi all,

I am translating a Matlab program into C/C++.

Here is one function in Matlab that I am having difficulty with
translating:

isfinite(x)

Here are the documents:

What are the equivalent statement in C/C++? Basically I am trying to
handle the non-finite numbers or invalid numbers in C/C++ in a nice
way...

Thanks!

--------------------------------------------

K>> help isfinite
ISFINITE True for finite elements.
ISFINITE(X) returns an array that contains 1's where
the elements of X are finite and 0's where they are not.
For example, ISFINITE([pi NaN Inf -Inf]) is [1 0 0 0].

For any X, exactly one of ISFINITE(X), ISINF(X), or ISNAN(X)
is 1 for each element.

See also isnan, isinf.


Reference page in Help browser
doc isfinite

K>> help isnan
ISNAN True for Not-a-Number.
ISNAN(X) returns an array that contains 1's where
the elements of X are NaN's and 0's where they are not.
For example, ISNAN([pi NaN Inf -Inf]) is [0 1 0 0].

See also isfinite, isinf.


Reference page in Help browser
doc isnan

K>> help isinf
ISINF True for infinite elements.
ISINF(X) returns an array that contains 1's where the
elements of X are +Inf or -Inf and 0's where they are not.
For example, ISINF([pi NaN Inf -Inf]) is [0 0 1 1].

See also isfinite, isnan.


Reference page in Help browser
doc isinf
 
T

Thomas Richter

In comp.lang.c++ Luna Moon said:
Here is one function in Matlab that I am having difficulty with
translating:

isfinite(x)

C99: isinf(x)

So long,
Thomas
 
H

hurcan solter

Thanks! You all pointed to C99.

How do I use C99 in either MSVS 2003.NET or Intel C++ compiler ?

for VC7.1 there are MS extensions called _isnan and _finite declared
in float.h
 
M

Michael Wild

hurcan said:
for VC7.1 there are MS extensions called _isnan and _finite declared
in float.h


urks! the c++ standard library should provide these! i do not know how
standards-complying ms stuff is, but you should be able to use

#include <cmath>

....

double f;

....

if( std::isinf(f) ) {
...
}


hth


michael
 
C

Chris Torek

In comp.lang.c++

Actually, it was five different newsgroups (although comp.lang.c++
was one of the five). I have reduced it to just two here.

C99: isinf(x)

This is the wrong test: it checks to see if x is infinite (+Infinity
or -Infinity).

C99 already has an isfinite(), so presumably the original poster
does not have a C99 compiler available.

It can be difficult to write isfinite() on some machines -- those
where (a) you pretty much have to do it in assembly, and (b) an
attempt to pass a "NaN-like" value causes a trap before you can
get to the assembly code in the first place -- but for most modern
IEEE machines, you can write isfinite() yourself if you have to.
 
C

CBFalconer

Michael said:
hurcan solter wrote:
.... snip ...

urks! the c++ standard library should provide these! i do not
know how standards-complying ms stuff is, but you should be able
to use

#include <cmath>
...
double f;
...
if( std::isinf(f) ) {
...
}

Since you are using C++ I have corrected the newsgroups
distribution. F'ups set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,287
Messages
2,571,430
Members
48,120
Latest member
ToTSlider

Latest Threads

Top