Module: Distribution::MathExtension::ApproxFactorial
- Defined in:
- lib/distribution/math_extension.rb
Overview
Module to calculate approximated factorial Based (again) on Luschny formula, with 16 digits of precision
Reference
Class Method Summary collapse
-
.stieltjes_factorial(x) ⇒ Object
Valid upto 11 digits.
- .stieltjes_ln_factorial(z) ⇒ Object
- .stieltjes_ln_factorial_big(z) ⇒ Object
Class Method Details
.stieltjes_factorial(x) ⇒ Object
Valid upto 11 digits
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/distribution/math_extension.rb', line 170 def self.stieltjes_factorial(x) y = x; _p = 1; while y < 8 do _p = _p*y; y = y+1 end lr= stieltjes_ln_factorial(y) r = Math.exp(lr) #puts "valid: #{5/2.0+(13/2.0)*Math::log(x)}" if r.infinite? r=BigMath.exp(BigDecimal(lr.to_s),20) r = (r*x) / (_p*y) if x < 8 r=r.to_i else r = (r*x) / (_p*y) if x < 8 end r end |
.stieltjes_ln_factorial(z) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/distribution/math_extension.rb', line 146 def self.stieltjes_ln_factorial(z) a0 = 1.quo(12); a1 = 1.quo(30); a2 = 53.quo(210); a3 = 195.quo(371); a4 = 22999.quo(22737); a5 = 29944523.quo(19733142); a6 = 109535241009.quo(48264275462); zz = z+1; (1.quo(2))*Math.log(2*Math::PI)+(zz-1.quo(2))*Math.log(zz) - zz + a0.quo(zz+a1.quo(zz+a2.quo(zz+a3.quo(zz+a4.quo(zz+a5.quo(zz+a6.quo(zz))))))) end |
.stieltjes_ln_factorial_big(z) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/distribution/math_extension.rb', line 157 def self.stieltjes_ln_factorial_big(z) a0 = 1/12.0; a1 = 1/30.0; a2 = 53/210.0; a3 = 195/371.0; a4 = 22999/22737.0; a5 = 29944523/19733142.0; a6 = 109535241009/48264275462.0; zz = z+1; BigDecimal("0.5") * BigMath.log(BigDecimal("2")*BigMath::PI(20),20) + BigDecimal((zz - 0.5).to_s) * BigMath.log(BigDecimal(zz.to_s),20) - BigDecimal(zz.to_s) + BigDecimal( ( a0 / (zz+a1/(zz+a2/(zz+a3/(zz+a4/(zz+a5/(zz+a6/zz)))))) ).to_s) end |