Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Fractional Exponent Question Flexico Programmer named Tim Posts: 16 Threads: 7 Joined: Dec 2016 Reputation: 0 Likes received: 0 #1 Dec-03-2016, 08:07 PM This response had me confused for a while: ```>>> (-6)**(1/3) (0.9085602964160701+1.5736725951324722j) >>> abs(_) 1.8171205928321397 >>> (-_)**3 -6.0```Until I realized that in the complex number plane, numbers must have more than one cube root, just like on the real number line numbers have more than one square root. Is there a way to force the power function to return a real answer if one is available? sparkz_alot Da Bishop Posts: 1,298 Threads: 32 Joined: Sep 2016 Reputation: 30 Likes received: 267 #2 Dec-03-2016, 09:41 PM hmm ```>>> >>> -6 ** (1/3) -1.8171205928321397 >>> >>> ``` If it ain't broke, I just haven't gotten to it yet. OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch" Python 3.6.5, IDE: PyCharm 2018 Community Edition Flexico Programmer named Tim Posts: 16 Threads: 7 Joined: Dec 2016 Reputation: 0 Likes received: 0 #3 Dec-04-2016, 01:00 AM (This post was last modified: Dec-04-2016, 01:35 AM by Flexico.) That's the equivalent of -(6**(1/3)) because the power operator takes precedence over the negation sign. Not the same as what I'm trying to do. It does give the correct answer in this case, but it might not for other fractional powers. Alright, I made a function using the mpmath library that does what I want: ```import mpmath def root(z, n): x = [mpmath.root(z,n,k) for k in range(n)] for y in x: if mpmath.im(y) == 0: return y return x[0]```The "mpmath.root(z,n,k)" part returns a list of all nth roots of z, and the for loop checks if any of them are real. Leaving this here just in case someone else encounters this. =] Ok, let's try again and see if the tabs stay in this time: ```import mpmath def root(z, n): x = [mpmath.root(z,n,k) for k in range(n)] for y in x: if mpmath.im(y) == 0: return y return x[0]``` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post comparing fractional parts of floats Skaperen 4 431 Mar-19-2019, 03:19 AM Last Post: casevh Negative numbers and fractional powers Flexico 1 1,809 Dec-08-2016, 04:12 PM Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)