Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Square and Cube roots.
#1
What is an easy way to get the square or cube root of a number and not get the part after the decimal? I just want the whole number or maybe the whole number and a remainder. No decimals. Is that possible?
Thanks
Reply
#2
Use int() to ignore the non-integer values:
>>> x = 9.4392
>>> x
9.4392
>>> int(x)
9
>>> y = 3.8743
>>> y
3.8743
>>> int(y)
3
To get a root, use a fraction as an exponent. The square root of 9 is 3. 3 squared is 9. 3**2 is 9. 9**(1/2) is 3.
The cube root of 27 is 3. 3 cubed is 27. 3**3 is 27. 27**(1/3) is 3.
Reply
#3
If you are willing to install an external library, or are working with large integer numbers, you should look at gmpy2

>>> import gmpy2
>>> gmpy2.iroot_rem(128,3)
(mpz(5), mpz(3))
iroot_rem(x,N) returns the integer part of the N-th of x and the remainder.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im at square one even with trying to install python origen 1 366 Jan-12-2024, 05:39 AM
Last Post: ndc85430
  Finding all roots of a function thonpy 3 2,438 Apr-16-2021, 03:02 AM
Last Post: bowlofred
  pip unistall in alternate roots confminn 0 1,449 Aug-21-2020, 08:03 PM
Last Post: confminn
  square root of 5 input numbers mrityunjoy 1 2,034 Jun-10-2020, 11:08 AM
Last Post: perfringo
  square root of 6 input numbers mrityunjoy 3 2,619 Jun-07-2020, 06:35 AM
Last Post: ndc85430
  How to simplify square finding program? meknowsnothing 3 2,927 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  cropping a picture (always square) Leon 1 2,152 Aug-13-2018, 10:04 AM
Last Post: Leon
  Error when trying to square a number pistacheoowalnuttanker 5 3,838 Jul-20-2018, 02:23 AM
Last Post: pistacheoowalnuttanker
  I'm having a problem with this cube root code Duc311003 2 2,879 May-16-2018, 07:13 PM
Last Post: buran
  "³" math cube symbol encoding Ragnar 5 9,614 Mar-09-2018, 04:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020