C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Value of INF (double)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rob.Meyer1@gmail.com
Guest





PostPosted: Tue Sep 27, 2005 11:02 pm    Post subject: Value of INF (double) Reply with 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.

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





PostPosted: Tue Sep 27, 2005 11:08 pm    Post subject: Re: Value of INF (double) Reply with quote



[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





PostPosted: Tue Sep 27, 2005 11:28 pm    Post subject: Re: Value of INF (double) Reply with quote



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





PostPosted: Wed Sep 28, 2005 12:04 am    Post subject: Re: Value of INF (double) Reply with 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 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





PostPosted: Wed Sep 28, 2005 12:16 am    Post subject: Re: Value of INF (double) Reply with quote

<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





PostPosted: Wed Sep 28, 2005 12:44 am    Post subject: Re: Value of INF (double) Reply with quote

[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





PostPosted: Wed Sep 28, 2005 1:38 am    Post subject: Re: Value of INF (double) Reply with quote


<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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.