Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- catcode`@=11
- begingroup
- catcode`P=12
- catcode`T=12
- lowercase{
- defx{defrem@pt##1.##2PT{##1ifnum##2>z@.##2fi}}}
- expandafterendgroupx
- defstrip@pt{expandafterrem@ptthe}
- defsetdimenzerotofontheightanddepth#1#2{
- dimen0=fontcharhtfont`#1
- advancedimen0 by fontchardpfont`#1
- ifx#2
- else
- advancedimen0 by fontcharhtfont`#2
- advancedimen0 by fontchardpfont`#2
- fi
- }
- setdimenzerotofontheightanddepth fg
- edefdimzeroamt{strip@ptdimen0}
- Dimen0 is: thedimen0, Baselineskip is: thebaselineskip
- dimen1=baselineskip
- Unfortunately it also shows the dimzeroamount when I do the calculation: dividedimen1 by dimzeroamt
- The ratio is: strip@ptdimen1
- The real (manually calculated ratio) is $0.90947dots$
- catcode`@=12
- bye
- documentclass{article}
- usepackage{graphics}
- makeatletter
- begin{document}
- defsetdimenzerotofontheightanddepth#1#2{
- dimen8=fontcharhtfont`#1
- advancedimen8 by fontchardpfont`#1
- ifx#2
- else
- advancedimen8 by fontcharhtfont`#2
- advancedimen8 by fontchardpfont`#2
- fi
- }
- setdimenzerotofontheightanddepth fg
- Dimen8 is: thedimen8, Baselineskip is: thebaselineskip
- Gscale@divtmp baselineskip{dimen8}
- gives tmp
- The real (manually calculated ratio) is $0.90947dots$
- end{document}
- dividedimen0 by <number>
- input expl3-generic
- ExplSyntaxOn
- cs_new_protected:Nn ioiooiioio_getfactor:Nnnn
- {% #1 is a control sequence, #2 the dimension, #3 and #4 two characters
- tl_set:Nx #1
- {
- fp_eval:n
- {
- dim_to_fp:n { #2 } /
- dim_to_fp:n
- {
- fontcharhtfont`#3 + fontcharhtfont`#4
- +
- fontchardpfont`#3 + fontchardpfont`#4
- }
- }
- }
- }
- cs_set_eq:NN getfactor ioiooiioio_getfactor:Nnnn
- ExplSyntaxOff
- getfactortest{baselineskip}{f}{g}
- test
- bye
- dim_max:nn { fontcharhtfont`#3 } { fontcharhtfont`#4 }
- +
- dim_max:nn { fontchardpfont`#3 } { fontchardpfont`#4 }
- catcode`@=11
- begingroup
- catcode`P=12
- catcode`T=12
- lowercase{
- defx{defrem@pt##1.##2PT{##1ifnum##2>z@.##2fi}}}
- expandafterendgroupx
- defstrip@pt{expandafterrem@ptthe}
- defsetdimenzerotofontheightanddepth#1#2{
- dimen8=fontcharhtfont`#1
- advancedimen8 by fontchardpfont`#1
- ifx#2
- else
- advancedimen8 by fontcharhtfont`#2
- advancedimen8 by fontchardpfont`#2
- fi
- }
- setdimenzerotofontheightanddepth fg%
- edefdimzeroamt{strip@ptdimen8 %
- }%
- dimen0=baselineskip%
- count0=65536
- loop%
- ifdimdimen0<8192p@%
- dimen0=2dimen0%
- dividecount0 by 2 %
- repeat
- dividedimen8count0
- dividedimen0dimen8
- strip@ptdimen0
- %edefx{strip@ptdimen0}
- The real (manually calculated ratio) is $0.90947dots$%
- catcode`@=12
- bye
- 2^16 * numberdividend / numberdivisor
- newdimenfnheight
- fnheight=13.19443pt baselineskip=12pt
- {lccode`?=`p lccode`!=`t lowercase{gdefignorept#1?!{#1}}}
- expandafterignoreptthe
- dimexprthenumexpr 65536*numberbaselineskip/numberfnheight sprelax
- input apnum
- newdimenfnheight
- fnheight=13.19443pt baselineskip=12pt
- evaldefOUT{numberbaselineskip / numberfnheight}
- OUT % OUT=.90947485284083681234 because apFRAC=20 by default.
- bye
- makeatletter
- newcommandSetToRatio[3]{% sets #1 to be the ratio #2/#3, where #2 and #3
- % are lengths (registers or expressions).
- % The ratio #2/#3 should evaluate to less than 16384 in absolute value to
- % avoid arithmetic overflow. It will be computed as fixed point
- % number with about 4 or 5 digits after decimal mark.
- edef #1%
- {strip@ptdimexpr
- numexprdimexpr#2relax*65536/dimexpr#3relaxrelax sprelax}%
- }
- makeatother
- SetToRatio{foo}{20000sp}{1sp}
- % j'avais fait cela dans https://tex.stackexchange.com/a/328894/4686
- % Octobre 2016
- documentclass{article}
- makeatletter
- countdefddf@cnta=z@
- countdefddf@cntb=tw@
- countdefddf@cntc=4
- countdefddf@cntd=6
- % % if used with Plain TeX, un-comment this
- % % LaTeX loop or any loop allowing elserepeat:
- %
- % longdefloop #1repeat{%
- % def iterate {#1relax expandafter iterate fi}iterate
- % let iterate relax }
- % defdivdef #1#2#3{% if using plain tex
- newcommanddivdef [3]{%
- % description:
- % computes R = #2/#3 as nearest multiple of 1/65536 (ties go to even)
- % then define the macro #1 to be the decimal expansion of this up to five digits
- % after decimal mark.
- begingroup
- dimen@ #3relax % denominator
- dimen@ii #2relax % numerator
- ifdimdimen@<z@
- dimen@-dimen@
- dimen@ii-dimen@ii
- fi
- ifdimdimen@ii<z@
- defddf@sgn{-}dimen@ii-dimen@ii
- else
- letddf@sgnempty % no @empty in Plain !
- fi
- ddf@cntadimen@ % non negative denominator (we hope non zero...)
- ddf@cntbdimen@ii % non negative numerator
- divideddf@cntbddf@cnta % integer part of ratio, will be stored in ddf@cntd
- ddf@cntdddf@cntb
- multiplyddf@cntb-ddf@cnta % no overflow possible because TeX's division truncates
- advanceddf@cntbdimen@ii % now numerator in ddf@cntb is < denom
- count@z@ % will store fractional part as a multiple of sp's
- ifnumddf@cntb>z@
- ifnumddf@cnta>32768relax
- ddf@cnta65536relax
- loop
- ddf@cntcdimen@ % denominator
- advanceddf@cntc-ddf@cntb
- ifnumddf@cntb<ddf@cntc
- divideddf@cntatw@
- advanceddf@cntbddf@cntb
- else
- ifnumddf@cntb=ddf@cntc
- divideddf@cntatw@
- advancecount@ddf@cnta
- ddf@cntaz@ % abort the loop here
- else
- advancecount@ddf@cnta % not same order as in previous branch!
- divideddf@cntatw@
- ddf@cnta-ddf@cnta
- advanceddf@cntcddf@cntc
- ddf@cntbddf@cntc
- fi
- fi
- ifnumddf@cnta=z@else % signed quantity: can not do if foo>z@ ...
- repeat
- % it is possible here that count@ is 65536
- % in case of a tie at the last unit the rounding was to even!
- else
- % here denom <= 2^15=32768 (=0.5pt), hence 65536num <= 2^31 - 65536
- multiplyddf@cntb65536relax
- % extra steps to do rounding
- ddf@cntcddf@cnta
- divideddf@cntctw@
- advanceddf@cntbddf@cntc % no overflow possible
- ddf@cntcddf@cntb % need to keep copy for later branch
- divideddf@cntcddf@cnta
- count@ddf@cntc
- ifoddddf@cnta
- % odd denom, no tie possible
- else
- multiplyddf@cntcddf@cnta
- ifnumddf@cntb=ddf@cntc
- % implement "ties go to even", the rounding was "up"
- ifoddcount@advancecount@m@nefi
- fi
- fi
- % to get count@ 65536 we would need to have N/D >= 65535.5/65536
- % i.e. N/D >= 1 - 1/131072, but N/D<= 1 - 1/D, D<=32768, hence
- % despite the rounding this branch always produces count@ < 65536.
- fi
- fi
- dimen@count@ sprelax
- % (thecount@) % debug check
- expandafterdivdef@endthedimen@ #1%
- }
- begingroup
- catcode`P 12
- catcode`T 12
- lowercase{gdefdivdef@end #1.#2PT}#3{%
- advanceddf@cntd #1relax % almost always #1=0
- ifnum#2>z@
- edefx{endgroupedefnoexpand#3{ddf@sgntheddf@cntd.#2}}%
- else
- ddf@cntdddf@sgnddf@cntd
- edefx{endgroupedefnoexpand#3{theddf@cntd}}%
- fi
- x
- }endgroup
- begin{document}
- ttfamily
- divdefFOO{20000sp}{1sp}
- meaningFOO
- divdefFOO{355pt}{113pt}
- meaningFOO
- divdefFOO{1pt}{7pt}
- meaningFOO
- divdefFOO{10000pt}{7pt}
- meaningFOO
- divdefFOO{1000000000sp}{7sp}
- meaningFOO
- end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement