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

后端 未结 6 1867
梦谈多话
梦谈多话 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 14:09

    It's more straightforward to avoid the problem! For example:

    mx = 12000                   # maximum for cubes
    crmx = int(mx**(1/3)) + 1    # largest test integer
    
    result = max([n for n in range(crmx) if n**3 < mx])
    
    # result = 22
    

    Floating point arithmetic is always approximate. For example:

    .99999999999999999.is_integer() gives True

    .9999999999999999.is_integer() gives False

    (Your interpreter's mileage may differ.)

提交回复
热议问题