Bug #1792

Fixnum#& 等が、Rational などを受けつける

Added by tadayoshi funaba almost 5 years ago. Updated over 1 year ago.

Status:Closed
Priority:Normal
Assignee:Kenta Murata
Category:-
Target version:2.0.0
ruby -v:- Backport:

Description

=begin
1 & 1.5 がエラーになるので、
1 & Rational(3,2)

1 & BigDecimal('1.5')
もエラーにすべきかもしれません。
=end


Related issues

Related to ruby-trunk - Feature #5310: Integral objects Assigned 09/13/2011

Associated revisions

Revision 33108
Added by Kenta Murata over 2 years ago

  • numeric.c (bit_coerce): A Fixnum and a Bignum are only permitted for bitwise arithmetic with a Fixnum. #1792
  • test/ruby/test_fixnum.rb: add tests for the above change.
  • bignum.c (bit_coerce): A Fixnum and a Bignum are only permitted for bitwise arithmetic with a Bignum. #1792
  • test/ruby/test_bignum.rb: add tests for the above change.

Revision 38560
Added by Kenta Murata over 1 year ago

  • include/ruby/intern.h: add the prototype declaration of
    rbnumcoerce_bit.

  • numeric.c (rbnumcoerce_bit): the new coerce function for bitwise
    binary operation.

  • bignum.c (rbbigand): use coerce to convert the argument, which isn't
    a Fixnum nor a Bignum, to the corresponding Integer object so that
    bitwise operations can support Integer-mimic objects.
    [Bug #1792]

  • bignum.c (rbbigor): ditto.

  • bignum.c (rbbigxor): ditto.

  • numeric.c (bit_coerce): ditto.

  • numeric.c (fix_and): ditto.

  • numeric.c (fix_or): ditto.

  • numeric.c (fix_xor): ditto.

  • test/ruby/test_integer.rb: add tests for the above changes.

  • test/ruby/test_bignum.rb: ditto.

History

#1 Updated by ujihisa . over 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

=begin

=end

#2 Updated by Koichi Sasada almost 3 years ago

これはどうしましょうか.

#3 Updated by Yukihiro Matsumoto almost 3 years ago

  • ruby -v changed from ruby 1.9.2dev (2009-07-19 trunk 24205) [i686-linux] to -

まつもと ゆきひろです

In message "Re: [Ruby 1.9 - Bug #1792] Fixnum#& 等が、Rational などを受けつける"
on Sat, 11 Jun 2011 14:54:43 +0900, Koichi Sasada redmine@ruby-lang.org writes:

|Issue #1792 has been updated by Koichi Sasada.
|
|これはどうしましょうか.

じゃ、エラーってことで。

#4 Updated by Kenta Murata over 2 years ago

これは 1.9.3 にバックポートしても良いですか?

#5 Updated by Motohiro KOSAKI over 2 years ago

これは 1.9.3 にバックポートしても良いですか?

以下はどのくら深刻なのでしょうか。

1 & 1.5 がエラーになるので、
1 & Rational(3,2)

1 & BigDecimal('1.5')
もエラーにすべきかもしれません。

freeze 後に仕様変更的な修正を入れるのは可能なら避けたいという思いがあります。
もし仮に修正漏れがあり、1.9.4で再fixとなると、エンドユーザ視点では1.9.2, 1.9.3, 1.9.4で
それぞれ微妙に挙動が異なることになり迷惑だからです。

しかし、1) 修正に自信がある、または 2) 重要度的に外せれない ということであれば反対はしません。

#6 Updated by Kenta Murata over 2 years ago

  • Status changed from Assigned to Closed

むらたです。

そこまで重要ではないと思うので、バックポートはしないことにします。

#7 Updated by Yui NARUSE over 2 years ago

  • Status changed from Closed to Assigned
  • Assignee changed from Yukihiro Matsumoto to Kenta Murata

これの影響で以下のようなエラーが出ています。
2) Error:
testnum2long(TestNumeric):
TypeError: can't convert Object into Integer for bitwise arithmetic
/home/naruse/ruby/test/ruby/test
numeric.rb:227:in &'
/home/naruse/ruby/test/ruby/test_numeric.rb:227:in
test_num2long'

#8 Updated by Kenta Murata over 2 years ago

  • Status changed from Assigned to Closed

ごめんなさいごめんなさい。
r33113 で修正しました。

