summaryrefslogtreecommitdiff
path: root/ext/bigdecimal
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2020-12-19 17:06:14 +0900
committerKenta Murata <mrkn@mrkn.jp>2020-12-19 22:13:59 +0900
commitdf3deb3baa0eabc66a47879be115ae3ffca4daba (patch)
tree4afbe897c5034efbe0913c5c839a5422a883dcad /ext/bigdecimal
parent654f6fbf19c0ca2e01765a909b4d952b7f0f5baa (diff)
[bigdecimal] Add BigDecimal#n_significant_digits
https://github.com/ruby/bigdecimal/commit/981dc48f95 https://github.com/ruby/bigdecimal/commit/9ecf880ec04
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r--ext/bigdecimal/bigdecimal.c32
1 files changed, 30 insertions, 2 deletions
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)
* 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);