What's the second minimum value that a decimal can represent?

微笑、不失礼 提交于 2019-12-06 06:01:43

The second-minimum value is Decimal.MinValue + 1.

This can be inferred from the documentation for decimal:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value is of the form, ((-2^96 to 2^96) / 10^(0 to 28)), where -2^96-1 is equal to MinValue, and 2^96-1 is equal to MaxValue.

From the above we can infer that on the extreme edges of the legal value range, the scaling factor is 1 (10 to the power 0) and therefore that's the smallest quantum when a decimal value is modified.

Live proof.

According to MSDN, a decimal is represented like ((-2^96 to 2^96) / 10^(0 to 28)), where -2^96-1 is equal to MinValue, and 2^96-1 is equal to MaxValue, so the smallest difference between two decimals is 1/10^28.
That difference is only possible between small decimals though. Generally, as a decimal becomes larger (no matter the sign), you lose decimal points, until there are none left.

UPDATE: As also pointed out in the comments, you can't actually change decimal.MinValue by adding the smallest decimal value (as above). Decimal has 1 bit for the sign, 96 bit for a number and a scaling factor (10^x) by which the number is divided.

In order to get to such a large negative number, the exponent portion of the scaling factor must be set to 0 (-> 10^0 == 1), because setting it to anything higher would cause the number to be divided by that and thus it would get smaller.

That means, for such a number, the smallest difference would be 1/10^0, or 1.

So you are looking for this:

decimal.MinValue + 1m;

http://msdn.microsoft.com/en-us/library/system.decimal.minvalue.aspx

Decimal.MinValue + 1

So: -79,228,162,514,264,337,593,543,950,334.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!