[metapost] Bug? uncaught overflow with "length"
Toby Thurston
thurston at eml.cc
Tue May 24 14:39:33 CEST 2022
I was investigating Peano-Gosper curves this morning, and came across some surprising behaviour with “length”. Consider this toy program
path p;
p = (0, 0) -- (0, 2) -- (1, 3) -- (1, 1) -- (21, 0) -- (21, 2) -- (21, 4) -- (0, 0);
for i=1 upto 5:
p := p & p & p & p & p & p & p;
show (i, length p);
endfor
end.
Each loop makes the path 7 times longer. Here’s my ouptput:
% This is MetaPost, version 2.02 (TeX Live 2022) (kpathsea version 6.3.4)
% (/usr/local/texlive/2022/texmf-dist/metapost/base/mpost.mp
% (/usr/local/texlive/2022/texmf-dist/metapost/base/plain.mp
% Preloading the plain mem file, version 1.005) ) (./bug.mp
% >> (1,49)
% >> (2,343)
% >> (3,2401)
% >> (4,16807)
% >> (5,-13423) )
% Transcript written on bug.log.
As you can see on the fifth iteration, I get a negative number from length instead 7**6 = 117649. Apparently the number has overflowed. Since this is much larger than 2*15-1 I am not surprised about the overflow, but I *am* surprised that there was no Arithmetic Overflow error, or anything else to alert the user than the returned length was nonsense.
Of course, it works fine if I try it with -numbersystem=double
But what is even more surprising is that I get exactly the same surprising negative number with no warning or error from Metafont:
% This is METAFONT, Version 2.71828182 (TeX Live 2022) (preloaded base=mf)
% (bug.mp
% >> (1,49)
% >> (2,343)
% >> (3,2401)
% >> (4,16807)
% >> (5,-13423) )
% Transcript written on bug.log.
Toby Thurston
More information about the metapost
mailing list.