@@ -2355,31 +2355,6 @@ BigDecimal_abs(VALUE self)
2355
2355
return CheckGetValue (c );
2356
2356
}
2357
2357
2358
- /* call-seq:
2359
- * sqrt(n)
2360
- *
2361
- * Returns the square root of the value.
2362
- *
2363
- * Result has at least n significant digits.
2364
- */
2365
- static VALUE
2366
- BigDecimal_sqrt (VALUE self , VALUE nFig )
2367
- {
2368
- ENTER (5 );
2369
- BDVALUE c , a ;
2370
- size_t mx , n ;
2371
-
2372
- GUARD_OBJ (a , GetBDValueMust (self ));
2373
- mx = a .real -> Prec * (VpBaseFig () + 1 );
2374
-
2375
- n = check_int_precision (nFig );
2376
- n += VpDblFig () + VpBaseFig ();
2377
- if (mx <= n ) mx = n ;
2378
- GUARD_OBJ (c , NewZeroWrapLimited (1 , mx ));
2379
- VpSqrt (c .real , a .real );
2380
- return CheckGetValue (c );
2381
- }
2382
-
2383
2358
/* Return the integer part of the number, as a BigDecimal.
2384
2359
*/
2385
2360
static VALUE
@@ -4577,7 +4552,6 @@ Init_bigdecimal(void)
4577
4552
rb_define_method (rb_cBigDecimal , "dup" , BigDecimal_clone , 0 );
4578
4553
rb_define_method (rb_cBigDecimal , "to_f" , BigDecimal_to_f , 0 );
4579
4554
rb_define_method (rb_cBigDecimal , "abs" , BigDecimal_abs , 0 );
4580
- rb_define_method (rb_cBigDecimal , "sqrt" , BigDecimal_sqrt , 1 );
4581
4555
rb_define_method (rb_cBigDecimal , "fix" , BigDecimal_fix , 0 );
4582
4556
rb_define_method (rb_cBigDecimal , "round" , BigDecimal_round , -1 );
4583
4557
rb_define_method (rb_cBigDecimal , "frac" , BigDecimal_frac , 0 );
@@ -4655,9 +4629,6 @@ static int gfDebug = 1; /* Debug switch */
4655
4629
#endif /* BIGDECIMAL_DEBUG */
4656
4630
4657
4631
static Real * VpConstOne ; /* constant 1.0 */
4658
- static Real * VpConstPt5 ; /* constant 0.5 */
4659
- #define maxnr 100UL /* Maximum iterations for calculating sqrt. */
4660
- /* used in VpSqrt() */
4661
4632
4662
4633
enum op_sw {
4663
4634
OP_SW_ADD = 1 , /* + */
@@ -5063,11 +5034,6 @@ VpInit(DECDIG BaseVal)
5063
5034
/* Const 1.0 */
5064
5035
VpConstOne = NewOneNolimit (1 , 1 );
5065
5036
5066
- /* Const 0.5 */
5067
- VpConstPt5 = NewOneNolimit (1 , 1 );
5068
- VpConstPt5 -> exponent = 0 ;
5069
- VpConstPt5 -> frac [0 ] = 5 * BASE1 ;
5070
-
5071
5037
#ifdef BIGDECIMAL_DEBUG
5072
5038
gnAlloc = 0 ;
5073
5039
#endif /* BIGDECIMAL_DEBUG */
@@ -6892,174 +6858,6 @@ VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
6892
6858
return f ;
6893
6859
}
6894
6860
6895
- /*
6896
- * m <- d
6897
- */
6898
- VP_EXPORT void
6899
- VpDtoV (Real * m , double d )
6900
- {
6901
- size_t ind_m , mm ;
6902
- SIGNED_VALUE ne ;
6903
- DECDIG i ;
6904
- double val , val2 ;
6905
-
6906
- if (isnan (d )) {
6907
- VpSetNaN (m );
6908
- goto Exit ;
6909
- }
6910
- if (isinf (d )) {
6911
- if (d > 0.0 ) VpSetPosInf (m );
6912
- else VpSetNegInf (m );
6913
- goto Exit ;
6914
- }
6915
-
6916
- if (d == 0.0 ) {
6917
- VpSetZero (m , 1 );
6918
- goto Exit ;
6919
- }
6920
- val = (d > 0. ) ? d : - d ;
6921
- ne = 0 ;
6922
- if (val >= 1.0 ) {
6923
- while (val >= 1.0 ) {
6924
- val /= (double )BASE ;
6925
- ++ ne ;
6926
- }
6927
- }
6928
- else {
6929
- val2 = 1.0 / (double )BASE ;
6930
- while (val < val2 ) {
6931
- val *= (double )BASE ;
6932
- -- ne ;
6933
- }
6934
- }
6935
- /* Now val = 0.xxxxx*BASE**ne */
6936
-
6937
- mm = m -> MaxPrec ;
6938
- memset (m -> frac , 0 , mm * sizeof (DECDIG ));
6939
- for (ind_m = 0 ; val > 0.0 && ind_m < mm ; ind_m ++ ) {
6940
- val *= (double )BASE ;
6941
- i = (DECDIG )val ;
6942
- val -= (double )i ;
6943
- m -> frac [ind_m ] = i ;
6944
- }
6945
- if (ind_m >= mm ) ind_m = mm - 1 ;
6946
- VpSetSign (m , (d > 0.0 ) ? 1 : -1 );
6947
- m -> Prec = ind_m + 1 ;
6948
- m -> exponent = ne ;
6949
-
6950
- VpInternalRound (m , 0 , (m -> Prec > 0 ) ? m -> frac [m -> Prec - 1 ] : 0 ,
6951
- (DECDIG )(val * (double )BASE ));
6952
-
6953
- Exit :
6954
- return ;
6955
- }
6956
-
6957
- /*
6958
- * y = SQRT(x), y*y - x =>0
6959
- */
6960
- VP_EXPORT int
6961
- VpSqrt (Real * y , Real * x )
6962
- {
6963
- Real * f = NULL ;
6964
- Real * r = NULL ;
6965
- size_t y_prec ;
6966
- SIGNED_VALUE n , e ;
6967
- ssize_t nr ;
6968
- double val ;
6969
-
6970
- /* Zero or +Infinity ? */
6971
- if (VpIsZero (x ) || VpIsPosInf (x )) {
6972
- VpAsgn (y ,x ,1 );
6973
- goto Exit ;
6974
- }
6975
-
6976
- /* Negative ? */
6977
- if (BIGDECIMAL_NEGATIVE_P (x )) {
6978
- VpSetNaN (y );
6979
- return VpException (VP_EXCEPTION_OP , "sqrt of negative value" , 0 );
6980
- }
6981
-
6982
- /* NaN ? */
6983
- if (VpIsNaN (x )) {
6984
- VpSetNaN (y );
6985
- return VpException (VP_EXCEPTION_OP , "sqrt of 'NaN'(Not a Number)" , 0 );
6986
- }
6987
-
6988
- /* One ? */
6989
- if (VpIsOne (x )) {
6990
- VpSetOne (y );
6991
- goto Exit ;
6992
- }
6993
-
6994
- n = (SIGNED_VALUE )y -> MaxPrec ;
6995
- if (x -> MaxPrec > (size_t )n ) n = (ssize_t )x -> MaxPrec ;
6996
-
6997
- /* allocate temporally variables */
6998
- /* TODO: reconsider MaxPrec of f and r */
6999
- f = NewOneNolimit (1 , y -> MaxPrec * (BASE_FIG + 2 ));
7000
- r = NewOneNolimit (1 , (n + n ) * (BASE_FIG + 2 ));
7001
-
7002
- nr = 0 ;
7003
- y_prec = y -> MaxPrec ;
7004
-
7005
- VpVtoD (& val , & e , x ); /* val <- x */
7006
- e /= (SIGNED_VALUE )BASE_FIG ;
7007
- n = e / 2 ;
7008
- if (e - n * 2 != 0 ) {
7009
- val /= BASE ;
7010
- n = (e + 1 ) / 2 ;
7011
- }
7012
- VpDtoV (y , sqrt (val )); /* y <- sqrt(val) */
7013
- y -> exponent += n ;
7014
- n = (SIGNED_VALUE )roomof (BIGDECIMAL_DOUBLE_FIGURES , BASE_FIG );
7015
- y -> MaxPrec = Min ((size_t )n , y_prec );
7016
- f -> MaxPrec = y -> MaxPrec + 1 ;
7017
- n = (SIGNED_VALUE )(y_prec * BASE_FIG );
7018
- if (n > (SIGNED_VALUE )maxnr ) n = (SIGNED_VALUE )maxnr ;
7019
-
7020
- /*
7021
- * Perform: y_{n+1} = (y_n - x/y_n) / 2
7022
- */
7023
- do {
7024
- y -> MaxPrec *= 2 ;
7025
- if (y -> MaxPrec > y_prec ) y -> MaxPrec = y_prec ;
7026
- f -> MaxPrec = y -> MaxPrec ;
7027
- VpDivd (f , r , x , y ); /* f = x/y */
7028
- VpAddSub (r , f , y , -1 ); /* r = f - y */
7029
- VpMult (f , VpConstPt5 , r ); /* f = 0.5*r */
7030
- if (y_prec == y -> MaxPrec && VpIsZero (f ))
7031
- goto converge ;
7032
- VpAddSub (r , f , y , 1 ); /* r = y + f */
7033
- VpAsgn (y , r , 1 ); /* y = r */
7034
- } while (++ nr < n );
7035
-
7036
- #ifdef BIGDECIMAL_DEBUG
7037
- if (gfDebug ) {
7038
- printf ("ERROR(VpSqrt): did not converge within %ld iterations.\n" , nr );
7039
- }
7040
- #endif /* BIGDECIMAL_DEBUG */
7041
- y -> MaxPrec = y_prec ;
7042
-
7043
- converge :
7044
- VpChangeSign (y , 1 );
7045
- #ifdef BIGDECIMAL_DEBUG
7046
- if (gfDebug ) {
7047
- VpMult (r , y , y );
7048
- VpAddSub (f , x , r , -1 );
7049
- printf ("VpSqrt: iterations = %" PRIdSIZE "\n" , nr );
7050
- VPrint (stdout , " y =% \n" , y );
7051
- VPrint (stdout , " x =% \n" , x );
7052
- VPrint (stdout , " x-y*y = % \n" , f );
7053
- }
7054
- #endif /* BIGDECIMAL_DEBUG */
7055
- y -> MaxPrec = y_prec ;
7056
-
7057
- Exit :
7058
- rbd_free_struct (f );
7059
- rbd_free_struct (r );
7060
- return 1 ;
7061
- }
7062
-
7063
6861
/*
7064
6862
* Round relatively from the decimal point.
7065
6863
* f: rounding mode
0 commit comments