Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

.net - .NET中小数,浮点数和双精度之间的区别?(Difference between decimal, float and double in .NET?)

What is the difference between decimal , float and double in .NET?

(.NET中的decimalfloatdouble float什么区别?)

When would someone use one of these?

(有人什么时候会使用其中之一?)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

float and double are floating binary point types .

(floatdouble浮动二进制点类型 。)

In other words, they represent a number like this:

(换句话说,它们代表这样的数字:)

10001.10010110011

The binary number and the location of the binary point are both encoded within the value.

(二进制数和二进制点的位置都编码在该值内。)

decimal is a floating decimal point type .

(decimal是一种浮动的小数点类型 。)

In other words, they represent a number like this:

(换句话说,它们代表这样的数字:)

12345.65789

Again, the number and the location of the decimal point are both encoded within the value – that's what makes decimal still a floating point type instead of a fixed point type.

(同样, 小数点的数字和位置都在值中进行编码-这就是使decimal仍然是浮点类型而不是定点类型的原因。)

The important thing to note is that humans are used to representing non-integers in a decimal form, and expect exact results in decimal representations;

(需要注意的重要一点是,人类习惯于以十进制形式表示非整数,并期望以十进制表示形式得到准确的结果。)

not all decimal numbers are exactly representable in binary floating point – 0.1, for example – so if you use a binary floating point value you'll actually get an approximation to 0.1.

(并非所有十进制数字都可以在二进制浮点数中精确表示(例如0.1),因此,如果您使用二进制浮点数,则实际上将近似为0.1。)

You'll still get approximations when using a floating decimal point as well – the result of dividing 1 by 3 can't be exactly represented, for example.

(当使用浮点小数点时,您仍然会得到近似值-例如,不能精确表示1除以3的结果。)

As for what to use when:

(至于什么时候使用:)

  • For values which are "naturally exact decimals" it's good to use decimal .

    (对于“自然精确的小数”值,最好使用decimal 。)

    This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too.

    (这通常适用于人类发明的任何概念:财务价值是最明显的例子,但也有其他一些例子。)

    Consider the score given to divers or ice skaters, for example.

    (例如,考虑给潜水员或溜冰者的分数。)

  • For values which are more artefacts of nature which can't really be measured exactly anyway, float / double are more appropriate.

    (对于更多自然伪造的值,无论如何也无法真正准确地测量它们,则使用float / double更为合适。)

    For example, scientific data would usually be represented in this form.

    (例如,科学数据通常以这种形式表示。)

    Here, the original values won't be "decimally accurate" to start with, so it's not important for the expected results to maintain the "decimal accuracy".

    (在这里,原始值一开始就不会是“十进制精度”,因此对于预期结果而言,保持“十进制精度”并不重要。)

    Floating binary point types are much faster to work with than decimals.

    (浮点二进制点类型比十进制小得多。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...