I' m trying to use xfp
to create some automated homework solutions. For short documents it works like a charm, but for longer one the compiling time grows exponentially. For example:
\def\GDUbeta{30} % upstream beta
\def\GDUMa{3.0} % upstream Mach number
\def\GDUP{101} % upstream pressure
\def\GDUT{295} % upstream temp
%------ Modify initial values above this line ------
\def\GDUtheta{\fpeval{atand(2*cotd(\GDUbeta)*(\GDUMa^2*sind(\GDUbeta)^2-1)/( (1.4+cosd(2*\GDUbeta))*\GDUMa^2+2 ))}} % upstream theta
\def\GDUMan{\fpeval{\GDUMa*sind(\GDUbeta)}} % upstream Ma normal
\def\GDUPratio{\fpeval{1+2*1.4/(1.4+1)*(\GDUMan^2-1)}} % upstream pressure ratio
\def\GDUTratio{\fpeval{(1+2*1.4/(1.4+1)*(\GDUMan^2-1))*((2+(1.4-1)*\GDUMan^2)/((1.4+1)*\GDUMan^2))}} % upstream pressure ratio
\def\GDDMan{\fpeval{sqrt((2+(1.4-1)*\GDUMan^2)/(2*1.4*\GDUMan^2-(1.4-1)))}}
% downstream Ma normal
\def\GDDMa{\fpeval{\GDDMan/sind(\GDUbeta-\GDUtheta)}} % downstream Ma
% Closed form solution of beta --->
\def\GDtanmu{\fpeval{tan(asin(1/\GDDMa))}} % tan Mach angle
\def\GDClosec{\fpeval{\GDtanmu^2}}
\def\GDClosea{\fpeval{((1.4-1)/2+(1.4+1)/2*\GDClosec)*tand(\GDUtheta)}}
\def\GDCloseb{\fpeval{((1.4+1)/2+(1.4+3)/2*\GDClosec)*tand(\GDUtheta)}}
\def\GDClosed{\fpeval{sqrt(4*(1-3*\GDClosea*\GDCloseb)^3/((27*\GDClosea^2*\GDClosec+9*\GDClosea*\GDCloseb-2)^2)-1)}}
\def\GDClosee{\fpeval{(\GDCloseb+9*\GDClosea*\GDClosec)/(2*(1-3*\GDClosea*\GDCloseb))}}
\def\GDClosef{\fpeval{\GDClosed*(27*\GDClosea^2*\GDClosec+9*\GDClosea*\GDCloseb-2)/(6*\GDClosea*(1-3*\GDClosea*\GDCloseb))}}
\def\GDCloseg{\fpeval{tan(1/3*atan(1/\GDClosed))}}
\def\GDDbeta{\fpeval{atand(\GDClosee-\GDClosef*\GDCloseg)}}
% <--- Closed form solution of beta
\def\GDphi{\fpeval{\GDDbeta-\GDUtheta}} % angle of reflected shock
\def\GDRMa{\fpeval{\GDDMa*sind(\GDDbeta)}} % reflect shock Mach number
\def\GDRPratio{\fpeval{1+2*1.4/(1.4+1)*(\GDRMa^2-1)}} % reflect pressure ratio
\def\GDRTratio{\fpeval{\GDRPratio*((2+(1.4-1)*\GDRMa^2)/((1.4+1)*\GDRMa^2))}} % reflect temp ratio
\def\GDRMan{\fpeval{sqrt((2+(1.4-1)*\GDRMa^2)/(2*1.4*\GDRMa^2-(1.4-1)))}} % reflect pressure ratio
\def\GDRAMa{\fpeval{\GDRMan/sind(\GDphi)}} % reflected actual Mach number
\def\GDRP{\fpeval{\GDRPratio*\GDUPratio*\GDUP}} % reflected pressure
\def\GDRT{\fpeval{\GDRTratio*\GDUTratio*\GDUT}} % reflected temp
\def\GDSPratio{\fpeval{(1+(1.4-1)/2*\GDRAMa^2)^(1.4/(1.4-1))}} % stagnation pressure ratio
\def\GDSP{\fpeval{\GDSPratio*\GDRP/1000}} % stagnation pressure
\def\GDSTratio{\fpeval{1+(1.4-1)/2*\GDUMa^2}} % stagnation temp ratio
\def\GDST{\fpeval{\GDSTratio*\GDUT}} % stagnation temp
%------ DO NOT modify calculations above this line ------
The above calculation is 100% working, the problem is every time I insert these intermediate values in the homework solutions, it's calling all macros above it recursively and making it very very slow to compile.
I know this is kind of ugly coding in LaTeX, but is there any way to expand and cache fpeval
to floating point numbers, just to avoid calling these macros for exponentially growing number of times?
\expandafter\NextStep\expanded{{\fpeval{1+1}}}
will result in\NextStep{2}
.edef
?expl3
layer you can initialise and usefp
-variables, so\ExplSyntaxOn\fp_new:N \l__guanyang_gdu_beta_fp \fp_set:Nn \l__guanyang_gdu_beta_fp {3.0}\ExplSyntaxOff
\edef
is the way to go.\edef
is the solution for my case