Skip to content

Fix bug with BigInteger.TrailingZeroCount #77727

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

Merged
merged 7 commits into from
Nov 10, 2022

Conversation

dakersnar
Copy link
Contributor

Fixes #77720

@ghost ghost added the area-System.Numerics label Nov 1, 2022
@ghost ghost assigned dakersnar Nov 1, 2022
@ghost
Copy link

ghost commented Nov 1, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #77720

Author: dakersnar
Assignees: -
Labels:

area-System.Numerics

Milestone: -

@tkekalainen
Copy link
Contributor

tkekalainen commented Nov 1, 2022

This doesn't seem to fix the negative number path that also has the extra increment? Also, the two new tests check the same value. Did you mean -BigInteger.Pow(2, 1000) instead of BigInteger.Pow(-2, 1000)?

dakersnar and others added 2 commits November 1, 2022 12:32
…nericMath.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>
@tfenise
Copy link
Contributor

tfenise commented Nov 2, 2022

Why separate paths for positive and negative numbers in the first place? uint.TrailingZeroCount(x) and uint.TrailingZeroCount(~x + 1) are equal.

@dakersnar
Copy link
Contributor Author

@tfenise Yep, I see what you mean after looking closer at your explanation in the bug report. Thanks for bringing this to our attention!

@tkekalainen
Copy link
Contributor

@dakersnar

@tfenise Yep, I see what you mean after looking closer at your explanation in the bug report. Thanks for bringing this to our attention!

Well, it was I who wrote the bug report and noted the unneeded path, but I'm glad @tfenise brought it up again, because I probably wouldn't have had the courage to do so. 🙂

…nteger.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>
@dakersnar
Copy link
Contributor Author

@tkekalainen Sounds like I need more coffee too. Thanks 😄.

@tannergooding tannergooding merged commit 97643fd into dotnet:main Nov 10, 2022
@tannergooding
Copy link
Member

/backport to release/7.0

@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3438765946

@github-actions
Copy link
Contributor

@tannergooding backporting to release/7.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix bug with BigInteger.TrailingZeroCount
Applying: Update src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs
Applying: Update negative code path
Applying: Simplify logic for handling negative values
Applying: Update src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs
Applying: Update comment
error: sha1 information is lacking or useless (src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0006 Update comment
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@github-actions
Copy link
Contributor

@tannergooding an error occurred while backporting to release/7.0, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

tannergooding pushed a commit to tannergooding/runtime that referenced this pull request Nov 10, 2022
* Fix bug with BigInteger.TrailingZeroCount

* Update src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>

* Update negative code path

* Simplify logic for handling negative values

* Update src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>

* Update comment

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>
@dakersnar dakersnar deleted the fix-77720 branch November 10, 2022 17:23
carlossanlop pushed a commit that referenced this pull request Nov 11, 2022
* Fix bug with BigInteger.TrailingZeroCount

* Update src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>

* Update negative code path

* Simplify logic for handling negative values

* Update src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>

* Update comment

Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>

Co-authored-by: Drew Kersnar <18474647+dakersnar@users.noreply.github.com>
Co-authored-by: Tommi Kekäläinen <76913562+tkekalainen@users.noreply.github.com>
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Numerics.BigInteger.TrailingZeroCount is broken for numbers with a lot of trailing zeros.
5 participants