13

When using pgfmathsetmacro with very small of high exponents, it doesn't work. The example below exits by complaining a division by zero. Is there any way to increase the precision that this works or another package which handles this example?

\documentclass{article}

\usepackage{tikz}

\begin{document}

\pgfmathsetmacro{\a}{10e-32}
\pgfmathsetmacro{\b}{10e-30}

\pgfmathsetmacro{\c}{\a/\b}
\c
\pgfmathsetmacro{\c}{1/\b}
\c
\end{document}
student
  • 29,003

3 Answers3

15

The number range in the default math engine of pgf is quite restricted because of the limitations of TeX's numbers. Also the library for fixed point arithmetic does not help with these exponents, because this library only covers ten digits before and after the decimal point.

The example can be processed with the floating point unit library:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fpu}

\begin{document}

\pgfset{fpu}
\pgfmathsetmacro{\a}{10e-32}
\pgfmathsetmacro{\b}{10e-30}

\pgfset{fpu/output format=fixed}
\pgfmathsetmacro{\c}{\a/\b}
\c

\pgfset{fpu/output format=sci}
\pgfmathsetmacro{\c}{1/\b}
\c
\end{document}

Result

Heiko Oberdiek
  • 271,626
12

Use a better floating point management framework, the one in expl3.

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}

\fpeval{10e-32/10e-30}

\end{document}

Note that, differently from pgf, the floating point macros are expandable.

Thus, you get the same output from

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\newcommand{\varA}{10e-32}
\newcommand{\varB}{10e-30}

\begin{document}

\fpeval{\varA/\varB}

\end{document}

enter image description here

Consult texdoc interface3 for more information about the supported functions and the syntax.

egreg
  • 1,121,712
6

Use lualatex:

\documentclass{article}
\begin{document}    
\directlua{tex.print(10^(-32)/10^(-30))}

\directlua{tex.print(1/10^(-30))}
\end{document}

enter image description here