lualatex / luamplib

generic TeX package - including MetaPost code in LuaTeX documents
http://ctan.org/pkg/luamplib
16 stars 11 forks source link

Metapost's numbersystem implemented? #21

Closed franckpastor closed 10 years ago

franckpastor commented 10 years ago

Hello,

Up to know the last version of luamplib works perfectly with every MetaPost coding I have used with.

There is still something however I miss with luampib. Recently, the Metapost maintainers have implemented double-precision, floating-point arithmetics in its core, and made it available to the user via the "numbersystem" read-only internal variable. If one processes a Metapost file, say "hello.mp", with the instruction

mpost --numbersystem="double" hello.mp

the internal numeric representation and the computations are made with this double floating-point number system. (One can switch back to the old scaled number system, by setting the --numbersystem flag to "scaled", which is still the default one anyway.) It is something I had been waiting for a long time, and I already use it as often as possible, although it is not yet officially documented.

My question: would it be possible to use the double number system via luamplib? If I'm asking too much at the moment don't hesitate to say it. Thanks to you luamplib has already made giant steps forward!

dohyunkim commented 10 years ago

luamplib can do nothing about what luatex engine does not provide. Again I can find nothing about "numbersystem" in the luatex manual. Moreover, It's not easy for me to make an MWE by which I can figure out the difference between those numbersystem's.

  u := 3.14159265359;
  draw origin -- (u,0);

The result of mpost --numbersystem="double" differs only by 0.000003bp from that of "scaled" option. It seems to be a meaningless difference. Would you please provide an MWE?

phi-gamma commented 10 years ago

@franckpastor You’re not the first to ask this question: http://tex.stackexchange.com/a/99312

franckpastor commented 10 years ago

@ Kim Dohyun: Firstly, the default number system is hopelessly limited concerning the range of number it can handle: integers multiple of 2^(-16)=1/65536, up to 2^12=4096 for direct results and up to 2^15=32768 for intermediate computations.

For example, if one only asks for:

show 10**5;

it's already too much for MetaPost's default number system:

    ! Arithmetic overflow.
    <recently read> ;

    l.5 show 10**5;

Even when complying with the range of numbers tolerated by MetaPost, which can lead to difficult contortions, you can obtain very imprecise results. Another example, if i wish to compute 10*5(10**-4), we've already seen that MetaPot can't hande this directly. You have to enter something like:

show 10*4(10*-4)10;

and it gives

9.15527

while the right result is, of course, 10.

Similarly, .0001*10000 can't be entered directly, and show .0001*1000*10; results in 1.06812 instead of 1.

You can see that these are not exactly meaningless errors…

With numbersystem="double" you don't have this kind of problems. You can enter the desired computation directly:

show 10*5(10**-4);

and you obtain, of course, the right result (here 10).

A recent example: last week I wished to graphically represent the function y=(38-x)^3 with MetaPost for my teaching, for 0<=x<=27. Before being manually scaled by myself for graphical representations, the graph coordinates had to been computed by MetaPost, but it was impossible to handle for him with numbersystem="scaled", the numbers were too big, I would have had to enter 0.1(38-t))^3 instead.

It becomes difficult to bear if you want to represent a lot of functions like this, and the double number system is really a relief in that way. With numbersystem set to "double" I can enter this kind function directly.

franckpastor commented 10 years ago

@phi-gamma: I was not aware of this thread from tex.stackexchange, thanks. So If I understand it correctly, this bit of code

function load()
    local mpx = mplib.new {
        find_file   = finder,
        ini_version = true,
        --math_mode = 1,
        --math_mode = double,
        --math_mode = mp_math_mode_double,
    }
    mpx:execute(format("input %s ;", currentformat))
    return mlx
end

should work with recent versions of LuaTeX? As for the TeX distribution, I'm using TeX Live 2013 via MacTeX, and thus LuaTeX version beta-0.76.0-2013061817 (rev 4627).

NB: the math flag for the command-line mpost command has been replaced by the numbersystem flag. Maybe this lua load() must be accordingly adapted? (I still know nothing of Lua).

dohyunkim commented 10 years ago

@phi-gamma Thanks for the info. @franckpastor Internally still math_mode. I just committed the code implementing numbersystem option to the github repo. The default value scaled can be changed by declaring

\mplibnumbersystem{double}
franckpastor commented 10 years ago

Great! One more time, thank you very much! I'll try it as soon as it becomes available on CTAN (i don't know how to install it by myself on my own texmf directory, the relevant README instructions apparently don't work for my distribution).

dohyunkim commented 10 years ago

@franckpastor A series of commands like the following works for me.

git clone https://github.com/lualatex/luamplib.git
cd luamplib/
make unpack  # or better: luatex luamplib.dtx
cp luamplib.sty luamplib.lua /usr/local/texlive/2013/texmf-dist/tex/luatex/luamplib/
franckpastor commented 10 years ago

It works now and I know why it didn't work previously. Quite silly: The convenient developer tools were no more available on my system, and I didn't know it! They are now. (I upgraded my system some time ago and it seems that the developer tools (e.g. gcc) of my old system have been discarded in the process…)

Now the only problem I got is that the Liberation Mono fonts, necessary for the documentation file, were not installed on my system. I've fixed that and luamplib.pdf has been typeset.

\mplibnumbersystem{double} works perfectly on my test files. Cheers!