13

Why does \pgfmathresult display the result of 3x4 as 12.0?

\documentclass{article}

\usepackage{pgf}

\begin{document}

\pgfmathparse{3*4}
\pgfmathresult

\end{document}

This code yields a document whose sole contents is "12.0".

In some parts of the document I'm working on, I'll need decimal places, but not for grade 2 math.

How do I fix this?

5
  • 1
    \pgfmathparse{int(3*4)}. for more options, how to use pgfmath, see TikZ manual, part IV: Mathematical and Object-Oriented Engines.
    – Zarko
    Sep 12, 2016 at 23:53
  • Where in that section is the part on decimal places? Sep 13, 2016 at 0:33
  • 1
    In TikZ & PGF manual. Recent version is 3.0.1a. It is part of your package installation as file pgfmanual or you can find it on ctan.ijs.si/tex-archive/graphics/pgf/base/doc/pgfmanual.pdf
    – Zarko
    Sep 13, 2016 at 7:49
  • I still can't find it. Maybe I'm on the right pages, but see no relation to the number of decimal places. Would you mind letting me know the specific page numbers of the PDF I should be reading? Sep 15, 2016 at 19:45
  • part IV: Mathematical and Object-Oriented Engines, pp. 921. math function are sumarised on pp. 928, in section 89.3 Syntax for Mathematical Expressions: Functions
    – Zarko
    Sep 15, 2016 at 21:01

2 Answers 2

15

First, you are not formatting the number when you say \pgfmathresult. Second, you have not specified any particular format for number printing. (Although the default format will do, in this case.)

Compare:

\documentclass{article}
\usepackage{pgf}
\begin{document}

\pgfmathparse{3*4}
\pgfmathresult

\pgfmathprintnumber\pgfmathresult

\pgfmathparse{int(3*4)}
\pgfmathresult

\pgfkeys{% just for example - doesn't actually matter here
  /pgf/number format/int detect,
}

\pgfmathparse{3*4}
\pgfmathprintnumber\pgfmathresult


\end{document}

comparison

PGF doesn't know you are typesetting Grade 2 maths rather than a doctoral dissertation in nuclear engineering!

7
  • Thanks! I used the \pgfmathparse{int(3*4)} option. I'll be starting a new thread for what I believe is a more complicated but related issue now. :) Sep 13, 2016 at 0:47
  • 1
    Not to quibble, but the integer representation "12" is far more likely to occur in a doctoral dissertation on number theory than the decimal/floating point representation "12.0". The latter is more likely in scholarly work dealing with applied math.
    – Deepak
    Sep 13, 2016 at 10:11
  • @Deepak True. If such a dissertation includes any actual numbers at all, of course.
    – cfr
    Sep 13, 2016 at 10:12
  • @cfr Also very true - lots of very high level math has practically no numbers in it! :) Although numbers would likely feature in a number theory paper, at least as coefficients. But papers on algebraic topology and the like will be dense in symbols, with numbers mainly featuring in the Lemmas and the references.
    – Deepak
    Sep 13, 2016 at 10:13
  • @Deepak Will nuclear engineering do?
    – cfr
    Sep 13, 2016 at 10:14
3

The standard representation returned by PGF is with a decimal part, even if it is .0, due to how the internal work (it's TeX that adds .0 when requested to print the value of a \dimen register and PGF just strips the trailing pt), unless you declare the operation to return an integer.

An alternative is using expl3, whose floating point module adds a decimal part only if nonzero:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n % get a user level version
\ExplSyntaxOff

\begin{document}

\fpeval{3*4}

\end{document}

Note you don't even need the double step of \pgfmathresult. The accuracy of the floating point module is according to IEEE standards, contrary to what PGF provides.

Try the following with PGF and you'll know what road to take.

\documentclass{article}
\usepackage{expl3,siunitx}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n % get a user level version
\ExplSyntaxOff

\begin{document}

\fpeval{333*444}

\num[group-separator={\,}]{\fpeval{333*444}}

\num[group-separator={,}]{\fpeval{333*444}}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.