Value of INF (double)

R

Rob.Meyer1

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::Decimal() 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.
 
P

Pete Becker

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().
 
R

Rob.Meyer1

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

double Fraction::Decimal() 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.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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

double Fraction::Decimal() 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
 
W

Walter Bright

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
math.h header file:

#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
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top