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

Fix invalid "double.ToString" behavior with two section separators #85565

Closed

Conversation

Maximys
Copy link
Contributor

@Maximys Maximys commented Apr 30, 2023

Fixes #70460

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 30, 2023
@ghost
Copy link

ghost commented Apr 30, 2023

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

Issue Details

null

Author: Maximys
Assignees: -
Labels:

area-System.Numerics

Milestone: -

@tannergooding
Copy link
Member

Can you link to the corresponding bug that describes the behavior and what you're fixing?

@tannergooding tannergooding added the needs-author-action An issue or pull request that requires more info or actions from the author. label May 1, 2023
@Maximys
Copy link
Contributor Author

Maximys commented May 2, 2023

Can you link to the corresponding bug that describes the behavior and what you're fixing?

Yes, of course: #70460
Current PR, current fixes is invalid (a lot of test was broken), I'll try to fix it later.

@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label May 2, 2023
@Maximys Maximys force-pushed the bugfix/70460-invalid-tostring-output branch from 64a81bb to 78e40a1 Compare May 3, 2023 13:32
@danmoseley
Copy link
Member

@Maximys If you put "Fixes #70460" on its own line in the top comment, the issue will be closed on merge.

@Maximys
Copy link
Contributor Author

Maximys commented May 3, 2023

@danmoseley , thank you.

@Maximys
Copy link
Contributor Author

Maximys commented May 3, 2023

I think, my fix is ready. How I can change PR from "draft" to normal?

@danmoseley
Copy link
Member

click this
image

@danmoseley danmoseley changed the title fix(70460): Fix invalid "double.ToString" behavior with two section separators Fix invalid "double.ToString" behavior with two section separators May 3, 2023
@Maximys Maximys marked this pull request as ready for review May 3, 2023 16:33
@@ -731,6 +731,9 @@ public static IEnumerable<object[]> ToString_TestData()
{
yield return new object[] { -4567.0, "G", null, "-4567" };
yield return new object[] { -4567.89101, "G", null, "-4567.89101" };
yield return new object[] { -0.001, "+0.00;-0.00", null, "+0.00" };
Copy link
Member

@tannergooding tannergooding May 3, 2023

Choose a reason for hiding this comment

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

I think this needs to print -0.00 given that -0.001 rounds to -0.0d and not +0.0d

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@Maximys Maximys May 22, 2023

Choose a reason for hiding this comment

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

@tannergooding , MSDN have next info about ";" section separator:

The first section applies to positive values and zeros, and the second section applies to negative values.

If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, the resulting zero is formatted according to the first section.

If I correctly understand this info, then output "+0.00" is absolutly valid and my commit 6c3d9e6 with fixes by your comment was invalid.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

The section was written without the consideration around -0.0 and its relevance to floating-point code. We only made the push towards ensuring things were IEEE 754 compliant starting around .NET Core 2.1

The general intent of -0.0 is to represent a very small negative number that was rounded towards zero, but which may not itself be zero. Preserving this semantic and treating it as negative is generally desirable and consistent with the handling we expose elsewhere.

We should likely update the docs to cover that:

  • One section - The format string applies to all values.
  • Two sections - The first section applies to positive values and zeros, and the second section applies to negative values including negative zero for IEEE 754 floating-point types
  • Three sections - The first section applies to positive values, the second section applies to negative values, and the third section applies to zero including negative zero for IEEE 754 floating-point types

That still preserves the ability to differentiate between +0 vs -0 or to differentiate them based on sign as appropriate, with the default preserving the same behavior as occurs for regular format strings.

CC. @jeffhandley for secondary input -- This may be something we want to take to API review for secondary weigh in; but the original intent when we made the IEEE 754 compat fixes back in 3.0 was along the lines of the above.

Copy link
Member

Choose a reason for hiding this comment

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

I support the approach @tannergooding described there, which as I understand it, keeps our current functional behavior intact and adjusts the docs to reflect the behavior we've had for several years now.

I don't think we need to float this past API review unless we're considering a change in functional behavior or a new concept.

public static void Test_ToString_NotNetFramework()
[Theory]
[MemberData(nameof(ToString_TestData_NotNetFramework))]
public static void Test_ToString_NotNetFramework(double d, string format, IFormatProvider provider, string expected)
Copy link
Member

Choose a reason for hiding this comment

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

Corresponding tests need to be added to Single and potentially Half

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Maximys Maximys force-pushed the bugfix/70460-invalid-tostring-output branch 3 times, most recently from 9267ffb to c47405f Compare May 23, 2023 14:30
@Maximys
Copy link
Contributor Author

Maximys commented May 24, 2023

@tannergooding , there is last failed test of my PR build and it's linked with Linux environment. Could you get me some instruction about how I can run build of .NET Runtime inside Docker container with Linux? I trying next command for build container, but it's failed:
docker run -v C:\runtime\:/runtime -w /runtime mcr.microsoft.com/dotnet-buildtools/prereqs:ubuntu-18.04 ./build.sh

/runtime/eng/common/tools.sh: line 501: /runtime//.dotnet/dotnet: No such file or directory
Build failed with exit code 127. Check errors above.

I think it linked with invalid path for build("/runtime//.dotnet/dotnet"), but I'm not sure.

@tannergooding
Copy link
Member

Sorry for the delay, I missed the last message and this never got put on my backlog

In general the instructions for building the runtime are here: https://github.com/dotnet/runtime/blob/main/docs/workflow/README.md

I personally don't use docker or any such related workflow, so I can't help on specifics around that.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

The docker instructions can be found here: https://github.com/dotnet/runtime/blob/main/docs/workflow/building/coreclr/linux-instructions.md#build-using-docker

Here you can find the list of things that are required to build the repo on Linux: https://github.com/dotnet/runtime/blob/main/docs/workflow/requirements/linux-requirements.md#toolchain-setup

On Windows, the most convinient way of debugging a failing unit test is just adding the following snipplet to the begining of the test:

System.Diagnostics.Debugger.Launch(); // Windows asks you to choose the debugger, you point it to VS and just debug the failure

On Linux and macOS this API does not work, but you can simply add following code to the failing test:

while (!System.Diagnostics.Debugger.IsAttached)
{
    Thread.Sleep(TimeSpan.FromSeconds(1));
}

And use your IDE to attach debugger to a running process. In case of this particular case you need to run the following commands:

# to build CLR in Release and Libraries in Debug
./build.sh -subset clr+libs -rc Release
# To run the specific test
./dotnet.sh build ./src/libraries/System.Runtime.Numerics/tests/System.Runtime.Numerics.Tests.csproj /t:Test /p:xunitoptions="-method System.Numerics.Tests.ToStringTest.RunCustomFormatNumberScaling"
# To rebuild System.Private.CoreLib.dll without rebuilding the rest of the product
./build.sh clr.corelib+clr.nativecorelib+libs.PreTest -rc Release

Since the tests are failing I am going to "request changes" and turn it into DRAFT PR. Please let me know if you need more help.

@ghost ghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label Nov 17, 2023
@adamsitnik adamsitnik marked this pull request as draft November 17, 2023 16:07
@ghost ghost added the no-recent-activity label Dec 1, 2023
@ghost
Copy link

ghost commented Dec 1, 2023

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 Dec 16, 2023

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 Dec 16, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 15, 2024
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.

Double.ToString with two section separators and negative zero placing additional negative sign in output
5 participants