r-lib/sparsevctrs

Fix bug to do with rounding when materializing

Closed this issue · 2 comments

library(sparsevctrs)

sparse_double(1, 1, 10)[] + 1
#>  [1] 2 1 1 1 1 1 1 1 1 1

sparse_double(1, 1, 10) + 1
#>  [1] 3.448243e-314 2.673252e-314 2.681506e-314 2.546673e-314 4.940656e-323
#>  [6]  0.000000e+00  2.000000e+00  1.000000e+00  1.000000e+00  1.000000e+00

round(sparse_double(1, 1, 10) + 1)
#>  [1] 2 1 1 1 1 1 1 1 1 1

Function responsible: https://github.com/EmilHvitfeldt/sparsevctrs/blob/main/src/altrep-sparse-double.c#L15-L55

it gets weirder

library(sparsevctrs)

sparse_double(1, 1, 10) + 1
#>  [1] 3.448243e-314 2.536478e-314 2.409270e-314 2.416748e-314 4.940656e-323
#>  [6]  0.000000e+00  2.000000e+00  1.000000e+00  1.000000e+00  1.000000e+00

x <- sparse_double(1, 1, 10)
x + 1
#>  [1] 2 1 1 1 1 1 1 1 1 1

Created on 2024-05-13 with reprex v2.0.2

more weirdness

library(sparsevctrs)

# remove the recycling and it works
sparse_double(1, 1, 10) + c(1,2,3,4,5,6,7,8,9,10)
#>  [1]  2  2  3  4  5  6  7  8  9 10

# flip it around and it doesnt
c(1,2,3,4,5,6,7,8,9,10) + sparse_double(1, 1, 10)
#>  [1] 3.448243e-314 2.635947e-314 2.719385e-314 2.638648e-314 4.940656e-323
#>  [6]  0.000000e+00  2.000000e+00  2.000000e+00  3.000000e+00  4.000000e+00

Created on 2024-05-13 with reprex v2.0.2