【发布时间】:2011-10-29 03:28:54
【问题描述】:
我想测试一些代码以确保它能够正确处理NAN、INF 和-INF 输入。
我知道there exists functions that returnNAN、INF和-INF,但作为Double:
unit IEEE754;
...
function NAN: Double;
function PositiveInfinity: Double;
function NegativeInfinity: Double;
除了在我的情况下,我需要测试 Currency 何时是这三个边缘情况值之一。不幸的是,您无法将其中任何一个转换为Double:
Test(NAN);
procedure Test(const Value: Currency);
...
将Double NAN 转换为Currency 时出现EInvalidOp 无效浮点运算异常。
可能将NAN分配给Currency吗?
也许,将NAN 分配给Currency 不是可能,而是不可能 - 我可以忽略这种极端情况.
我可以忽略这种极端情况吗?
是否可以“将货币值设置为 NAN、INF 或 -INF?”
{ David Heffernan says it's impossible for a currency to contain INF,-INF or NAN.
So there's no need to test for it.
http://stackoverflow.com/questions/7096966/set-a-currency-value-to-nan-inf-or-inf
//Handle NaN, where exponent is -32767
test(NAN, 'NAN');
//Handle +inf, where exponent is 32767 and Negative is true
test(PositiveInfinity, 'INF');
//Handle -inf, where expondent is 32767 and Negative is true
test(NegativeInfinity, '-INF');
}
【问题讨论】:
-
你添加的代码到底应该是什么?货币没有指数。
-
您正在查看一段 sn-p 代码,却没有意识到它在测试什么。您可能还记得,
Exponent是 Delphi 的FloatToDecimal返回的“指数”,它将货币转换为Digits和Exponent。 (stackoverflow.com/questions/7069204/…) -
确实如此。我正在看一段代码,除非你能理解。所以我想知道为什么它被发布。我知道 FloatToDecimal 的作用,它不是在货币参数的情况下你期望它做的事情。我猜你的代码应该是某种证明,但由于没有人可以遵循它,它不能很好地达到这个目的。
-
它诞生于公认答案的 cmets。
-
但它完全没用,因为除了你之外没有人可以从中检索到任何意义。请注意,
NAN、NegativeInfinity和PositiveInfinity是双倍,而不是货币。
标签: delphi nan delphi-5 infinity