Sign in to follow this  
Followers 0
aleph01

error when calculating root of negative number

8 posts in this topic

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 this post


Link to post
Share on other sites



#3 ·  Posted (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_number

Br,

UEZ

Edited 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by boththose

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

Share this post


Link to post
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.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
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 this post


Link to post
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 this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0