Re: Can someone explain this?
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