6

I get an unexpected result using the calc package. I simply try to multiply the real value '10.01' by the entire value '10000' and to store the result in a counter. But the returned value is not the expected '100100' but '100099' instead.

What have I to do to avoid some mistake with the calc package ?

Here is the very simple used code :

\documentclass{article}

\usepackage{calc}

\newcounter{r}
\begin{document}
  \setcounter{r}{10000 * \real{10.01}}
  \ther
\end{document}

Thanks.

2
  • 1
    Accuracy with TeX is not really good. There are more accurate implementation of floating point numbers.
    – egreg
    Oct 13 '14 at 23:21
  • In this case, \setcounter{r}{10000 * 1001 /100} gives the right answer, but such tricks may not always be available, depending on the inputs. Oct 14 '14 at 2:16
11

TeX uses fixed point binary arithmetic and calc uses this arithmetic that can't guarantee great accuracy.

You get a better result using implementations of floating point numbers, for example the one in expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\roundtointeger}{m}
 {
  \fp_eval:n { round ( #1, 0 ) }
 }
\ExplSyntaxOff

\newcounter{r}

\begin{document}
\setcounter{r}{\roundtointeger{10000 * 10.01}}

\ther
\end{document}

enter image description here

1
  • Thanks a lot for this help. I get now the correct result using the fp package.
    – Daryonyx
    Oct 14 '14 at 17:55
1

Thanks to the egreg answer, i found another package to fix my problem :

\documentclass{article}

\usepackage{fp}

\newcounter{r}
\begin{document}
  \FPeval{\result}{round(10000 * 10.01,0)}
  \setcounter{r}{\result}
  \ther
\end{document}

Thank you for your help.

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.