bigdecimal 4.1.0 → 4.1.2
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/ext/bigdecimal/bigdecimal.c +180 -237
- data/ext/bigdecimal/bigdecimal.h +6 -1
- data/ext/bigdecimal/div.h +13 -13
- data/ext/bigdecimal/extconf.rb +4 -0
- data/ext/bigdecimal/missing/dtoa.c +184 -137
- data/ext/bigdecimal/ntt.h +5 -5
- data/lib/bigdecimal/math.rb +1 -1
- data/lib/bigdecimal.rb +18 -1
- metadata +1 -1
data/ext/bigdecimal/ntt.h
CHANGED
|
@@ -68,7 +68,7 @@ ntt(int size_bits, uint32_t *input, uint32_t *output, uint32_t *tmp, int r_base,
|
|
|
68
68
|
|
|
69
69
|
// rmax**(1 << shift) % prime == 1
|
|
70
70
|
// r**size % prime == 1
|
|
71
|
-
uint32_t rmax = mod_pow(r_base, base, prime);
|
|
71
|
+
uint32_t rmax = mod_pow((uint32_t)r_base, (uint32_t)base, prime);
|
|
72
72
|
uint32_t r = mod_pow(rmax, (uint32_t)1 << (shift - size_bits), prime);
|
|
73
73
|
|
|
74
74
|
if (dir < 0) r = mod_pow(r, prime - 2, prime);
|
|
@@ -123,7 +123,7 @@ ntt_multiply(size_t a_size, size_t b_size, uint32_t *a, uint32_t *b, uint32_t *c
|
|
|
123
123
|
return;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
int ntt_size_bits = bit_length(b_size - 1) + 1;
|
|
126
|
+
int ntt_size_bits = (int)bit_length(b_size - 1) + 1;
|
|
127
127
|
if (ntt_size_bits > MAX_NTT32_BITS) {
|
|
128
128
|
rb_raise(rb_eArgError, "Multiply size too large");
|
|
129
129
|
}
|
|
@@ -133,7 +133,7 @@ ntt_multiply(size_t a_size, size_t b_size, uint32_t *a, uint32_t *b, uint32_t *c
|
|
|
133
133
|
uint32_t batch_size = ntt_size - (uint32_t)b_size;
|
|
134
134
|
uint32_t batch_count = (uint32_t)((a_size + batch_size - 1) / batch_size);
|
|
135
135
|
|
|
136
|
-
uint32_t *mem = ruby_xcalloc(
|
|
136
|
+
uint32_t *mem = ruby_xcalloc(ntt_size * 9, sizeof(uint32_t));
|
|
137
137
|
uint32_t *ntt1 = mem;
|
|
138
138
|
uint32_t *ntt2 = mem + ntt_size;
|
|
139
139
|
uint32_t *ntt3 = mem + ntt_size * 2;
|
|
@@ -177,12 +177,12 @@ ntt_multiply(size_t a_size, size_t b_size, uint32_t *a, uint32_t *b, uint32_t *c
|
|
|
177
177
|
// so this sum doesn't overflow uint32_t.
|
|
178
178
|
for (int j = 0; j < 3; j++) {
|
|
179
179
|
// Index check: if dig[j] is non-zero, assign index is within valid range.
|
|
180
|
-
if (dig[j]) c[idx * batch_size + i + 1 - j] += dig[j];
|
|
180
|
+
if (dig[j]) c[idx * batch_size + i + 1 - (uint32_t)j] += dig[j];
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
184
|
uint32_t carry = 0;
|
|
185
|
-
for (int32_t i = (
|
|
185
|
+
for (int32_t i = (int32_t)(a_size + b_size - 1); i >= 0; i--) {
|
|
186
186
|
uint32_t v = c[i] + carry;
|
|
187
187
|
c[i] = v % NTT_DECDIG_BASE;
|
|
188
188
|
carry = v / NTT_DECDIG_BASE;
|
data/lib/bigdecimal/math.rb
CHANGED
|
@@ -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)
|
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'"
|
|
@@ -350,7 +357,17 @@ module BigMath
|
|
|
350
357
|
prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
|
|
351
358
|
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
|
|
352
359
|
return BigDecimal::Internal.nan_computation_result if x.nan?
|
|
353
|
-
|
|
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
|
+
|
|
354
371
|
return BigDecimal(1) if x.zero?
|
|
355
372
|
|
|
356
373
|
# exp(x * 10**cnt) = exp(x)**(10**cnt)
|