Problem with "Casting"

This is a discussion on Problem with "Casting" within the C++ Programming forums, part of the General Programming Boards category; Hello Everybody, I ve got this error "Invalid floating point operation" whne i compile this: ... #define Pi = 3.141596 ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21

    Problem with "Casting"

    Hello Everybody,

    I ve got this error "Invalid floating point operation" whne i compile this:

    ...
    #define Pi = 3.141596
    ...
    float c;
    int n;

    c= (float) (2.0*n+1.0) * Pi/2.0;

    Maybe this is an easy question but i cannot yet solve it.

    Any suggest?

    Bye.
    Miguel.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> #define Pi = 3.141596

    That's not the proper syntax for a #define.

    I would use a constant:
    Code:
    const double Pi = 3.141596;

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    Quote Originally Posted by Daved View Post
    >> #define Pi = 3.141596

    That's not the proper syntax for a #define.

    I would use a constant:
    Code:
    const double Pi = 3.141596;
    Thanks, but i made the change and the error remains "invalid floating overpoint"

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Isn't there specific functions defined to handle type casting? hint, hint.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What change did you make? Did you remove the #define or fix it?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    yes, i removed #define and put const double Pi ... but remains the error

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It works for me. Perhaps you can post the latest code and the exact error message?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    i'm going to probe,

    P.S. I'm working with Borland C++ 4.0

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    With const double Pi, you need a semicolon.

    Use either
    Code:
    #define PI 3.14159265358979324
    or
    Code:
    const double PI = 3.14159265358979324;
    In future, you have to post the exact error you get if you want help.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    Well , the error remains, this is the excact error:
    "Project iterarmulti.exe raised exception class EInvalidOp with message 'Invalid floating point operation'. Process stopped . Use Step or Run to continue."

    and i put

    const double Pi = 3.141592653589;

    well any suggest?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, please post the code that exhibits that behavior. You should be able to make a small example that shows the error. If you can't make a small example, then the problem is somewhere in the code you're not showing.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    is n initialized?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    Quote Originally Posted by Daved View Post
    Yes, please post the code that exhibits that behavior. You should be able to make a small example that shows the error. If you can't make a small example, then the problem is somewhere in the code you're not showing.
    O.K. Daved, this is the program
    Code:
    #include<stdio>
    #include<stlib>
    #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);
             }     
       }//end program

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    #include<stdio>
    #include<stlib>
    #include<math>
    Those should have .h extensions.

    That doesn't compile without errors. Maybe you could try fixing those first . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Unfortunately what you posted has syntax errors and doesn't compile. Did you try it? When I fix the syntax errors and run it, it still works fine on the line you say you are having trouble with.

    Your current usage of the #define is fine, your problem is somewhere else.

Page 1 of 3 123 LastLast
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, 10: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, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21