VB.NET 单一数据类型计算问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13399241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:09:04  来源:igfitidea点击:

VB.NET Single data type calculation issue

vb.netvb.net-2010

提问by Albert Tobing

I want to perform a basic calculation with fractional numbers using vb.net.

我想使用 vb.net 用小数执行基本计算。

Dim a As Single= 7200.5
Dim b As Single= 7150.3
Dim c As Single= a - b

'Expected result = 50.2
MsgBox(a.ToString + " - " + b.ToString + " = " + c.ToString.Trim)
'Produced result is: 50.2002

Dim single1 As Single
Dim single2 As Single
Dim single3 As Single

single1 = 425000
single2 = 352922.2
single3 = single1 - single2

'Expected result is: 72077.8
MsgBox(single3.ToString)
'Produced result is: 72077.81

How can the results be so inaccurate for such a simple calculation? The problem is solved when I change the data type to Decimal, but Decimalobjects consume more memory (16 bytes). Is there any alternative data type that i can use to perform simple fractional calculations with accurate results?

这么简单的计算,结果怎么会这么不准确呢?当我将数据类型更改为 时,问题解决了Decimal,但Decimal对象消耗了更多内存(16 字节)。是否有任何替代数据类型可用于执行具有准确结果的简单分数计算?

回答by Sean Airey

This is to do with the way floating point numbers are stored in memory, and a Single in .Net is a single precision floating point number, which is much less accurate than a Decimal or a Double for storing decimal numbers.

这与浮点数在内存中的存储方式有关,.Net 中的 Single 是单精度浮点数,其精度远不如 Decimal 或 Double 来存储十进制数。

When the computer calculates your number, it only has binary fractions to use and in a single precision floating point number, they're not very accurate.

当计算机计算您的数字时,它只有二进制分数可供使用,并且在单精度浮点数中,它们不是很准确。

See http://en.wikipedia.org/wiki/Single-precision_floating-point_formatfor more information.

有关更多信息,请参阅http://en.wikipedia.org/wiki/Single-precision_floating-point_format

EDIT: There's some more information specific to VB.Net here: http://msdn.microsoft.com/en-us/library/ae382yt8(v=vs.110).aspx

编辑:这里有一些特定于 VB.Net 的更多信息:http: //msdn.microsoft.com/en-us/library/ae382yt8(v=vs.110) .aspx

回答by Steven Doggart

The Singleand Doubledata types are not precise. They use the floating point method to store their values. Floating points use less memory and allow for faster calculations, but they are imprecise. That is the trade-off that you have to accept if you are going to use them. If precision is important, then they are not an option for you. Decimalis precise (to a certain number of fractional digits, that is), so usually, that is the best choice for precise fractional numbers in most cases. If you really need to save memory, and you are guaranteed that your numbers will be within a certain range, then you could use an Int16, Int32, or Int64instead. For instance, if you only care about two fractional digits, you could simply multiply everything by 100 and then just divide by 100 (using Decimaltypes for the division) before displaying it. In that way, you can store many numbers and perform many operations using less memory, and only need to use the Decimal data type when you need to display a result.

SingleDouble数据类型不是精确。它们使用浮点方法来存储它们的值。浮点使用更少的内存并允许更快的计算,但它们不精确。如果您要使用它们,那么这就是您必须接受的权衡。如果精度很重要,那么它们不适合您。 Decimal是精确的(即一定数量的小数位数),因此通常,在大多数情况下,这是精确小数的最佳选择。如果您确实需要节省内存,并且保证您的数字在某个范围内,那么您可以使用Int16Int32、 或Int64代替。例如,如果您只关心两个小数位数,您可以简单地将所有内容乘以 100,然后再除以 100(使用Decimal类型),然后再显示。这样,您可以使用更少的内存存储许多数字并执行许多操作,并且只需要在需要显示结果时使用 Decimal 数据类型。

Dim a As Integer = 720050 '7200.5
Dim b As Integer = 715030 '7150.3
Dim c As Integer = a - b
Dim cDisplay As Decimal = CDec(c) / CDec(100)
MessageBox.Display(String.Format("{0} - {1} = {2}", a, b, c))

回答by Serge Bollaerts

You can use the Decimaldata type instead. It will work great! This is because Decimalis a fixed point value, whereas Singleand Doubleare floating point values (with loss of precision).

您可以改用Decimal数据类型。它会很好用!这是因为Decimal是定点值,而SingleDouble是浮点值(精度损失)。