将货币值设置为 NAN、INF 或 -INF?
我想测试一些代码以确保它正确处理 NAN
、INF
和 -INF
输入。
我知道存在返回的函数 NAN、
INF
和 -INF
,但作为Double
:
unit IEEE754;
...
function NAN: Double;
function PositiveInfinity: Double;
function NegativeInfinity: Double;
除了我的情况外,我需要测试 Currency
何时是这三个边缘情况值之一。不幸的是,您无法将其中任何一个转换为 Double
:
Test(NAN);
procedure Test(const Value: Currency);
...
转换 Double
时出现 EInvalidOp
无效浮点运算 异常> 将NAN
转换为货币
。
是否可以将NAN
分配给货币
?
也许,将 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');
}
i want to test some code to make sure it handles NAN
, INF
and -INF
inputs properly.
i know there exists functions that return NAN
, INF
and -INF
, but as a Double
:
unit IEEE754;
...
function NAN: Double;
function PositiveInfinity: Double;
function NegativeInfinity: Double;
Except in my case i need to test when a Currency
is one of these three edge-case values. Unfortunatly you cannot convert any of these to a Double
:
Test(NAN);
procedure Test(const Value: Currency);
...
There's an EInvalidOp
Invalid floating point operation exception when converting a Double
NAN
to a Currency
.
Is it possible to assign a NAN
to a Currency
?
Perhaps, rather than it being possible to assign a NAN
to a Currency
, it is instead not possible - and i can just ignore this edge case.
Can i ignore this edge case?
Is it possible to "Set a Currency value to NAN, INF or -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');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
货币不是 IEEE754 浮点类型,并且没有 NAN 或 INF 值。
文档解释了货币是作为隐式比例为 10000 的 64 位整数实现的,并且可能值的范围是 -922337203685477.5808 到 922337203685477.5807。由于这涵盖了 64 位整数的整个范围,因此没有可用于像 NAN 或 INF 这样的标记值的位模式。
Currency is not an IEEE754 float type and does not have NAN or INF values.
The documentation explains that Currency is implemented as a 64 bit integer with implicit scale of 10000 and that the range of possible values is -922337203685477.5808 to 922337203685477.5807. Since this covers the full range of a 64 bit integer it follows that there are no bit patterns available for sentinel values like NAN or INF.