-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
double
representation of decimal.MaxValue
cannot be converted properly to decimal
#68042
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescription
Reproduction Stepsdouble d = Decimal.ToDouble(decimal.MaxValue);
decimal converted = (decimal)(d); Expected behaviorThe operation succeeds and Actual behaviorthrows Regression?No response Known WorkaroundsNo response Configurationreproduces in net6.0 and net5.0. I haven't tried others. Other informationNo response
|
This is kinda false, this exact value is not representable. The nearest round-trip value appears to be Doubles can no longer represent all integers over 2 ^ 53, or 9007199254740992 (16 digits). |
I did a bit of digging and it looks like it follows from the rules of explicit conversions between different numeric types. Specifically, these two rules -
and
In our case, we go from - |
Note that its actually: This is still "too large" and should throw as specified. -- I had misread the conversion direction at first thinking the conversion to double was throwing, when it shouldn't. There is still a separate issue where precision is unnecessarily lost for other values: double x = Math.BitDecrement(79228162514264337593543950335.0);
decimal y = (decimal)x;
Console.WriteLine(x.ToString("G99")); // 79228162514264328797450928128
Console.WriteLine(y.ToString("G99")); // 79228162514264300000000000000 |
Closing this and moving to the more in-depth issue above |
Description
decimal.MaxValue
(79228162514264337593543950335) is representable within the range of double (as 7.9228162514264338E+28), however trying to cast a value back todecimal
it throwsOverflowException
"Value was either too large or too small for a Decimal."Reproduction Steps
Expected behavior
The operation succeeds and
converted
== 7.9228162514264338E+28Actual behavior
throws
OverflowException
"Value was either too large or too small for a Decimal."Regression?
No response
Known Workarounds
No response
Configuration
reproduces in net6.0 and net5.0. I haven't tried others.
Other information
No response
The text was updated successfully, but these errors were encountered: