aleph01 Posted April 26, 2012 Share Posted April 26, 2012 To get practice coding with AutoIt, I'm trying to code a root calculator. It calculates roots to the nth power for positive numbers, but won't calculate an odd root of a negative number. For instance, when asked for the cube root of -27, it should return -3, but it returns an error instead. Everything else seems to work fine. Can anyone see an error in my code or is there a better way to approach this?Thanks,_aleph_#include <math.au3> dim $rootword $odd = 0 $root = InputBox ("Root Calculator", "What root do you wish to calculate? (2 = square root, 3 = cube root, etc.") If $root <= 0 Then Msgbox (0, "Error", "Unable to perform zero or negative root calculations.") Exit EndIf If $root = 2 Then $rootword = ("square") ElseIf $root = 3 Then $rootword = ("cube") Else $rootword = $root & "th" EndIf $num = InputBox ("Root Calculator", "Of what number do you wish to calculate the root?") If $num < 0 Then $odd = _MathCheckDiv ($root, 2) EndIf If $odd <> 2 Then MsgBox (0, "The " & $rootword & " root of " & $num & " is:", $num ^ (1/$root)) Else MsgBox (0, "Unable to Calculate", "One cannot calculate an even root of a negative number, silly!") EndIf Meds. They're not just for breakfast anymore. Link to comment Share on other sites More sharing options...
JohnOne Posted April 26, 2012 Share Posted April 26, 2012 I don't think you can calculate the root of a negative number. I've seen fellas come to blows about this in the pub. I'm out of here. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
UEZ Posted April 26, 2012 Share Posted April 26, 2012 (edited) Complex numbers are not supported by AutoIt.Have a look here how to get the root of negative numbers: http://en.wikipedia.org/wiki/Complex_numberBr,UEZ Edited April 26, 2012 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
iamtheky Posted April 26, 2012 Share Posted April 26, 2012 (edited) or just calculate it all positive like, and return the negative. #include <math.au3> dim $rootword $odd = 0 $root = InputBox ("Root Calculator", "What root do you wish to calculate? (2 = square root, 3 = cube root, etc.") If $root <= 0 Then Msgbox (0, "Error", "Unable to perform zero or negative root calculations.") Exit EndIf If $root = 2 Then $rootword = ("square") ElseIf $root = 3 Then $rootword = ("cube") Else $rootword = $root & "th" EndIf $num = InputBox ("Root Calculator", "Of what number do you wish to calculate the root?") If $num < 0 Then $odd = _MathCheckDiv ($root, 2) EndIf If $odd <> 2 and $num >= 0 Then MsgBox (0, "The " & $rootword & " root of " & $num & " is:", $num ^ (1/$root)) Elseif $odd <> 2 and $num < 0 Then $num = stringtrimleft ($num , 1) $negcube = $num^(1/3) $negcube = "-" & $negcube MsgBox (0, '' , $negcube) Else MsgBox (0, "Unable to Calculate", "One cannot calculate an even root of a negative number, silly!") EndIf Edit - forgot about 0 Edited April 26, 2012 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
jdelaney Posted April 26, 2012 Share Posted April 26, 2012 aren't those imaginary IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
jchd Posted April 26, 2012 Share Posted April 26, 2012 Imaginary? Geez, no!The OP is asking the the cube root of -27. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
iamtheky Posted April 26, 2012 Share Posted April 26, 2012 This one makes Wolfram do it for you, at least until they change the slightest thing about their returns. #include <inet.au3> #include <array.au3> $num = "-125" $source = _inetgetsource ("http://www.wolframalpha.com/input/?i=" & $num & "^%281%2F3%29") ;~ msgbox (0, '' , $source) $exp = stringregexp($source , 'scannerresult_0200_1(.*?)id="i_0200_1"' , 3) $url = stringregexp($exp[0] , 'src="(.*?)"' , 3) shellexecute($url[0]) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
jdelaney Posted April 26, 2012 Share Posted April 26, 2012 (edited) well played, only math I kept up on (used in real world) was statistics Edited April 26, 2012 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
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