# Value of INF (double)

Discussion in 'C++' started by Rob.Meyer1@gmail.com, Sep 28, 2005.

1. ### Guest

I'm working on a class for working with fractions and one of the public
functions is to return the decimal value of the fraction which is
simple enough except for when the denominator is 0. I don't want to
return 0.0 because thats not what it is, I would rather return NaN or
INF but I can't figure out how to do that. Does anyone know the
expression for either of these in doubles? Or even the bit pattern
that represents INF and/or NaN? I can't seem to find it posted
anywhere.

double Fraction:ecimal() const{
if (IsValid())
return double(n) / d;
else
return ???;
}

//n is numerator, d is denominator, of course. ??? is the expression I
need to figure out.

, Sep 28, 2005

2. ### Pete BeckerGuest

wrote:
> I would rather return NaN or
> INF but I can't figure out how to do that.

They mean very different things. But unless you're talking about
0.0/0.0. the correct value is infinity, not NaN.

> Does anyone know the
> expression for either of these in doubles?

numeric_limits<double>::infinity() and numeric_limits<double>::quite_NaN().

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Pete Becker, Sep 28, 2005

3. ### Pete BeckerGuest

Pete Becker wrote:
>
> numeric_limits<double>::quite_NaN().
>

Of course, that should be numeric_limits<double>::quiet_NaN().

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Pete Becker, Sep 28, 2005
4. ### Guest

I'm having some troubles getting that to compile, here's what i've got
now:

double Fraction:ecimal() const{
if (d != 0)
//return decimal equivelent
return double(n) / d;
else if (n != 0)
//return INF
return numeric_limits<double>::infinity();
else
//return NaN
return numeric_limits<double>::quiet_NaN();
}

The errors I am getting are:
error C2039: 'infinity' : is not a member of 'operator``global
namespace'''
error C2039: 'quiet_NaN' : is not a member of 'operator``global
namespace'''
error C2062: type 'double' unexpected
error C2062: type 'double' unexpected
error C2065: 'numeric_limits' : undeclared identifier

The first two are on the lines you would expect, type double errors are
on lines 7 and 10 (from code above) and I find it odd that the last
error only appears once and not twice, it points to line 7.

I'm guessing I slaughtered the code pretty bad, I've never messed
around with calling some barely heard-of function in a standard library
somewhere. I would have guessed it would be included in iostream
somewhere, maybe not? Thanks for the help in advance, would be nice to
know how to get this one working.

, Sep 28, 2005
5. ### =?iso-8859-1?Q?Ali_=C7ehreli?=Guest

<> wrote in message
news:...
> I'm having some troubles getting that to compile, here's what i've got
> now:
>
> double Fraction:ecimal() const{
> if (d != 0)
> //return decimal equivelent
> return double(n) / d;
> else if (n != 0)
> //return INF
> return numeric_limits<double>::infinity();
> else
> //return NaN
> return numeric_limits<double>::quiet_NaN();
> }
>
> The errors I am getting are:
> error C2039: 'infinity' : is not a member of 'operator``global
> namespace'''
> error C2039: 'quiet_NaN' : is not a member of 'operator``global
> namespace'''
> error C2062: type 'double' unexpected
> error C2062: type 'double' unexpected
> error C2065: 'numeric_limits' : undeclared identifier

The error messages are not very helpful, but are probably due to the fact
that <limits> is not included. This should work:

#include <limits>

using std::numeric_limits;

double Decimal()
{
return numeric_limits<double>::infinity();
}

int main()
{
Decimal();
}

Ali

=?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 28, 2005
6. ### Pete BeckerGuest

wrote:
> I would have guessed it would be included in iostream
> somewhere, maybe not?
>

Don't guess. Look it up. That's what the documentation is for.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Pete Becker, Sep 28, 2005
7. ### Walter BrightGuest

<> wrote in message
news:...
> I'm working on a class for working with fractions and one of the public
> functions is to return the decimal value of the fraction which is
> simple enough except for when the denominator is 0. I don't want to
> return 0.0 because thats not what it is, I would rather return NaN or
> INF but I can't figure out how to do that. Does anyone know the
> expression for either of these in doubles? Or even the bit pattern
> that represents INF and/or NaN? I can't seem to find it posted
> anywhere.

If you're using Digital Mars C/C++ or any compiler with a C99 conformant

#include <math.h>

INFINITY for infinity
NAN for nan

Not all floating point implementations support the notion of INFINITY or
NAN.

-Walter
www.digitalmars.com C, C++, D programming language compilers

Walter Bright, Sep 28, 2005