[Ruby Problem Report] Strange behaviour of class Fixnum in exponentation

  • Thread starter Wolfgang Nádasi-Donner
  • Start date
W

Wolfgang Nádasi-Donner

The german Ruby forum recognized a strange behaviour of the Fixnum class.

In exponentation Fixnum is much slower than Bignum, which lets us assume, that some kind of a problem occurs
in the implementation. Example:

require 'benchmark'

N = 10**6
B = 65891264863465298234965902602612457060348499377
F = 4628

Benchmark.bmbm(10) do |bm|
bm.report('Bignum') do N.times do
B ** 0.5
end end

bm.report('Fixnum') do N.times do
F ** 0.5
end end
end
user system total real

Bignum 3.856000 0.000000 3.856000 ( 3.946000)
Fixnum 7.861000 0.020000 7.881000 ( 8.081000)

Further investigations leads to the responsible C code:

static VALUE
fix_pow(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
long a, b;

b = FIX2LONG(y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
a = FIX2LONG(x);
if (b > 0) {
return rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
}
return rb_num_coerce_bin(x, y); // <-- das dauert
}

/* vs. */
VALUE
rb_big_pow(x, y)
VALUE x, y;
{
double d;
long yy;

if (y == INT2FIX(0)) return INT2FIX(1);
switch (TYPE(y)) {
case T_FLOAT: // <-- das geht schnell
d = RFLOAT(y)->value;
break;

case T_BIGNUM:
...

case T_FIXNUM:
...

default:
return rb_num_coerce_bin(x, y);
}
return rb_float_new(pow(rb_big2dbl(x), d));
}

Is it possible to put this Mail to right place for "Ruby Problem Reports". I didn't find out the right place.

Best regards, Wolfgang Nádasi-Donner
 
D

dm1

Wolfgang said:
The german Ruby forum recognized a strange behaviour of the Fixnum class.

In exponentation Fixnum is much slower than Bignum, which lets us assume,
that some kind of a problem occurs in the implementation. Example:

Is it possible to put this Mail to right place for "Ruby Problem Reports".
I didn't find out the right place.

Best regards, Wolfgang Nádasi-Donner

The right place for ruby bug reports is on Rubyforge. There is a 'Report a
Ruby Bug' link on the welcome page (top right). Here is the direct link:

http://rubyforge.org/tracker/?atid=1698&group_id=426&func=browse

Best regards

Denis
 
W

Wolfgang Nádasi-Donner

Wolfgang said:
The right place for ruby bug reports is on Rubyforge. There is a 'Report a
Ruby Bug' link on the welcome page (top right). Here is the direct link:

http://rubyforge.org/tracker/?atid=1698&group_id=426&func=browse

Best regards

Denis

The Problem is, that this page, as well as several others, require a login. I want nothing else than report
this strange behaviour to someone who is responsible an not get a new account in some place.

Best regards, Wolfgang Nádasi-Donner
 
A

Austin Ziegler

The Problem is, that this page, as well as several others, require a logi= n. I want
nothing else than report this strange behaviour to someone who is respons= ible and
not get a new account in some place.

1. Having an account on RubyForge is generally a good idea if you're
going to be working with
Ruby at all.

2. You can send bug reports directly to ruby-core (at) ruby-lang.org
without any problems,
although you may need to subscribe to post. I don't remember.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
C

Charles Mills

Wolfgang said:
The german Ruby forum recognized a strange behaviour of the Fixnum class.

In exponentation Fixnum is much slower than Bignum, which lets us
assume, that some kind of a problem occurs
in the implementation. Example:


require 'benchmark'

N = 10**6
B = 65891264863465298234965902602612457060348499377
F = 4628

Benchmark.bmbm(10) do |bm|
bm.report('Bignum') do N.times do
B ** 0.5
end end

bm.report('Fixnum') do N.times do
F ** 0.5
end end
end

user system total real

Bignum 3.856000 0.000000 3.856000 ( 3.946000)
Fixnum 7.861000 0.020000 7.881000 ( 8.081000)


Further investigations leads to the responsible C code:


static VALUE
fix_pow(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
long a, b;

b = FIX2LONG(y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
a = FIX2LONG(x);
if (b > 0) {
return rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
}
return rb_num_coerce_bin(x, y); // <-- das dauert
}

You chould change the above code to and it should solve the speed
problem. untested code:

static VALUE
fix_pow(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
long a, b;

b = FIX2LONG(y);
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
a = FIX2LONG(x);
if (b > 0) {
return rb_big_pow(rb_int2big(a), y);
}
return rb_float_new(pow((double)a, (double)b));
} else if (TYPE(y) == T_FLOAT) {
long a = FIX2LONG(x);
return rb_float_new(pow((double)a, RFLOAT(y)->value));
}
return rb_num_coerce_bin(x, y); // <-- das dauert
}

-Charlie
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Strange behaviour of class Fixnum in exponentation"

|You chould change the above code to and it should solve the speed
|problem. untested code:

I will merge this code. Thank you!

matz.
 
C

Charles Mills

Yukihiro said:
Hi,

In message "Re: Strange behaviour of class Fixnum in exponentation"
|You chould change the above code to and it should solve the speed
|problem. untested code:

I will merge this code. Thank you!

matz.

Cool. Your welcome.

Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top