I just discovered that vim obviously allows division by zero:

:let a=42/0
:echo a

prints 2147483647 (which is the value of a).

Is this documented somewhere and why does vim allow division by zero?

  • 2
    Try :echo 42/0.0 to see another result :) – VanLaser Sep 2 '15 at 14:13
up vote 8 down vote accepted

This behavior is documented under eval section:

When dividing a Number by zero the result depends on the value:
      0 / 0  = -0x80000000  (like NaN for Float)
     >0 / 0  =  0x7fffffff  (like positive infinity)
     <0 / 0  = -0x7fffffff  (like negative infinity)
    (before Vim 7.2 it was always 0x7fffffff)

Here is why :

42 / 0 tends to +infinity

And how does Vim represent the largest number available ?

2147483647

See :h limits

Furthermore, the float2nr function documentation states :

When the value of {expr} is out of range for a |Number| the
result is truncated to 0x7fffffff or -0x7fffffff.  NaN results
in -0x80000000.

So you have here your 2 numbers : + 2147483647 and - 2147483647.

The last number -2147483648 is used for representing the NaN value.

This is confirmed by the eval section on it (mea culpa: @cuonglm posted it just before me) :

When dividing a Number by zero the result depends on the value:
    0 / 0  = -0x80000000    (like NaN for Float)
   >0 / 0  =  0x7fffffff    (like positive infinity)
   <0 / 0  = -0x7fffffff    (like negative infinity)

As @VanLaser stated, this only work for integer, for floating point number you have more consistency :

 1/0.0     =  inf
 1/0.0 + 1 =  inf
 1/0.0 - 1 =  inf

-1/0.0     = -inf
-1/0.0 - 1 = -inf
-1/0.0 + 1 = -inf
  • In that case, why is division of a negative number by 0 not the minimum number? -> vi.stackexchange.com/questions/4623/… – Jacob Krall Sep 2 '15 at 21:51
  • I've edited my question – nobe4 Sep 3 '15 at 6:30
  • 2147483647 certainly is much closer to zero than to infinity. So, to represent infinity with such a small number doesn't seem helpful, at least not to me. – René Nyffenegger Sep 4 '15 at 13:43

This behavior is useful in Calculus when using something called a Limit.

Lim n -> 0^+ of 1/n = +inf

This can also be written as: As n -> 0^+, 1/n -> +inf

It is read like so.. As n approaches zero from the right, the function 1/n approaches positive infinity.

To see a visual explanation of this reasoning, pop over to http://www.wolframalpha.com/input/?i=limit+n-%3E0+of+1%2Fn

As for Vim-script specifically, AFAIK not many people do much more than logic and integer arithmetic with it. It could be the case that this behavior seemed like a good idea at the time, and is merely a legacy artifact at this point.

  • Do you have a source for the last paragraph? Integer division by zero is undefined in C, any behaviour you see is dependent on the processor, etc. – muru Sep 6 '15 at 16:13
  • Oh, you're right. I'm removing the paragraph. – Shane Sep 6 '15 at 18:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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