5

Has anyone else noticed that in Metapost the odd operator appears to fail with negative numbers?

Here's my test program (which works with both Metafont and Metapost).

  for i=-2 upto 2: show(i); show(odd i); endfor end

If I run this through Metafont I get

This is METAFONT, Version 2.7182818 (TeX Live 2014) (preloaded base=mf)
(test.mf
>> -2
>> false
>> -1
>> true
>> 0
>> false
>> 1
>> true
>> 2
>> false )
Transcript written on test.log.

but Metapost gives this:

This is MetaPost, version 1.902 (TeX Live 2014) (kpathsea version 6.2.0)
(/usr/local/texlive/2014/texmf-dist/metapost/base/mpost.mp
(/usr/local/texlive/2014/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.005) ) (./test.mf
>> -2
>> false
>> -1
>> false
>> 0
>> false
>> 1
>> true
>> 2
>> false )
Transcript written on test.log.

The value for -1 appears to be wrong. This appears to be a bug.

Edit: November 2020

This bug was fixed in Metapost revision 2037, Sept 2014, so you should upgrade if you still see it.

This is MetaPost, version 2.00 (TeX Live 2019) (kpathsea version 6.3.1) 
**oddity.mp
(/usr/local/texlive/2019/texmf-dist/metapost/base/mpost.mp
(/usr/local/texlive/2019/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.005) ) (./oddity.mp
>> -2
>> false
>> -1
>> true
>> 0
>> false
>> 1
>> true
>> 2
>> false )
5
  • It's not a new feature: I tested with Metapost from TeX Live 2012 and the result is the same.
    – egreg
    Sep 1, 2014 at 13:57
  • odd in metapost returns false for all negative integers. Sep 1, 2014 at 13:59
  • @AndrewSwann That's what happens; but where is it written? On page 85 of the most recent manual (table 10) we read “Is the closest integer odd or even?” and no mention of negative integer is made.
    – egreg
    Sep 1, 2014 at 14:09
  • @egreg I don't find a description anywhere, the comment was just based on the behaviour. As far as I can see in the sources odd is ((x) & 1), which I would have expected to give correct answers for negative x Sep 1, 2014 at 14:18
  • I've sent a report to the mailing list metapost@tug.org.
    – Thruston
    Sep 1, 2014 at 14:26

1 Answer 1

4

As a work around, you can write (i mod 2)=1 instead of odd i. This behaves as expected for both positive and negative integers but not quite for decimals. To get the advertised behaviour of the odd primitive you need: (round i mod 2)=1.

1
  • 2
    Or odd abs(i)
    – egreg
    Sep 1, 2014 at 15:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.