@@ -947,7 +947,7 @@ BigDecimal_coerce(VALUE self, VALUE other)
947
947
Real * b ;
948
948
949
949
if (RB_TYPE_P (other , T_FLOAT )) {
950
- GUARD_OBJ (b , GetVpValueWithPrec (other , DBLE_FIG , 1 ));
950
+ GUARD_OBJ (b , GetVpValueWithPrec (other , 0 , 1 ));
951
951
obj = rb_assoc_new (VpCheckGetValue (b ), self );
952
952
}
953
953
else {
@@ -1005,7 +1005,7 @@ BigDecimal_add(VALUE self, VALUE r)
1005
1005
1006
1006
GUARD_OBJ (a , GetVpValue (self , 1 ));
1007
1007
if (RB_TYPE_P (r , T_FLOAT )) {
1008
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1008
+ b = GetVpValueWithPrec (r , 0 , 1 );
1009
1009
}
1010
1010
else if (RB_TYPE_P (r , T_RATIONAL )) {
1011
1011
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -1063,7 +1063,7 @@ BigDecimal_sub(VALUE self, VALUE r)
1063
1063
1064
1064
GUARD_OBJ (a , GetVpValue (self ,1 ));
1065
1065
if (RB_TYPE_P (r , T_FLOAT )) {
1066
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1066
+ b = GetVpValueWithPrec (r , 0 , 1 );
1067
1067
}
1068
1068
else if (RB_TYPE_P (r , T_RATIONAL )) {
1069
1069
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -1113,7 +1113,7 @@ BigDecimalCmp(VALUE self, VALUE r,char op)
1113
1113
break ;
1114
1114
1115
1115
case T_FLOAT :
1116
- GUARD_OBJ (b , GetVpValueWithPrec (r , DBLE_FIG , 0 ));
1116
+ GUARD_OBJ (b , GetVpValueWithPrec (r , 0 , 0 ));
1117
1117
break ;
1118
1118
1119
1119
case T_RATIONAL :
@@ -1326,7 +1326,7 @@ BigDecimal_mult(VALUE self, VALUE r)
1326
1326
1327
1327
GUARD_OBJ (a , GetVpValue (self , 1 ));
1328
1328
if (RB_TYPE_P (r , T_FLOAT )) {
1329
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1329
+ b = GetVpValueWithPrec (r , 0 , 1 );
1330
1330
}
1331
1331
else if (RB_TYPE_P (r , T_RATIONAL )) {
1332
1332
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -1354,7 +1354,7 @@ BigDecimal_divide(Real **c, Real **res, Real **div, VALUE self, VALUE r)
1354
1354
1355
1355
GUARD_OBJ (a , GetVpValue (self , 1 ));
1356
1356
if (RB_TYPE_P (r , T_FLOAT )) {
1357
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1357
+ b = GetVpValueWithPrec (r , 0 , 1 );
1358
1358
}
1359
1359
else if (RB_TYPE_P (r , T_RATIONAL )) {
1360
1360
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -1420,7 +1420,7 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
1420
1420
1421
1421
GUARD_OBJ (a , GetVpValue (self , 1 ));
1422
1422
if (RB_TYPE_P (r , T_FLOAT )) {
1423
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1423
+ b = GetVpValueWithPrec (r , 0 , 1 );
1424
1424
}
1425
1425
else if (RB_TYPE_P (r , T_RATIONAL )) {
1426
1426
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -1521,7 +1521,7 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
1521
1521
1522
1522
GUARD_OBJ (a , GetVpValue (self , 1 ));
1523
1523
if (RB_TYPE_P (r , T_FLOAT )) {
1524
- b = GetVpValueWithPrec (r , DBLE_FIG , 1 );
1524
+ b = GetVpValueWithPrec (r , 0 , 1 );
1525
1525
}
1526
1526
else if (RB_TYPE_P (r , T_RATIONAL )) {
1527
1527
b = GetVpValueWithPrec (r , a -> Prec * VpBaseFig (), 1 );
@@ -2416,7 +2416,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
2416
2416
if (NIL_P (prec )) {
2417
2417
n += DBLE_FIG ;
2418
2418
}
2419
- exp = GetVpValueWithPrec (vexp , DBLE_FIG , 1 );
2419
+ exp = GetVpValueWithPrec (vexp , 0 , 1 );
2420
2420
break ;
2421
2421
2422
2422
case T_RATIONAL :
@@ -2832,7 +2832,8 @@ rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
2832
2832
char buf [DBLE_FIG + BASE_FIG + 2 + 1 ];
2833
2833
int decpt , negative_p ;
2834
2834
char * e ;
2835
- char * p = BigDecimal_dtoa (d , 2 , digs , & decpt , & negative_p , & e );
2835
+ const int mode = digs == 0 ? 0 : 2 ;
2836
+ char * p = BigDecimal_dtoa (d , mode , digs , & decpt , & negative_p , & e );
2836
2837
int len10 = (int )(e - p );
2837
2838
if (len10 >= (int )sizeof (buf ))
2838
2839
len10 = (int )sizeof (buf ) - 1 ;
@@ -3009,6 +3010,7 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
3009
3010
3010
3011
VALUE copy = TypedData_Wrap_Struct (rb_cBigDecimal , & BigDecimal_data_type , 0 );
3011
3012
vp = VpCopy (NULL , vp );
3013
+ /* TODO: rounding */
3012
3014
BigDecimal_wrap_struct (copy , vp );
3013
3015
return VpCheckGetValue (vp );
3014
3016
}
@@ -3049,19 +3051,28 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
3049
3051
}
3050
3052
3051
3053
/* call-seq:
3052
- * BigDecimal(initial, digits=0, exception: true)
3054
+ * BigDecimal(arg, exception: true)
3055
+ * BigDecimal(arg, digits, exception: true)
3053
3056
*
3054
- * Create a new BigDecimal object.
3057
+ * Returns <i>arg</i> converted to a BigDecimal. Numeric types are converted
3058
+ * directly. Other types except for String are first converted to String
3059
+ * by <code>to_str</code>. Strings can be converted when it has appropriate
3060
+ * forms of decimal numbers. Exceptions can be suppressed by passing
3061
+ * <code>exception: false</code>.
3055
3062
*
3056
- * initial:: The initial value, as an Integer, a Float, a Rational,
3057
- * a BigDecimal, or a String.
3063
+ * When <i>arg</i> is a Float and <i>digits</i> is <code>0</code>, the number
3064
+ * of digits is determined by the algorithm of <code>dtoa</code> function
3065
+ * written by David M. Gay. That algorithm is based on "How to Print Floating-
3066
+ * Point Numbers Accurately" by Guy L. Steele, Jr. and Jon L. White [Proc. ACM
3067
+ * SIGPLAN '90, pp. 112-126].
3058
3068
*
3059
- * If it is a String, spaces are ignored and unrecognized characters
3060
- * terminate the value.
3069
+ * arg:: The value converted to a BigDecimal.
3061
3070
*
3062
- * digits:: The number of significant digits, as an Integer. If omitted or 0,
3063
- * the number of significant digits is determined from the initial
3064
- * value.
3071
+ * If it is a String, spaces are ignored and unrecognized characters
3072
+ * terminate the value.
3073
+ *
3074
+ * digits:: The number of significant digits, as an Integer. If omitted,
3075
+ * the number of significant digits is determined from <i>arg</i>.
3065
3076
*
3066
3077
* The actual number of significant digits used in computation is
3067
3078
* usually larger than the specified number.
@@ -3306,7 +3317,7 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
3306
3317
infinite = isinf (flo );
3307
3318
nan = isnan (flo );
3308
3319
if (!infinite && !nan ) {
3309
- vx = GetVpValueWithPrec (x , DBLE_FIG , 0 );
3320
+ vx = GetVpValueWithPrec (x , 0 , 0 );
3310
3321
}
3311
3322
break ;
3312
3323
@@ -3459,7 +3470,7 @@ BigMath_s_log(VALUE klass, VALUE x, VALUE vprec)
3459
3470
infinite = isinf (flo );
3460
3471
nan = isnan (flo );
3461
3472
if (!zero && !negative && !infinite && !nan ) {
3462
- vx = GetVpValueWithPrec (x , DBLE_FIG , 1 );
3473
+ vx = GetVpValueWithPrec (x , 0 , 1 );
3463
3474
}
3464
3475
break ;
3465
3476
0 commit comments