C#infinity Double(和Float)vs Decimal(以及Int64) [英] C# infinity Double (and Float) vs Decimal (as well as Int64)

查看:93
本文介绍了C#infinity Double(和Float)vs Decimal(以及Int64)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MS选择在 Double Float 中插入 Infinity 的表示,但不是在Decimal和Int64中插入?



你有:

Double。 PositiveInfinity &浮点。 PositiveInfinity (以及 Double.Epsilon & Float.Epsilon )在十进制中缺失。



Q 为什么?





此外:我理解十进制的概念基本上是为货币操作和其他现实世界计算而设计的。



但这并不能解释我的问题。

原因是偶数十进制和Int64当然有它的限制,

这些限制
如果你乘以MaxValue,则Int64中的
MaxValue你得到 1 !!!!

在十进制中抛出一个异常。 (我可能住在一起)



您可以在两者中轻松实现 Infinity

可以说一个异常是一个期望的响应,但Int64的行为在我看来完全是愚蠢的。

MS chose to insert a representation of Infinity in Double and Float but not in Decimal and Int64?

You have:
Double.PositiveInfinity & Float.PositiveInfinity (as well as Double.Epsilon & Float.Epsilon) which are missing in Decimal.

Q: Why?


Moreover: I understand the concept of decimal being basically designed more for currency operations and other "real world" calculations.

But being that doesn''t really explain my question.
The reason is that even decimal and Int64 has it''s limits of course,
these limits
in Int64 if you multiply MaxValue by MaxValue you get 1 !!!!
in Decimal it throws an exception. (which I may live with)

You could easily implement Infinity in both.
Arguably an exception is a desired response, but the Int64 behavior is totally silly in my opinion.

推荐答案

简短的回答是''因为那是'什么标准说''; IEEE浮点定义无穷大和NaN,而整数标准不定义。尽管在该术语的最字面意义上具有浮点,但是十进制是基于整数标准(并且显然 long 是本机整数类型)。 ''特殊''位模式通常是''大多数位'',因此它代表整数类型中的小负数。



你可以改变使用已检查的{...}或未经检查的{...}构造在.Net中的本机类型的溢出行为,并使用编译器选项设置默认值:请参阅 MSDN [ ^ ]。
The short answer is ''because that''s what the standard says''; IEEE floating point defines infinities and NaNs, whereas the integer standards don''t. Decimal, despite having a floating point in the most literal sense of that term, is based on the integer standards (and obviously long is a native integer type). The ''special'' bit patterns are typically ''most bits on'' and as such would represent small negative numbers in an integer type.

You can alter the overflow behaviour of native types in .Net with the checked{...} or unchecked{...} constructs, with a compiler option setting the default: see MSDN[^].






看看这里:

http://stackoverflow.com/questions/986268/why-is-nan-not-a-number-only-available-for-doubles [ ^ ]

StackOverflow问题是关于为什么NaN仅适用于 double s的问题,但它也适用于 PositiveInfinity
Hi,

Have a look here:
http://stackoverflow.com/questions/986268/why-is-nan-not-a-number-only-available-for-doubles[^]
The StackOverflow question is a question about why NaN is only available for doubles, but it also applies to PositiveInfinity.


这篇关于C#infinity Double(和Float)vs Decimal(以及Int64)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