2

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.

  • It's not a new feature: I tested with Metapost from TeX Live 2012 and the result is the same. – egreg Sep 1 '14 at 13:57
  • odd in metapost returns false for all negative integers. – Andrew Swann Sep 1 '14 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 '14 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 – Andrew Swann Sep 1 '14 at 14:18
  • I've sent a report to the mailing list metapost@tug.org. – Thruston Sep 1 '14 at 14:26
2

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.

| improve this answer | |
  • 1
    Or odd abs(i) – egreg Sep 1 '14 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.