-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
InexactError when dividing or multiplying Dates.Millisecond #15322
Comments
If you look here https://github.com/JuliaLang/julia/blob/master/base/dates/types.jl#L15-L20 julia> x_min = DateTime("2016-02-22T15:38:45.230")
2016-02-22T15:38:45.23
julia> x_max = DateTime("2016-02-22T19:27:35.360")
2016-02-22T19:27:35.36
julia> x_max-x_min
13730130 milliseconds
julia> (x_max-x_min)/15
915342 milliseconds
julia> (x_max-x_min)/16
ERROR: InexactError()
in /(::Base.Dates.Millisecond, ::Int64) at ./dates/periods.jl:57
in eval(::Module, ::Any) at ./boot.jl:267 So if div((x_max-x_min),16)
858133 milliseconds or (if you really want the decimal number) (x_max-x_min).value/16
858133.125 |
Indeed, your first example only works because the division of 14952000 by 20 is exact, so the floating point result is converted without loss to an integer. Note that instead of I understand this can be annoying, especially for newcomers, but Julia usually doesn't do automatic rounding. @quinnj? |
I see! Thanks for clarifying. |
That said, it doesn't make much sense to allow using The same problem happens with non-integer multiplication. In that case, we don't have an equivalent of |
This is the reason why I often have to convert DateTimes to unixtime before plotting with a package like Gadfly, since things like boxplots or density plots require |
Boxplots need to compute the mean so I understand why they fail currently, but why would a density plot fail without |
I just ran into this problem myself when trying to rescale an interval of time by a Float64 (e.g. |
This is unexpected - I expected finding the mean between two dates as (b-a)/2 ... and that actually works almost always, until one day ... The following code for i in range(1,length=10)
println((DateTime(2000,2,1) - DateTime(2000,1,1)) / i)
end fails at i==7! ... who'd have guessed that (and would have tested for it?)?? |
The most straightforward solution I found for this is to do a rounding based on the smallest increment the Dates library can handle and fits my use case.
This explicit conversion should be allowed as a flag somewhere, maybe with some documentation about it. Or even as another version of the functions that throw
|
@peteristhegreat See this issue Add rounding support for
|
Oh, might be worth linking Discourse comment here too: |
Because of the current behavior, it is difficult to use using Dates
using Statistics
t1 = now() #t1 is start time
sleep(2)
t2 = now() #t2 is end of first task and start of 2nd
sleep(1)
t3 = now() # t3 is end of 2nd task
d1 = t2-t1 # how long first task took
d2 = t3-t2 # how long 2nd task took
durations = [d1, d2]
print(durations)
print(mean(durations)) # attempt to get average time. Fails erratically. This fails "randomly"; here are 2 consecutive runs. The first succeeds and the second fails:
I guess as the number of entries in It's unclear if there is any documented way get the average. I always get function meanDuration(ds)
Millisecond(round(mean([d.value for d in ds])))
end but it assumes the inputs are all |
Normal behaviour with second precision:
14952000 milliseconds
747600 milliseconds
Behaviour if date times have sub-second precision:
13730129 milliseconds
LoadError: InexactError()
while loading In[51], in expression starting on line 7
in / at dates/periods.jl:49
The text was updated successfully, but these errors were encountered: