如何计算分量非常大的 2 向量的大小?
我正在使用一种算法,该算法生成的数字大于扩展类型允许的数字,这会导致运行时错误。
var
a, b, magn: Extended;
...
a := -3,6854775808e-3109;
b := 2,3020377564e+3549;
magn:= a * a + b * b; //EInvalidOp, "invalid floating point operation"
我该如何解决这个问题?
我正在将算法 SmbPitchShift Site http://www.dspdimension.com c++ 编写为 pascal。如果有人有 Pascal 语言,我会感谢
I'm working with an algorithm that generates numbers larger than the Extended
type allows, which leads to runtime errors.
var
a, b, magn: Extended;
...
a := -3,6854775808e-3109;
b := 2,3020377564e+3549;
magn:= a * a + b * b; //EInvalidOp, "invalid floating point operation"
How can I solve this problem?
I'm writing the algorithm SmbPitchShift Site http://www.dspdimension.com c + + to pascal. If someone has it in Pascal, I will thank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确实想要向量大小的平方,那么这是不可能的。这确实是溢出了。
如果您确实想要大小,那么您可以像这样避免溢出:
根据我的经验,使用低效、非标准的
Extended
类型而不是Double
通常表示你的算法有问题。总是有一种方法可以使用 Double 算术来表达算法。If you really do want the square of the magnitude of the vector then that's not possible. That really is an overflow.
If you actually want the magnitude, then you can avoid the overflow like this:
In my experience, using the inefficient, non-standard
Extended
type rather thanDouble
is usually indicative of a problem with your algorithm. Invariably there is a way to express the algorithm usingDouble
arithmetic.如果我理解正确的话,问题是异常,而不是溢出。合理的解决方案是使用基于 SSE2 的数学,它允许饱和而不是溢出。 (IOW,如果有溢出,它会更改为类型中的最高有效数字)
据我所知,Delphi 目前不支持 SSE2 代码生成或内在函数,而这些在高级语言中是可行的。
但由于 64 位 Delphi 即将推出,并且 x86_64 通常更喜欢 SSE2 而不是 FPU,因此这种情况可能会在短期内发生变化。
If I understood you correctly, the problem is the exception, rather than the overflow. The logical solution would be to use SSE2 based math which allows for saturation rather than overflow. (IOW if something overflows, it changes to the highest valid number in the type)
To my best knowledge, Delphi does not currently support SSE2 code generation or intrinsics that would make this doable in higher level language.
But since a 64-bit Delphi is imminent, and x86_64 generally favours SSE2 over FPU, that might change short term.