Project

General

Profile

Actions

Bug #4308

closed

Wrong Bignum calculation in some case.

Bug #4308: Wrong Bignum calculation in some case.

Added by shouichi (Shouichi Kamiya) almost 15 years ago. Updated over 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

Updated by naruse (Yui NARUSE) almost 15 years ago Actions #1

  • 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

Updated by nobu (Nobuyoshi Nakada) almost 15 years ago Actions #2

=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: PDF Atom