0

I defined a simple sqrt function to calculate a square root of a given number.

def sqrt(n):
    low = 0
    up = n  
    for i in range(50):
        mid = float(low+up)/2
        if pow(mid,2) < n:
            low = mid
        elif pow(mid,2) > n:
            up = mid
        else:
            break
    return mid

When I do:

print(sqrt(9))

I get 3.0 as the output. However when I do:

print(int(sqrt(9))) I get 2.

Could somebody please help me understand why this is happening?

Thank you.

Vizag
  • 743
  • 1
  • 7
  • 30
  • 1
    When I run your first code I don't get `3.0` I get `2.9999999999999973` `int()` just cuts off the decimal and gives you `2`. Maybe `round()` would give you the results you want. – Mark Jan 02 '20 at 03:39
  • 2
    I think sqrt is already defined in python (which does the exact thing you want to do). Why are you redefining it? – Swetank Poddar Jan 02 '20 at 03:40
  • 1
    If `print(sqrt(9))` printed that for you, you must be on Python 2, which is obsolete, incompatible with tons of useful libraries and features, and unsupported by the Python development team. You should really get on Python 3. – user2357112 Jan 02 '20 at 03:44
  • ^How did you immediately recognize that this was a Python version issue? Just curious – Vizag Jan 02 '20 at 04:27
  • I've seen the same problem before. – user2357112 Jan 02 '20 at 08:23

3 Answers3

2

Actually, sqrt(9) returns 2.9999999999999973 [1], at least in Python version 3.5 (and perhaps all versions 3.x?). And int returns only the integer component of the number. That's why you get 2 as the result, as the integer component of 2.9999999999999973 is indeed 2.

If you want to round the result to the nearest integer, you can do round(sqrt(9)) which produces 3.

[1] To understand why this happens and how to fix this issue, you can refer to this post.

Kristada673
  • 3,512
  • 6
  • 39
  • 93
  • "Actually, int(sqrt(9)) returns 2.9999999999999973 for me" - uh, what? – user2357112 Jan 02 '20 at 03:43
  • @user2357112supportsMonica oops, my bad, I must have typed the `int` as a typo. I've corrected the answer now. – Kristada673 Jan 02 '20 at 03:47
  • Hey. Thanks for your answer. I am using Python 2.7 and the output I get for `sqrt(9)` is `3.0`. – Vizag Jan 02 '20 at 03:58
  • Output of `sqrt(9)` is unfortunately not the same as what you think `print(sqrt(9))` will print... – MadaManu Jan 02 '20 at 04:00
  • @MadaManu I see. I'm using Python 3.5, and did not know 2.7 treated it a little differently. I'll edit my answer to reflect the Python version. – Kristada673 Jan 02 '20 at 04:01
  • @MadaManu, why do you say that? – Vizag Jan 02 '20 at 04:01
  • `Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> >>> >>> >>> >>> def sqrt(n): ... low = 0 ... up = n ... for i in range(50): ... mid = float(low+up)/2 ... if pow(mid,2) < n: ... low = mid ... elif pow(mid,2) > n: ... up = mid ... else: ... break ... return mid ... >>> sqrt(9) 2.9999999999999973 >>> print(sqrt(9)) 3.0 >>> print(int(sqrt(9))) 2 >>>` – MadaManu Jan 02 '20 at 04:02
  • Sorry - pasting code on comments section is not great. I updated my answer below to see it clearer. – MadaManu Jan 02 '20 at 04:03
1

It is because of rounding. Your method definition actually computes the sqrt(9) to be 2.9999999999999973. However the rounding is not applied to casting, therefore the decimal is lost in the casting of int(2.9999999999999973). When you print directly, python applies rounding which brings this value up to 3.

Python 2.7 output:

Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> def sqrt(n):
...     low = 0
...     up = n
...     for i in range(50):
...         mid = float(low+up)/2
...         if pow(mid,2) < n:
...             low = mid
...         elif pow(mid,2) > n:
...             up = mid
...         else:
...             break
...     return mid
...
>>> sqrt(9)
2.9999999999999973
>>> print(sqrt(9))
3.0
>>> print(int(sqrt(9)))
2
>>>
MadaManu
  • 868
  • 4
  • 10
0

If you want to get converted into int you can use the following:

print(int(round(sqrt(9))))
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20