# A problem with bigdecimal.c, and proposed solution (long)

**URL:** <https://rubytalk.org/t/a-problem-with-bigdecimal-c-and-proposed-solution-long/14329>\
**Category:** ruby-talk\
**Created:** [21 October 2004 17:39 UTC](https://rubytalk.org/t/a-problem-with-bigdecimal-c-and-proposed-solution-long/14329 "2004-10-21T17:39:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Goizueta1](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@Javier\_Goizueta1](https://rubytalk.org/u/Javier_Goizueta1)\
**Post date:** [21 October 2004 17:39 UTC](https://rubytalk.org/t/a-problem-with-bigdecimal-c-and-proposed-solution-long/14329/1 "2004-10-21T17:39:53Z")

</div>

This is about a problem in ruby/ext/bigdecimal.c. that makes  
BigDecimal#to\_f use only 13 significant digits sometimes and  
That may also affect the precision of BigDecimal#sqrt.  
The problem can only be noticed in mswin32 Ruby.

To see the problem in action take a look at the result of:

&nbsp;&nbsp;format "%.16f",BigDecimal('1.234567890123456').to\_f

The function VpVtoD() that converts BigDecimal values to double  
has one problem which is hidden by the internal high precision  
of most compilers' floating point, but that reveals itself  
when compiled with Microsoft Visual C/C++.  
Note that mswin32, which uses that compiler, is the most popular Ruby  
implementation on Windows, because the "One-Click Installer" uses it,  
so this is an important issue.

In big\_decimal.c, DBLE\_FIG is computed as:

&nbsp;&nbsp;&nbsp;&nbsp;v = 1.0;  
&nbsp;&nbsp;&nbsp;&nbsp;DBLE\_FIG = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;while(v + 1.0 \> 1.0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++DBLE\_FIG;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v /= 10;  
&nbsp;&nbsp;&nbsp;&nbsp;}

In mswin32, DBLE\_FIG becomes 16, which corresponds correctly  
to (one more than) the real decimal precision of a Float  
in IEEE double precision.  
Other compilers on the same hardware (such as GCC, Borland or Digital  
Mars),  
use the internal precision of IEEE extended double numbers (80 bits)  
as much as possible, and DBLE\_FIG has a value of 20.

Note: you can avoid the internal high precision of most compilers  
storing in variables the intermediate values of an expression.

The problem with VpVtoD is that the number of "figures" (internal  
base-BASE digits, with BASE=4) to be converted is computed as:

&nbsp;&nbsp;fig =(DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG;

Which does not take into account that the first "figure", m-\>frac[0],  
may contain less than BASE\_FIG decimal digits.

For example, BigDecimal('1.234567890123456') produces an object  
with { 1, 2345, 6789, 123, 4560 } in m-\>frac. In this case,  
the first "figure" holds just one significative decimal digit.  
Then, as only 4 figures are used, only 13 decimal significative  
digits are converted.  
Note that 4 "figures" can hold up to 16=DBL\_FIG decimal digits,  
but in this case they hold only 13.  
With other compilers we have DBLE\_FIG=20, so fig=5, and at least  
17 decimal digits are converted, which is ok.

The result of this is, for example, that

&nbsp;&nbsp;format "%.16f",BigDecimal('1.234567890123456').to\_f

produces "1.2345678901229999" in mswin32  
(would produce "1.2345678901230000" if it wasn't for the accumulated  
errors when converting decimal fractions to binary floating point).

In general, this problem causes, under mswin32 versions of Ruby,  
incorrect values for BigDecimal#to\_f and BigDecimal#sqrt.

VpSqrt() uses VpVtoD() so it is affected by the problem, but  
it also uses DBLE\_FIG directly, and may have similar problems,  
but I haven't look at it.

What I propose as a solution is:

1. Instead of the rather ambiguous DBLE\_FIG, define a DOUBLE\_DIGITS  
value, as the number of decimal digits that a Float can hold  
(exactly for integral values), which is one less than BASE\_FIG when  
correctly computed without in extra internal precision;  
BASE\_FIG would then be the number of decimal digits necessary  
for the round-trip conversion double-\>BigDecimal-\>double when  
proper rounding is used in the conversions.  
For round-trip conversion with truncation, DOUBLE\_DIGITS+1 (=DBLE\_FIG+2)  
digits are needed.  
DOUBLE\_DIGITS is 15 for IEEE double, and corresponds  
to DBL\_DIG in \<float.h\> so it doesn't need to be computed.  
Anyway, it can be computed (in Ruby, assuming Float uses double) as:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(BITS\*Math.log(2)/Math.log(10)).floor

BITS corresponds to DBL\_MANT\_DIG in \<float.h\> and can be computed as:

&nbsp;&nbsp;x = 1.0  
&nbsp;&nbsp;bits = 0  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;bits += 1  
&nbsp;&nbsp;&nbsp;&nbsp;x /= 2  
&nbsp;&nbsp;end while 1!=x+1  
&nbsp;&nbsp;bits

Or also as: 2-Math.frexp(Float::EPSILON)[1]  
In any case, care should be taken so that DOUBLE\_DIGITS depends only on  
the underlying double type properties, and not on compiler handling of  
floating point operations.

2. In VpVtoD(), compute fig to assure that FLOAT\_DIGITS+2 are  
always converted, which preserves round-trip conversions,  
for example as:

&nbsp;&nbsp;&nbsp;&nbsp;fig = 1 + (DOUBLE\_DIGITS + BASE\_FIG) / BASE\_FIG;

3. To avoid breaking existing code, BigDecimal#double\_fig could be  
defined  
to return DOUBLE\_DIGITS+2, assuming that double\_fig was interpreted as  
enough decimal digits to represent unambiguosly a Float value.

Here is a patch for bigdecimal.c Rev.1.43 that does these things  
I also have replaced (DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG in VpSqrt,  
but have I'm not really sure what I'm doing there. I haven't  
done extensive testing on this, but seems to work.

--------------------------------------------------------------BEGIN OF  
PATCH  
--- Rev.1.43/bigdecimal.c 2004-10-19 12:24:40.000000000 +0200  
+++ bigdecimal.c 2004-10-21 17:10:33.000000000 +0200  
@@ -1370,7 +1370,7 @@  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* The value of BASE\*\*2 + BASE must be represented \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* within one U\_LONG. \*/  
static U\_LONG HALF\_BASE = 5000L;/\* =BASE/2 \*/  
-static S\_LONG DBLE\_FIG = 8; /\* figure of double \*/  
+static S\_LONG DOUBLE\_DIGITS = DBL\_DIG; /\* figure of double \*/  
static U\_LONG BASE1 = 1000L; /\* =BASE/10 \*/

static Real \*VpConstOne; /\* constant 1.0 \*/  
@@ -1515,7 +1515,7 @@  
VP\_EXPORT U\_LONG  
VpDblFig(void)  
{  
- return DBLE\_FIG;  
+ return DOUBLE\_DIGITS+2;  
}

VP\_EXPORT U\_LONG  
@@ -1760,7 +1760,7 @@  
&nbsp;&nbsp;\* by one U\_LONG word(LONG) in the computer used.

> **···**
>
> \*  
> &nbsp;&nbsp;\* [Returns]  
> - \* DBLE\_FIG ... OK  
> + \* DOUBLE\_DIGITS ... OK  
> &nbsp;&nbsp;\*/  
> VP\_EXPORT U\_LONG  
> VpInit(U\_LONG BaseVal)  
> @@ -1799,15 +1799,6 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gnAlloc = 0;  
> #endif /\* \_DEBUG \*/
> 
> - /\* Determine # of digits available in one 'double'. \*/  
> -  
> - v = 1.0;  
> - DBLE\_FIG = 0;  
> - while(v + 1.0 \> 1.0) {  
> - ++DBLE\_FIG;  
> - v /= 10;  
> - }  
> -  
> #ifdef \_DEBUG  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(gfDebug) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("VpInit: BaseVal = %lu\n", BaseVal);  
> @@ -1819,7 +1810,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> #endif /\* \_DEBUG \*/
> 
> - return DBLE\_FIG;  
> + return DOUBLE\_DIGITS;  
> }
> 
> VP\_EXPORT Real \*  
> @@ -3401,7 +3392,7 @@  
> &nbsp;&nbsp;\* [Output]  
> &nbsp;&nbsp;\* \*d ... fraction part of m(d = 0.xxxxxxx). where # of 'x's is fig.  
> &nbsp;&nbsp;\* \*e ... U\_LONG,exponent of m.  
> - \* DBLE\_FIG ... Number of digits in a double variable.  
> + \* DOUBLE\_DIGITS ... Number of digits in a double variable.  
> &nbsp;&nbsp;\*  
> &nbsp;&nbsp;\* m -\> d\*10\*\*e, 0\<d\<BASE  
> &nbsp;&nbsp;\* [Returns]  
> @@ -3448,7 +3439,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto Exit;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Normal number \*/  
> - fig =(DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG;  
> + fig = 1 + (DOUBLE\_DIGITS + BASE\_FIG) / BASE\_FIG;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind\_m = 0;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mm = Min(fig,(m-\>Prec));  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*d = 0.0;  
> @@ -3465,7 +3456,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(gfDebug) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VPrint(stdout, " VpVtoD: m=%\n", m);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(" d=%e \* 10 \*\*%ld\n", \*d, \*e);  
> - printf(" DBLE\_FIG = %ld\n", DBLE\_FIG);  
> + printf(" DOUBLE\_DIGITS = %ld\n", DOUBLE\_DIGITS);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> #endif /\*\_DEBUG \*/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f;  
> @@ -3663,7 +3654,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VpDtoV(y, sqrt(val)); /\* y \<- sqrt(val) \*/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y-\>exponent += n;  
> - n = (DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG;  
> + n = (DOUBLE\_DIGITS + BASE\_FIG) / BASE\_FIG;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y-\>MaxPrec = (U\_LONG)Min(n , y\_prec);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f-\>MaxPrec = y-\>MaxPrec + 1;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = y\_prec\*((S\_LONG)BASE\_FIG);  
> --------------------------------------------------------------END OF  
> PATCH
> 
> With these changes VpVtoD() works better, but it's still not perfect;  
> in particular, x.to\_s.to\_f is better than x.to\_f, even  
> if we truncate x.to\_s at DOUBLE\_DIGITS+2 significative digits, which  
> in theory is what VpVtoD does.
> 
> For example:
> 
> &nbsp;&nbsp;(BigDecimal('1')/3).to\_f != 1.0/3
> 
> but
> 
> &nbsp;&nbsp;(BigDecimal('1')/3).to\_s.to\_f == 1.0/3
> 
> or, using no more digits than VpVtoD() does:
> 
> &nbsp;&nbsp;s,d,b,e = (BigDecimal('1')/3).split  
> &nbsp;&nbsp;n = [d.size, BigDecimal.double\_fig].min  
> &nbsp;&nbsp;(d[0...n]+"E#{e-n}").to\_f == 1.0/3 # this is true!
> 
> Which proves that VpVtoD could do better. (note that these examples  
> are meant for the patched version of bigdecimal)
> 
> - - - - - -
> 
> Last minute note:  
> I've realized that in the unpatched version of bigdecimal.c:
> 
> &nbsp;&nbsp;(BigDecimal('1')/3).to\_f == 1.0/3
> 
> That means that adding more than necessary "figures" degrades  
> the result. This could be solved by being more careful when  
> computing fig:
> 
> &nbsp;&nbsp;fig =(DOUBLE\_DIGITS+K + 4-n + BASE\_FIG - 1) / BASE\_FIG;
> 
> where n is the number of significant digits in m-\>frac[0], i.e.  
> one more than the (floor of the) base-BASE logarithm of m-\>frac[0].  
> For BASE=4 n would be (d0\<10 ? 1 : (d0\<100 ? 2 : (d0\<1000 ? 3 : 4)))  
> with d0=m-\>frac[0].
> 
> With K=1 (convert 16 digits for IEEE) this satifies:
> 
> &nbsp;&nbsp;(BigDecimal('1')/3).to\_f == 1.0/3
> 
> But not with K=2, which is theoretically what we should take  
> (17 digits for IEEE) to have enough precision in all cases.
> 
> Anyway, I think this should be look at carefully by someone else,  
> including  
> the VpSqrt code. It would be nice if someone writes a more precise  
> VpDtoV.
> 
> - - - - - -
> 
> Finally I'd like to propose the inclusion of some Float-related  
> constants in the Ruby core.  
> I sometimes have used this:
> 
> &nbsp;&nbsp;class Float  
> &nbsp;&nbsp;&nbsp;&nbsp;x,bits = 1.0,0  
> &nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bits += 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x /= 2  
> &nbsp;&nbsp;&nbsp;&nbsp;end while 1!=x+1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Number of binary digits of precision in a Float  
> &nbsp;&nbsp;&nbsp;&nbsp;BITS = bits
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Number of decimal digits of precision in a Float  
> &nbsp;&nbsp;&nbsp;&nbsp;DIGITS = (BITS\*Math.log(2)/Math.log(10)).floor
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Decimal precision required to represent a Float  
> &nbsp;&nbsp;&nbsp;&nbsp;DIGITS2 = DIGITS+2
> 
> &nbsp;&nbsp;&nbsp;&nbsp;EPSILON = Math.ldexp(0.5,2-BITS) unless const\_defined?(:EPSILON)  
> &nbsp;&nbsp;end
> 
> These constants (EPSILON has already been defined) could  
> be defined in the core using \<float.h\> constants DBL\_MANT\_DIG and  
> DBL\_DIG.  
> The values for IEEE double precision are:  
> &nbsp;&nbsp;Float::BITS = 53  
> &nbsp;&nbsp;Float::DIGITS = 15  
> &nbsp;&nbsp;Float::DIGITS2 = 17  
> &nbsp;&nbsp;Float::EPSILON = 2.2204460492503131E-16
> 
> - - - - - - - -  
> Regards,
> 
> --Javier

---

<div class="post-metadata">

**Author:** ![Javier\_Goizueta1](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@Javier\_Goizueta1](https://rubytalk.org/u/Javier_Goizueta1)\
**Post date:** [21 October 2004 18:27 UTC](https://rubytalk.org/t/a-problem-with-bigdecimal-c-and-proposed-solution-long/14329/2 "2004-10-21T18:27:46Z")

</div>

> Last minute note:  
> I've realized that in the unpatched version of bigdecimal.c:
> 
> &nbsp;&nbsp;(BigDecimal('1')/3).to\_f == 1.0/3
> 
> That means that adding more than necessary "figures" degrades  
> the result. This could be solved by being more careful when  
> computing fig:
> 
> &nbsp;&nbsp;fig =(DOUBLE\_DIGITS+K + 4-n + BASE\_FIG - 1) / BASE\_FIG;

I've done a small change in the while loop of that improves the  
accuracy: accumulate div by multiplying, not dividing:

&nbsp;&nbsp;&nbsp;&nbsp;while(ind\_m \< mm) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;div \*=(flt\_t)((S\_INT)BASE);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*d += ((flt\_t) ((S\_INT)m-\>frac[ind\_m++])) / div;  
&nbsp;&nbsp;&nbsp;&nbsp;}

Using this change and the previous one properly applied, the VpVtoD  
function is working quite well now; Here's a patch from Rev.1.43 with  
all the changes:

--------------------------------------------------------------BEGIN OF  
PATCH  
--- Rev.1.43/bigdecimal.c 2004-10-19 12:24:40.000000000 +0200  
+++ bigdecimal.c 2004-10-21 20:21:58.000000000 +0200  
@@ -1370,7 +1370,7 @@  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* The value of BASE\*\*2 + BASE must be represented \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* within one U\_LONG. \*/  
static U\_LONG HALF\_BASE = 5000L;/\* =BASE/2 \*/  
-static S\_LONG DBLE\_FIG = 8; /\* figure of double \*/  
+static S\_LONG DOUBLE\_DIGITS = DBL\_DIG; /\* figure of double \*/  
static U\_LONG BASE1 = 1000L; /\* =BASE/10 \*/

static Real \*VpConstOne; /\* constant 1.0 \*/  
@@ -1515,7 +1515,7 @@  
VP\_EXPORT U\_LONG  
VpDblFig(void)  
{  
- return DBLE\_FIG;  
+ return DOUBLE\_DIGITS+2;  
}

VP\_EXPORT U\_LONG  
@@ -1760,7 +1760,7 @@  
&nbsp;&nbsp;\* by one U\_LONG word(LONG) in the computer used.

> **···**
>
> \*  
> &nbsp;&nbsp;\* [Returns]  
> - \* DBLE\_FIG ... OK  
> + \* DOUBLE\_DIGITS ... OK  
> &nbsp;&nbsp;\*/  
> VP\_EXPORT U\_LONG  
> VpInit(U\_LONG BaseVal)  
> @@ -1799,15 +1799,6 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gnAlloc = 0;  
> #endif /\* \_DEBUG \*/
> 
> - /\* Determine # of digits available in one 'double'. \*/  
> -  
> - v = 1.0;  
> - DBLE\_FIG = 0;  
> - while(v + 1.0 \> 1.0) {  
> - ++DBLE\_FIG;  
> - v /= 10;  
> - }  
> -  
> #ifdef \_DEBUG  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(gfDebug) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("VpInit: BaseVal = %lu\n", BaseVal);  
> @@ -1819,7 +1810,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> #endif /\* \_DEBUG \*/
> 
> - return DBLE\_FIG;  
> + return DOUBLE\_DIGITS;  
> }
> 
> VP\_EXPORT Real \*  
> @@ -3401,7 +3392,7 @@  
> &nbsp;&nbsp;\* [Output]  
> &nbsp;&nbsp;\* \*d ... fraction part of m(d = 0.xxxxxxx). where # of 'x's is fig.  
> &nbsp;&nbsp;\* \*e ... U\_LONG,exponent of m.  
> - \* DBLE\_FIG ... Number of digits in a double variable.  
> + \* DOUBLE\_DIGITS ... Number of digits in a double variable.  
> &nbsp;&nbsp;\*  
> &nbsp;&nbsp;\* m -\> d\*10\*\*e, 0\<d\<BASE  
> &nbsp;&nbsp;\* [Returns]  
> @@ -3413,7 +3404,8 @@  
> VP\_EXPORT int  
> VpVtoD(double \*d, S\_LONG \*e, Real \*m)  
> {  
> - U\_LONG ind\_m, mm, fig;  
> + int n;  
> + U\_LONG ind\_m, mm, fig, d0;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double div;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int f = 1;
> 
> @@ -3448,14 +3440,20 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto Exit;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Normal number \*/  
> - fig =(DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG;  
> + d0 = m-\>frac[0];  
> + n = 1;  
> + while (d0\>=10) {  
> + n++;  
> + d0 /= 10;  
> + }  
> + fig = (DOUBLE\_DIGITS+2 + 4-n + BASE\_FIG - 1) / BASE\_FIG;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ind\_m = 0;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mm = Min(fig,(m-\>Prec));  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*d = 0.0;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;div = 1.;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(ind\_m \< mm) {  
> - div /=(double)((S\_INT)BASE);  
> - \*d = \*d +((double) ((S\_INT)m-\>frac[ind\_m++])) \* div;  
> + div \*=(double)((S\_INT)BASE);  
> + \*d += ((double) ((S\_INT)m-\>frac[ind\_m++])) / div;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*e = m-\>exponent \* ((S\_INT)BASE\_FIG);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*d \*= VpGetSign(m);  
> @@ -3465,7 +3463,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(gfDebug) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VPrint(stdout, " VpVtoD: m=%\n", m);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(" d=%e \* 10 \*\*%ld\n", \*d, \*e);  
> - printf(" DBLE\_FIG = %ld\n", DBLE\_FIG);  
> + printf(" DOUBLE\_DIGITS = %ld\n", DOUBLE\_DIGITS);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> #endif /\*\_DEBUG \*/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f;  
> @@ -3663,7 +3661,7 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VpDtoV(y, sqrt(val)); /\* y \<- sqrt(val) \*/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y-\>exponent += n;  
> - n = (DBLE\_FIG + BASE\_FIG - 1) / BASE\_FIG;  
> + n = (DOUBLE\_DIGITS + BASE\_FIG) / BASE\_FIG;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y-\>MaxPrec = (U\_LONG)Min(n , y\_prec);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f-\>MaxPrec = y-\>MaxPrec + 1;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = y\_prec\*((S\_LONG)BASE\_FIG);  
> --------------------------------------------------------------END OF  
> PATCH
> 
> Note: in my previous post,  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;format "%.16f",BigDecimal('1.234567890123456').to\_f
> 
> meant to be
> 
> &nbsp;&nbsp;format "%.15f",BigDecimal('1.234567890123456').to\_f
> 
> --Javier
