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

Period addition is not associative #9214

Closed
stevengj opened this issue Dec 1, 2014 · 8 comments
Closed

Period addition is not associative #9214

stevengj opened this issue Dec 1, 2014 · 8 comments
Assignees
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@stevengj
Copy link
Member

stevengj commented Dec 1, 2014

julia> p1 = Dates.Second(2) + (Dates.Millisecond(7) + Dates.Millisecond(1))
2 seconds, 8 milliseconds

julia> p2 = (Dates.Second(2) + Dates.Millisecond(7)) + Dates.Millisecond(1)
2 seconds, 7 milliseconds, 1 millisecond

julia> p2 == p1
false

Seems like the problem could be fixed by making the CompoundPeriod constructor canonicalize its contents by combining periods of the same type.

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Also not commutative:

julia> Dates.Millisecond(1) + (Dates.Second(2) + Dates.Millisecond(7))
ERROR: `+` has no method matching +(::Millisecond, ::CompoundPeriod)

@stevengj stevengj added the kind:bug Indicates an unexpected problem or unintended behavior label Dec 1, 2014
@quinnj
Copy link
Member

quinnj commented Dec 1, 2014

Ok, I have the commutative fix.

Yeah, we should probably compress the periods as they're added to CompoundPeriod so there are no duplicates.

@quinnj quinnj closed this as completed in e69713b Dec 1, 2014
quinnj added a commit that referenced this issue Dec 1, 2014
Make Period + CompoundPeriod communatative. Partially fixes #9214
@quinnj quinnj reopened this Dec 1, 2014
@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Also not that == is just using object equality for CompoundPeriod, which is not correct.

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Seems like this should do it in the constructor:

type CompoundPeriod
    periods::Array{Period,1}
    function CompoundPeriod(p::Vector{Period})
        n = length(p)
        n < 2 && return new(p)
        sort!(p, rev=true, lt=periodisless)
        # canonicalize p by merging equal period types                          
        i = j = 1
        while j <= n
            k = j+1
            while k <= n
                if typeof(p[j]) == typeof(p[k])
                    p[j] += p[k]
                    k += 1
                else
                    break
                end
            end
            p[i] = p[j]
            i += 1
            j = k
        end
        return new(resize!(p, i-1))
    end
end

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Correction: should also canonicalize to eliminate zeros.

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

I'll submit a PR shortly

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Shouldn't CompoundPeriod be a subtype of AbstractTime (like Period)?

@stevengj
Copy link
Member Author

stevengj commented Dec 1, 2014

Also notice that (+)(x::CompoundPeriod,y::Period) and (-)(x::CompoundPeriod,y::Period) currently mutate x (via push!), which seems undesirable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants