bigdecimal 4.1.0-java → 4.1.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bigdecimal/math.rb +5 -5
- data/lib/bigdecimal.rb +11 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 508de7de8109fd323385fff22a46cc75894ab6f8bb2ed1072e35417d19ea5165
|
|
4
|
+
data.tar.gz: 9078b415245017a30f888495a35bb204f28c794ec0adf1548f4ecc763004e72e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3cd67b017f9596717fbe4afe6b8f962d607f858c41a9f8fdea6be237d810fcbe6d9f6600a5d7f137f3f459382603f2ae13a11a9f87254bbffce447ca2e4e5742
|
|
7
|
+
data.tar.gz: 3eb505bcd0ae9de7e7ae5090856ec8fdfa439b485d2c3ed63e448fb369e24fefc1cd37b3c1cadaf7e5798444952aea6f0db968ef182323f5419019cb46520894
|
data/lib/bigdecimal/math.rb
CHANGED
|
@@ -144,7 +144,7 @@ module BigMath
|
|
|
144
144
|
x = -x if neg = x < 0
|
|
145
145
|
ex = x.exponent / 3
|
|
146
146
|
x = x._decimal_shift(-3 * ex)
|
|
147
|
-
y = BigDecimal(Math.cbrt(x
|
|
147
|
+
y = BigDecimal(Math.cbrt(BigDecimal::Internal.fast_to_f(x)), 0)
|
|
148
148
|
BigDecimal::Internal.newton_loop(prec + BigDecimal::Internal::EXTRA_PREC) do |p|
|
|
149
149
|
y = (2 * y + x.div(y, p).div(y, p)).div(3, p)
|
|
150
150
|
end
|
|
@@ -304,7 +304,7 @@ module BigMath
|
|
|
304
304
|
|
|
305
305
|
# Solve tan(y) - x = 0 with Newton's method
|
|
306
306
|
# Repeat: y -= (tan(y) - x) * cos(y)**2
|
|
307
|
-
y = BigDecimal(Math.atan(x
|
|
307
|
+
y = BigDecimal(Math.atan(BigDecimal::Internal.fast_to_f(x)), 0)
|
|
308
308
|
BigDecimal::Internal.newton_loop(n) do |p|
|
|
309
309
|
s = sin(y, p)
|
|
310
310
|
c = (1 - s * s).sqrt(p)
|
|
@@ -605,7 +605,7 @@ module BigMath
|
|
|
605
605
|
return BigDecimal(1) if x > 5000000000 # erf(5000000000) > 1 - 1e-10000000000000000000
|
|
606
606
|
|
|
607
607
|
if x > 8
|
|
608
|
-
xf = x
|
|
608
|
+
xf = BigDecimal::Internal.fast_to_f(x)
|
|
609
609
|
log10_erfc = -xf ** 2 / Math.log(10) - Math.log10(xf * Math::PI ** 0.5)
|
|
610
610
|
erfc_prec = [prec + log10_erfc.ceil, 1].max
|
|
611
611
|
erfc = _erfc_asymptotic(x, erfc_prec)
|
|
@@ -647,7 +647,7 @@ module BigMath
|
|
|
647
647
|
# erfc(x) = 1 - erf(x) < exp(-x**2)/x/sqrt(pi)
|
|
648
648
|
# Precision of erf(x) needs about log10(exp(-x**2)/x/sqrt(pi)) extra digits
|
|
649
649
|
log10 = 2.302585092994046
|
|
650
|
-
xf = x
|
|
650
|
+
xf = BigDecimal::Internal.fast_to_f(x)
|
|
651
651
|
high_prec = prec + BigDecimal::Internal::EXTRA_PREC + ((xf**2 + Math.log(xf) + Math.log(Math::PI)/2) / log10).ceil
|
|
652
652
|
BigDecimal(1).sub(erf(x, high_prec), prec)
|
|
653
653
|
end
|
|
@@ -698,7 +698,7 @@ module BigMath
|
|
|
698
698
|
# sqrt(2)/2 + k*log(k) - k - 2*k*log(x) < -prec*log(10)
|
|
699
699
|
# and the left side is minimized when k = x**2.
|
|
700
700
|
prec += BigDecimal::Internal::EXTRA_PREC
|
|
701
|
-
xf = x
|
|
701
|
+
xf = BigDecimal::Internal.fast_to_f(x)
|
|
702
702
|
kmax = (1..(xf ** 2).floor).bsearch do |k|
|
|
703
703
|
Math.log(2) / 2 + k * Math.log(k) - k - 2 * k * Math.log(xf) < -prec * Math.log(10)
|
|
704
704
|
end
|
data/lib/bigdecimal.rb
CHANGED
|
@@ -76,9 +76,18 @@ class BigDecimal
|
|
|
76
76
|
end
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
# Fast and rough conversion to float for mathematical calculations.
|
|
80
|
+
# Bigdecimal#to_f is slow when n_significant_digits is large.
|
|
81
|
+
# This is because to_f internally converts BigDecimal to String
|
|
82
|
+
# to get the exact nearest float representation.
|
|
83
|
+
# TODO: Remove this workaround when BigDecimal#to_f is optimized.
|
|
84
|
+
def self.fast_to_f(x) # :nodoc:
|
|
85
|
+
x.n_significant_digits < 40 ? x.to_f : x.mult(1, 20).to_f
|
|
86
|
+
end
|
|
87
|
+
|
|
79
88
|
# Calculates Math.log(x.to_f) considering large or small exponent
|
|
80
89
|
def self.float_log(x) # :nodoc:
|
|
81
|
-
Math.log(x._decimal_shift(-x.exponent)
|
|
90
|
+
Math.log(fast_to_f(x._decimal_shift(-x.exponent))) + x.exponent * Math.log(10)
|
|
82
91
|
end
|
|
83
92
|
|
|
84
93
|
# Calculating Taylor series sum using binary splitting method
|
|
@@ -268,7 +277,7 @@ class BigDecimal
|
|
|
268
277
|
|
|
269
278
|
ex = exponent / 2
|
|
270
279
|
x = _decimal_shift(-2 * ex)
|
|
271
|
-
y = BigDecimal(Math.sqrt(x
|
|
280
|
+
y = BigDecimal(Math.sqrt(BigDecimal::Internal.fast_to_f(x)), 0)
|
|
272
281
|
Internal.newton_loop(prec + BigDecimal::Internal::EXTRA_PREC) do |p|
|
|
273
282
|
y = y.add(x.div(y, p), p).div(2, p)
|
|
274
283
|
end
|