Can you make "tan()" crash, by passing it pi/2 (which results in infinity)?
I tested it out, with float and double, and I cannot pass it exactly pi/2, so it does not crash. I've also tried pi/2+pi, pi/2+2pi, etc.
Is it possible?
Can you make "tan()" crash, by passing it pi/2 (which results in infinity)?
I tested it out, with float and double, and I cannot pass it exactly pi/2, so it does not crash. I've also tried pi/2+pi, pi/2+2pi, etc.
Is it possible?
Last edited by MatthewDoucette; 04-06-2006 at 07:02 PM.
well, can you even get an exact pi to divide?
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Since the double type cannot accurately represent "pi/2", you only get a very large number for the double value slightly below pi/2 and a very large negative number for the double value slightly above pi/2.
Also, in general the math functions will return infinity if passed something that results in an overflow. For example exp(1000).
Isn't it supposed to be a good thing that tan() can't crash your program?
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
Not true.Originally Posted by cwr
Only some floating point formats (e.g. IEEE) support the notion of "infinity". There is no requirement that a C compiler employs such a floating point format. The actual definition of floating point in C (or C++) only requires that a floating point represent a value in the form sign*mantissa*10^exponent where ^ represents "to the power of", sign is +/-1, mantissa is in the range 0 to 1, and exponent is a signed integral value. In practice, there are plenty of C compilers that target systems which do not support IEEE or similar floating point formats.
I don't know if this make any difference, but tan(pi/2) is not actually inifinity. By definition tan is sin/cos and since the sin of pi/2 is 1 and the cos of pi/2 is 0 the tan of pi/2 is an undefined value, because you cannot divide anything by zero. On the other hand, the limit of x as it approaches tan(pi/2) from the left is infinity.
That is true; should tan(pi/2) return infinity, or should it return negative infinity? But of course mathematically pi/2 is not in tan's domain, so saying tan(pi/2) is just nonsense.
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
I assume, with the last sentence, you intended to say something like "the limit as x tends to pi/2 from the left of tan (x) is positive infinity".Originally Posted by CrazedBrit
In addition, "the limit as x tends to pi/2 from the right of tan (x) is negative infinity".
The fact that the left and right limits have different values means that tan(pi/2) has no value (infinite or otherwise).
This kind of statement is in general not true.Originally Posted by grumpy
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
"Undefined"? Then why didn't they make it like
?Code:double tan(double x) { if(x / PI * 2 == (int)(x / PI * 2)) { throw exception; } }
Ok, mathematically speaking, tan(x) TENDS to infinity as x TENDS to (2K+1)PI/2, but is UNDEFINED for (2k+1)PI/2. The single point. Make sense?
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
One explanation is that it's mathematically incorrect. And another is because (as cwr said) PI/2 cannot be precisely represented in floating point, so it is not possible for any floating point value to be exactly equal to pi/2.Originally Posted by jafet
No.Originally Posted by jafet
Mathematically, tan(x) is a function with discontinuities at all values x = (2k+1)pi/2 where k is an integer. Those discontinuities exist because cos(x) is zero for all x = (2k+1)pi/2 and because sin(x) is either 1 or -1. And because the cos((2k+1)pi/2 + delta) = -cos((2k+1)pi/2 - delta) --- i.e. cos(x) changes sign as x crosses left to right through (2k+1)pi/2.
Um... yeah, that's what I meant, sorry if I wasn't clear.
And the throw-exception-to-annoy-beginners-who-obviously-know-nothing-about-exceptions thing is just a joke
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
This is all I really wanted to know. I can check to see if the result is finite, if it is not then I can handle it and make sure my program doesn't crash. I believe this is the solution I was looking for, and I am satisfied with it.Originally Posted by jafet
Oh, and sorry about the "results in infinity", as I knew it was actually undefined (as it approaches both positive and negative infinity depending on which direction to approach it from.) I should have been more technically accurate!Originally Posted by MatthewDoucette
I am not sure what MSVC++ would return IF I could pass tan() pi/2 perfectly somehow. For now, I have just been checking the results to be finite or not, and if they are non-finite I cannot use them and handle the problem appropriately.
C only supports floating point operations (IEEE format or whatever else). If you want fixed point operations, you have to implement it yourself. Also, IEEE format is sign * mantissa *2^exponent. Remember, its base 2, not base 10.Originally Posted by grumpy