In memory, the way integers are stored is:
- first 0L
- then positive integers (ascending)
- then
NA_integer_
- then negative integers (ascending)
So I wouldn't say that NA_integer_
is stored as the minimal integer, even if .Internal(inspect(NA_integer_))
suggests it. Maybe better to say it is stored where other languages usually store the minimal integer?
Look at the 4 last elements of each vector below.
# first zero
serialize(0L, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 00 00 00 00
# then positive integers (ascending)
serialize(1L, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 00 00 00 01
serialize(.Machine$integer.max, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 7f ff ff ff
# then NA_integer_
serialize(NA_integer_, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 80 00 00 00
# then negative integers (ascending, i.e. descending in absolute value)
serialize(-.Machine$integer.max, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 80 00 00 01
serialize(-1L, connection = NULL)
#> [1] 58 0a 00 00 00 03 00 04 04 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#> [26] 00 0d 00 00 00 01 ff ff ff ff
Created on 2024-10-09 with reprex v2.1.0