Am 8 Oct 2007 um 19:49 hat Dirk Traulsen geschrieben:

> Output:
> =================================================
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] 
.. 
> x.divmod(y)
> -----------
> [6, 0.08]
> [6, 0.00666666666666667]
> [6, 0.0466666666666667]
> [6, 0.0766666666666667]
> [8, 0.00333333333333341]
> =================================================
> 
> As you can see the divisor 7 is wrongly displayed as 6, while the 
> modulos are correct.
> 

I tested Numeric#divmod on two more systems: SUSE 10.1 and Kubuntu 
6.10.
They both run Ruby 1.8.4 and under both systems Numeric#divmod works 
correctly!
So this bug was either introduced between Ruby 1.8.4 and 1.8.6 or it is 
a problem depending on the OS.

Could somebody please confirm these findings? Especially with other 
System and Ruby version combinations this would be really helpful to 
nail down this massive bug. A computer should really calculate 
correctly...

Dirk Traulsen


The testcode:

x=[0.58, 0.59, 0.63, 0.66, 0.67]
y=1/12.0

z1=x.map {|a| a/y}
z2=x.map {|a| a.divmod(y).inspect}

a=[RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM]
puts "ruby #{a[0]} (#{a[1]}) [#{a[2]}]"
puts
print 'x='
p x
puts 'y=1/12.0'
puts
puts "x/y\n---"
puts z1
puts
puts "x.divmod(y)\n-----------"
puts z2


gives under
SUSE 10.1:
===================================================
ruby 1.8.4 (2005-12-24) [i586-linux]

x=[0.58, 0.59, 0.63, 0.66, 0.67]
y=1/12.0

x/y
---
6.96
7.08
7.56
7.92
8.04

x.divmod(y)
-----------
[6.0, 0.08]
[7.0, 0.00666666666666667]
[7.0, 0.0466666666666667]
[7.0, 0.0766666666666667]
[8.0, 0.00333333333333341]
===================================================


and Kubuntu 6.10:
===================================================
ruby 1.8.4 (2005-12-24) [i586-linux]

x=[0.58, 0.59, 0.63, 0.66, 0.67]
y=1/12.0

x/y
---
6.96
7.08
7.56
7.92
8.04

x.divmod(y)
-----------
[6.0, 0.08]
[7.0, 0.00666666666666667]
[7.0, 0.0466666666666667]
[7.0, 0.0766666666666667]
[8.0, 0.00333333333333341]
===================================================