ruby-changes:64344
From: Kenta <ko1@a...>
Date: Sat, 19 Dec 2020 22:14:43 +0900 (JST)
Subject: [ruby-changes:64344] df3deb3baa (master): [bigdecimal] Add BigDecimal#n_significant_digits
https://git.ruby-lang.org/ruby.git/commit/?id=df3deb3baa From df3deb3baa0eabc66a47879be115ae3ffca4daba Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Sat, 19 Dec 2020 17:06:14 +0900 Subject: [bigdecimal] Add BigDecimal#n_significant_digits https://github.com/ruby/bigdecimal/commit/981dc48f95 https://github.com/ruby/bigdecimal/commit/9ecf880ec04 diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index fa00395..4e49de8 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -359,8 +359,9 @@ BigDecimal_double_fig(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L359 * 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) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L450 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) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L3609 /* 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); diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 3003e44..445a3f2 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1962,6 +1962,57 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1962 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) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/