Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Oct 16, 2012

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

@ghost ghost mentioned this pull request Oct 16, 2012
@matz
Copy link
Member

matz commented Oct 16, 2012

Since float numbers and integers are more interchangeable in mruby than CRuby, "1 % 0" and "1.0 % 0" should give the same result. I am not sure which way we should go. Let me think for a while.

@matz matz closed this in 1bf70ea Oct 16, 2012
@matz
Copy link
Member

matz commented Oct 16, 2012

I decided to fix it other way around. 1.divmod(0) returns [Inf,NaN] now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants