mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a
BigDecimal object from an Integer. * test/bigdecimal/test_bigdecimal.rb (test_new_with_integer): add for testing the above change. * ext/bigdecimal/bigdecimal.c (BigDecimal_global_new): replace its body with a BigDecimal_new call. * test/bigdecimal/test_bigdecimal.rb (test_global_new_with_integer): add for testing the above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
899d2c14b0
commit
b54141bb1b
3 changed files with 66 additions and 34 deletions
14
ChangeLog
14
ChangeLog
|
@ -1,3 +1,17 @@
|
||||||
|
Tue May 31 22:44:00 2011 Kenta Murata <mrkn@mrkn.jp>
|
||||||
|
|
||||||
|
* ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a
|
||||||
|
BigDecimal object from an Integer.
|
||||||
|
|
||||||
|
* test/bigdecimal/test_bigdecimal.rb (test_new_with_integer):
|
||||||
|
add for testing the above change.
|
||||||
|
|
||||||
|
* ext/bigdecimal/bigdecimal.c (BigDecimal_global_new): replace its body
|
||||||
|
with a BigDecimal_new call.
|
||||||
|
|
||||||
|
* test/bigdecimal/test_bigdecimal.rb (test_global_new_with_integer):
|
||||||
|
add for testing the above change.
|
||||||
|
|
||||||
Tue May 31 22:24:39 2011 Tadayoshi Funaba <tadf@dotrb.org>
|
Tue May 31 22:24:39 2011 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
* ext/date/date_core.c: use simple/complex mode instead of light/right mode.
|
* ext/date/date_core.c: use simple/complex mode instead of light/right mode.
|
||||||
|
|
|
@ -1738,33 +1738,17 @@ BigDecimal_power(VALUE self, VALUE p)
|
||||||
return ToValue(y);
|
return ToValue(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
/* call-seq:
|
||||||
BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
|
|
||||||
{
|
|
||||||
ENTER(5);
|
|
||||||
Real *pv;
|
|
||||||
size_t mf;
|
|
||||||
VALUE nFig;
|
|
||||||
VALUE iniValue;
|
|
||||||
|
|
||||||
if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
|
|
||||||
mf = 0;
|
|
||||||
} else {
|
|
||||||
mf = GetPositiveInt(nFig);
|
|
||||||
}
|
|
||||||
SafeStringValue(iniValue);
|
|
||||||
GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
|
|
||||||
return ToValue(pv);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* new(initial, digits)
|
* new(initial, digits)
|
||||||
*
|
*
|
||||||
* Create a new BigDecimal object.
|
* Create a new BigDecimal object.
|
||||||
*
|
*
|
||||||
* initial:: The initial value, as a String. Spaces are ignored, unrecognized characters terminate the value.
|
* initial:: The initial value, as a String. Spaces are ignored, unrecognized
|
||||||
|
* characters terminate the value.
|
||||||
*
|
*
|
||||||
* digits:: The number of significant digits, as a Fixnum. If omitted or 0, the number of significant digits is determined from the initial value.
|
* digits:: The number of significant digits, as a Fixnum. If omitted or 0,
|
||||||
|
* the number of significant digits is determined from the initial
|
||||||
|
* value.
|
||||||
*
|
*
|
||||||
* The actual number of significant digits used in computation is usually
|
* The actual number of significant digits used in computation is usually
|
||||||
* larger than the specified number.
|
* larger than the specified number.
|
||||||
|
@ -1778,16 +1762,36 @@ BigDecimal_new(int argc, VALUE *argv, VALUE self)
|
||||||
VALUE nFig;
|
VALUE nFig;
|
||||||
VALUE iniValue;
|
VALUE iniValue;
|
||||||
|
|
||||||
if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
|
if (rb_scan_args(argc, argv, "11", &iniValue, &nFig) == 1) {
|
||||||
mf = 0;
|
mf = 0;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
mf = GetPositiveInt(nFig);
|
mf = GetPositiveInt(nFig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (TYPE(iniValue)) {
|
||||||
|
case T_FIXNUM:
|
||||||
|
/* fall through */
|
||||||
|
case T_BIGNUM:
|
||||||
|
return ToValue(GetVpValue(iniValue, 1));
|
||||||
|
|
||||||
|
case T_STRING:
|
||||||
|
/* fall through */
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
SafeStringValue(iniValue);
|
SafeStringValue(iniValue);
|
||||||
GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
|
GUARD_OBJ(pv, VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
|
||||||
|
|
||||||
return ToValue(pv);
|
return ToValue(pv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
|
||||||
|
{
|
||||||
|
return BigDecimal_new(argc, argv, rb_cBigDecimal);
|
||||||
|
}
|
||||||
|
|
||||||
/* call-seq:
|
/* call-seq:
|
||||||
* BigDecimal.limit(digits)
|
* BigDecimal.limit(digits)
|
||||||
*
|
*
|
||||||
|
|
|
@ -29,6 +29,13 @@ class TestBigDecimal < Test::Unit::TestCase
|
||||||
assert_raise(ArgumentError) { BigDecimal("1", -1) }
|
assert_raise(ArgumentError) { BigDecimal("1", -1) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_global_new_with_integer
|
||||||
|
assert_equal(BigDecimal("1"), BigDecimal(1))
|
||||||
|
assert_equal(BigDecimal("-1"), BigDecimal(-1))
|
||||||
|
assert_equal(BigDecimal((2**100).to_s), BigDecimal(2**100))
|
||||||
|
assert_equal(BigDecimal((-2**100).to_s), BigDecimal(-2**100))
|
||||||
|
end
|
||||||
|
|
||||||
def test_new
|
def test_new
|
||||||
assert_equal(1, BigDecimal.new("1"))
|
assert_equal(1, BigDecimal.new("1"))
|
||||||
assert_equal(1, BigDecimal.new("1", 1))
|
assert_equal(1, BigDecimal.new("1", 1))
|
||||||
|
@ -44,6 +51,13 @@ class TestBigDecimal < Test::Unit::TestCase
|
||||||
assert_equal( 1, BigDecimal.new("1E1111111111111111111").infinite?)
|
assert_equal( 1, BigDecimal.new("1E1111111111111111111").infinite?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_new_with_integer
|
||||||
|
assert_equal(BigDecimal("1"), BigDecimal.new(1))
|
||||||
|
assert_equal(BigDecimal("-1"), BigDecimal.new(-1))
|
||||||
|
assert_equal(BigDecimal((2**100).to_s), BigDecimal.new(2**100))
|
||||||
|
assert_equal(BigDecimal((-2**100).to_s), BigDecimal.new(-2**100))
|
||||||
|
end
|
||||||
|
|
||||||
def _test_mode(type)
|
def _test_mode(type)
|
||||||
BigDecimal.mode(type, true)
|
BigDecimal.mode(type, true)
|
||||||
assert_raise(FloatDomainError) { yield }
|
assert_raise(FloatDomainError) { yield }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue