raise ZeroDivisionError instead of crashing. · Pull Request #492 · mruby/mruby
a) On OpenBSD/amd64, mruby dumps core when we call Integer#divmod with 0:
% bin/mruby -e 'p 1.divmod(0)'
zsh: floating point exception (core dumped) bin/mruby -e 'p 1.divmod(0)'
In contrast, CRuby raises an exception:
% ruby -e 'p 1.divmod(0)'
-e:1:in `divmod': divided by 0 (ZeroDivisionError)
from -e:1:in `<main>'
%
Integer#divmod is not defined in ISO Ruby but CRuby's behavior looks reasonable.
b) Integer#% and Float#% may return NaN:
% bin/mruby -e 'p 1 % 0'
NaN
% bin/mruby -e 'p 1.0 % 0.0'
NaN
But ISO Ruby says that "i % 0" should raise a ZeroDivisionError exception. CRuby implements that.
This pull-request makes Fixnum#%, Float#% and Fixnum#divmod compatible with CRuby.
- 1.1 % 0.0 = NaN
- 1 % 0.0 => ZeroDivisionError
- 1 % 0 => ZeroDivisionError
- 0 % 0 => ZeroDivisionError
- 0.0 % 0 => ZeroDivisionError
- 0 % 0.0 => ZeroDivisionError
- 1.divmod(0) => ZeroDivisionError
- 0.divmod(0) => ZeroDivisionError