bigdecimal 4.1.1-java → 4.1.2-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 +6 -6
- data/lib/bigdecimal.rb +20 -12
- 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: 44ae86902ea163c37539fb69ed9510c61829bdfbcb6e1ba02f90ca8aa4c25603
|
|
4
|
+
data.tar.gz: c7aa4d33cf57cf5a78f1dd23ffeb3fab30ea423189c77873904c5e059922708b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0183b35444f3360f1a04ca9895efb7fb8d943fff443fe8f08381125b5fa8d3b3270d4b1099d3545c30899ed4cee1dec6f37377cf8f6a9f0dbd5f559b35b4fb51'
|
|
7
|
+
data.tar.gz: f1b848b5219995f13ac18e90cd4cdc8f0a780d4b536984d5c55d41cdefc7eb19571f0e2ac06afda3b1a82fecb1c00f99c11ea50742953ef59f156febfd914840
|
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(
|
|
147
|
+
y = BigDecimal(Math.cbrt(x.to_f), 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(
|
|
307
|
+
y = BigDecimal(Math.atan(x.to_f), 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 =
|
|
608
|
+
xf = x.to_f
|
|
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)
|
|
@@ -637,7 +637,7 @@ module BigMath
|
|
|
637
637
|
return BigDecimal::Internal.nan_computation_result if x.nan?
|
|
638
638
|
return BigDecimal(1 - x.infinite?) if x.infinite?
|
|
639
639
|
return BigDecimal(1).sub(erf(x, prec + BigDecimal::Internal::EXTRA_PREC), prec) if x < 0.5
|
|
640
|
-
return BigDecimal
|
|
640
|
+
return BigDecimal::Internal.underflow_computation_result if x > 5000000000 # erfc(5000000000) < 1e-10000000000000000000 (underflow)
|
|
641
641
|
|
|
642
642
|
if x >= 8
|
|
643
643
|
y = _erfc_asymptotic(x, 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 =
|
|
650
|
+
xf = x.to_f
|
|
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 =
|
|
701
|
+
xf = x.to_f
|
|
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
|
@@ -57,6 +57,13 @@ class BigDecimal
|
|
|
57
57
|
BigDecimal::INFINITY
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def self.underflow_computation_result # :nodoc:
|
|
61
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_UNDERFLOW)
|
|
62
|
+
raise FloatDomainError, 'Exponent underflow'
|
|
63
|
+
end
|
|
64
|
+
BigDecimal(0)
|
|
65
|
+
end
|
|
66
|
+
|
|
60
67
|
def self.nan_computation_result # :nodoc:
|
|
61
68
|
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
|
|
62
69
|
raise FloatDomainError, "Computation results to 'NaN'"
|
|
@@ -76,18 +83,9 @@ class BigDecimal
|
|
|
76
83
|
end
|
|
77
84
|
end
|
|
78
85
|
|
|
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
|
-
|
|
88
86
|
# Calculates Math.log(x.to_f) considering large or small exponent
|
|
89
87
|
def self.float_log(x) # :nodoc:
|
|
90
|
-
Math.log(
|
|
88
|
+
Math.log(x._decimal_shift(-x.exponent).to_f) + x.exponent * Math.log(10)
|
|
91
89
|
end
|
|
92
90
|
|
|
93
91
|
# Calculating Taylor series sum using binary splitting method
|
|
@@ -277,7 +275,7 @@ class BigDecimal
|
|
|
277
275
|
|
|
278
276
|
ex = exponent / 2
|
|
279
277
|
x = _decimal_shift(-2 * ex)
|
|
280
|
-
y = BigDecimal(Math.sqrt(
|
|
278
|
+
y = BigDecimal(Math.sqrt(x.to_f), 0)
|
|
281
279
|
Internal.newton_loop(prec + BigDecimal::Internal::EXTRA_PREC) do |p|
|
|
282
280
|
y = y.add(x.div(y, p), p).div(2, p)
|
|
283
281
|
end
|
|
@@ -359,7 +357,17 @@ module BigMath
|
|
|
359
357
|
prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
|
|
360
358
|
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
|
|
361
359
|
return BigDecimal::Internal.nan_computation_result if x.nan?
|
|
362
|
-
|
|
360
|
+
if x.infinite? || x.exponent >= 21 # exp(10**20) and exp(-10**20) overflows/underflows 64-bit exponent
|
|
361
|
+
if x.positive?
|
|
362
|
+
return BigDecimal::Internal.infinity_computation_result
|
|
363
|
+
elsif x.infinite?
|
|
364
|
+
# exp(-Infinity) is +0 by definition, this is not an underflow.
|
|
365
|
+
return BigDecimal(0)
|
|
366
|
+
else
|
|
367
|
+
return BigDecimal::Internal.underflow_computation_result
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
363
371
|
return BigDecimal(1) if x.zero?
|
|
364
372
|
|
|
365
373
|
# exp(x * 10**cnt) = exp(x)**(10**cnt)
|