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::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.
     
    , Sep 28, 2005
    #1
    1. Advertising

  2. Pete Becker Guest

    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
    #2
    1. Advertising

  3. Pete Becker Guest

    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
    #3
  4. Guest

    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.
     
    , Sep 28, 2005
    #4
  5. <> wrote in message
    news:...
    > 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
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 28, 2005
    #5
  6. Pete Becker Guest

    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
    #6
  7. <> 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
    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
     
    Walter Bright, Sep 28, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hans
    Replies:
    3
    Views:
    2,510
    Ryan Stewart
    May 16, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    checking double for Inf or NaN - how?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 18, 2006, in forum: C Programming
    Replies:
    55
    Views:
    2,529
    Keith Thompson
    Jun 3, 2006
  4. zl2k
    Replies:
    10
    Views:
    1,194
  5. Xavier Roche
    Replies:
    4
    Views:
    1,845
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page