зеркало из https://github.com/github/ruby.git
[bigdecimal] Add BigDecimal#n_significant_digits
https://github.com/ruby/bigdecimal/commit/981dc48f95 https://github.com/ruby/bigdecimal/commit/9ecf880ec04
This commit is contained in:
Родитель
654f6fbf19
Коммит
df3deb3baa
|
@ -359,8 +359,9 @@ BigDecimal_double_fig(VALUE self)
|
|||
* internal storage properties.
|
||||
*
|
||||
* This method is deprecated and will be removed in the future.
|
||||
* Instead, use BigDecimal#precision for obtaining the number of decimal
|
||||
* digits.
|
||||
* Instead, use BigDecimal#n_significant_digits for obtaining the number of
|
||||
* significant digits in scientific notation, and BigDecimal#precision for
|
||||
* obtaining the number of digits in decimal notation.
|
||||
*
|
||||
* BigDecimal('5').precs #=> [9, 18]
|
||||
*/
|
||||
|
@ -449,6 +450,32 @@ BigDecimal_precision(VALUE self)
|
|||
return SSIZET2NUM(precision);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
BigDecimal_n_significant_digits(VALUE self)
|
||||
{
|
||||
ENTER(1);
|
||||
|
||||
Real *p;
|
||||
GUARD_OBJ(p, GetVpValue(self, 1));
|
||||
|
||||
ssize_t n = p->Prec;
|
||||
while (n > 0 && p->frac[n-1] == 0) --n;
|
||||
if (n <= 0) {
|
||||
return INT2FIX(0);
|
||||
}
|
||||
|
||||
int nlz, ntz;
|
||||
|
||||
BDIGIT x = p->frac[0];
|
||||
for (nlz = BASE_FIG; x > 0; x /= 10) --nlz;
|
||||
|
||||
x = p->frac[n-1];
|
||||
for (ntz = 0; x > 0 && x % 10 == 0; x /= 10) ++ntz;
|
||||
|
||||
ssize_t n_digits = BASE_FIG * n - nlz - ntz;
|
||||
return SSIZET2NUM(n_digits);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq: hash
|
||||
*
|
||||
|
@ -3582,6 +3609,7 @@ Init_bigdecimal(void)
|
|||
/* instance methods */
|
||||
rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0);
|
||||
rb_define_method(rb_cBigDecimal, "precision", BigDecimal_precision, 0);
|
||||
rb_define_method(rb_cBigDecimal, "n_significant_digits", BigDecimal_n_significant_digits, 0);
|
||||
|
||||
rb_define_method(rb_cBigDecimal, "add", BigDecimal_add2, 2);
|
||||
rb_define_method(rb_cBigDecimal, "sub", BigDecimal_sub2, 2);
|
||||
|
|
|
@ -1962,6 +1962,57 @@ class TestBigDecimal < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_n_significant_digits_only_integer
|
||||
assert_equal(0, BigDecimal(0).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(1).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(-1).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(10).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(-10).n_significant_digits)
|
||||
assert_equal(3, BigDecimal(101).n_significant_digits)
|
||||
assert_equal(3, BigDecimal(-101).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(100_000_000_000_000_000_000).n_significant_digits)
|
||||
assert_equal(1, BigDecimal(-100_000_000_000_000_000_000).n_significant_digits)
|
||||
assert_equal(21, BigDecimal(100_000_000_000_000_000_001).n_significant_digits)
|
||||
assert_equal(21, BigDecimal(-100_000_000_000_000_000_001).n_significant_digits)
|
||||
assert_equal(3, BigDecimal("111e100").n_significant_digits)
|
||||
assert_equal(3, BigDecimal("-111e100").n_significant_digits)
|
||||
end
|
||||
|
||||
def test_n_significant_digits_only_fraction
|
||||
assert_equal(1, BigDecimal("0.1").n_significant_digits)
|
||||
assert_equal(1, BigDecimal("-0.1").n_significant_digits)
|
||||
assert_equal(1, BigDecimal("0.01").n_significant_digits)
|
||||
assert_equal(1, BigDecimal("-0.01").n_significant_digits)
|
||||
assert_equal(2, BigDecimal("0.11").n_significant_digits)
|
||||
assert_equal(2, BigDecimal("-0.11").n_significant_digits)
|
||||
assert_equal(1, BigDecimal("0.000_000_000_000_000_000_001").n_significant_digits)
|
||||
assert_equal(1, BigDecimal("-0.000_000_000_000_000_000_001").n_significant_digits)
|
||||
assert_equal(3, BigDecimal("111e-100").n_significant_digits)
|
||||
assert_equal(3, BigDecimal("-111e-100").n_significant_digits)
|
||||
end
|
||||
|
||||
def test_n_significant_digits_full
|
||||
assert_equal(2, BigDecimal("1.1").n_significant_digits)
|
||||
assert_equal(2, BigDecimal("-1.1").n_significant_digits)
|
||||
assert_equal(3, BigDecimal("1.01").n_significant_digits)
|
||||
assert_equal(3, BigDecimal("-1.01").n_significant_digits)
|
||||
assert_equal(5, BigDecimal("11111e-2").n_significant_digits)
|
||||
assert_equal(5, BigDecimal("-11111e-2").n_significant_digits)
|
||||
assert_equal(21, BigDecimal("100.000_000_000_000_000_001").n_significant_digits)
|
||||
assert_equal(21, BigDecimal("-100.000_000_000_000_000_001").n_significant_digits)
|
||||
end
|
||||
|
||||
def test_n_significant_digits_special
|
||||
BigDecimal.save_exception_mode do
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
|
||||
|
||||
assert_equal(0, BigDecimal("Infinity").n_significant_digits)
|
||||
assert_equal(0, BigDecimal("-Infinity").n_significant_digits)
|
||||
assert_equal(0, BigDecimal("NaN").n_significant_digits)
|
||||
end
|
||||
end
|
||||
|
||||
def test_initialize_copy_dup_clone_frozen_error
|
||||
bd = BigDecimal(1)
|
||||
bd2 = BigDecimal(2)
|
||||
|
|
Загрузка…
Ссылка в новой задаче