Thread: Sqrt REAL problem

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Exclamation Sqrt REAL problem

    This is a funtion that detects when two circles colide, for that I calculate the distance between the centers of both circles and then compare it with the sum of the radius of both circles. I have no problem with that.

    The PROBLEM is that sqrt seems to have a limit range and so if d is bigger than that range when you run the program it ends and says"sqrtOMAIN error,floating point errorOMAIN". So I have to make d smaller, I tried dividing by 1000 but then the formula loses its utility because the other member of the formula hasnt changed.

    What can I do to make d smaller whithout altering the formula integrity??

    Are there any other solution to my problem?




    Code:
    void colision(void)
    {
      double d=(x-x_c)*(x-x_c)+(y-y_c)*(y-y_c);
      
      d=sqrt(d); 
      
      if(d<=RADIO_O+RADIO_J){  
        borrar_j();
        x=X_I;
        y=Y_I;
        hecho=1;
        vidas-=1;
        }
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    write your own square root formula.

    i'm not sure i get what you mean by limit.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Honestly, I don't see why you need to use square root at all.
    Code:
    bool checkCollision( Circle a, Circle b )
    {
        int distance;
    
        distance = calcDistance( a, b );
    
        if( distance >=  a.radius + b.radius )
            return 1;
        return 0;
    }
    Now, why do you need to use square root? I believe you can just use something simple to calculate the length of a line between a given set of points on a graph. (The math escapes me at the moment, but it has to do with something simple like: A^2 + B^2 = C^2. In other words, you can make it into a right angle triangle, and calculate the remainder's line length.)

    Actually I guess that does require the square root... Hm.. I'm pretty sure there is a way to find the length of a line between two given poitns without using the square root.

    [edit]
    Just how far apart are we talking here? I can get the square root of a billion with no problems.

    I do get odd behaviour if I pass it negative a billion though. Assuming your math is correct, you should never be passing it a value less than or equal to zero. Simple checks should solve that for you.

    IE: If a.x_coord == b.x_coord, just simple subtraction works. If a.y_coord == b.y_coord, the same applies.
    [/edit]

    Quzah.
    Last edited by quzah; 06-28-2002 at 08:32 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    DOMAIN errors are when you try and do something like sqrt on a negative number

    Didn't you read the manual page?
    For instance
    On success, the square root is returned. If X is real and positive,
    then the result is positive. If X is real and negative, the global
    value `errno' is set to `EDOM' (domain error).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. A REAL math problem
    By axon in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 10-07-2003, 06:40 AM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM