What's the second minimum value that a decimal can represent? That is the value which is larger than Decimal.MinValue and smaller than any other values that a decimal can represent. How can I obtain this value in C#? Thanks!
3 Answers
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.
Comments
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; 11 Comments
Decimal.MinValue + 0.000...1.(Decimal.MinValue + 0.000...1) == Decimal.MinValue.Decimal.MinValue + 0.000001m is.Decimal.MinValue such that Decimal.MinValue + x > Decimal.MinValue where x < 1.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.
4 Comments
Decimal type can't represent any intermediate values between -79,228,162,514,264,337,593,543,950,335 and -79,228,162,514,264,337,593,543,950,334. The smallest discrete step is 1.