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

Nanosecond precision when subtracting Time objects #1771

Closed
robin850 opened this issue Jun 27, 2014 · 1 comment
Closed

Nanosecond precision when subtracting Time objects #1771

robin850 opened this issue Jun 27, 2014 · 1 comment

Comments

@robin850
Copy link
Contributor

Hello,

When using Time#- passing a Time object, the precision is inconsistent with what MRI returns. In the following example, the nanosecond precision (9 decimals) should be used but for a strange reason, the number of digits is greater than 9. Looking at the code, this condition is met ; thus, the opMinus method is triggered so this line is as well but the number of digits returned is not the expected one:

>> time1 = Time.utc(2000, 1, 2, 23, 59, 59, Rational(999999999, 1000));
>> time2 = Time.utc(2000, 1, 2, 0, 0, 0, Rational(1, 1000));

>> (time1.nsec - time2.nsec) / 1000000000.0
=> 0.999999998 # Expected result
>> diff = time1 - time2;
>> diff - diff.floor
=> 0.9999999979918357

This has been tested with JRuby 1.7.13. A RubySpec should be added here ; even if the problem is not exactly the same, the spec should fit.

Have a nice day.

@rjnienaber
Copy link

The output seems to be identical for most current ruby versions:

ruby 1.9.3p550 (2014-10-27) [x86_64-darwin14.5.0]
0.999999998
0.9999999979918357

ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin14]
0.999999998
0.9999999979918357

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
0.999999998
0.9999999979918357

jruby 1.7.24 (1.9.3p551) 2016-01-20 bd68d85 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_51-b16 +jit [darwin-x86_64]
0.999999998
0.9999999979918357

jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.51-b03 on 1.8.0_51-b16 +jit [darwin-x86_64]
0.999999998
0.9999999979918357

@kares kares added this to the Invalid or Duplicate milestone Jan 17, 2017
@kares kares closed this as completed Jan 17, 2017
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

No branches or pull requests

3 participants