浮点数的后处理

浮点数的后处理

columnWithFloatingPointNumbers向表 添加了计算列\table。然后我尝试计算此新添加列的几何平均值。但是我收到此错误:

! Package PGF Math Error: Unknown operator `Y' or `Y1' (in '1Y1.1328e1]').

如果我使用\pgfmathprintnumberto,我会收到此错误:

! Package PGF Math Error: Could not parse input '$11.33$' as a floating point number, sorry. The unreadable part was near '$11.33$'..

以下是 MWE:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}


\begin{document}
\pgfplotstableread{table.txt}\table

\pgfplotstablecreatecol[create col/assign/.code={
\global\edef\tp{\thisrow{Sp}}
\global\edef\p{\thisrow{P}}
\pgfkeys{/pgf/fpu=true}
\pgfmathparse{\tp * 14.16 / \p}
\pgfkeys{/pgf/fpu=false}
\global\edef\entry{\pgfmathresult}
%\pgfmathprintnumberto[/pgf/number format/.cd,fixed,precision=2]{\entry}{\entryb}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{columnWithFloatingPointNumbers}\table

\pgfplotstabletypeset[columns={cA, P, Sp,columnWithFloatingPointNumbers }, columns/cA/.style={string type},columns/cB/.style={string type},columns/columnWithFloatingPointNumbers/.style={column name={FP}, column type=l, numeric type}]\table 
\end{document}

这是带有几何平均数部分的 MWE:(我需要几何平均数部分来与列一起工作columnWithFloatingPointNumbers

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}


\begin{document}
\pgfplotstableread{table.txt}\table

\pgfplotstablecreatecol[create col/assign/.code={
\global\edef\tp{\thisrow{Sp}}
\global\edef\p{\thisrow{P}}
\pgfkeys{/pgf/fpu=true}
\pgfmathparse{\tp * 14.16 / \p}
\pgfkeys{/pgf/fpu=false}
\global\edef\entry{\pgfmathresult}
%\pgfmathprintnumberto[/pgf/number format/.cd,fixed,precision=2]{\entry}{\entryb}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{columnWithFloatingPointNumbers}\table

\pgfplotstabletypeset[columns={cA, P, Sp,columnWithFloatingPointNumbers }, columns/cA/.style={string type},columns/cB/.style={string type},columns/columnWithFloatingPointNumbers/.style={column name={FP}, column type=l, numeric type}]\table 

\foreach \col in {P} {% I want geomean for the column  columnWithFloatingPointNumbers
    \pgfmathsetmacro\sum{0}%
    \foreach \x in {1,2,...,6} {%           
        \pgfmathtruncatemacro\ir{\x-1}  % get number without .0
        \pgfplotstablegetelem{\ir}{\col}\of\table%
        \pgfmathsetmacro{\val}{\pgfplotsretval}%
            \pgfkeys{/pgf/fpu=true}%
                \pgfmathparse{\sum + (ln \val)}%
            \pgfkeys{/pgf/fpu=false}%
            \global\let\sum\pgfmathresult%
    }%
    \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{\sum /  6} %
    \pgfkeys{/pgf/fpu=false}%
    \global\let\sum\pgfmathresult%
    \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{exp \sum}%
    \pgfkeys{/pgf/fpu=false}%
    \global\let\sum\pgfmathresult%
    \pgfmathprintnumberto{\sum}{\fsum}%
    Geomean: \fsum
}%
\end{document}

答案1

问题在于生成最终列的方式。使用creat col/expr更简单,使用浮点库,并生成正确类型的结果。

示例输出

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}

\begin{document}
\pgfplotstableread{table.txt}\mytable

\pgfplotstableset{
  create on use/columnWithFloatingPointNumbers/.style={
    create col/expr={\thisrow{Sp}*14.16/\thisrow{P}}}
}

\pgfplotstabletypeset[columns={cA,P,Sp,columnWithFloatingPointNumbers},
  columns/cA/.style={string type},
  columns/cB/.style={string type},
  columns/columnWithFloatingPointNumbers/.style={
  column name={FP}, column type=l, numeric type}]\mytable 

\foreach \col in {P,Sp,columnWithFloatingPointNumbers} {
  \pgfmathsetmacro\mysum{0}%
  \pgfplotstableforeachcolumnelement{\col}\of\mytable\as\val{%
     \pgfkeys{/pgf/fpu=true}%
         \pgfmathparse{\mysum + (ln \val)}%
     \pgfkeys{/pgf/fpu=false}%
     \global\let\mysum\pgfmathresult%
  }%
  \pgfplotstablegetrowsof\mytable
  \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{exp(\mysum / \pgfplotsretval)}%
  \pgfkeys{/pgf/fpu=false}%
  \global\let\mysum\pgfmathresult%
  \pgfmathprintnumberto{\mysum}{\fsum}%
  Geomean of \col: \fsum\par
}%

\end{document}

在上面的代码中,我还试图避免重复使用标准乳胶命令的名称,使用pgfplotstable命令来运行列的元素,并缩短了数学计算。

相关内容