# Dimension too large with \pgfmathsetmacro

I want to define varafterglowepsdown, varafterglowa and varafterglowb and use them in a tikz function like this:

\newcommand{\varafterglowepsdown}{0.0001}
\pgfmathsetmacro{\varafterglowb}{ln(\varafterglowepsdown) - ln(1 + \varafterglowepsdown)}
\pgfmathsetmacro{\varafterglowa}{(1 + \varafterglowepsdown) / exp(\varafterglowb)}

\begin{tikzpicture}[declare function={
% some function using varafterglowa, varafterglowb, and varafterglowepsdown
}]
% some axis definition
\end{tikzpicture}


However on the third line I get a "Dimension too large." error.

How do I properly define varafterglowa?

A complementary approach to percusse's answer would be to use the LaTeX3 FPU (this provides an IEEE 754 floating point implementation in an expandable form):

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\protected\def\fpset#1#2{\edef#1{\fpeval{#2}}}
\begin{document}

\newcommand{\varafterglowepsdown}{0.0001}
\fpset{\varafterglowb}{ln(\varafterglowepsdown) - ln(1 + \varafterglowepsdown)}
\fpset{\varafterglowa}{(1 + \varafterglowepsdown) / exp(\varafterglowb)}
\varafterglowa

\end{document}


The result here is a 'pure' number so should be usable where pgf needs a number. You can use fpeval directly in many places rather than store the result.

• What does the \cs_new_eq:NN \fpeval \fp_eval:n line do? – Robert Hegner Jan 20 '16 at 10:19
• @RobertHegner In expl3 syntax _ and : are 'letters'. To let us access the command \fp_eval:n with a 'document' name I've made a copy of it as \fpeval: \cs_new_eq:NN does that (it's more-or-less \let). – Joseph Wright Jan 20 '16 at 11:23
• @JosephWright I think it would be great if \fpeval and \fpset comes with the box with batteries included. – percusse Jan 20 '16 at 12:23
• @percusse I've thought about that, but there are issues. One of the design aims for expl3 is that as far as possible it keeps 'out of the way' in terms of existing documents. It adds no document-level syntax on at design-level only \ExplSyntax(On|Off), \ProvidesExpl... and \GetdInfo. So adding \fpeval would be 'risky'. – Joseph Wright Jan 20 '16 at 12:26
• @JosephWright Maybe a math frontend as PGF parser might be a solution say a pars3 package which offers the document level macros. – percusse Jan 20 '16 at 12:32

There is a strict limitation in TeX about how big numbers can get which is plus minus 16384. Hence anything going above or anything that comes sufficiently close to zero in the divisor causes overflows. Here exp(-9...) is practically zero for TeX and it is done.

Instead you can use more precise calculation engines written for TeX for example FP or TikZ' internal fpu library.

\begin{tikzpicture}

\pgfkeys{/pgf/fpu}% Turn on the fp engine
\newcommand{\varafterglowepsdown}{0.0001}
\pgfmathsetmacro{\varafterglowb}{ln(\varafterglowepsdown) - ln(1 + \varafterglowepsdown)}
\pgfmathsetmacro{\varafterglowa}{(1 + \varafterglowepsdown) / exp(\varafterglowb)}
\pgfkeys{/pgf/fpu=false}% Turn it off

\node{\pgfmathfloattofixed\varafterglowa\pgfmathresult};% Show the fixed number
\end{tikzpicture}


The downside with fpu is that you have to convert the results to the fixed notation from it's own float notation.