|
-
October 5th, 2010, 03:18 PM
#1
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
-
October 5th, 2010, 03:56 PM
#2
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.
-
October 5th, 2010, 05:55 PM
#3
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.
-
October 5th, 2010, 06:05 PM
#4
Re: Infinity or out of range?
 Originally Posted by ninja9578
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.
-
October 5th, 2010, 06:52 PM
#5
Re: Infinity or out of range?
 Originally Posted by ninja9578
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.
-
October 6th, 2010, 04:12 AM
#6
Re: Infinity or out of range?
 Originally Posted by ninja9578
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
-
October 6th, 2010, 06:54 AM
#7
Re: Infinity or out of range?
 Originally Posted by potatoCode
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;
}
-
October 6th, 2010, 07:45 AM
#8
Re: Infinity or out of range?
I'll stand corrected (not that I was 100% 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 ÷ +0 is positive infinity when a is positive, negative infinity when a is negative, and NaN when a = ±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.
-
October 6th, 2010, 12:40 PM
#9
Re: Infinity or out of range?
 Originally Posted by ninja9578
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;
}
Yep.
 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
 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).
 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
 Originally Posted by C++03 Standard Section 5.6 Paragraph 4
The binary / operator yields the quotient, and the binary % 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|