17

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?

1
  • 2
    Try :echo 42/0.0 to see another result :)
    – VanLaser
    Sep 2, 2015 at 14:13

3 Answers 3

9

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)
15

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
3
  • In that case, why is division of a negative number by 0 not the minimum number? -> vi.stackexchange.com/questions/4623/… Sep 2, 2015 at 21:51
  • I've edited my question
    – nobe4
    Sep 3, 2015 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. Sep 4, 2015 at 13:43
2

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.

2
  • 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, 2015 at 16:13
  • Oh, you're right. I'm removing the paragraph.
    – Shane
    Sep 6, 2015 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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