2

I want to figure out the bit-width of the built-in Number (integer actually) of Vimscript . In particular, I want to tell whether a 32-bit or 64-bit integer is in use. Currently I test whether the maximum constants of both widths can be represented:

let s:INT32_MAX = 2147483647 " 2^31-1
let s:INT64_MAX = 9223372036854775807 " 2^63-1

if s:INT64_MAX > 0
  let s:INT_WIDTH = 64
  let s:INT_MAX = s:INT64_MAX
else
  let s:INT_WIDTH = 32
  let s:INT_MAX = s:INT32_MAX
endif
let s:INT_MIN = -s:INT_MAX - 1

This approach has the annoying magic numbers in code. Although it can be replaced by float2nr(pow(2, 31)), which is exactly s:INT_MAX above, this requires has('float'). I wonder if there is any feature that can be tested like has('num64'). I searched through the :h +feature-list but found nothing more relevant than OS information like +win32. Any insight on this question is appreciated.

2

You can directly test for the 64-bit number support feature using:

has('num64')

The minimum and maximum numbers can be obtained using 0/0 and 1/0 respectively:

let VARNUM_MIN = 0/0
let VARNUM_MAX = 1/0

To get the bit-depth of these numbers, you can use printf with a binary format.

let bits = len(printf('%b', 1/0))         " returns 63
let bits_nan = len(printf('%b', 0/0))     " returns 64

You can just check if bits is greater than 32, or less than or equal to 32.

  • The 0/0 and 1/0 are nice shots. But the printf('%b', 1/0) did not work for me. I got E767 too many arguments to printf(). Maybe there is no %b specifier in my version of Vim? – cgsdfc Jun 11 '18 at 16:48
  • Anyway, is there really no has() way in Vim? – cgsdfc Jun 11 '18 at 16:52
  • Use %x is ok. 4 * len(printf('%x', 1/0)). – cgsdfc Jun 11 '18 at 16:53
  • Yes there is has('num64'). – Mass Jun 11 '18 at 16:54
  • Anyway, is there really 64-bit Number in VimL? The help says vaguely that a number can be 64-bit or 32-bit. – cgsdfc Jun 11 '18 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.