summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--ext/bigdecimal/bigdecimal.c23
-rw-r--r--test/bigdecimal/test_bigdecimal.rb19
3 files changed, 47 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index da07f5688a9..1cf32b0bcde 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Thu Sep 18 22:35:03 2008 Yusuke Endoh <mame@tsg.ne.jp>
+
+ * ext/bigdecimal/bigdecimal.c (VpCtoV): 1E1000...000 is interpreted as
+ Infinity. [ruby-dev:36159]
+
+ * ext/bigdecimal/bigdecimal.c (VpPower): Infinity ** 1 returns
+ Infinity instead of NaN. [ruby-dev:36159]
+
+ * test/bigdecimal/test_bigdecimal.rb: add tests for above.
+
Thu Sep 18 21:57:32 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_comparable): make ascii8bit string to be
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 754c3199911..3051143b075 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -3944,7 +3944,12 @@ VpCtoV(Real *a, const char *int_chr, U_LONG ni, const char *frac, U_LONG nf, con
es = e*((S_INT)BASE_FIG);
e = e * 10 + exp_chr[i] - '0';
if(es>e*((S_INT)BASE_FIG)) {
- return VpException(VP_EXCEPTION_INFINITY,"exponent overflow",0);
+ VpException(VP_EXCEPTION_INFINITY,"exponent overflow",0);
+ sign = 1;
+ if(int_chr[0] == '-') sign = -1;
+ if(signe > 0) VpSetInf(a, sign);
+ else VpSetZero(a, sign);
+ return 1;
}
++i;
}
@@ -4633,8 +4638,20 @@ VpPower(Real *y, Real *x, S_INT n)
}
goto Exit;
}
- if(!VpIsDef(x)) {
- VpSetNaN(y); /* Not sure !!! */
+ if(VpIsNaN(x)) {
+ VpSetNaN(y);
+ goto Exit;
+ }
+ if(VpIsInf(x)) {
+ if(n==0) {
+ VpSetOne(y);
+ goto Exit;
+ }
+ if(n>0) {
+ VpSetInf(y, (n%2==0 || VpIsPosInf(x)) ? 1 : -1);
+ goto Exit;
+ }
+ VpSetZero(y, (n%2==0 || VpIsPosInf(x)) ? 1 : -1);
goto Exit;
}
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index c3899033e32..e9d1d0bdb6c 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -595,7 +595,18 @@ class TestBigDecimal < Test::Unit::TestCase
assert_equal(BigDecimal.new("-Infinity"), BigDecimal.new("-0") ** -1)
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
assert_equal(true, (BigDecimal.new("NaN") ** 1).nan?)
- assert_equal(true, (BigDecimal.new("Infinity") ** 1).nan?) # OK?
+
+ assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("Infinity") ** 2).sign)
+ assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("Infinity") ** 1).sign)
+ assert_equal(1, BigDecimal.new("Infinity") ** 0)
+ assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("Infinity") ** -1).sign)
+ assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("Infinity") ** -2).sign)
+
+ assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("-Infinity") ** 2).sign)
+ assert_equal(BigDecimal::SIGN_NEGATIVE_INFINITE, (BigDecimal.new("-Infinity") ** 1).sign)
+ assert_equal(1, BigDecimal.new("-Infinity") ** 0)
+ assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (BigDecimal.new("-Infinity") ** -1).sign)
+ assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("-Infinity") ** -2).sign)
end
def test_limit
@@ -676,6 +687,10 @@ class TestBigDecimal < Test::Unit::TestCase
assert_equal(10, BigDecimal.new("1E+1"))
assert_equal(1, BigDecimal.new("+1"))
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
- assert_equal(0, BigDecimal.new("1E1" + "0" * 100)) # OK? must it be inf?
+
+ assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, BigDecimal.new("1E1" + "0" * 10000).sign)
+ assert_equal(BigDecimal::SIGN_NEGATIVE_INFINITE, BigDecimal.new("-1E1" + "0" * 10000).sign)
+ assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, BigDecimal.new("1E-1" + "0" * 10000).sign)
+ assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, BigDecimal.new("-1E-1" + "0" * 10000).sign)
end
end