Unexplained nan values

I have a question about why this specific function sometimes outputs nan values only when working on an arduino.

What the function is supposed to return true only if it finds an intersection between 3 circles ,and if so, it sets X,Y values to variables. By the way I didn’t code it.

boolean calculateThreeCircleIntersection(double x0, double y0, double r0,double x1, double y1, double r1,double x2, double y2, double r2) { 

    double a, dx, dy, d, h, rx, ry;
    double point2_x, point2_y;
    dx = x1 - x0;
    dy = y1 - y0;

    // Determine the straight-line distance between the centers. 
    d = sqrt((dy*dy) + (dx*dx));

    // Check for solvability. 
    if (d > (r0 + r1))
    {
      // no solution. circles do not intersect. 
      return false;
    }
    if (d < abs(r0 - r1))
    {
      // no solution. one circle is contained in the other 
      return false;
    }

    // 'point 2' is the point where the line through the circle
    //intersection points crosses the line between the circle
    //centers.


    // Determine the distance from point 0 to point 2. 
    a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

    // Determine the coordinates of point 2.
    point2_x = x0 + (dx * a/d);
    point2_y = y0 + (dy * a/d);

    // Determine the distance from point 2 to either of the
    // intersection points.

    h = sqrt((r0*r0) - (a*a));

    // Now determine the offsets of the intersection points from
    // point 2.

    rx = -dy * (h/d);
    ry = dx * (h/d);

    // Determine the absolute intersection points. 
    intersectionPoint1_x = point2_x + rx;
    intersectionPoint2_x = point2_x - rx;
    intersectionPoint1_y = point2_y + ry;
    intersectionPoint2_y = point2_y - ry;

    Serial.println("INTERSECTION Circle1 AND Circle2:  (");
    Serial.print( intersectionPoint1_x );
    Serial.print(",");
    Serial.print(intersectionPoint1_y);
    Serial.print(") AND (");
    Serial.print( intersectionPoint2_x );
    Serial.print(intersectionPoint2_y);
    Serial.print(")");
    // Lets determine if circle 3 intersects at either of the above intersection points. 
    dx = intersectionPoint1_x - x2;
    dy = intersectionPoint1_y - y2;
    double d1 = sqrt((dy*dy) + (dx*dx));

    dx = intersectionPoint2_x - x2;
    dy = intersectionPoint2_y - y2;
    double d2 = sqrt((dy*dy) + (dx*dx));

    if(abs(d1 - r2) < EPSILON) {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: ("); 
      Serial.print( intersectionPoint1_x );
      Serial.print( "," );
      Serial.print(intersectionPoint1_y);
      Serial.print( ")" );
    }
    else if(abs(d2 - r2) < EPSILON) {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: (");
      Serial.print(intersectionPoint2_x);
      Serial.print( "," );
      Serial.print( intersectionPoint2_y);
      Serial.print(")"); //here was an error
    }
    else {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: NONE");
    }
    return true;
  }

Thanks!

What happens if d is 0?

What happens if d is 0?

I have put an arguments to check if d = 0 and still doesn't work.

I have put an arguments to check if d = 0 and still doesn't work.

But, you didn't post the new code...

Have you looked for other places where you potentially divide by zero?

What about the case with two identical circles, at the same centre?

boolean calculateThreeCircleIntersection(double x0, double y0, double r0,double x1, double y1, double r1,double x2, double y2, double r2) { 

    double a, dx, dy, d, h, rx, ry;
    double point2_x, point2_y;
    dx = x1 - x0;
    dy = y1 - y0;

    // Determine the straight-line distance between the centers. 
    d = sqrt((dy*dy) + (dx*dx));

    // Check for solvability. 
    if (d > (r0 + r1))
    {
      // no solution. circles do not intersect. 
      return false;
    }
    if ( d == 0){
    return false;
    }
    if (d < abs(r0 - r1))
    {
      // no solution. one circle is contained in the other 
      return false;
    }

    // 'point 2' is the point where the line through the circle
    //intersection points crosses the line between the circle
    //centers.


    // Determine the distance from point 0 to point 2. 
    a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

    // Determine the coordinates of point 2.
    point2_x = x0 + (dx * a/d);
    point2_y = y0 + (dy * a/d);

    // Determine the distance from point 2 to either of the
    // intersection points.

    h = sqrt((r0*r0) - (a*a));

    // Now determine the offsets of the intersection points from
    // point 2.

    rx = -dy * (h/d);
    ry = dx * (h/d);

    // Determine the absolute intersection points. 
    intersectionPoint1_x = point2_x + rx;
    intersectionPoint2_x = point2_x - rx;
    intersectionPoint1_y = point2_y + ry;
    intersectionPoint2_y = point2_y - ry;

    Serial.println("INTERSECTION Circle1 AND Circle2:  (");
    Serial.print( intersectionPoint1_x );
    Serial.print(",");
    Serial.print(intersectionPoint1_y);
    Serial.print(") AND (");
    Serial.print( intersectionPoint2_x );
    Serial.print(intersectionPoint2_y);
    Serial.print(")");
    // Lets determine if circle 3 intersects at either of the above intersection points. 
    dx = intersectionPoint1_x - x2;
    dy = intersectionPoint1_y - y2;
    double d1 = sqrt((dy*dy) + (dx*dx));

    dx = intersectionPoint2_x - x2;
    dy = intersectionPoint2_y - y2;
    double d2 = sqrt((dy*dy) + (dx*dx));

    if(abs(d1 - r2) < EPSILON) {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: ("); 
      Serial.print( intersectionPoint1_x );
      Serial.print( "," );
      Serial.print(intersectionPoint1_y);
      Serial.print( ")" );
    }
    else if(abs(d2 - r2) < EPSILON) {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: (");
      Serial.print(intersectionPoint2_x);
      Serial.print( "," );
      Serial.print( intersectionPoint2_y);
      Serial.print(")"); //here was an error
    }
    else {
      Serial.println("INTERSECTION Circle1 AND Circle2 AND Circle3: NONE");
    }
    return true;
  }

I have, I also thought of maybe I could’ve used a negative number in sqrt() but the number had to be positive

What about the case with two identical circles, at the same centre?

That is not possible as I state the values manually of each circle so they do not have the same centeres

That is not possible as I state the values manually of each circle so they do not have the same centeres

Please post a simplified, self contained sketch that calls the function, and causes it to fail.

Thank you guys, I have found the problem. It wasn't in the formula, thanks.

And . . ?

@Bakr: you discovered something and the usual process on this Forum is to share what you found with others so they don't go down the same blind alley.

I used a pointer of the function in another function which somehow led to corruption

Bakr: I used a pointer of the function in another function which somehow led to corruption

Which is precisely why we ask you to post ALL your code, so that other people can reproduce your results, without going off on some wild goose chase.