#9 Updated by Yui NARUSE over 2 years ago

散発的になってすいませんが、RubySpecも追従よろしくお願いします

#10 Updated by Koichi Sasada over 2 years ago

Is it intentional?

###

class IntegerMimic
def initialize n
@n = n
end

def to_int
@n
end
end

obj = IntegerMimic.new(10)

%w(& | ).each{|sym|
p sym
begin
p(3.send(sym, obj))
rescue => e
p e
end
}

#=>
ruby 1.9.4dev (2011-09-11 trunk 33248) [i386-mswin32_100]
"&"
#
"|"
#
""
#

(2011/08/29 7:25), Kenta Murata wrote:

Issue #1792 has been updated by Kenta Murata.

Status changed from Assigned to Closed

ごめんなさいごめんなさい。
r33113 で修正しました。


Bug #1792: Fixnum#& 等が、Rational などを受けつける
http://redmine.ruby-lang.org/issues/1792

Author: tadayoshi funaba
Status: Closed
Priority: Normal
Assignee: Kenta Murata
Category:
Target version: 1.9.x
ruby -v: -

=begin
1 & 1.5 がエラーになるので、
1 & Rational(3,2)

1 & BigDecimal('1.5')
もエラーにすべきかもしれません。
=end

--
// SASADA Koichi at atdot dot net

#11 Updated by Koichi Sasada over 2 years ago

I find out that bitwise operation doesn't call coerce.
How to make mimic Integer class?

###

class IntegerMimic
def initialize n
@n = n
end

def toint
p :to
int
@n
end

def coerce obj
p :coerce
[obj, @n]
end
end

obj = IntegerMimic.new(10)

%w(+ - * / % & | ).each{|sym|
p sym
begin
p(3.send(sym, obj))
rescue => e
p e
end
}

END
#=>
ruby 1.9.4dev (2011-09-11 trunk 33248) [i386-mswin32_100]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
#
"|"
#
""
#

#=>
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
:toint
2
"|"
:to
int
11
""
:to_int
9

--
// SASADA Koichi at atdot dot net

#12 Updated by Clifford Heath over 2 years ago

On 13/09/2011, at 9:27 AM, SASADA Koichi wrote:

I find out that bitwise operation doesn't call coerce.
How to make mimic Integer class?

The short answer: You can't.

The long answer:

You can imitate some of the behaviour of Integers, but when you
get into hashing and numeric operations, the different Ruby
interpreters all have different shortcuts that prevent your custom
code from being called.

See this post for more information:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

And a discussion thread that I started:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/381087

Clifford Heath.

#13 Updated by Kenta Murata over 2 years ago

  • Status changed from Closed to Open

I reopen this ticket because I rethink is bug.
But, unfortunately, I don't know how to fix this.
Using to_int cannot fix the original problem.
May Numeric#coerce is used for bitwise operation?

#14 Updated by Yukihiro Matsumoto over 2 years ago

Hi,

In message "Re: [Ruby 1.9 - Bug #1792][Open] Fixnum#& 等が、Rational などを受けつける"
on Wed, 14 Sep 2011 13:28:57 +0900, Kenta Murata muraken@gmail.com writes:

|I reopen this ticket because I rethink is bug.
|But, unfortunately, I don't know how to fix this.
|Using to_int cannot fix the original problem.
|May Numeric#coerce is used for bitwise operation?

Using coerce is OK for me. But it should cause NoMethodError when
implemented naively. That might cause confusion.

                        matz.

#15 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#16 Updated by Kenta Murata over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r38560.
tadayoshi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • include/ruby/intern.h: add the prototype declaration of
    rbnumcoerce_bit.

  • numeric.c (rbnumcoerce_bit): the new coerce function for bitwise
    binary operation.

  • bignum.c (rbbigand): use coerce to convert the argument, which isn't
    a Fixnum nor a Bignum, to the corresponding Integer object so that
    bitwise operations can support Integer-mimic objects.
    [Bug #1792]

  • bignum.c (rbbigor): ditto.

  • bignum.c (rbbigxor): ditto.

  • numeric.c (bit_coerce): ditto.

  • numeric.c (fix_and): ditto.

  • numeric.c (fix_or): ditto.

  • numeric.c (fix_xor): ditto.

  • test/ruby/test_integer.rb: add tests for the above changes.

  • test/ruby/test_bignum.rb: ditto.

Also available in: Atom PDF