# error when calculating root of negative number

## Recommended Posts

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.

##### Share on other sites

I don't think you can calculate the root of a negative number.

I'm out of here.

Monkey's are, like, natures humans.

##### Share on other sites

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_number

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

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```

Edited by boththose

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

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.

##### Share on other sites

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.
RegExp tutorial: enough to get started
PCRE 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)

##### Share on other sites

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])```

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

well played, only math I kept up on (used in real world) was statistics

Edited 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.

## Create an account

Register a new account