Project

General

Profile

Actions

Bug #4308

closed

Wrong Bignum calculation in some case.

Added by shouichi (Shouichi Kamiya) over 14 years ago. Updated about 14 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.5.0]
Backport:
[ruby-core:34809]

Description

=begin
I tried to calculate variance of an array but calculation returns wrong result
when numbers are big.The problem also happens in 1.8.7-p330.

I attached a file that recreates The problem.
=end


Files

wrong_bignum_calc.rb (514 Bytes) wrong_bignum_calc.rb shouichi (Shouichi Kamiya), 01/24/2011 06:59 AM
Actions #1

Updated by naruse (Yui NARUSE) over 14 years ago

  • Status changed from Open to Rejected

=begin
It seems from floating point's accuracy.
In line 9, it uses to_f and the value exceeds its limit.
=end

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 14 years ago

=begin
#!/usr/bin/env ruby

class Array
def sum
reduce(:+)
end

def var
empty? ? 0.0 : (map {|x| x ** 2}.sum * size - sum ** 2).fdiv(size ** 2)
end
end

All of these should clearly return 0.

p [1295218865, 1295218865, 1295218865, 1295218865, 1295218865].var
#=> 0.0

p [1295218865, 1295218865, 1295218865, 1295218865, 1295218865, 1295218865].var
#=> 0.0

p [1295218865, 1295218865, 1295218865, 1295218865, 1295218865, 1295218865, 1295218865].var
#=> 0.0

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0