Skip to content

Wrong result for sum of SparseVector with UInt indices #285

@kalmarek

Description

@kalmarek
julia> using SparseArrays

julia> x = SparseVector{Int, UInt8}(6, UInt8[1], [-1])
6-element SparseVector{Int64, UInt8} with 1 stored entry:
  [1]  =  -1

julia> sum(x)
-1

julia> versioninfo()
Julia Version 1.6.7
Commit 3b76b25b64 (2022-07-19 15:11 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: AMD Ryzen 7 PRO 4750U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, znver2)
Environment:
  JULIA_NUM_THREADS = 8

On the other hand:

julia> using SparseArrays

julia> x = SparseVector{Int, UInt8}(6, UInt8[1], [-1])
6-element SparseVector{Int64, UInt8} with 1 stored entry:
  [1]  =  -1

julia> sum(x)
0xffffffffffffffff

julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 7 PRO 4750U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, znver2)
  Threads: 8 on 16 virtual cores
Environment:
  JULIA_NUM_THREADS = 8

This is because on julia-1.6.7 size(x) is a Tuple{Int64} but on julia-1.8.2 it's Tuple{UInt8}.

as a bonus now sum is type unstable:

julia> x = SparseVector{Int, UInt8}(6, UInt8[1], [-1]); sum(x)
0xffffffffffffffff

julia> x = SparseVector{Int, UInt8}(1, UInt8[1], [-1]); sum(x)
-1

I understand that preserving the type of indices is useful for a very long sp vectors as per
#262
so I don't have a good solution for this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions