| View previous topic :: View next topic |
| Author |
Message |
Rob.Meyer1@gmail.com Guest
|
Posted: Tue Sep 27, 2005 11:02 pm Post subject: Value of INF (double) |
|
|
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.
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Sep 27, 2005 11:08 pm Post subject: Re: Value of INF (double) |
|
|
[email]Rob.Meyer1 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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.
| Quote: | 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)
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Sep 27, 2005 11:28 pm Post subject: Re: Value of INF (double) |
|
|
Pete Becker wrote:
| Quote: |
numeric_limits<double>::quite_NaN().
|
Of course, that should be numeric_limits<double>::quiet_NaN().
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
Rob.Meyer1@gmail.com Guest
|
Posted: Wed Sep 28, 2005 12:04 am Post subject: Re: Value of INF (double) |
|
|
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.
|
|
| Back to top |
|
 |
Ali Çehreli Guest
|
Posted: Wed Sep 28, 2005 12:16 am Post subject: Re: Value of INF (double) |
|
|
<Rob.Meyer1 (AT) gmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Wed Sep 28, 2005 12:44 am Post subject: Re: Value of INF (double) |
|
|
[email]Rob.Meyer1 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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)
|
|
| Back to top |
|
 |
Walter Bright Guest
|
Posted: Wed Sep 28, 2005 1:38 am Post subject: Re: Value of INF (double) |
|
|
<Rob.Meyer1 (AT) gmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
|