# python: how accurate math.sqrt(x) function is?

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?

Asked by: Elian586 | Posted: 06-12-2021

For proper rounding, use `round()`; it rounds to the nearest whole number, but returns a float. Then you may construct an `int` from the result.

(Most probably your code is not performance-critical and you will never notice any slowdown associated with `round()`. If you do, you probably should be using numpy anyway.)

Answered by: Kirsten836 | Posted: 07-01-2022

If you are very concerned with the accuracy of `sqrt`, you could use the decimal.Decimal class from the standard library, which provides its own `sqrt` function. The `Decimal` class can be set to greater precision than regular Python `float`s. That said, it may not matter if you are rounding anyways. The `Decimal` class results in exact numbers (from the docs):

The exactness [of Decimal] carries over into arithmetic. In decimal floating point, `0.1 + 0.1 + 0.1 - 0.3` is exactly equal to zero. In binary floating point, the result is `5.5511151231257827e-017`. While near to zero, the differences prevent reliable equality testing and differences can accumulate. For this reason, decimal is preferred in accounting applications which have strict equality invariants.

Answered by: Daryl572 | Posted: 07-01-2022

The solution is easy. If you're expecting an integer result, use int(math.sqrt(n)+.1). If the value is a little more or less than the integer result, it will round to the correct value.

Answered by: Aldus549 | Posted: 07-01-2022

# Similar questions

python - Which is more accurate, x**.5 or math.sqrt(x)?

I recently discovered that x**.5 and math.sqrt(x) do not always produce the same result in Python: Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 &gt;&gt;&gt; 8885558**.5 - math.sqrt(8885558) -4.5474735088646412e-13 Checking all integers below 10**7, the two methods produced different results for almost exactly 0.1% of the sam...

performance - Which is faster in Python: x**.5 or math.sqrt(x)?

I've been wondering this for some time. As the title say, which is faster, the actual function or simply raising to the half power? UPDATE This is not a matter of premature optimization. This is simply a question of how the underlying code actually works. What is the theory of how Python code works? I sent Guido van Rossum an email cause I really wanted to know the differences in these method...

python - Which is more accurate, x**.5 or math.sqrt(x)?

I recently discovered that x**.5 and math.sqrt(x) do not always produce the same result in Python: Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 &gt;&gt;&gt; 8885558**.5 - math.sqrt(8885558) -4.5474735088646412e-13 Checking all integers below 10**7, the two methods produced different results for almost exactly 0.1% of the sam...