• 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: Can someone explain this?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can someone explain this?


  • Subject: Re: Can someone explain this?
  • From: "Mark J. Reed" <email@hidden>
  • Date: Sat, 14 Mar 2009 14:22:57 -0400

> AppleScript returns (-9) when evaluating (-3^2) and has caused calculation errors in my work. > Excel does the same thing while perl and good old FORTRAN get it the way I learned in
> algebra class a hundred or so years ago.

I'm confused, because you seem to have that backwards.  At least
Applescript 2 returns +9, as does Excel, while FORTRAN and Perl return
-9.

Most langs seem to follow FORTRAN and interpret -x^y as -(x^y) rather
than (-x)^y.  Notable exceptions besides AppleScript and Excel are
bash, bc (the UNIX "basic calculator"), REXX, and TCL (whose
exponentiation operator is new as of 8.5; obviously if you're using
the pow() function instead, there's no ambiguity).

$ awk 'BEGIN {print -3**2}'
-9

$ echo $(( -3**2 ))  # bash
9

$ echo '?-3^2' | basic
Chipmunk BASIC v3.6.4(b7)
>-9

>$ echo '-3^2' | bc
9

$ cat pow.f
        PROGRAM Foo
        WRITE(*,*) -3**2
        END
$ g77 pow.f
$ ./a.out
 -9

$ cat pow.f
$ g77 pow.f
$ ./a.out

$ lua -e 'print(-3^2)'
-9

$ perl -le 'print -3**2'
-9

$ python -c 'print -3**2'
-9

$ echo 'say -3**2' | rexx
9

$ ruby -e 'puts -3**2'
-9

$ echo 'puts [expr -3**2]' | tclsh   # requires TCL 8.5, no **
operator in <= 8.4
9


--
Mark J. Reed <email@hidden>
 _______________________________________________
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: Can someone explain this?
      • From: Philip Aker <email@hidden>
References: 
 >Can someone explain this? (From: Michelle Steiner <email@hidden>)
 >Re: Can someone explain this? (From: Philip Aker <email@hidden>)
 >Re: Can someone explain this? (From: Deivy Marck Petrescu <email@hidden>)
 >Re: Can someone explain this? (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: Can someone explain this?
  • Next by Date: Re: Can someone explain this?
  • Previous by thread: Re: Can someone explain this?
  • Next by thread: Re: Can someone explain this?
  • Index(es):
    • Date
    • Thread