-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Incorrect results of right shifting huge BigIntegers #65633
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsDescriptionEnormous negative BigIntegers produce Reproduction Stepsusing System.Numerics;
BigInteger bigInt = new BigInteger(-1) << int.MaxValue;
Console.WriteLine(bigInt.GetBitLength());
BigInteger shiftedBigInt = bigInt >> 1;
Console.WriteLine(shiftedBigInt.GetBitLength());
Console.WriteLine(shiftedBigInt == new BigInteger(-1)); Expected behaviorThe program should output:
Actual behaviorThe program outputs:
Regression?No response Known WorkaroundsNo response Configuration
Windows 11 Pro Version 21H2
x64 Other information
|
@tannergooding Do you think this bug can be fixed without the significant refactoring we've been talking about for BigInteger, or should we wait to address it until we have that work scheduled? |
Yes, it should be. It would probably be easier after, but I don't think that its necessary or good to wait if someone has time to look at this for .NET 7 |
Per discussion with the team, we will consider backporting the fix for this issue into .NET 6 if we can get validation that the user's scenario is addressed with the fix. We'd also need to get information that describes the types of applications affected, if the issues are blocking production scenarios, and an understanding of when .NET 7 would be adopted into that environment. |
…ed in Issue #65633 (#67867) * Fix #65633, multiplication overflow causing incorrect right shift results * Add unit test reproducing incorrect right shift results for BigInteger (#65633) * Fix inconsistent formatting * Expand LargeNegativeBigIntegerShiftTest to validate values, expose private BigInteger members to tests * Apply suggestions from code review Co-authored-by: Tanner Gooding <[email protected]> * Restrict unit test to 64-bit processors * Document reasoning for restricting the test to 64-bit Co-authored-by: Dan Moseley <[email protected]> Co-authored-by: Tanner Gooding <[email protected]> Co-authored-by: Dan Moseley <[email protected]>
…ed in Issue dotnet#65633 (dotnet#67867) * Fix dotnet#65633, multiplication overflow causing incorrect right shift results * Add unit test reproducing incorrect right shift results for BigInteger (dotnet#65633) * Fix inconsistent formatting * Expand LargeNegativeBigIntegerShiftTest to validate values, expose private BigInteger members to tests * Apply suggestions from code review Co-authored-by: Tanner Gooding <[email protected]> * Restrict unit test to 64-bit processors * Document reasoning for restricting the test to 64-bit Co-authored-by: Dan Moseley <[email protected]> Co-authored-by: Tanner Gooding <[email protected]> Co-authored-by: Dan Moseley <[email protected]>
I am sorry for my late reply. But no, this doesn't block any production scenarios. I hit this issue when I was trying to self-educate by implementing a program that would compute lots of digits of pi. This has never been intended to be used in production and even for this purpose I don't need this functionality. |
Description
Enormous negative BigIntegers produce
-1
when right shifted by any value.Reproduction Steps
Expected behavior
The program should output:
Actual behavior
The program outputs:
Regression?
No response
Known Workarounds
No response
Configuration
<TargetFramework>net6.0</TargetFramework>
Windows 11 Pro Version 21H2
x64
Other information
BigIntegers
seem to not suffer from this issue, even if they are as enormous:(new BigInteger(1) << int.MaxValue) >> 1
seems to give expected results.BigIntegers
also seem to not suffer from this issue:(new BigInteger(-1) << (int.MaxValue - 1000)) >> 1
seems to give expected results.(new BigInteger(-1) << (int.MaxValue - 10)) >> 1
, however, still returns -1.(new BigInteger(-1) << int.MaxValue) >> (int.MaxValue - 4)
still gives -1.The text was updated successfully, but these errors were encountered: