Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

TimeSpan.FromMilliseconds(TimeSpan.MaxValue.TotalMilliseconds) #10352

Merged
merged 2 commits into from
Mar 28, 2017

Conversation

ezverev
Copy link

@ezverev ezverev commented Mar 21, 2017

TimeSpan.FromMilliseconds(TimeSpan.MaxValue.TotalMilliseconds) gives exception. I suggest the fix.

The problem is discussed here: https://connect.microsoft.com/VisualStudio/feedback/details/542235/timespan-structure-incorrectly-handles-values-close-to-min-and-max-value

The existing overflow checks are ignorant and superfluous.

@danmoseley
Copy link
Member

Thanks for the contribution @ezverev we'll take a look.
Usually it's a good idea to open an issue (in corefx repo) before creating a PR so we can agree we want to take the change.

@ezverev
Copy link
Author

ezverev commented Mar 22, 2017

@danmosemsft OK, noted.

@danmoseley
Copy link
Member

@tarekgh

@tarekgh
Copy link
Member

tarekgh commented Mar 24, 2017

@joshfree the proposed change looks good to me as it will do the rounding to the ticks instead of the milliseconds which would be more precise. I am seeing your comment on https://connect.microsoft.com/VisualStudio/feedback/details/542235/timespan-structure-incorrectly-handles-values-close-to-min-and-max-value

Do you still have a concern?

I already checked manually the changes with values around TimeSpan.MaxValue and TimeSpan.MinValue and not seeing any problem.

Hello e_zverev,
Thanks for the post. Because of the loss of precision of the Double data type, this conversion can generate an OverflowException for values that are near to but still in the range of either MinValue or MaxValue. For example, this causes an OverflowException in the following attempt to instantiate a TimeSpan object:

// http://msdn.microsoft.com/en-us/library/system.timespan.frommilliseconds(VS.95).aspx
// The following throws an OverflowException at runtime
TimeSpan maxSpan = TimeSpan.FromMilliseconds(TimeSpan.MaxValue.TotalMilliseconds);

For more information on Double precision, please see http://en.wikipedia.org/wiki/Double_precision

Sincerely,
Josh Free

@ezverev could you please resolve the merge conflict?

@tarekgh
Copy link
Member

tarekgh commented Mar 27, 2017

@ezverev please resolve the merge conflict. Thanks.

@tarekgh
Copy link
Member

tarekgh commented Mar 28, 2017

@ezverev did you have a chance to resolve the conflict?

@ezverev
Copy link
Author

ezverev commented Mar 28, 2017

Sorry for the delay. Resolved.

@tarekgh tarekgh merged commit 7951bc9 into dotnet:master Mar 28, 2017
@tarekgh
Copy link
Member

tarekgh commented Mar 28, 2017

Thanks @ezverev for submitting this fix

@jkotas
Copy link
Member

jkotas commented Mar 29, 2017

@tarekgh Do we need a regression test in corefx?

@jkotas
Copy link
Member

jkotas commented Mar 29, 2017

@ezverev ezverev deleted the patch-1 branch March 29, 2017 08:10
@ezverev
Copy link
Author

ezverev commented Mar 29, 2017

I will look into appending a test in order to assure there is no longer unneeded precision loss when constructing TimeSpan from a double of different scales.

Thanks for the constructive approach. I am sorry to rush the thing and passing over the expected "issue discussion".

@stephentoub
Copy link
Member

This appears to be breaking a bunch a bunch of corefx tests:
dotnet/corefx#17648

stephentoub added a commit to stephentoub/coreclr that referenced this pull request Mar 29, 2017
@danmoseley
Copy link
Member

@ezverev that's totally fine and it takes time to get used to get used to the "ways" of any OSS project. We're glad for your contribution and hopefully you will be interested to make more in future.

https://github.com/dotnet/corefx/issues/17619 may be interesting.

ezverev added a commit to ezverev/corert that referenced this pull request Mar 29, 2017
jkotas pushed a commit to dotnet/corert that referenced this pull request Mar 29, 2017
@ezverev
Copy link
Author

ezverev commented Mar 29, 2017

Working on the unit tests for modified TimeSpan I found a huge flaw in my fix. I got into the kid's trap with the overflow check. Existing unit tests do not cover it:

With my fix the TimeSpan.FromMilliseconds(922337203685477.62) gives incorrect result.

Please do revert the change that I've made. I will have to work on the issue further and enhance the unit tests before I resubmit the PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
9 participants