In R, zero has a latent sign: +0 and -0
print the same but behave differently under reciprocation:
When a numeric series contains zero, that sign can matter for derived series (for example, after inversion).
Support for signed zero is included in the discretes package so that the proper signed infinity results by inversion:
x <- arithmetic(0, 3, n_left = 1, n_right = 1)
x
#> Arithmetic series of length 3:
#> -3, 0, 3
1 / x
#> Reciprocal series of length 3:
#> -0.3333333, 0.3333333, Inf
1 / -x
#> Reciprocal series of length 3:
#> -Inf, -0.3333333, 0.3333333The functions has_negative_zero() and
has_positive_zero() report whether -0 or
+0 is a discrete value in the series. For example, the
integers contain a single positive zero:
Rules around signed zero follow those of base R. For example, negation can induce negative zeroes (although note that, in R, -0 is only possible as type “double” and not of type “integer”).
Like numeric vectors, a series can contain both signs of zero:
x <- dsct_union(-0, 0)
x
#> Union series of length 1:
#> 0
has_positive_zero(x)
#> [1] TRUE
has_negative_zero(x)
#> [1] TRUEHowever, since they are both identical values, only one zero is
enumerated by next_discrete(),
prev_discrete(), or get_discretes_*():
But, both signs remain latent and appear when the series is inverted:
When choosing to pull positive or negative zero from the series:
base::unique().dsct_union(), the zero from
the first series that has a zero is taken.