Project

General

Profile

Actions

Bug #1792

closed

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

Added by tadf (tadayoshi funaba) almost 15 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
ruby -v:
-
Backport:

Description

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

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


Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #5310: Integral objectsAssignedmrkn (Kenta Murata)Actions
Actions #1

Updated by ujihisa (Tatsuhiro Ujihisa) over 14 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Updated by ko1 (Koichi Sasada) almost 13 years ago

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

Updated by matz (Yukihiro Matsumoto) almost 13 years ago

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

まつもと ゆきひろです

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

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

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

Updated by mrkn (Kenta Murata) over 12 years ago

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

Updated by kosaki (Motohiro KOSAKI) over 12 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) 重要度的に外せれない ということであれば反対はしません。

Updated by mrkn (Kenta Murata) over 12 years ago

  • Status changed from Assigned to Closed

むらたです。

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

Updated by naruse (Yui NARUSE) over 12 years ago

  • Status changed from Closed to Assigned
  • Assignee changed from matz (Yukihiro Matsumoto) to mrkn (Kenta Murata)

これの影響で以下のようなエラーが出ています。
2) Error:
test_num2long(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'

Updated by mrkn (Kenta Murata) over 12 years ago

  • Status changed from Assigned to Closed

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

Updated by naruse (Yui NARUSE) over 12 years ago

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

Updated by ko1 (Koichi Sasada) over 12 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]
"&"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"|"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"^"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>

(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

Updated by ko1 (Koichi Sasada) over 12 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 to_int
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
"&"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"|"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"^"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>

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

--
// SASADA Koichi at atdot dot net

Updated by cjheath (Clifford Heath) over 12 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.

Updated by mrkn (Kenta Murata) over 12 years ago

  • Status changed from Closed to Open

I reopen this ticket because I rethink [ruby-core:39491] 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?

Updated by matz (Yukihiro Matsumoto) over 12 years ago

Hi,

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

|I reopen this ticket because I rethink [ruby-core:39491] 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.
Actions #15

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned
Actions #16

Updated by mrkn (Kenta Murata) over 11 years 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
    rb_num_coerce_bit.

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

  • bignum.c (rb_big_and): 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] [ruby-core:39491]

  • bignum.c (rb_big_or): ditto.

  • bignum.c (rb_big_xor): 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0