Skip to content

Commit

Permalink
[DOC] Make constants documented
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 6, 2023
1 parent eeccedf commit ab4733d
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,17 @@ BigDecimal_negative_zero(void)
return BIGDECIMAL_NEGATIVE_ZERO;
}

static inline VALUE
BigDecimal_literal(const char *str)
{
VALUE arg = rb_str_new_cstr(str);
VALUE val = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(val);
return val;
}

#define BIGDECIMAL_LITERAL(var, val) (BIGDECIMAL_ ## var = BigDecimal_literal(#val))

/* Document-class: BigDecimal
* BigDecimal provides arbitrary-precision floating point decimal arithmetic.
*
Expand Down Expand Up @@ -4381,7 +4392,6 @@ Init_bigdecimal(void)
#ifdef HAVE_RB_EXT_RACTOR_SAFE
rb_ext_ractor_safe(true);
#endif
VALUE arg;

id_BigDecimal_exception_mode = rb_intern_const("BigDecimal.exception_mode");
id_BigDecimal_rounding_mode = rb_intern_const("BigDecimal.rounding_mode");
Expand Down Expand Up @@ -4519,33 +4529,19 @@ Init_bigdecimal(void)
rb_define_const(rb_cBigDecimal, "SIGN_NEGATIVE_INFINITE", INT2FIX(VP_SIGN_NEGATIVE_INFINITE));

/* Positive zero value. */
arg = rb_str_new2("+0");
BIGDECIMAL_POSITIVE_ZERO = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(BIGDECIMAL_POSITIVE_ZERO);
BIGDECIMAL_LITERAL(POSITIVE_ZERO, +0);

/* Negative zero value. */
arg = rb_str_new2("-0");
BIGDECIMAL_NEGATIVE_ZERO = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(BIGDECIMAL_NEGATIVE_ZERO);
BIGDECIMAL_LITERAL(NEGATIVE_ZERO, -0);

/* Positive infinity value. */
arg = rb_str_new2("+Infinity");
BIGDECIMAL_POSITIVE_INFINITY = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(BIGDECIMAL_POSITIVE_INFINITY);
rb_define_const(rb_cBigDecimal, "INFINITY", BIGDECIMAL_LITERAL(POSITIVE_INFINITY, +Infinity));

/* Negative infinity value. */
arg = rb_str_new2("-Infinity");
BIGDECIMAL_NEGATIVE_INFINITY = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(BIGDECIMAL_NEGATIVE_INFINITY);
BIGDECIMAL_LITERAL(NEGATIVE_INFINITY, -Infinity);

/* 'Not a Number' value. */
arg = rb_str_new2("NaN");
BIGDECIMAL_NAN = f_BigDecimal(1, &arg, rb_cBigDecimal);
rb_gc_register_mark_object(BIGDECIMAL_NAN);

/* Special value constants */
rb_define_const(rb_cBigDecimal, "INFINITY", BIGDECIMAL_POSITIVE_INFINITY);
rb_define_const(rb_cBigDecimal, "NAN", BIGDECIMAL_NAN);
rb_define_const(rb_cBigDecimal, "NAN", BIGDECIMAL_LITERAL(NAN, NaN));

/* instance methods */
rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0);
Expand Down

0 comments on commit ab4733d

Please sign in to comment.