jfbu / texdimens

Utilities and documentation related to TeX dimensional units
1 stars 0 forks source link

\texdimenwithunit{dim1}{dim2} could and even should output a ratio R closer to mathematical dim1/dim2 #16

Closed jfbu closed 2 years ago

jfbu commented 2 years ago

The PR #15 is about a \texdimendivide{dim1}{dim2} doing an approximate computation of the mathematical dim1/dim2, to the extent possible in (e)TeX base handling of input and output of dimensions. Indeed current (0.99) \texdimenwithunit{dim1}{dim2} gives an ouput R is guaranteed to let R<dim2> be parsed by TeX into something close to dim1, but the way it does this lets this R diverge somewhat from the mathematical dim1/dim2, and very noticeably for small dimensions.

Indeed as TeX truncates when it multiplies, this R, whose specification is that R<dim2> should be near <dim1>, will by force usually be larger than the mathematical dim1/dim2 ratio.

In the current implementation the divergence is very noticeable for small dimensions. For example:

*\message{\texdimenwithunit{2sp}{3sp}}
0.83333
*\message{\number\dimexpr0.83333\dimexpr3sp}
2

At first one is comforted by the fact that 0.66666 indeed would not work:

*\message{\number\dimexpr0.66666\dimexpr3sp}
1

but the naive expected value 0.66667 does work:

*\message{\number\dimexpr0.66667\dimexpr3sp}
2

This is indication that perhaps the handling of \texdimenwithdivide{dim1}{dim2} is sub-optimal, particularly in the dim2<1pt branch. The chosen formula does work but isn't it a bit too secure?

As explained in https://github.com/jfbu/texdimens/issues/2#issuecomment-957921157:

the condition on N is that it should be at least ceil(U * psi) and at most ceil((U+1) * psi) -1.

No wonder then that round(U * psi) will not always work: if it rounds strictly down, we are doomed.

What about the M = round((U+0.5)*psi)̀ approach, will it work? (psi = 1/phi > 1).

and we use currently the round((U+0.5)*psi)̀ formula in this dim2<1pt case. But the closest to round(U * psi) (which is best we can do to approximate the mathematical dim1/dim2 is ceil(U*psi).

I propose \texdimenwithunit{dim1}{dim2} should implement, for dim2<1pt, the ceil(U*psi) formula so as to reduce the divergence from exact mathematical ratio dim1/dim2. This will make #15 unneeded.