• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Minus-Hyphen, Numeric Negation and Subtraction
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Minus-Hyphen, Numeric Negation and Subtraction


  • Subject: Re: Minus-Hyphen, Numeric Negation and Subtraction
  • From: Chris Page <email@hidden>
  • Date: Thu, 04 Jun 2009 21:08:52 -0700

On Jun 4, 2009, at 4:31 PM, Doug McNutt wrote:

(I was taught, in school, that -2^2 was a negative number but that's another question.)

“-” is an ambiguous character. In mathematics, there are several different entities with the same “minus sign” appearance. Programming languages generally only have the devastatingly ambiguous ASCII/Unicode “hyphen-minus” to work with, and only enough context to distinguish between unary and binary forms, so they are necessarily limited in their ability to interpret its meaning and usually must make a choice to support a subset of the available possible meanings. i.e., programming languages are not mathematics.

Although I agree they should attempt to be consistent with mathematics when possible, despite using a lot of similar visual representations and semantics, programming languages are necessarily distinct formal systems with their own rules.

In unary form preceding numeric digits, AppleScript uses hyphen-minus as part of numeric literals, not as a numeric negation operator. So, that’s the literal “-2” squared, not the _expression_ “2^2” negated. i.e., it isn’t about “operator precedence” so much as it is that the unary hyphen-minus isn’t an operator in this case. This is true in several programming languages, if that’s any comfort. (One could argue this is merely an implementation detail, just an indirect description of the operator precedence order, but I think if you keep this model in mind it will help when reading code.)

In “-(2^2)” the hyphen-minus is the numeric negation operator, because the parenthesis explicitly indicate that it is not part of a numeric literal.

Of course, what it really comes down to is that AppleScript was designed this way and if we changed it now it would break compatibility with existing scripts.

As a more general issue: This is one more example of why it is imperative that more programming languages escape the shackles of ASCII’s long-archaic limitations and support Unicode in source. Then programming languages could, for example, distinguish between minus sign “−” for subtraction and superscript minus “⁻” for indicating negative numeric literals.

-- 
Chris Page

 The other, other AppleScript Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Minus-Hyphen, Numeric Negation and Subtraction
      • From: "Mark J. Reed" <email@hidden>
References: 
 >Re: Same code gives different answers in 2 different scripts (From: Shane Stanley <email@hidden>)
 >Re: Same code gives different answers in 2 different scripts (From: Doug McNutt <email@hidden>)

  • Prev by Date: Re: Operator vs. Command precedence
  • Next by Date: Re: “round” command rounding algorithms
  • Previous by thread: Re: Same code gives different answers in 2 different scripts
  • Next by thread: Re: Minus-Hyphen, Numeric Negation and Subtraction
  • Index(es):
    • Date
    • Thread