Try separating out every single operation to its own statement (on its own line) and make sure the values that you get are what you expect.
well, i forgot to put ";" in
but...Code:float z= 1/2.0-x*tan(x)+x
It does actually help if you provide actual code that illustrates your problem, rather than paraphrasing. The code as given will not even compile.
There is no header named stdio. It is either stdio.h (from C) or cstdio (C++). There is no header named stlib. Presumably you mean either stdlib.h or cstdlib
You are missing a semi-colon from the first line of foriginal() which also means the code will not compile.
There are some minor issues due to implicit conversions between types in your code (eg when PI is substituted, the compiler will see a literal of type double) but that observation does not explain you getting a runtime error.
Try cutting and pasting actual code that illustrates your problem, and info such as what compiler/settings you are using, rather than playing "blind mans bluff" with us.
As grumpy stated, if you give code that is different than what you're using, then we can't help you find the problem.
Find exact code that gives the error for you, then copy and paste it into a post.
Also, are you sure the error you are getting is not somewhere else in the code?
(2*1+1)*PI/2.0-PI/180.0 is 3*PI/2 - 0.01745 which is 4.712-0.01745 which is about 4.69. That's hardly something that should cause a runtime error.
Does your equation always has a stable solution? or some starting points can actually walk into infinity?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Assuming his code is vaguely correct, my guess is 'fabs(xn-x)'.
Check 'xn' and 'x' for validity... the expression can raise a floating point exception, I believe, if either is NAN.
Soma
here it goes the original
Code:#include<stdio> #include<stdlib> #include<iostream> #include<math> #define PI 3.14159265358979324 using namespace std; float foriginal( float x) { float z=1/2.0-x*tan(x)+x; return(z); } float iterar(int n) { float a,b,xn,x,c,eps; eps=0.001; c= (float) (2*n +1)* PI/2.0 - PI/180.0 ; //when i==1 the error appears!!!!! xn=c; do { x=xn; xn=foriginal(x); } while (fabs(xn-x)>eps); printf("The root is = %9.7f , f(x)= %9.7f \n",x,foriginal(x)-x); } int main() { int i; float a,b; for(i=0;i<10;i++) { iterar(i); } }
yeah, it has stable solution every interval -PI/2 + kPI,PI/2 + kPI, k belongs to integer set
Narrow it down some more. You say it happens when i is 1, right? So change main to this:Does it still happen then? If not, then what is i when it happens?Code:int main() { iterar(1); }
If yes, then change your iterar function to this:Does it still happen then? If yes, then obviously that line is not your problem. If no, then move that line to its own program and see if it still causes a problem.Code:float iterar(int n) { float a,b,xn,x,c,eps; eps=0.001; c= 4.6949358; //when i==1 c will be this anyway. xn=c; do { x=xn; xn=foriginal(x); } while (fabs(xn-x)>eps); printf("The root is = %9.7f , f(x)= %9.7f \n",x,foriginal(x)-x); }
Or perhaps add some debugging printf statement like:
Code:float foriginal( float x) { float z=1/2.0-x*tan(x)+x; printf("x = %g, z = %g\n", x, z); return(z); }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
> float z=1/2.0-x*tan(x)+x;
Are you sure you're not missing some parentheses here?
no, i did not miss anything
I thought that the problem was the "CASTING", c is float, n is integer...
>I thought that the problem was the "CASTING", c is float, n is integer...
I think the problem is the formula in foriginal(). Do what Dave suggested and add a printf(), and you'll see some rather large numbers. Just a wild guess but maybe:
Code:float z = 1 / (2.0 - x*tan(x)) + x;
The green portion is indeed integer - and as long as 2 * n + 1 isn't overflowing the integer value, it will have absolutely no effect whatsoever to cast this to float, since the blue portion is float already, and thus will force the integer portion to be implicitly cast to float by the compiler anyways.Code:c= (float) (2*n +1)* PI/2.0 - PI/180.0;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.