Thread: Problem with "Casting"

  1. #31
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It will have an effect - it casts to float, while the compiler would cast to double. So you potentially lose precision.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    It will have an effect - it casts to float, while the compiler would cast to double. So you potentially lose precision.
    Yeah, ok. But for the purposes of this discussion, it won't make any noticable difference, since the values we're dealing with in the calculation are close together.

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

  3. #33
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    well, the function is O:K, the error appears before the function works

  4. #34
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem has nothing to do with type conversions (aka castings).

    In iterar(), x becomes a large negative number, eventually approaching the negative value with greatest magnitude that can be stored in a floating point variable. With very large (as in distance from zero) values, the computation of tan(x) triggers a floating point overflow (simply because the computation will typically be approximated by some Taylor series or similar approximation, which means a power of x is computed).

    You will need to adapt your algorithm to avoid situations of very large values being iteratively computed.

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> well, the function is O:K, the error appears before the function works

    How do you know? Have you narrowed down the problem and made the code smaller? Keep removing unnecessary code until you make a tiny program that still has the error.

  6. #36
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    You are right,
    i ook this part of program and execute it and works! That is the error is not about casting..
    thii is the program
    Code:
    #include<stdio>
    #include<stdlib>
    #include<iostream>
    #include<math>
    #include<conio>
    #define PI 3.14159265358979324
    using namespace std;
    int main()
      {
        float c;
        int i;
        for (i=0;i<10;i++)
         {
           c= (float) (2*i +1)* PI/2.0 - PI/180.0 ; 
           printf("&#37;7.4f   \n",c);
         }
         getch();
       }

  7. #37
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    i mean I took

  8. #38
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    I dont know how to solve this problem, the problem remains!!!!

  9. #39
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you see the posts by others in this thread indicating where they think the error might be, like grumpy's post? What have you done to look at those? Can you narrow down the problem by calling printf before calling tan(x) to see what x is as suggested earlier?

  10. #40
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    i am tryng.

  11. #41
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mcaro72 View Post
    i am tryng.
    We know!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #42
    Registered User
    Join Date
    Apr 2008
    Location
    Barranquilla-Colombia
    Posts
    21
    Thank you all(Daved,et al.)
    I solved the problems,
    The problem was with the function.

    Gracias!!!!

  13. #43
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Thumbs up

    Glad you got it working mcaro!

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