转换“十进制或数字”数据类型时丢失精度/溢出的原因

时间:2021-06-08 16:31:58

I'm currently learning "numeric" data type on MSDN and have encountered the following phrase.

我目前正在MSDN上学习“数字”数据类型并遇到以下短语。

Converting from decimal or numeric to float or real can cause some loss of precision. Converting from int, smallint, tinyint, float, real, money, or smallmoney to either decimal or numeric can cause overflow.

从decimal或numeric转换为float或real会导致一些精度损失。从int,smallint,tinyint,float,real,money或smallmoney转换为十进制或数字可能会导致溢出。

I dont really understand the reason behind loss of precision/overflow when converting "decimal or numeric" data type. Could someone please explain it to me?

转换“十进制或数字”数据类型时,我真的不明白丢失精度/溢出的原因。有人可以向我解释一下吗?

3 个解决方案

#1


1  

Each different type of numerical representation has a different amount it can store in memory. A simple example would be using int and tinyint.

每种不同类型的数字表示具有可以存储在存储器中的不同量。一个简单的例子是使用int和tinyint。

A tinyint has 2 Bytes of storage and so can hold a max value of 32,767. An int has 4 Bytes an so can hold a max value of 2,147,483,647.

tinyint有2个字节的存储空间,因此可以容纳最大值32,767。一个int有4个字节,所以可以保持最大值2,147,483,647。

If you try and convert an int into a tinyint, and the value of the int is greater than the limit of the tinyint (> 32,767) then you will run into issues. Some languages will wrap round, others will overflow.

如果您尝试将int转换为tinyint,并且int的值大于tinyint的限制(> 32,767),那么您将遇到问题。有些语言将包围,其他语言将溢出。

#2


1  

The documentation covers it clearly.

文档清楚地涵盖了它。

Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.

用于浮点数字数据的近似数字数据类型。浮点数据是近似值;因此,并非数据类型范围中的所有值都可以准确表示。

Floating numbers in .NET or any framework are going to be approximate-number data types.

.NET或任何框架中的浮动数字将是近似数字数据类型。

#3


1  

Decimal or Numeric data types are in fact integer with decimal separator position. They can store each value between min and max values, regarding declared precision. As Ryan said, you can store approx 64k different values within four bytes ranging from -32k to 32k. If you make a four bytes float type, the range would be much wider, but there are still 64k different values to be stored. Some of the values within the range are impossible to be stored precisely.

十进制或数字数据类型实际上是带小数分隔符位置的整数。它们可以将每个值存储在最小值和最大值之间,与声明的精度相关。正如Ryan所说,你可以在-32k到32k之间的四个字节内存储大约64k个不同的值。如果使用四字节浮点类型,则范围会更宽,但仍有64k个不同的值要存储。范围内的某些值无法精确存储。

#1


1  

Each different type of numerical representation has a different amount it can store in memory. A simple example would be using int and tinyint.

每种不同类型的数字表示具有可以存储在存储器中的不同量。一个简单的例子是使用int和tinyint。

A tinyint has 2 Bytes of storage and so can hold a max value of 32,767. An int has 4 Bytes an so can hold a max value of 2,147,483,647.

tinyint有2个字节的存储空间,因此可以容纳最大值32,767。一个int有4个字节,所以可以保持最大值2,147,483,647。

If you try and convert an int into a tinyint, and the value of the int is greater than the limit of the tinyint (> 32,767) then you will run into issues. Some languages will wrap round, others will overflow.

如果您尝试将int转换为tinyint,并且int的值大于tinyint的限制(> 32,767),那么您将遇到问题。有些语言将包围,其他语言将溢出。

#2


1  

The documentation covers it clearly.

文档清楚地涵盖了它。

Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.

用于浮点数字数据的近似数字数据类型。浮点数据是近似值;因此,并非数据类型范围中的所有值都可以准确表示。

Floating numbers in .NET or any framework are going to be approximate-number data types.

.NET或任何框架中的浮动数字将是近似数字数据类型。

#3


1  

Decimal or Numeric data types are in fact integer with decimal separator position. They can store each value between min and max values, regarding declared precision. As Ryan said, you can store approx 64k different values within four bytes ranging from -32k to 32k. If you make a four bytes float type, the range would be much wider, but there are still 64k different values to be stored. Some of the values within the range are impossible to be stored precisely.

十进制或数字数据类型实际上是带小数分隔符位置的整数。它们可以将每个值存储在最小值和最大值之间,与声明的精度相关。正如Ryan所说,你可以在-32k到32k之间的四个字节内存储大约64k个不同的值。如果使用四字节浮点类型,则范围会更宽,但仍有64k个不同的值要存储。范围内的某些值无法精确存储。