Fun with Exponents

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
Veggiesama
Dragon
Posts: 619
Joined: Wed Aug 29, 2007 1:18 am

Fun with Exponents

Post by Veggiesama »

I was explaining how exponents work to a young friend of mine, and I happened to have MapTool loaded at the time, so I punched this in:

Code: Select all

[-5^2]
Order of operations states you should do exponents first, so the above problem can be rewritten as -(5^2) = -(25).

MapTool returns a positive 25. Wrong!

-5^2 is not the same as (-5)^2. In other words,

Code: Select all

[if(-5^2 == (-5)^2, "TRUE", "FALSE")]
should return FALSE, not TRUE.
My D&D 4e Campaign FrameworkMy Shadowrun 4e Campaign Framework
RPGA#: 5223846427 — Skype: Veggiesama — Fear the ferret.

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Fun with Exponents

Post by aliasmask »

I think stating it as -5^2 is the same thing as -1*5^2 where the multiplication is done first. Now if you do 1-5^2 it is done as you expect where 5^2 is first then the subtraction. So, I think MT has it right.

edit: unless you're saying the exponent trumps multiplication too? After testing, it does.. so, you're right.

User avatar
Veggiesama
Dragon
Posts: 619
Joined: Wed Aug 29, 2007 1:18 am

Re: Fun with Exponents

Post by Veggiesama »

Yeah, from Wikipedia:

1. terms inside brackets
2. exponents and roots
3. multiplication and division
4. addition and subtraction

On the other hand, Wiki goes on to say:
"Unfortunately, there exist differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −3^2 is interpreted to mean −(3^2) = −9, but in some applications and programming languages, notably the application Microsoft Office Excel and the programming language bc, unary operators have a higher priority than binary operators, that is, the unary minus (negation) has higher precedence than exponentiation, so in those languages −3^2 will be interpreted as (−3)^2 = 9. In any case where there is a possibility that the notation might be misinterpreted, it is advisable to use parentheses to clarify which interpretation is intended."

I learn something new every day, I guess.
My D&D 4e Campaign FrameworkMy Shadowrun 4e Campaign Framework
RPGA#: 5223846427 — Skype: Veggiesama — Fear the ferret.

User avatar
DrVesuvius
Giant
Posts: 199
Joined: Wed Jun 18, 2008 2:07 pm

Re: Fun with Exponents

Post by DrVesuvius »

I think the confusion comes from the fact that in the expression "-5^2", the minus sign isn't really an operator, but an inherent part of the discrete number "-5".

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Fun with Exponents

Post by Azhrei »

DrVesuvius wrote:I think the confusion comes from the fact that in the expression "-5^2", the minus sign isn't really an operator, but an inherent part of the discrete number "-5".
Um, that depends.

In many languages it would be classified as a unary operator if it were followed by a variable name or other expression. If the definition of "expression" includes any integer value, then it can be a unary operator in front of an integer.

The distinction comes about because of the way an LR(1) parser works. I looked at the grammar used by the MT parser and I don't see any specification of precedence so I can't tell. It might be that the order of the items in the file specifies the precedence...

User avatar
AidyBaby
Dragon
Posts: 383
Joined: Tue Feb 07, 2006 12:55 pm
Contact:

Re: Fun with Exponents

Post by AidyBaby »

DrVesuvius wrote:I think the confusion comes from the fact that in the expression "-5^2", the minus sign isn't really an operator, but an inherent part of the discrete number "-5".
Yep, how else (simply put) would you square -5?
D&D qualities are related inversely to those of Poker... and I love both.
http://www.yorkpoker.co.uk

User avatar
Veggiesama
Dragon
Posts: 619
Joined: Wed Aug 29, 2007 1:18 am

Re: Fun with Exponents

Post by Veggiesama »

(-5)^2

^_^
My D&D 4e Campaign FrameworkMy Shadowrun 4e Campaign Framework
RPGA#: 5223846427 — Skype: Veggiesama — Fear the ferret.

User avatar
whited
Cave Troll
Posts: 45
Joined: Sun Aug 29, 2010 8:58 pm
Location: Seattle, WA

Re: Fun with Exponents

Post by whited »

Azhrei wrote:I looked at the grammar used by the MT parser and I don't see any specification of precedence so I can't tell. It might be that the order of the items in the file specifies the precedence...
I've been reading up on ANTLR lately - how the rules are nested is how the precedence is defined. The MT parser grammar reads "-5^2" as (-5)^2

That is, unary operators "-", "+" and "!" are higher precedence than "^" because they are nested deeper in the grammar rules.

Post Reply

Return to “MapTool”