From 346563a3ccfb00855edf16d04882dd8f136f16ed Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sat, 28 Sep 2024 15:53:54 +0700 Subject: [PATCH] refactor: simplify divide and reciprocal function --- .../pgf/math/pgfmathfunctions.basic.code.tex | 191 +----------------- 1 file changed, 11 insertions(+), 180 deletions(-) diff --git a/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex b/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex index 1607c386e..ee34faada 100644 --- a/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex +++ b/tex/generic/pgf/math/pgfmathfunctions.basic.code.tex @@ -61,192 +61,23 @@ % divide function. % -\newif\ifpgfmath@divide@period - \pgfmathdeclarefunction{divide}{2}{% - \begingroup% - \pgfmath@x=#1pt\relax% - \pgfmath@y=#2pt\relax% - \let\pgfmath@sign=\pgfmath@empty% - \ifdim0pt=\pgfmath@y% - \pgfmath@error{You've asked me to divide `#1' by `#2', % - but I cannot divide any number by `#2'}{}% - \fi% - \afterassignment\pgfmath@xa% - \c@pgfmath@counta\the\pgfmath@y\relax% - \ifdim0pt=\pgfmath@xa% - \divide\pgfmath@x by\c@pgfmath@counta% - \else% - \ifdim0pt>\pgfmath@x% - \def\pgfmath@sign{-}% - \pgfmath@x=-\pgfmath@x% - \fi% - \ifdim0pt>\pgfmath@y% - \expandafter\def\expandafter\pgfmath@sign\expandafter{\pgfmath@sign-}% - \pgfmath@y=-\pgfmath@y% - \fi% - \ifdim1pt>\pgfmath@y% - \pgfmathreciprocal@{\pgfmath@tonumber{\pgfmath@y}}% - \pgfmath@x=\pgfmath@sign\pgfmathresult\pgfmath@x% - \else% - \def\pgfmathresult{0}% - \pgfmath@divide@periodtrue% - \c@pgfmath@counta=0\relax% - \pgfmathdivide@@% - \pgfmath@x=\pgfmath@sign\pgfmathresult pt\relax% - \fi% - \fi% - \pgfmath@returnone\pgfmath@x% - \endgroup% -} - -\def\pgfmath@small@number{0.00002} - -\def\pgfmathdivide@@{% - \let\pgfmath@next=\relax% - \ifdim\pgfmath@small@number pt<\pgfmath@x% - \ifdim\pgfmath@small@number pt<\pgfmath@y% - \ifdim\pgfmath@y>\pgfmath@x% - \ifpgfmath@divide@period% - \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult.}% - \pgfmath@divide@periodfalse% - \fi% - \pgfmathdivide@dimenbyten\pgfmath@y% - \ifdim\pgfmath@y>\pgfmath@x% - \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult0}% - \fi% - \else% - \c@pgfmath@counta=\pgfmath@x% - \c@pgfmath@countb=\pgfmath@y% - \divide\c@pgfmath@counta by\c@pgfmath@countb% - \pgfmath@ya=\c@pgfmath@counta\pgfmath@y% - \advance\pgfmath@x by-\pgfmath@ya% - \def\pgfmath@next{% - \toks0=\expandafter{\pgfmathresult}% - \edef\pgfmathresult{\the\toks0 \the\c@pgfmath@counta}% - }% - \ifpgfmath@divide@period - \else - % we are behind the period. It may happen that the - % result is more than one digit - in that case, - % introduce special handling: - \ifnum\c@pgfmath@counta>9 % - \expandafter\pgfmathdivide@advance@last@digit\pgfmathresult CCCCC\@@ - \advance\c@pgfmath@counta by-10 % - \ifnum\c@pgfmath@counta=0 - \let\pgfmath@next=\relax - \fi - \fi - \fi - \pgfmath@next - \fi% - \let\pgfmath@next=\pgfmathdivide@@% - \fi% - \fi% - \pgfmath@next% -} - -% advances the last digit found in the number. Any missing digits are -% supposed to be filled with 'C'. -\def\pgfmathdivide@advance@last@digit#1.#2#3#4#5#6#7\@@{% - \pgfmath@ya=\pgfmathresult pt % - \if#2C% - \pgfmath@xa=1pt % - \else - \if#3C% - \pgfmath@xa=0.1pt % - \else - \if#4C% - \pgfmath@xa=0.01pt % - \else - \if#5C% - \pgfmath@xa=0.001pt % - \else - \if#6C% - \pgfmath@xa=0.0001pt % - \else - \pgfmath@xa=0.00001pt % - \fi - \fi - \fi - \fi - \fi - \advance\pgfmath@ya by\pgfmath@xa - \edef\pgfmathresult{\pgfmath@tonumber@notrailingzero\pgfmath@ya}% -}% - -{ -\catcode`\p=12 -\catcode`\t=12 -\gdef\Pgf@geT@NO@TRAILING@ZERO#1.#2pt{% - #1.% - \ifnum#2=0 \else #2\fi + \edef\pgfmathresult{\pgfmath@tonumber{\dimexpr + \numexpr + 65536*\dimexpr #1pt\relax + /\dimexpr #2pt\relax + \relax + sp\relax}}% } -} -\def\pgfmath@tonumber@notrailingzero#1{\expandafter\Pgf@geT@NO@TRAILING@ZERO\the#1} - - -\def\pgfmathdivide@dimenbyten#1{% - \edef\pgfmath@temp{\pgfmath@tonumber{#1}}% - \expandafter\pgfmathdivide@@dimenbyten\pgfmath@temp\@@#1\@@} -\def\pgfmathdivide@@dimenbyten#1.#2\@@#3\@@{% - \pgfmath@tempcnta=#1\relax% - \divide\pgfmath@tempcnta by10\relax% - \pgfmath@tempcntb=\pgfmath@tempcnta% - \multiply\pgfmath@tempcnta by-10\relax% - \advance\pgfmath@tempcnta by#1\relax% - #3=\the\pgfmath@tempcntb.\the\pgfmath@tempcnta#2pt\relax% -} - % reciprocal function. % \pgfmathdeclarefunction{reciprocal}{1}{% - \begingroup% - \expandafter\pgfmath@x#1pt\relax% - \ifdim\pgfmath@x=0pt\relax% - \pgfmath@error{You asked me to calculate `1/#1', but I cannot divide any number by zero}{}% - \fi% - \edef\pgfmath@reciprocaltemp{\pgfmath@tonumber{\pgfmath@x}}% - \expandafter\pgfmathreciprocal@@\pgfmath@reciprocaltemp0000000\pgfmath@} -\def\pgfmathreciprocal@@#1.#2#3#4#5#6#7\pgfmath@{% - \c@pgfmath@counta#2#3#4#5#6\relax% - % If the number is an integer, use TeX arithmetic. - \ifnum\c@pgfmath@counta=0\relax% - \pgfmath@x1pt\relax% - \divide\pgfmath@x#1\relax% - \else% - \ifnum#1>100\relax% - \c@pgfmath@counta#1#2#3#4\relax% - \c@pgfmath@countb1000000000\relax% - \divide\c@pgfmath@countb\c@pgfmath@counta% - \c@pgfmath@counta\c@pgfmath@countb% - \divide\c@pgfmath@counta10000\relax% - \pgfmath@x\c@pgfmath@counta pt\relax% - \multiply\c@pgfmath@counta-10000\relax% - \advance\c@pgfmath@countb\c@pgfmath@counta% - \pgfmath@y\c@pgfmath@countb pt\relax% - \divide\pgfmath@y1000000\relax% - \advance\pgfmath@x\pgfmath@y% - \else% - \c@pgfmath@counta#1#2#3#4#5#6\relax% - \c@pgfmath@countb1000000000\relax% - \divide\c@pgfmath@countb\c@pgfmath@counta% - \c@pgfmath@counta\c@pgfmath@countb% - \divide\c@pgfmath@counta10000\relax% - \pgfmath@x\c@pgfmath@counta pt\relax% - \multiply\c@pgfmath@counta-10000\relax% - \advance\c@pgfmath@countb\c@pgfmath@counta% - \pgfmath@y\c@pgfmath@countb pt\relax% - \pgfmath@y.1\pgfmath@y% Yes! This way is more accurate. Go figure... - \pgfmath@y.1\pgfmath@y% - \pgfmath@y.1\pgfmath@y% - \pgfmath@y.1\pgfmath@y% - \advance\pgfmath@x\pgfmath@y% - \fi% - \fi% - \pgfmath@returnone\pgfmath@x% - \endgroup% + \edef\pgfmathresult{\pgfmath@tonumber{\dimexpr + \numexpr + 65536*65536/\dimexpr #1pt\relax + \relax + sp\relax}}% } % div function.