@@ -2354,31 +2354,6 @@ BigDecimal_abs(VALUE self)
2354
2354
return CheckGetValue (c );
2355
2355
}
2356
2356
2357
- /* call-seq:
2358
- * sqrt(n)
2359
- *
2360
- * Returns the square root of the value.
2361
- *
2362
- * Result has at least n significant digits.
2363
- */
2364
- static VALUE
2365
- BigDecimal_sqrt (VALUE self , VALUE nFig )
2366
- {
2367
- ENTER (5 );
2368
- BDVALUE c , a ;
2369
- size_t mx , n ;
2370
-
2371
- GUARD_OBJ (a , GetBDValueMust (self ));
2372
- mx = a .real -> Prec * (VpBaseFig () + 1 );
2373
-
2374
- n = check_int_precision (nFig );
2375
- n += VpDblFig () + VpBaseFig ();
2376
- if (mx <= n ) mx = n ;
2377
- GUARD_OBJ (c , NewZeroWrapLimited (1 , mx ));
2378
- VpSqrt (c .real , a .real );
2379
- return CheckGetValue (c );
2380
- }
2381
-
2382
2357
/* Return the integer part of the number, as a BigDecimal.
2383
2358
*/
2384
2359
static VALUE
@@ -3916,7 +3891,6 @@ Init_bigdecimal(void)
3916
3891
rb_define_method (rb_cBigDecimal , "dup" , BigDecimal_clone , 0 );
3917
3892
rb_define_method (rb_cBigDecimal , "to_f" , BigDecimal_to_f , 0 );
3918
3893
rb_define_method (rb_cBigDecimal , "abs" , BigDecimal_abs , 0 );
3919
- rb_define_method (rb_cBigDecimal , "sqrt" , BigDecimal_sqrt , 1 );
3920
3894
rb_define_method (rb_cBigDecimal , "fix" , BigDecimal_fix , 0 );
3921
3895
rb_define_method (rb_cBigDecimal , "round" , BigDecimal_round , -1 );
3922
3896
rb_define_method (rb_cBigDecimal , "frac" , BigDecimal_frac , 0 );
@@ -3988,9 +3962,6 @@ static int gfDebug = 1; /* Debug switch */
3988
3962
#endif /* BIGDECIMAL_DEBUG */
3989
3963
3990
3964
static Real * VpConstOne ; /* constant 1.0 */
3991
- static Real * VpConstPt5 ; /* constant 0.5 */
3992
- #define maxnr 100UL /* Maximum iterations for calculating sqrt. */
3993
- /* used in VpSqrt() */
3994
3965
3995
3966
enum op_sw {
3996
3967
OP_SW_ADD = 1 , /* + */
@@ -4396,11 +4367,6 @@ VpInit(DECDIG BaseVal)
4396
4367
/* Const 1.0 */
4397
4368
VpConstOne = NewOneNolimit (1 , 1 );
4398
4369
4399
- /* Const 0.5 */
4400
- VpConstPt5 = NewOneNolimit (1 , 1 );
4401
- VpConstPt5 -> exponent = 0 ;
4402
- VpConstPt5 -> frac [0 ] = 5 * BASE1 ;
4403
-
4404
4370
#ifdef BIGDECIMAL_DEBUG
4405
4371
gnAlloc = 0 ;
4406
4372
#endif /* BIGDECIMAL_DEBUG */
@@ -6225,174 +6191,6 @@ VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
6225
6191
return f ;
6226
6192
}
6227
6193
6228
- /*
6229
- * m <- d
6230
- */
6231
- VP_EXPORT void
6232
- VpDtoV (Real * m , double d )
6233
- {
6234
- size_t ind_m , mm ;
6235
- SIGNED_VALUE ne ;
6236
- DECDIG i ;
6237
- double val , val2 ;
6238
-
6239
- if (isnan (d )) {
6240
- VpSetNaN (m );
6241
- goto Exit ;
6242
- }
6243
- if (isinf (d )) {
6244
- if (d > 0.0 ) VpSetPosInf (m );
6245
- else VpSetNegInf (m );
6246
- goto Exit ;
6247
- }
6248
-
6249
- if (d == 0.0 ) {
6250
- VpSetZero (m , 1 );
6251
- goto Exit ;
6252
- }
6253
- val = (d > 0. ) ? d : - d ;
6254
- ne = 0 ;
6255
- if (val >= 1.0 ) {
6256
- while (val >= 1.0 ) {
6257
- val /= (double )BASE ;
6258
- ++ ne ;
6259
- }
6260
- }
6261
- else {
6262
- val2 = 1.0 / (double )BASE ;
6263
- while (val < val2 ) {
6264
- val *= (double )BASE ;
6265
- -- ne ;
6266
- }
6267
- }
6268
- /* Now val = 0.xxxxx*BASE**ne */
6269
-
6270
- mm = m -> MaxPrec ;
6271
- memset (m -> frac , 0 , mm * sizeof (DECDIG ));
6272
- for (ind_m = 0 ; val > 0.0 && ind_m < mm ; ind_m ++ ) {
6273
- val *= (double )BASE ;
6274
- i = (DECDIG )val ;
6275
- val -= (double )i ;
6276
- m -> frac [ind_m ] = i ;
6277
- }
6278
- if (ind_m >= mm ) ind_m = mm - 1 ;
6279
- VpSetSign (m , (d > 0.0 ) ? 1 : -1 );
6280
- m -> Prec = ind_m + 1 ;
6281
- m -> exponent = ne ;
6282
-
6283
- VpInternalRound (m , 0 , (m -> Prec > 0 ) ? m -> frac [m -> Prec - 1 ] : 0 ,
6284
- (DECDIG )(val * (double )BASE ));
6285
-
6286
- Exit :
6287
- return ;
6288
- }
6289
-
6290
- /*
6291
- * y = SQRT(x), y*y - x =>0
6292
- */
6293
- VP_EXPORT int
6294
- VpSqrt (Real * y , Real * x )
6295
- {
6296
- Real * f = NULL ;
6297
- Real * r = NULL ;
6298
- size_t y_prec ;
6299
- SIGNED_VALUE n , e ;
6300
- ssize_t nr ;
6301
- double val ;
6302
-
6303
- /* Zero or +Infinity ? */
6304
- if (VpIsZero (x ) || VpIsPosInf (x )) {
6305
- VpAsgn (y ,x ,1 );
6306
- goto Exit ;
6307
- }
6308
-
6309
- /* Negative ? */
6310
- if (BIGDECIMAL_NEGATIVE_P (x )) {
6311
- VpSetNaN (y );
6312
- return VpException (VP_EXCEPTION_OP , "sqrt of negative value" , 0 );
6313
- }
6314
-
6315
- /* NaN ? */
6316
- if (VpIsNaN (x )) {
6317
- VpSetNaN (y );
6318
- return VpException (VP_EXCEPTION_OP , "sqrt of 'NaN'(Not a Number)" , 0 );
6319
- }
6320
-
6321
- /* One ? */
6322
- if (VpIsOne (x )) {
6323
- VpSetOne (y );
6324
- goto Exit ;
6325
- }
6326
-
6327
- n = (SIGNED_VALUE )y -> MaxPrec ;
6328
- if (x -> MaxPrec > (size_t )n ) n = (ssize_t )x -> MaxPrec ;
6329
-
6330
- /* allocate temporally variables */
6331
- /* TODO: reconsider MaxPrec of f and r */
6332
- f = NewOneNolimit (1 , y -> MaxPrec * (BASE_FIG + 2 ));
6333
- r = NewOneNolimit (1 , (n + n ) * (BASE_FIG + 2 ));
6334
-
6335
- nr = 0 ;
6336
- y_prec = y -> MaxPrec ;
6337
-
6338
- VpVtoD (& val , & e , x ); /* val <- x */
6339
- e /= (SIGNED_VALUE )BASE_FIG ;
6340
- n = e / 2 ;
6341
- if (e - n * 2 != 0 ) {
6342
- val /= BASE ;
6343
- n = (e + 1 ) / 2 ;
6344
- }
6345
- VpDtoV (y , sqrt (val )); /* y <- sqrt(val) */
6346
- y -> exponent += n ;
6347
- n = (SIGNED_VALUE )roomof (BIGDECIMAL_DOUBLE_FIGURES , BASE_FIG );
6348
- y -> MaxPrec = Min ((size_t )n , y_prec );
6349
- f -> MaxPrec = y -> MaxPrec + 1 ;
6350
- n = (SIGNED_VALUE )(y_prec * BASE_FIG );
6351
- if (n > (SIGNED_VALUE )maxnr ) n = (SIGNED_VALUE )maxnr ;
6352
-
6353
- /*
6354
- * Perform: y_{n+1} = (y_n - x/y_n) / 2
6355
- */
6356
- do {
6357
- y -> MaxPrec *= 2 ;
6358
- if (y -> MaxPrec > y_prec ) y -> MaxPrec = y_prec ;
6359
- f -> MaxPrec = y -> MaxPrec ;
6360
- VpDivd (f , r , x , y ); /* f = x/y */
6361
- VpAddSub (r , f , y , -1 ); /* r = f - y */
6362
- VpMult (f , VpConstPt5 , r ); /* f = 0.5*r */
6363
- if (y_prec == y -> MaxPrec && VpIsZero (f ))
6364
- goto converge ;
6365
- VpAddSub (r , f , y , 1 ); /* r = y + f */
6366
- VpAsgn (y , r , 1 ); /* y = r */
6367
- } while (++ nr < n );
6368
-
6369
- #ifdef BIGDECIMAL_DEBUG
6370
- if (gfDebug ) {
6371
- printf ("ERROR(VpSqrt): did not converge within %ld iterations.\n" , nr );
6372
- }
6373
- #endif /* BIGDECIMAL_DEBUG */
6374
- y -> MaxPrec = y_prec ;
6375
-
6376
- converge :
6377
- VpChangeSign (y , 1 );
6378
- #ifdef BIGDECIMAL_DEBUG
6379
- if (gfDebug ) {
6380
- VpMult (r , y , y );
6381
- VpAddSub (f , x , r , -1 );
6382
- printf ("VpSqrt: iterations = %" PRIdSIZE "\n" , nr );
6383
- VPrint (stdout , " y =% \n" , y );
6384
- VPrint (stdout , " x =% \n" , x );
6385
- VPrint (stdout , " x-y*y = % \n" , f );
6386
- }
6387
- #endif /* BIGDECIMAL_DEBUG */
6388
- y -> MaxPrec = y_prec ;
6389
-
6390
- Exit :
6391
- rbd_free_struct (f );
6392
- rbd_free_struct (r );
6393
- return 1 ;
6394
- }
6395
-
6396
6194
/*
6397
6195
* Round relatively from the decimal point.
6398
6196
* f: rounding mode
0 commit comments