我输入了这个:
myModel.Series.Add(new FunctionSeries((x) => Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1, "x^2 + y^2 = 16") { Color = OxyColors.Red });
myModel.Series.Add(new FunctionSeries((x) => - Math.Sqrt(16 - Math.Pow(x, 2)), -4, 4, 0.1) { Color = OxyColors.Red });
OxyPlot吸引了它:
怎么解决?
这是因为
Math.Sqrt(16 - Math.Pow(x, 2))
为NaN
返回x = 4
,因为16 - Math.Pow(x, 2)
是通过双精度计算的。这意味着结果不等于0(在这种情况下为-3,5527136788005E-14
)。像Math.Sqrt(-3,5527136788005E-14)
这样的负平方根未定义,如MSDN所述。
你可以通过禁止负数来修复它。只需要计算出最大值,就像0一样
Math.Sqrt(Math.Max(16 - Math.Pow(x, 2), 0))