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?
m = int(math.sqrt(n)), which is equivalent to what you wrote, but only because(int)-->intand then you callintwith the parentheses around the next part of your statement.math.sqrtthe only thing you can do is to either write your own implementation ofmath.sqrtor write your own implementation of casting to integer.sqrtspecifically.