Thread: Problem with "Casting"

  1. #16
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    Quote Originally Posted by vart View Post
    is n initialized?
    yeah!

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    well, i forgot to put ";" in
    Code:
     float z= 1/2.0-x*tan(x)+x
    but...

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  7. #22
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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

  8. #23
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    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  = &#37;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);
             }
    
        }

  9. #24
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    yeah, it has stable solution every interval -PI/2 + kPI,PI/2 + kPI, k belongs to integer set

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Narrow it down some more. You say it happens when i is 1, right? So change main to this:
    Code:
      int main()
        {
           iterar(1);
        }
    Does it still happen then? If not, then what is i when it happens?

    If yes, then change your iterar function to this:
    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  = &#37;9.7f           , f(x)= %9.7f \n",x,foriginal(x)-x);
      }
    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.

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > float z=1/2.0-x*tan(x)+x;
    Are you sure you're not missing some parentheses here?

  13. #28
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    no, i did not miss anything

    I thought that the problem was the "CASTING", c is float, n is integer...

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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;

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    c= (float) (2*n +1)* PI/2.0 - PI/180.0;
    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.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM