在以下示例中,您将看到 (0,0) 处的样本未使用0.5 倍(蓝色),平方根它是(绿色)。你怎么解释这个?
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[samples at={0,0.02,...,1.5,2}]
\addplot[green,thick]{sqrt(x)};
\addplot[blue,thick]{(x)^(0.5)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
sqrt
库中的函数(fpu
默认情况下在 PGFPlots 中处于活动状态)会检查 的情况sqrt(0)
,但pow
函数不会检查 的情况0^x
。该pow
函数使用方程#1^#2 = exp( #2 * ln(#1) )
,因此它尝试计算 0 的对数,但失败了,因此坐标被丢弃。
0^x
可以通过重新定义宏来添加检查\pgfmathfloatpow@
,如下所示:
\makeatletter
\def\pgfmathfloatpow@#1#2{%
\begingroup%
\expandafter\pgfmathfloat@decompose@tok#1\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
\ifcase\pgfmathfloat@a@S\relax
% 0 ^ #2 = 0
\pgfmathfloatcreate{0}{0.0}{0}%
\else
\expandafter\pgfmathfloat@decompose@tok#2\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
\ifcase\pgfmathfloat@a@S\relax
% #1 ^ 0 = 1
\pgfmathfloatcreate{1}{1.0}{0}%
\or
% #2 > 0
\pgfmathfloatpow@@{#1}{#2}%
\or
% #2 < 0
\pgfmathfloatpow@@{#1}{#2}%
\or
% #2 = nan
\edef\pgfmathresult{#2}%
\or
% #2 = inf
\edef\pgfmathresult{#2}%
\or
% #2 = -inf
\pgfmathfloatcreate{0}{0.0}{0}%
\fi
\fi
\pgfmath@smuggleone\pgfmathresult
\endgroup
}%
\makeatother
或许值得提交错误报告为了这。
您的例子可以像这样修复:
\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\def\pgfmathfloatpow@#1#2{%
\begingroup%
\expandafter\pgfmathfloat@decompose@tok#1\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
\ifcase\pgfmathfloat@a@S\relax
% 0 ^ #2 = 0
\pgfmathfloatcreate{0}{0.0}{0}%
\else
\expandafter\pgfmathfloat@decompose@tok#2\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
\ifcase\pgfmathfloat@a@S\relax
% #1 ^ 0 = 1
\pgfmathfloatcreate{1}{1.0}{0}%
\or
% #2 > 0
\pgfmathfloatpow@@{#1}{#2}%
\or
% #2 < 0
\pgfmathfloatpow@@{#1}{#2}%
\or
% #2 = nan
\edef\pgfmathresult{#2}%
\or
% #2 = inf
\edef\pgfmathresult{#2}%
\or
% #2 = -inf
\pgfmathfloatcreate{0}{0.0}{0}%
\fi
\fi
\pgfmath@smuggleone\pgfmathresult
\endgroup
}%
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[samples at={0,0.02,...,1.5,2}]
\addplot[green,thick]{sqrt(x)};
\addplot[blue,thick]{x^0.5};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
该问题已得到修复;升级到 PGF 3.0.0 将消除该问题。
这是使用 PGF 3.0.0 的输入文件:
PGF 变更日志中的相关行如下
2013-08-24 Christian Feuersaenger <cfeuersaenger@[xxxxx]net>
- fixed bug in fpu: 0^0 and 0^x both produced nan. Now we get
0^0=1 and 0^x = 0.