Graphics, Figures & Tablespgf | Simplify FPU Functions Code

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

pgf | Simplify FPU Functions Code

Post by usr345 »

I have 2 values:
  • a = 1414.165
  • b = 15888.9
and need to calculate the formula: a / (b * 12)

I need double precision and 7 digits after the dot, so I used pgf floating point functions. Here is the code:

Code: Select all

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{fpu}
%a
\def\a{1414.165}
\pgfmathfloatparsenumber{\a}
\let\ad\pgfmathresult
%b
\def\b{15888.9}
\pgfmathfloatparsenumber{\b}
\let\bd\pgfmathresult
%12
\def\months{12}
\pgfmathfloatparsenumber{\months}
\let\months\pgfmathresult
\begin{document}
\pgfmathfloatdivide{\ad}{\bd}
\pgfmathfloatdivide{\pgfmathresult}{\months}
\pgfmathfloattofixed{\pgfmathresult}
\let\result\pgfmathresult
$\frac{\a}{\b \cdot 12} = \result$
\end{document}
The code is very complex.
  1. How to avoid defining float representations of all the variables.
  2. How to use 12 as a static variable instead of setting it to the variable and converting to float?
  3. How to avoid calling \pgfmathfloatdivide twice and use a nice formula to calculate the result?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

pgf | Simplify FPU Functions Code

Post by usr345 »

ok, here is the solution:

Code: Select all

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{fp}
%a, b
\def\a{1414.165}
\def\b{15888.9}
\begin{document}
\FPeval\result{\a / (\b * 12)}%
\FPround{\result}{\result}{7}
$\frac{\a}{\b \cdot 12} = \result$
\end{document}
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

pgf | Simplify FPU Functions Code

Post by josephwright »

Using an up-to-date LaTeX3 programming layer (l3kernel), this is easy

Code: Select all

\documentclass[12pt]{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpcalc \fp_to_decimal:n
\ExplSyntaxOff
%a
\def\a{1414.165}
%b
\def\b{15888.9}
%12
\def\months{12}
\begin{document}
$\frac{\a}{\b \cdot \months} = \fpcalc{round( \a/(\b * \months) , 7 )}$
\end{document}
I've rounded to 7 decimal places as requested.

If you want to hard code '12', it is also easy

Code: Select all

\documentclass[12pt]{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpcalc \fp_to_decimal:n
\ExplSyntaxOff
%a
\def\a{1414.165}
%b
\def\b{15888.9}
\begin{document}
$\frac{\a}{\b \cdot 12} = \fpcalc{round( \a/(\b * 12) , 7 )}$
\end{document}
Joseph Wright
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

pgf | Simplify FPU Functions Code

Post by Stefan Kottwitz »

By the way, to understand what these lines mean:

Code: Select all

\ExplSyntaxOn
\cs_new_eq:NN \fpcalc \fp_to_decimal:n
\ExplSyntaxOff
have a look at Joseph's article: Programming LaTeX3: Category codes, tokens and token lists.

Stefan
LaTeX.org admin
Post Reply