Skip to content

Obscure regression in 32-bit Julia 0.6.3 #27345

@nalimilan

Description

@nalimilan

In Julia 0.6.3, StatsBase.wmedian gives incorrect results on 32-bit. In the simplified function below, the result is 8.5 on 0.6.2 and 0.7.0-alpha, but 10.0 on 0.6.3. Everything is OK on 64-bit.

FWIW, the bug goes away just by adding @show statements.

function wmedian(v::AbstractVector{<:Real}, wt::AbstractVector{<:Real})
    midpoint = sum(wt)/2
    maxval, maxind = findmax(wt)
    if maxval > midpoint
        v[maxind]
    else
        permute = sortperm(v)
        cumulative_weight = zero(eltype(wt))
        i = 0
        for (_i, p) in enumerate(permute)
            i = _i
            if cumulative_weight == midpoint
                i += 1
                break
            elseif cumulative_weight > midpoint
                cumulative_weight -= wt[p]
                break
            end
            cumulative_weight += wt[p]
        end
        if cumulative_weight == midpoint
            middle(v[permute[i-2]], v[permute[i-1]])
        else
            middle(v[permute[i-1]])
        end
    end
end

julia> wmedian([1, 2, 4, 7, 10, 15], [1/3, 1/3, 1/3, 1, 1, 1])
10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    regressionRegression in behavior compared to a previous versionsystem:32-bitAffects only 32-bit systems

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions