Jump to content

Negation of a number to the power


Recommended Posts

Calculations of the form -x^y take place in AutoIt contrary to the mathematical standard (https://en.wikipedia.org/wiki/Order_of_operations#Unary_minus_sign). According to this standard, the power calculation is first, followed by the negation. This is what happens in the computer algebra system Maxima, as well as in Javascript.

AutoIt handles this the other way round, first the negation, then the power calculation. The list 'operator precedence' ('From highest precedence to lowest') reflects this misleadingly by placing the '^' sign above the '-' sign, thus suggesting a precedence of power calculation over negation.

It is not acceptable to have to convert expressions of the form -x^y in mathematically correct formulas to -(x^y) just because AutoIt does not adhere to the mathematical standard.

Edited by Konrad-Franz
Link to comment
Share on other sites

Based on your own link :

Unary minus sign
There are differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −32 is interpreted to mean −(32) =9.[1][19]

In some applications and programming languages, notably Microsoft Excel, PlanMaker (and other spreadsheet applications) and the programming language bc, unary operators have a higher priority than binary operators, that is, the unary minus has higher precedence than exponentiation, so in those languages −32 will be interpreted as (3)2 = 9.[20] This does not apply to the binary minus operator −; for example in Microsoft Excel while the formulas =−2^2, =-(2)^2 and =0+−2^2 return 4, the formula =0−2^2 and =−(2^2) return −4.

There are different conventions.  If we change it to your preference, someone else will complain that it does not follow Excel and other languages.

It is a "dam if you do, dam if you don't" dilemma. 

Link to comment
Share on other sites

  • Moderators

Konrad-Franz,

Welcome to the AutoIt forum.

As a programmer of some 50+ years experience I would never rely on operator precedence in any mathematical expression and would always use parentheses to make the order explicit. While this may make the original formulation of the expression slightly more onerous, the advantages of not having to rely on the arbitrary operator precedence (amply shown in the post above) of the various programming languages that might be used to evaluate it far outweigh this minor inconvenience.

M23

Edited by Melba23
Older than I thought!

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Konrad-Franz there are indeed differing conventions among applications and programming languages.

For instance Wolfram Mathematica also handles -2^3 as (-2)^3 = -8

Would you go as far as say that Mathematica isn't mathematically sound and call Stephen Wolfram an impostor?

Edited by jchd
Thinkos!

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)

Link to comment
Share on other sites

Hello Nine. If you calculate like AutoIt -3^2 = 9 and 3^2 = 9 then the minus sign is obviously meaningless. Do you consider this logical? If 'spreadsheet applications' and 'other languages' (which? relevant?) ignore the mathematical (and logical!) standard, they should change their behaviour and not AutoIt also take this erroneous path.

Melba23, hello to you too. If you would get from a mathematical programme like Maxima endless formulas which you have to transfer to AutoIt, you would not consider to add hundreds of (mostly unnecessary) parantheses. No, you would long for a consistent standard to which AutoIt adheres too.

 

Link to comment
Share on other sites

I said "Wolfram Mathematica", not "Wolfram Alpha".

Edited by jchd

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)

Link to comment
Share on other sites

  • Moderators

Konrad-Franz,

I would argue that the author of Maxima is at fault for not offering the ability to produce precedence-explicit expressions given the known differences between the various platforms on which they might be used. 

But then I would say that, wouldn't I!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The question made me wonder and look twice since the discrepancy was blatant: if fact I'm by default using an overloaded version of Minus I need for special purposes.

Living my specific environment, indeed:
FullForm[-a^b]
Times[-1,Power[a,b]]

Sorry for noise.

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)

Link to comment
Share on other sites

OK jchd. And what is 'Wolfram Mathematica" saying to -3^2? The same same like AutoIt (9) or the mathematical solution (-9).

By the way: Your example -2^3 is not helpful, because in case of an exponentiation with a negative base and an odd exponent the result will be negative regardless of whether you write the expression (-2)^3 = -8 or -(2^3) = -8. However, it becomes problematic with even exponents.

Edited by Konrad-Franz
Link to comment
Share on other sites

Yes I answered a bit hastily! Yet I prefer typing defensively with potentially ambiguous constructs.

At least in Mma, we have the possibility to decide precedence, associativity, distributivity, ... of every user function.

Edited by jchd

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...