0

Can anyone please explain how line 21 works on the following code?

https://code.sololearn.com/c8JQ9WWyfDo6/?ref=app

25th Dec 2020, 5:09 PM
Samia Haque
Samia Haque - avatar
6 Réponses
+ 4
The conditional (fabs((mid * mid) - a) > 1e-6) is comparing the square of mid with the original squared value, a. If the absolute difference is within tolerance of +/- 0.000001 then mid is accepted as the square root of a, and the binary search ends.
25th Dec 2020, 6:09 PM
Brian
Brian - avatar
+ 4
1e-6 is often called 'e notation', or 'exponential'. It is C syntax to represent a literal constant in scientific notation. In this case the constant is 1x10^(-6), which is 0.000001. Another example: 6.022×10²³ can be expressed as 6.022e23.
25th Dec 2020, 7:05 PM
Brian
Brian - avatar
+ 4
Checking against a tolerance is used when the search technique has potential for looping too long, or even infinitely. Floating point has a minimum value that it can represent (machine epsilon). At some point, finding the midpoint between two values cannot be resolved with more precision than machine epsilon, so the computation must be abandoned and accepted as is. In C, the compiler has its own epsilons of tolerance, often larger than the machine epsilon. They are used internally for the same reasons. The constants are defined in float.h as FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON.
25th Dec 2020, 7:30 PM
Brian
Brian - avatar
+ 2
Thank you Brian
25th Dec 2020, 7:36 PM
Samia Haque
Samia Haque - avatar
0
Why 1e-6 Brian? I am just confused with that. I am not getting the logic perfectly.
25th Dec 2020, 6:52 PM
Samia Haque
Samia Haque - avatar
0
Why did we use this condition? Brian why the difference need to be greater than that?
25th Dec 2020, 7:07 PM
Samia Haque
Samia Haque - avatar