3

\FPeval returns a multiple values error when using ceil or floor, but not trunc or round.

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{fp}

\begin{document}

\newcommand{\first}{78}
\newcommand{\second}{12}
\FPeval{\result}{ceil(\first / \second, 2)}

\result

\end{document}

The error is: "FP error: Evaluation results in multiple values!." Replacing ceil with round works without errors.

2
  • I cannot answer the question, but I can confirm that the error also appears when compiled using lualatex, and that the graphicx has nothing to do with it.
    – user287367
    Commented Sep 25, 2023 at 21:51
  • It turns out I was reading the documentation for xfp and not fp...
    – nvrain
    Commented Sep 25, 2023 at 22:21

2 Answers 2

2

fp doesn't provide the ceil nor floor operator with \FPeval, only round and trunc (see the documentation). Regardless, it's more convenient to use xfp (should form part of any default LaTeX installation):

enter image description here

\documentclass{article}

% \usepackage{xfp} % Should form part of any default LaTeX installation
\usepackage{fp}

\begin{document}

\newcommand{\first}{78}%
\newcommand{\second}{12}%
\FPeval{\result}{round((\first) / (\second), 2)}

\result

\fpeval{ceil(\first / \second, 2)}

\end{document}
2
  • Is there a way to save the result of \fpeval into a variable like \FPeval{\result}{<expression>}?
    – nvrain
    Commented Sep 25, 2023 at 22:11
  • @nvrain: Yes. For example, \edef\result{\fpeval{<expression>}}.
    – Werner
    Commented Sep 25, 2023 at 22:16
0

The fp package provides neither floor nor ceiling functions. There are round and trunc (and clip that removes trailing zeros). It's quite puzzling that these functions have not been implemented, actually.

Anyway, the official syntax for round and trunc is not with a comma: round(3.14159:2) would return 3.14. But it turns out that commas can be used as well.

Implementing floor and ceiling in fp should not be that hard, but there's something better available, which has the functions.

With no package at all, provided your LaTeX is not more than a couple of years old, you can do

\fpeval{ceil(\first/\second,2)}

and get 6.5. This is fully expandable, so it can be used even in something like

\fpeval{ceil(\first/\second,2)}\mylen

where \mylen is some length parameter defined with \newlength. Thus you shouldn't need to store the result in macros.

You can do it, of course, but I'd not perpetuate the bad practice of not checking about definedness of the commands we want to use.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\definefp}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \fp_new:c { l_nvrain_fp_##1_fp }
   }
 }
\NewDocumentCommand{\setfp}{mm}
 {
  \fp_set:cn { l_nvrain_fp_#1_fp } { #2 }
 }
\NewExpandableDocumentCommand{\usefp}{m}
 {
  \fp_use:c { l_nvrain_fp_#1_fp }
 }
\ExplSyntaxOff

\definefp{raw,result}

\begin{document}

\newcommand{\first}{78}
\newcommand{\second}{11}% to make the thing more interesting

\setfp{raw}{\first/\second}
\setfp{result}{ceil(\first/\second,2)}

Raw: \usefp{raw}

Result: \usefp{result}

\end{document}

This way, you're sure that you don't clobber existing commands, which can very well happen with \FPeval that never checks about the name of the command it will store the result in.

You can employ \usefp in any place where a floating point number is expected, even in the second argument to \setfp or, in general, in the argument of \fpeval.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .