0
Can anyone please explain how line 21 works on the following code?
6 Answers
+ 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.
+ 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.
+ 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.
+ 2
Thank you Brian
0
Why 1e-6 Brian? I am just confused with that. I am not getting the logic perfectly.
0
Why did we use this condition? Brian why the difference need to be greater than that?