Ken*_*rey 8 .net c# complex-numbers
我惊讶地发现System.Numerics.Complex.NET中的数据类型不会产生数学上准确的结果.
Complex.Sqrt(-1) != Complex.ImaginaryOne
Run Code Online (Sandbox Code Playgroud)
而不是(0,1),我得到(6.12303176911189E-17,1),看起来很像舍入错误.
现在我意识到浮点运算有时会导致这样的结果,但通常使用整数将避免舍入误差.
为什么这个看似基本的操作会产生明显错误的结果?
Ken*_*rey 10
看看反编译的Sqrt方法.
public static Complex Sqrt(Complex value)
{
return Complex.FromPolarCoordinates(Math.Sqrt(value.Magnitude), value.Phase / 2.0);
}
Run Code Online (Sandbox Code Playgroud)
事实上,使用极坐标和弧度会导致舍入误差.value.Phase / 2.0将返回pi/2,这不是一个完全可表示的数字.当从极坐标(1,pi/2)转换时,当真实坐标接近零时,舍入误差变得可见.
| 归档时间: |
|
| 查看次数: |
648 次 |
| 最近记录: |