When I call the fabs function for the double argument z (at that moment equal to -10.47749), I suddenly get an output value of type int equal to 4991. What is the problem and how to correctly take the module from the number?

double segment_area(double d, double friend_r, double foe_r) { double z = (foe_r * foe_r) - (friend_r * friend_r); double alpha = 2.0 * asin(friend_r - ((((d - (fabs(z) / d)) / 2.0) * ((d - (fabs(z) / d)) / 2.0))) / friend_r); printf("%lf\n", alpha); return 0; } 

Debag version of the function, which, given the distance of the centers of the circles and their radii, considers the radian measure of the central angle formed by the intersection points of these circles

  • one
    Give the problem piece of code and its context. - Harry
  • Added, but, as it seems to me, it will only distract attention from the problem - because the problem occurs after the function is called; Input data are correct - Alexander Chi
  • one
    @AlexanderChi, please add a minimal, self-sufficient and reproducible example . For example, here I called your function and fabs with some parameters and received some answer, but it is absolutely unclear if there is a problem. - yeputons
  • It is likely that the problem is actually in another place of the program, and here you just get the consequences. - yeputons
  • Hmm, and when executing a program when selecting the expression fabs (z), the debugger will not show the result of the execution, right? IDE Code :: Blocks with MinGW, GDB / CDB Debugger - Alexander Chi

2 answers 2

Fix all your constants on double:

 double segment_area(double d, double friend_r, double foe_r) { double z = (foe_r * foe_r) - (friend_r * friend_r); double alpha = 2.0 * asin(friend_r - ((((d - (fabs(z) / d)) / 2.0) * ((d - (fabs(z) / d)) / 2.0))) / friend_r); printf("%lf\n", alpha); return 0; } 
  • Unfortunately, it did not help (During the execution of the program I check the return value of the fabs (z) with the debugger, and it shows: int 4991 But, just in case, I will correct and the constants = D - Alexander Chi

Oddly enough, the code conversion helped:

 double segment_area(double d, double friend_r, double foe_r) { double z = (foe_r * foe_r) - (friend_r * friend_r); z = fabs(z); double alpha = 2.0 * asin(friend_r - ((((d - (z / d)) / 2.0) * ((d - (z / d)) / 2.0))) / friend_r); printf("%lf\n", alpha); return 0; 

}

However, I still do not understand the reason for this behavior of the function.