How do I get a whole number as a result for a cube root?

后端 未结 6 1934
梦谈多话
梦谈多话 2021-01-13 13:15

I am creating a problem which requires me to find the cube root of certain numbers, some of them have whole number roots, but a lot of them don\'t.

I have numbers li

6条回答
  •  滥情空心
    2021-01-13 13:56

    Avoid any hacks with floats and tolerance levels, these are often unreliable for large inputs. A better tool for the job is to use a multiple precision arithmetic library such as gmpy2:

    >>> import gmpy2
    >>> root, exact = gmpy2.iroot(125, 3)
    >>> print(root)
    5
    >>> print(exact)
    True
    

提交回复
热议问题