我正在将Fortran转换为C#,并且我已经使用双精度实现了我自己的Complex结构。在Fortran我有一个复杂的计算
c = a/b
a = (-6.756993318630313D-178,2.237434940548793D-178) COMPLEX(8)
b = (1.381489403189045D-175,-3.000542776128005D-175) COMPLEX(8)
c = (-1.470726933303390D-003,-1.574781630470373D-003) COMPLEX(8)
当我尝试进行此计算时,我的简单C#实现会给出NaN
。
public static Complex operator /(Complex complex1, Complex complex2)
{
var denominator = complex2.Real * complex2.Real + complex2.Imaginary * complex2.Imaginary;
return new Complex((complex1.Real * complex2.Real + complex1.Imaginary * complex2.Imaginary) / denominator,
(-complex1.Real * complex2.Imaginary + complex2.Real * complex1.Imaginary) / denominator);
}
考虑到算法,获得NaN
并不奇怪。编写英特尔Fortran编译器的人显然比我更聪明。任何人都可以提供与Fortran中使用的算法一样好的算法吗?