6
$\begingroup$

Bug introduced in 9.0 or earlier and persisting through 11.1


First off:

$Version
(* "11.0.0 for Microsoft Windows (64-bit) (July 28, 2016)" *)

$CharacterEncoding
(* "WindowsANSI" *)

I would like to use an unused operator to define a binary function, e.g ± or ·, as these are part of the Windows ANSI code page (i.e. Windows-1252). Doing this in a Mathematica notebook is all well and good, but if I try to do it in a source file that I feed to wolfram -script ..., the parser acts up:

a_±b_:={a+b,a-b}
Print[5±3]

SetDelayed::write: Tag Times in _ (a_±b) is Protected.

So it seems this gets parsed as two tokens a_±b (i.e. a pattern named a with head ±b, which is not a thing) and _. If I insert a space in front of the ±, everything works fine:

a_ ±b_:={a+b,a-b}
Print[5±3]

{8, 2}

I'm seeing the exact same behaviour with ·.

Is there a good reason why the first version isn't parsed correctly or is this a bug?

$\endgroup$
  • 1
    $\begingroup$ It is probably best to stick to ASCII in .m files, otherwise they will not be portable between platforms. I learned this the hard way. You could use \[PlusMinus] instead. Related: mathematica.stackexchange.com/q/144902/12 (I know that this is not an answer.) $\endgroup$ – Szabolcs May 17 '17 at 9:24
  • $\begingroup$ @Szabolcs Well, I'm not actually using this in production code but for code golf, but even if I wasn't then before I start using \[PlusMinus] I'd rather go back to defining a simple function because I find this ASCII longhand notation very unreadable. $\endgroup$ – Martin Ender May 17 '17 at 9:25
  • $\begingroup$ I would argue that this is just an encoding. If you use the front end as the editor, you see ±. But it encodes this as \[PlusMinus]. This is comparable to how UTF-8 encodes some characters on 4 bytes instead of 1. But this probably won't be bought by whoever judges your code golf. $\endgroup$ – Szabolcs May 17 '17 at 9:53
  • $\begingroup$ BTW I just tested this on a Mac, where the default is UTF8, and there is no problem at all with parsing 5±3, no spaces. What does Print@Head[5±3] output when you put it in a script? $\endgroup$ – Szabolcs May 17 '17 at 9:55
  • 1
    $\begingroup$ Works fine in Math8.0.4 on Win7x64. So, this bug was introduced in 9.0. $\endgroup$ – innaiz May 17 '17 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.