Rounding errors could make time_alloc imprecise

This commit is contained in:
Lukas Joeressen
2015-06-15 15:03:44 +02:00
parent 97a18ff4d9
commit 95fa22a65c
2 changed files with 11 additions and 2 deletions
+3 -2
View File
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "mruby.h"
#include "mruby/class.h"
#include "mruby/data.h"
@@ -208,12 +209,12 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
out_of_range:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
}
tm->usec = (time_t)((sec - tm->sec) * 1.0e6 + usec);
tm->usec = (time_t)llrint((sec - tm->sec) * 1.0e6 + usec);
while (tm->usec < 0) {
tm->sec--;
tm->usec += 1000000;
}
while (tm->usec > 1000000) {
while (tm->usec >= 1000000) {
tm->sec++;
tm->usec -= 1000000;
}
+8
View File
@@ -203,3 +203,11 @@ assert('day of week methods') do
assert_false t.friday?
assert_false t.saturday?
end
assert('2000 times 500us make a second') do
t = Time.utc 2015
2000.times do
t += 0.0005
end
t.usec == 0
end