关于浮点数和复数运算(+
, -
, *
, /
)的具体行为,我没有找到太多信息。对于浮点数,这相对容易。但对于复数,由于特殊的数字(无穷大、负无穷大、非数字)和舍入问题(https://medium.com/@smcallis_71148/complex-arithmetic-is-complicated-873ec0c69fc5),情况就不太一样了。
是否有可能更好地指定复数的精确行为,或者指向相关的规范?似乎IEEE-754并没有提到复数。当然,我们可以查看编译器的源代码,但我希望这些细节能在规范中包含。
背景:我正在为Go语言开发一个名为TinyGo的编译器。我想实现复数运算,但不确定应该如何实现。
4条答案
按热度按时间ldxq2e6h1#
It will help us if you write down in more detail what you think we should specify.
For example, based on the compiler source code, it appears that this is how addition is implemented, assuming
a
andb
are complex numbers:Multiplication appears to be done this way (ignoring widening/narrowing to do the multiplication in
float64
instead offloat32
forcomplex64
):The spec could list this as pseudo-code, for example. That would make an implementation trivial. Of course, the spec should then also specify whether the widening done here is required or not - because I would want to avoid it in my implementation if it is not absolutely necessary.
When I take a look at the C11 spec regarding complex numbers, the given code is quite big. It takes special care of NaN and Infinity, which the Go compiler appears to completely ignore. So perhaps this is even a bug in the Go compiler, I don't know what the intended behavior is. It certainly shows that the Go language could use some more words regarding how complex numbers ought to work (and if the spec doesn't care about NaN/Inf, that's fine to me, but it should be specified IMHO).
bxgwgixi2#
是的,我们的复数相当不精确。
复数运算有很多边缘情况。有多个无穷大(inf+0i, 0+inf*i等)和非数字(nan)。据我所知,我们没有对这些边缘情况进行归一化处理。
此外,复数的舍入问题也不明确。你需要计算像
a*b+c*d
这样的形式的浮点表达式,但在计算过程中允许多少次舍入?r9f1avp53#
It will help us if you write down in more detail what you think we should specify.
In general the complex number supports follows Annex G of the C11 programming language standard, which describes how complex numbers should work in C.
u0sqgete4#
/cc @griesemer for spec-related issues.