[Rd] precedence (was 'historical NA question')

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu May 8 09:35:50 CEST 2014


On Wed, May 7, 2014 at 11:00 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
>
>
> Is there a language where "- 2^2" gives a different answer than "-2^2"?
>   (Substitute ** or any other exponentiation operator for ^ if you
> like.)  This is important, because I'd like to avoid ever attempting any
> important calculation in that language.
>

I just checked that with javascript, and it gives the same answer so
that's okay. I tried JS because it has some fun things to do with
numbers (especially if you let it coerce strings):

 > "2" - "1"
1
 > "2" + "1"
"21"

The Javascript Best Practices Document[1] says you should never use
the plus sign for arithmetic addition, instead use a double negative
and rely on "subtract" converting to numeric:

 > "2"- -"1"
 3

Given that web pages written with JS get all their data from web
content as strings, this can happen more often than you think.

This is an example to show to everyone who says "Can we use + to
concatenate strings in R please!?".

Barry

[1] A completely fictional publication I invented just now, but hey,
I've seen worse than this.



More information about the R-devel mailing list