3

I’m trying to use an expel floating point variable (\l_@@_last_machine_row_x_fp) inside a calculation of a dimension (\l_@@_bar_width_dim). In the following code \BarA works (with a solution from this answer), while \BarB doesn’t work.

\documentclass[parskip]{scrartcl}

\usepackage{xparse}

\ExplSyntaxOn\makeatletter

% @@ = mymodule

\dim_new:N \l_@@_bar_width_dim

\dim_new:N \l_@@_x_dim
\dim_set:Nn \l_@@_x_dim { 8mm }

\fp_new:N \l_@@_last_machine_row_x_fp

\NewDocumentCommand { \BarA } { m } {
   \fp_add:Nn \l_@@_last_machine_row_x_fp { 1.5 }% arbitrary value
   \dim_set:Nn \l_@@_bar_width_dim {
      \fp_to_dim:n {
         (#1 - \l_@@_last_machine_row_x_fp)
         *
         \l_@@_x_dim
      }
   }
   \rule { \l_@@_bar_width_dim } { 1ex }
}
\NewDocumentCommand { \BarB } { m } {
   \fp_add:Nn \l_@@_last_machine_row_x_fp { 1.5 }% arbitrary value
   \dim_set:Nn \l_@@_bar_width_dim {
      (#1 - \l_@@_last_machine_row_x_fp)
      *
      \l_@@_x_dim
   }
   \rule { \l_@@_bar_width_dim } { 1ex }
}

\ExplSyntaxOff\makeatother

\begin{document}
\BarA{5}% arbitrary value

\BarB{15}% arbitrary value
\end{document}

And I wonder why I need \fp_to_dim:n in this case, since \l_@@_x_dim is already a dimension multiplied by a unit free value (#1 - \l_@@_last_machine_row_x_fp).

My example code is taken from a package im developing at the moment and obviously does’t make much sense for itself ;-)

4
  • 1
    I think \dim_set:Nn \l_@@_bar_width_dim { \fp_to_decimal:n { ... } \l_@@_x_dim } should also work (and would IMHO be a bit clearer). Anyway, you need to convert the fp from its internal representation into something usable in \dimexpr...\relax (which in the end is what evaluates the dimension expression) – cgnieder Feb 4 '16 at 11:56
  • @clemens: Thanks. \fp_to_decimal is indeed clearer, but then I must omit the *. Would you make an answer out of your comment? And you said that the fp has to be converted, then why (#1 - \fp_use:N \l_@@_last_machine_row_x_fp) doesn’t work? – Tobi Feb 4 '16 at 13:20
  • And is there an overview for all the different variable types when is need \xx_use:N \l_tmpa_xx and when a single ` \l_tmpa_xx` is enough? – Tobi Feb 4 '16 at 13:21
  • @Tobi All of the variable types have accessors: tl is an exception in that the accessor is not required. – Joseph Wright Feb 4 '16 at 13:27
3

If I try

\fp_set:Nn \l_tmpa_fp { 3.14 }
\cs_show:N \l_tmpa_fp

I get

> \l_tmpa_fp=macro:->\s__fp \__fp_chk:w 10{1}{3140}{0000}{0000}{0000};.

which shows that \l_tmpa_fp is not suitable for being used in the context of a <dimension expression>. By the way, the standard meaning of \s__fp is \relax, which is relevant as far as it shows the reason for errors when you try doing that.

The functions in the fp module are made so that expandability is ensured, but with the side effect of not simply storing a number; think to the case of the expression 0.1234e-12 that you can't simply use in the context you want.

On the other hand, \fp_to_decimal:n returns a decimal representation of the <floating point expression> given as argument, which can be used as a factor in front of a dimension expression.

I'd be wary of using \l_@@_x_dim in the context of a floating point expression, even if it produces the same result: they are different animals. There is \dim_to_fp:n for conversion.

1
  • Thanks for your explanation! Regarding “I'd be wary of using \l_@@_x_dim”: Actually I don't want to. The \fp_to_dim:n is only used because to was the first solution I found. Now I use \fp_to_decimal as suggested by clemens. – Tobi Feb 5 '16 at 12:50

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.