CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    1,689

    Infinity or out of range?

    Is there a way to tell if a function simply went out of range, or if the answer is actually infinity?

    Code:
    double i = 100. / 0.;  //inf
    double oor = pow(9., 100.); //out of range

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Infinity or out of range?

    I believe 1/0 is not inf, it is NaN, at best.

    I'd say only context can tell, or if you are using assembler maybe check if the overflow flags are set.

    That said, keep in mind a number's value can't actually be infinity, so if a mathematic function (like pow) returns infinity, than it went into overflow. Also, keep in mind there are select few cases where the answer actually IS infinity, and the only one I can think of is calculating the limit of a function that diverges. So you have to be careful of what you mean by "the answer actually is infinity".

    But short answer: NO.
    Last edited by monarch_dodra; October 5th, 2010 at 04:00 PM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Infinity or out of range?

    1/0 is infinity, its defined in both mathematics and computer science. Okay, I was just wondeing if there was a flag to tell or not.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Infinity or out of range?

    Quote Originally Posted by ninja9578 View Post
    1/0 is infinity [...].
    AFAIK the limit of 1/x is either +infinity or -infinity, depending on whether x approaches 0 from the positive or negative side.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Infinity or out of range?

    Quote Originally Posted by ninja9578 View Post
    1/0 is infinity, its defined in both mathematics and computer science. Okay, I was just wondeing if there was a flag to tell or not.
    And the names of such remarkable institutions of scholar is...?
    1/0 is undefined in algebraic term, the calculus theorem of the quotient ricocheting to the black hole as it is being eaten by another undefined zero does not apply here. Why? because this is the programming forum, the only possible exception that 1/0 is defined is if and only if both 1 and 0 are not atomic values.

    1/0 is a NaN, period.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Infinity or out of range?

    Quote Originally Posted by ninja9578 View Post
    Is there a way to tell if a function simply went out of range, or if the answer is actually infinity?
    In VC++ you can use this: http://msdn.microsoft.com/en-us/library/9st43tcf.aspx
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Infinity or out of range?

    Quote Originally Posted by potatoCode View Post
    1/0 is a NaN, period.
    Are you sure about that?

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main(int argc, char **argv) {
        cout << 1.0 / 0.0 << endl;
        cout << sqrt(-1) << endl;
        double num = 1.0 / 0.0;
        cout << ((num == num) ? "Number" : "Not a number") << endl;
        return 0;
    }
    Code:
    inf
    nan
    Number

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Infinity or out of range?

    I'll stand corrected (not that I was 100&#37; sure), provided the wikipedia quote:

    The IEEE floating-point standard, supported by almost all modern floating-point units, specifies that every floating point arithmetic operation, including division by zero, has a well-defined result. The standard supports signed zero, as well as infinity and NaN (not a number). There are two zeroes, +0 (positive zero) and −0 (negative zero) and this removes any ambiguity when dividing. In IEEE 754 arithmetic, a &#247; +0 is positive infinity when a is positive, negative infinity when a is negative, and NaN when a = &#177;0. The infinity signs change when dividing by −0 instead.
    If the result is infinity, or is actually overflow is up for debate.
    Last edited by monarch_dodra; October 6th, 2010 at 07:48 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Infinity or out of range?

    Quote Originally Posted by ninja9578 View Post
    Are you sure about that?

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main(int argc, char **argv) {
        cout << 1.0 / 0.0 << endl;
        cout << sqrt(-1) << endl;
        double num = 1.0 / 0.0;
        cout << ((num == num) ? "Number" : "Not a number") << endl;
        return 0;
    }
    Code:
    inf
    nan
    Number
    Yep.
    Quote Originally Posted by C++03 Standard Section 2.13.3 Paragraph 1
    A floating literal consists of an integer part, a decimal point, a fraction part, an e or E, an optionally signed
    integer exponent, and an optional type suffix. The integer and fraction parts both consist of a sequence of
    decimal (base ten) digits. Either the integer part or the fraction part (not both) can be omitted; either the
    decimal point or the letter e (or E) and the exponent (not both) can be omitted. The integer part, the
    optional decimal point and the optional fraction part form the significant part of the floating literal. The
    exponent, if present, indicates the power of 10 by which the significant part is to be scaled. If the scaled
    value is in the range of representable values for its type, the result is the scaled value if representable, else
    the larger or smaller representable value nearest the scaled value, chosen in an implementation-defined
    manner.
    The zero you talked about when you said 1/0 is defined in math and the zero you used in your example code are not the same,
    with the latter being non-atomic producing implementation defined result, thus, by the rule laid out by
    Quote Originally Posted by C++03Standard Section 1.3.1
    well-formed program
    a C + + program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition
    Rule (3.2).
    Quote Originally Posted by C++03 Standard Section 1.3.4
    1.3.4 ill-formed program
    input to a C + + implementation that is not a well-formed program (1.3.14)[/b]
    your example code is ill-formed to make any contradiction, let alone
    Quote Originally Posted by C++03 Standard Section 5.6 Paragraph 4
    The binary / operator yields the quotient, and the binary &#37; operator yields the remainder from the division
    of the first expression by the second. If the second operand of / or % is zero the behavior is undefined;
    qualifying as a valid program.

    I'm not a math doctor, nor a scientist,
    and I simply don't care anything else anyone brings to this discussion for the sole purpose of getting the upperhand on a topic
    If a C++ program contains any operations such as division by zero,
    I'm 100% sure to say 1/0 is a NaN, period.
    Last edited by potatoCode; October 6th, 2010 at 12:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured