# How to print number with predefined exponent base (pgf)?

How can I print a number with some predefined base of power of 10 (exponent base) using \pgfmathprintnumber

\documentclass[border=1cm]{standalone}
\usepackage{pgf}
\usepgflibrary{fpu}

\begin{document}
\pgfset{fpu=true}
\pgfmathsetmacro{\en}{1/sqrt(2e-3*2e-6)/(2*pi)}
\pgfset{fpu=false}
\pgfmathprintnumber[]{\en} % need 25 * 10^2
\end{document}


• pgf, even with the fpu library, is poor in computations: the number, with bc turns out to be 2516.46060522435201602583; with xfp I get 2516.460605224352, which is much more accurate than 2516.3 Dec 5 '19 at 21:20
• @egreg Thank you. Сan you, please, put the answer here with solution with xpf? Dec 5 '19 at 21:28
• Skillmon already did it. Dec 5 '19 at 22:15
• @egreg The PGF fpu is just completely false advertising, because it doesn't actually implement an FPU. Instead it just rescales all operands to avoid Dimension too large. Dec 6 '19 at 0:50

This does something like that. It allows you to set a fixed exponent. I did not dare to touch the \pgfmathprintnumber macro, so I wrote a macro \pgfmathprintnumberFE.

\documentclass[border=1cm]{standalone}
\usepackage{pgf}
\usepgflibrary{fpu}
\pgfkeys{/pgf/number format/.cd,custom exponent/.initial=2}%
\newcommand{\pgfmathprintnumberFE}[2][]{%
\begingroup
\pgfkeys{/pgf/number format/.cd,fixed,precision=0,#1}%
\pgfset{fpu=true}%
\pgfmathparse{#2}%
\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}%
\pgfset{fpu=false}%
\pgfmathtruncatemacro{\redexp}{\E-\pgfkeysvalueof{/pgf/number format/custom exponent}}%
\ifnum\pgfkeysvalueof{/pgf/number format/custom exponent}=0
\ensuremath{\pgfmathprintnumber{\M}}%
\else
\pgfmathsetmacro{\newnum}{\M*pow(10,\redexp)}%
\ensuremath{\pgfmathprintnumber{\newnum}\cdot10^{\pgfkeysvalueof{/pgf/number format/custom exponent}}}%
\fi
\endgroup}
\begin{document}
\pgfmathprintnumberFE[custom exponent=2]{1/sqrt(2e-3*2e-6)/(2*pi)}

\pgfmathprintnumberFE[custom exponent=1]{1/sqrt(2e-3*2e-6)/(2*pi)}

\pgfmathprintnumberFE[custom exponent=4]{123456}
\end{document}


• @Thank you. Interesting, why this possibility not implemented in in regular funds of \pgfmathprintnumber? Dec 5 '19 at 19:46
• @sergiokapone I cannot exclude that it some is included in some way, but I did not see an obvious way. The only thing I know that comes close is @sci exponent mark (see p. 1043 of pgfmanual v3.1.4) but it has a warning attached that it is not to be used to change the exponent.
– user194703
Dec 5 '19 at 19:57

With the help of siunitx. We have to convert the fpu number to scientific notation (the output of pgf's fpu is a bit strange for other packages).

\documentclass[border=1cm]{standalone}
\usepackage{pgf}
\usepgflibrary{fpu}
\usepackage{siunitx}

\newcommand\fixedexponent[2]
{%
\num
[%
fixed-exponent=#1,
scientific-notation=fixed,
round-mode=places,
round-precision=0,
]{#2}%
}

\begin{document}
\pgfset{fpu=true}
\pgfmathsetmacro{\en}{1/sqrt(2e-3*2e-6)/(2*pi)}
\pgfset{fpu=false}
\pgfmathprintnumber[]{\en} % need 25 * 10^2
% convert the number to scientific notation
\pgfset{fpu=true,fpu/output format=sci}
\pgfmathsetmacro\en{\en}
\pgfset{fpu=false}
\fixedexponent{2}{\en}
\end{document}


You could change the product symbol before the exponent with exponent-product=\cdot in the options of \num (or siunitx).

EDIT: As suggested by egreg one could use xfp, which uses the floating point engine of LaTeX3, for the calculations.

\documentclass[border=1cm]{standalone}

\usepackage{siunitx}
\usepackage{xfp}

\newcommand\fixedexponent[2]
{%
\num
[%
fixed-exponent=#1,
scientific-notation=fixed,
round-mode=places,
round-precision=0,
]{\fpeval{#2}}%
}

\begin{document}
\fixedexponent{2}{1/sqrt(2e-3*2e-6)/(2*pi)}
\end{document}

• If you remove all PGF code and do \usepackage{xfp} instead, just \fixedexponent{2}{\fpeval{1/sqrt(2e-3*2e-6)/(2*pi)}} will do what's wanted (and the computation is actually much more accurate; test with more decimal digits to see. Dec 5 '19 at 21:22
• @egreg good point, added it as an alternative, though I directly built \fpeval into \fixedexponent. Dec 5 '19 at 21:33