-
Notifications
You must be signed in to change notification settings - Fork 34
Description
Example:
x <- set_units(1:10, cm)
br <- set_units(2:4, `in`)
vctrs::vec_cast_common(x, br)
Error:
! Can't convert from `..2` <double> to <integer> due to loss of precision.
• Locations: 1, 2, 3Similarly:
x_m <- set_units(1:10, m)
br_ft <- set_units(2:6, ft)
vctrs::vec_cast_common(x_m, br_ft)
# fails with the same error messageBy contrast:
x <- set_units(1:10, cm)
br_ft2 <- set_units(2:4/12, ft)
vctrs::vec_cast_common(x, br_ft2)works. So does:
> x_in <- set_units(1:10, `in`)
> vctrs::vec_cast_common(x_in, br)and:
x_dbl <- set_units(as.double(1:10), cm)
vctrs::vec_cast_common(x_dbl, br)Lastly, you avoid the bug if the double happens to evaluate to integer values:
x <- set_units(1:10, cm)
br2 <- set_units(2:4 / 2.54, `in`)
vctrs::vec_cast_common(x, br2)
# works
The issue seems to be that units::vec_cast.units.units() recalls vec_cast with the underlying bare data. But if one of these is integer, and the other is double and is not integer-valued (e.g. because it's been converted), that fails.
This may make sense for reasons that I don't understand. It was surprising to me, though.
I think the issue is that vctrs correctly wants to fail if it has to cast a non-integer-valued double to an integer. But in the units case, we can be sure that the integer values are "really" doubles, i.e. they are on the real number line (... right?) If so, maybe a fix would be to add to_bare <- as.Double(to_bare) in vec_cast.units.units().
Package versions: units 0.8.0, vctrs 0.4.1, R 4.2.0.