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
Printable View
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
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.
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.
In VC++ you can use this: http://msdn.microsoft.com/en-us/library/9st43tcf.aspx
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
I'll stand corrected (not that I was 100% sure), provided the wikipedia quote:
If the result is infinity, or is actually overflow is up for debate.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.
Yep.
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,Quote:
Originally Posted by C++03 Standard Section 2.13.3 Paragraph 1
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
your example code is ill-formed to make any contradiction, let aloneQuote:
Originally Posted by C++03 Standard Section 1.3.4
qualifying as a valid program.Quote:
Originally Posted by C++03 Standard Section 5.6 Paragraph 4
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.