Consider the following code snippet in Python:
m = int(math.sqrt(n))
For n = 25, it should give m = 5 (and it does in my shell). But from my C experience I know that using such expression is a bad idea, as sqrt function may return a slightly lower value than the real value, and then after rounding i may get m = 4 instead of m = 5. Is this limitation also involved in python? And if this is the case, what is be the best way to write such expressions in python? What will happen if I use Java or C#?
Besides, if there is any inaccuracy, what factors controls the amount of it?
If you are very concerned with the accuracy of
sqrt, you could use the decimal.Decimal class from the standard library, which provides its ownsqrtfunction. TheDecimalclass can be set to greater precision than regular Pythonfloats. That said, it may not matter if you are rounding anyways. TheDecimalclass results in exact numbers (from the docs):