Skip to content

Check to see if we can do single nanosecond level math#5729

Closed
enebo wants to merge 6 commits intomasterfrom
time_to_f
Closed

Check to see if we can do single nanosecond level math#5729
enebo wants to merge 6 commits intomasterfrom
time_to_f

Conversation

@enebo
Copy link
Member

@enebo enebo commented May 13, 2019

This logic makes sure the time can fit into a single long. If so
we can do math operations which round in the same way as MRI. The
limit appears to be some time April 2262.

By using this logic I fixed 3 error in activesupport. I also fixed
another difference I was observing in .to_f in Oj.

Note: I am not saying what we were doing was wrong. The alternate
math we did made an acceptably close value, but just making this
change will reduce any noise we get from users complaining about
the lack of alignment with MRI.

The two main methods affected here are Time#- and Time.to_f.

@enebo enebo added this to the JRuby 9.2.8.0 milestone May 13, 2019
@enebo enebo requested review from headius and kares May 13, 2019 17:46
@enebo
Copy link
Member Author

enebo commented May 13, 2019

Oh I should add I feel a bit weird about writing any tests here. The rounding we had was acceptable before this change and adding a ruby/spec which specifies exact rounding would be the wrong thing to do. I still feel this reduces our friction from a minor incompatibility.

@enebo
Copy link
Member Author

enebo commented May 13, 2019

This change happens to break a test which fell out of: https://bugs.ruby-lang.org/issues/10135. The funny bit is they rejected the script as invalid due to the fact that float cannot round to base 2. Obviously they made it work but looking at source I think maybe it is the use of long long changing the rounding?

I am still somewhat ok with this PR because we round better in 2 libraries where I noticed us rounding differently. I did not check to see but I suspect we do a tiny bit better with one divide instead of two divides because there seems to be an extra chance at hitting a non-base-2 value. Although my motivation was never to 100% match MRI as much as reduce reporting of improper rounding from people who notice the discrepancy.

@kares
Copy link
Member

kares commented May 14, 2019

that test exclude looks suspicious ... otherwise looking good

@enebo
Copy link
Member Author

enebo commented May 14, 2019

@kares yeah so there is still some rounding issue where we can get different results. MRI even has two different compile modes for to_f although one uses rational so that one is fine. The other was a change in the last few years to use long long and somehow it rounds better than it did in the past? At the time that issue was opened though it rounded like we round with this patch, so that difference was small enough they rejected the issue.

My take on that test is it will not round exactly back to original number because it is not base-2 and it just happens to not round our way. With our non-long path we do two divisions so we increase our chances of our rounds not matching MRI. This new code path just removes one addition so we seem match a in more cases...

Copy link
Member

@headius headius left a comment

Choose a reason for hiding this comment

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

This seems fine to me but I'm bummed about the exclude also.

@headius headius modified the milestones: JRuby 9.2.8.0, JRuby 9.2.9.0 Aug 12, 2019
@enebo enebo modified the milestones: JRuby 9.2.9.0, JRuby 9.2.10.0 Oct 28, 2019
@enebo
Copy link
Member Author

enebo commented Oct 28, 2019

I am not going to land this right before 9.2.9.0 but I also want to do a little more testing to see how much the errors from new rounding here shift around.

@headius headius modified the milestones: JRuby 9.2.10.0, JRuby 9.3.0.0 Feb 18, 2020
@headius
Copy link
Member

headius commented Jul 8, 2021

Punting to 9.3.1 to evaluate the test regression before merging.

@headius headius modified the milestones: JRuby 9.3.0.0, JRuby 9.3.1.0 Jul 8, 2021
@headius headius modified the milestones: JRuby 9.3.1.0, JRuby 9.3.2.0 Oct 13, 2021
enebo added 2 commits October 14, 2021 15:57
This logic makes sure the time can fit into a single long.  If so
we can do math operations which round in the same way as MRI.  The
limit appears to be some time April 2262.

By using this logic I fixed 3 error in activesupport.  I also fixed
another difference I was observing in .to_f in Oj.

Note: I am not saying what we were doing was wrong.  The alternate
math we did made an acceptably close value, but just making this
change will reduce any noise we get from users complaining about
the lack of alignment with MRI.

The two main methods affected here are Time#- and Time.to_f.
@headius
Copy link
Member

headius commented Oct 14, 2021

I rebased this on latest master and pushed some changes to fix the regression. We shall see how it lands in CI.

@enebo I'm not sure what activesupport tests this fixed, so you may need to confirm they are still fixed.

@enebo enebo closed this Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants