Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify divide and reciprocal function #1361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 11 additions & 180 deletions tex/generic/pgf/math/pgfmathfunctions.basic.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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%
\[email protected]\pgfmath@y% Yes! This way is more accurate. Go figure...
\[email protected]\pgfmath@y%
\[email protected]\pgfmath@y%
\[email protected]\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.
Expand Down
Loading