Isaac C. answered 05/30/19
Physics, Chemistry, Math, and Computer Programming Tutor
When you are using floating point numbers in python, the best answer you can expect is double precision arithmetic, which means about 15 digits of precision.
In this case, the problem is that when you subtract that mall number from 100 you get a number very close to 100 and taking the square root gives a number very close to 10. However the difference from 10 is not displayed exactly correctly because of the limited precision. So yes, you have correctly identified the problem.
The best way to handle that situation is to detect when b2 is much greater than 4ac, and to then use an approximation for -b + sqrt(b2 -4ac) in those situations. (only when b is positive. If b is negative, then the addition is the problem)
When b is positive, and b2 is much greater than 4ac, then -b + sqrt(b^2 - 4ac) is approximately equal to -2ac/b. You might check, for example that absolute value of (b^2/(4ac) is greater than about 10 when b is positive. If so, then use the approximation, otherwise calculate normally.
If you use the approximation I gave, you will get x = -c/b for those problem situations. That is exactly what you want.. The other root with the minus sign - b - sqrt (b2 - 4ac) can be calculated normally when b is positive.
And for the cases where b is negative, then use the approximation with the negative square root.