Mat Posted July 11, 2009 Posted July 11, 2009 (edited) kk... This appears to be a general computing problem, as even the ms powertoy calculator failed to cope with it. Which is a sad surprise..., read Wus' post below for an explanation as to why calculators give odd answers.To see the problem simply, try work out -27^(1/3), which should be the same as " 3√-27 ", which is quite simply -3. For some reason, computers can't handle it. give solutions other than the one we want. Powertoy Calc ==> (1.5+2.5980762113533159402911695122588i) Autoit ==> -1.#INDGoogle ==> -3 all were inputted with this: "-27^(1/3)", only google got it. (Wus says: Pt calc got it right) My solution just checks to see if its negative, treats it as a positive, and implements it at the end. its simple, but i needed it for a project euler think I'm doing expandcollapse popup#include-once ; #INDEX# ======================================================================================================================== ; Title .........: Math root ; AutoIt Version : 3.3.0.0 / 3.3.1.1 ; Language ......: English ; Description ...: Deals with the nth root, including negative numbers. ; Author(s) .....: MDiesel ; ================================================================================================================================ ; #FUNCTION# ===================================================================================================================== ; Name...........: _RealRoot ; Description ...: Finds the real nth root of a number, including negatives. ; Syntax.........: _RealRoot($fNum, $nExp = 3) ; Parameters ....: $fNum - The Number ; $nExp - The root. ; Return values .: On Success: = Returns the result. ; On Failure: = Sets @Error and returns 0, which can be a valid responce, so check @Error first. ; @ERROR: = 0 = No error. ; 1 - Result is an imaginay number ; Author ........: MDiesel ; Remarks .......: ; Example .......: ConsoleWrite("Normal = " & -27^(1/3) & @CRLF & "_RealRoot = " & _RealRoot(-27, 3)) ; ================================================================================================================================ Func _RealRoot($fNum, $nExp = 3) Local $bNeg = False, $fRet = 0 If $nExp < 0 Then Return SetError (1, 0, $fNum) If $fNum < 0 Then ; is negative If Mod($nExp, 2) Then ; nExp is odd, so negative IS possible. $bNeg = True $fNum *= -1 Else Return SetError(1, 0, $fNum & "i") ; Imaginary number. EndIf EndIf $fRet = $fNum ^ (1 / $nExp) If $bNeg Then $fRet *= -1 Return $fRet EndFunc ; ==> _RealRoot MDiesel Edited July 11, 2009 by mdiesel AutoIt Project Listing
Wus Posted July 11, 2009 Posted July 11, 2009 kk... This appears to be a general computing problem, as even the ms powertoy calculator failed to cope with it. Which is a sad surprise...To see the problem simply, try work out -27^(1/3), which should be the same as " 3√-27 ", which is quite simply -3. For some reason, computers can't handle it. Powertoy Calc ==> (1.5+2.5980762113533159402911695122588i) Autoit ==> -1.#INDGoogle ==> -3 all were inputted with this: "-27^(1/3)", only google got it.Actually, you are wrong, not MS Powertoy calc. If your range is allowed to be the complex numbers then 1.5+2.5980762113533159402911695122588i is a solution. In general there will be n (not necessarily distinct) roots to an nth order equation. In this case the polynomial equation you are trying to solve is x^3 -27 = 0, which is cubic (or order 3) and thus has 3 roots.The three cubic roots of 27 are:-3-1.5 + 2.598076211353i-1.5 - 2.598076211353iWhat you probably wanted was the real solution, but that isn't always what you get, especially when you use a calculator that doesn't have a strong symbolic solver. What the MS Powertoy calc almost certainly does to find roots is to use Newtons Method, which as far as I know has no preference between real or imaginary roots, though I am not completely certain of that.
dantay9 Posted July 11, 2009 Posted July 11, 2009 Still could be useful. The more common answer that most people would be looking for is the -3. Then you could easily find the other two roots by factoring out the -3 from the first equation and solving the resulting equation.
Mat Posted July 11, 2009 Author Posted July 11, 2009 Actually, you are wrong, not MS Powertoy calc. If your range is allowed to be the complex numbers then 1.5+2.5980762113533159402911695122588i is a solution. In general there will be n (not necessarily distinct) roots to an nth order equation. In this case the polynomial equation you are trying to solve is x^3 -27 = 0, which is cubic (or order 3) and thus has 3 roots....kk... thank you Wus for giving me a headache, and also clearing that up, but I think I prefer -3 as an answer though. And that makes us both right, with me falsely accusing my calc. And since anyone who has a use for the other 2 solutions is probably clever enough to write their own function to get it, This function is not changingI know what I'm going to be reading up on tonight - I thought it was a bit strange a calc that can give me sqrt(2) to 512 figures in a couple of secs can't do nth roots, so at least I know its still a good calculator! As for autoit, I don't think it even tried... it saw the negative and went walk abouts Rename function ==> _RealRootIt just goes to show how much of a pain maths can be.MDiesel AutoIt Project Listing
Wus Posted July 11, 2009 Posted July 11, 2009 (edited) Still could be useful. The more common answer that most people would be looking for is the -3. Then you could easily find the other two roots by factoring out the -3 from the first equation and solving the resulting equation. You are absolutely correct on all points. I was not deriding the usefulness of the function, just trying to correct the misinformation given. ... but I think I prefer -3 as an answer though. Heh, I'd prefer to not be 20k in the hole right now, but that doesn't change the fact that I am. Without explicitly specifying that you wish the result to be real, the imaginary roots are just as valid. Incorrect assumptions on your part are just that, incorrect. It seems like you have remedied this however, via changing the functions name. I know what I'm going to be reading up on tonight... Awesome. It's great to see you are willing to learn. It just goes to show how much of a pain maths can be. Not-so-awesome. Imaginary numbers shouldn't be viewed as a pain, they were created because they were necessary for the development of mathematics, which in turn was necessary for the development of civilization. You may not know it, but almost all of electronics (read computers) is based on mathematics involving complex numbers and complex valued functions. Thus you probably wouldn't have a computer in front of you right now without the imaginary number i. _Root($fNum, $nBase = 3) I suggest you change the $nBase parameter to something else... say $nOrder. To me $nBase implies the base for a numbering system, which is decidedly not what you mean. EDIT: Typo. Edited July 11, 2009 by Wus
Mat Posted July 11, 2009 Author Posted July 11, 2009 (edited) hmmm... not sure why I called it $nBase... I probably would have done $nExp if I was thinking straight... Will change it now There were loads of errors in the code... and they've been fixed... MDiesel Edit: anyone interested in me writing a function to return all 3? Edited July 11, 2009 by mdiesel AutoIt Project Listing
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now