Skip to content
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

Fixes of invalid biginteger casting #56710

Conversation

Maximys
Copy link
Contributor

@Maximys Maximys commented Aug 2, 2021

This PR fix #49611.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Aug 2, 2021
@ghost
Copy link

ghost commented Aug 2, 2021

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

Issue Details

This PR fix #49611.

Author: Maximys
Assignees: -
Labels:

area-System.Numerics, community-contribution

Milestone: -

@@ -2461,6 +2482,54 @@ private static bool GetPartsForBitManipulation(ref BigInteger x, ref Span<uint>
return x._sign < 0;
}

private static bool IsEmpty([NotNullWhen(false)] uint[]? bits)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be IsNull instead? Maybe we can inline the bits == null check at the callsite (as there is only one caller).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, you are right. I'll fix it after complete review

@@ -2433,6 +2424,36 @@ public long GetBitLength()
return bitLength - 1;
}

private static double ConvertToDouble(uint[] bits,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We don't normally have a parameter per line.

@@ -2461,6 +2482,54 @@ private static bool GetPartsForBitManipulation(ref BigInteger x, ref Span<uint>
return x._sign < 0;
}

private static bool IsEmpty([NotNullWhen(false)] uint[]? bits)
{
bool returnValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this simply return bits is null?

{
bool returnValue;

// The maximum exponent for doubles is 1023, which corresponds to a uint bit length of 32.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The comment of bit length of 32 isn't great (I know it was already there before this PR) as reading it sounds like # of bits is more than 32 (implying a 32-bit integer).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last sentence also looks to be no longer true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannergooding , I think, comment is evil and Uncle Bob ( http://cleancoder.com/products ) absolutely agree with me. I had wanted to remove this comment, but it can very hard to understand what's the magic value 1024 in line below. I'll try to change this comment to more clear.

private static double ConvertToDouble(uint[] bits,
int length,
int sign)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to try and normalize this to be the same as the already known "good" logic used in our double parsing/formatting logic:
https://source.dot.net/#System.Private.CoreLib/Number.Parsing.cs,9927df209c6039f5,references and https://source.dot.net/#System.Private.CoreLib/Number.NumberToFloatingPointBits.cs,3b99f12cbc14a09d

The overall logic is a bit more complex since it needs to handle non integral numbers; but it also correctly handles rounding and various edge cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannergooding, thatk you for your comment, I had not know about it. I agree, that a widely used method is better than a new one, but I also believe that the provided methods contain more logic than necessary and may work little slower.
I'll try to understand provided methods and apply some one for remove my method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannergooding , I can't understand how to use https://source.dot.net/#System.Private.CoreLib/Number.Parsing.cs,9927df209c6039f5,references , because I can not add reference to System.Private.CoreLib .
Can you provide little example about reusing Number.NumberToDouble?

@tannergooding
Copy link
Member

@Maximys are you still working on this?

No particular rush, just wanted to ensure that was the case since many people took a break over the holidays. There are still several pending comments above that would need to be resolved.

@tannergooding tannergooding added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs more info labels Jan 7, 2022
@Maximys
Copy link
Contributor Author

Maximys commented Jan 8, 2022

@tannergooding , I forgot about this PR. I'll try to process pending comments today.

@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 8, 2022
@tannergooding tannergooding added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 24, 2022
@ghost ghost added the no-recent-activity label Feb 7, 2022
@ghost
Copy link

ghost commented Feb 7, 2022

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost
Copy link

ghost commented Feb 22, 2022

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@ghost ghost closed this Feb 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2022
@Maximys Maximys deleted the fix/49611-fixes-of-invalid-biginteger-casting branch May 24, 2023 04:42
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Numerics community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conversions from BigInteger to Double are not always as precise as from Long
5 participants