| ︙ | | | ︙ | |
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* Static functions defined in this file.
*/
static int AccumulateDecimalDigit(unsigned, int,
Tcl_WideUInt *, mp_int *, int);
static double MakeHighPrecisionDouble(int signum,
mp_int *significand, int nSigDigs, long exponent);
static double MakeLowPrecisionDouble(int signum,
Tcl_WideUInt significand, int nSigDigs,
long exponent);
#ifdef IEEE_FLOATING_POINT
static double MakeNaN(int signum, Tcl_WideUInt tag);
#endif
static double RefineApproximation(double approx,
mp_int *exactSignificand, int exponent);
static mp_err MulPow5(mp_int *, unsigned, mp_int *) MP_WUR;
static int NormalizeRightward(Tcl_WideUInt *);
|
|
|
|
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* Static functions defined in this file.
*/
static int AccumulateDecimalDigit(unsigned, int,
Tcl_WideUInt *, mp_int *, int);
static double MakeHighPrecisionDouble(int signum,
mp_int *significand, int nSigDigs, int exponent);
static double MakeLowPrecisionDouble(int signum,
Tcl_WideUInt significand, int nSigDigs,
int exponent);
#ifdef IEEE_FLOATING_POINT
static double MakeNaN(int signum, Tcl_WideUInt tag);
#endif
static double RefineApproximation(double approx,
mp_int *exactSignificand, int exponent);
static mp_err MulPow5(mp_int *, unsigned, mp_int *) MP_WUR;
static int NormalizeRightward(Tcl_WideUInt *);
|
| ︙ | | | ︙ | |
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
* significand. */
int numTrailZeros = 0; /* Number of trailing zeroes at the current
* point in the parse. */
int numDigitsAfterDp = 0; /* Number of digits scanned after the decimal
* point. */
int exponentSignum = 0; /* Signum of the exponent of a floating point
* number. */
long exponent = 0; /* Exponent of a floating point number. */
const char *p; /* Pointer to next character to scan. */
Tcl_Size len; /* Number of characters remaining after p. */
const char *acceptPoint; /* Pointer to position after last character in
* an acceptable number. */
Tcl_Size acceptLen; /* Number of characters following that
* point. */
int status = TCL_OK; /* Status to return to caller. */
|
|
|
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
* significand. */
int numTrailZeros = 0; /* Number of trailing zeroes at the current
* point in the parse. */
int numDigitsAfterDp = 0; /* Number of digits scanned after the decimal
* point. */
int exponentSignum = 0; /* Signum of the exponent of a floating point
* number. */
int exponent = 0; /* Exponent of a floating point number. */
const char *p; /* Pointer to next character to scan. */
Tcl_Size len; /* Number of characters remaining after p. */
const char *acceptPoint; /* Pointer to position after last character in
* an acceptable number. */
Tcl_Size acceptLen; /* Number of characters following that
* point. */
int status = TCL_OK; /* Status to return to caller. */
|
| ︙ | | | ︙ | |
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
|
break;
}
goto endgame;
case EXPONENT:
/*
* Found an exponent with at least one digit. Accumulate it,
* making sure to hard-pin it to LONG_MAX on overflow.
*/
acceptState = state;
acceptPoint = p;
acceptLen = len;
if (isdigit(UCHAR(c))) {
if (exponent < (LONG_MAX - 9) / 10) {
exponent = 10 * exponent + (c - '0');
} else {
exponent = LONG_MAX;
}
state = EXPONENT;
break;
}
goto endgame;
/*
|
|
|
|
|
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
|
break;
}
goto endgame;
case EXPONENT:
/*
* Found an exponent with at least one digit. Accumulate it,
* making sure to hard-pin it to INT_MAX on overflow.
*/
acceptState = state;
acceptPoint = p;
acceptLen = len;
if (isdigit(UCHAR(c))) {
if (exponent < (INT_MAX - 9) / 10) {
exponent = 10 * exponent + (c - '0');
} else {
exponent = INT_MAX;
}
state = EXPONENT;
break;
}
goto endgame;
/*
|
| ︙ | | | ︙ | |
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
|
*/
exponent = -exponent;
}
/*
* Adjust the exponent for the number of trailing zeros that
* have not been accumulated, and the number of digits after
* the decimal point. Pin any overflow to LONG_MAX/LONG_MIN
* respectively.
*/
if (exponent >= 0) {
if (exponent - numDigitsAfterDp > LONG_MAX - numTrailZeros) {
exponent = LONG_MAX;
} else {
exponent = exponent - numDigitsAfterDp + numTrailZeros;
}
} else {
if (exponent + numTrailZeros < LONG_MIN + numDigitsAfterDp) {
exponent = LONG_MIN;
} else {
exponent = exponent + numTrailZeros - numDigitsAfterDp;
}
}
/*
* The desired number is now significandWide * 10**exponent
|
|
|
|
|
|
|
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
|
*/
exponent = -exponent;
}
/*
* Adjust the exponent for the number of trailing zeros that
* have not been accumulated, and the number of digits after
* the decimal point. Pin any overflow to INT_MAX/INT_MIN
* respectively.
*/
if (exponent >= 0) {
if (exponent - numDigitsAfterDp > INT_MAX - numTrailZeros) {
exponent = INT_MAX;
} else {
exponent = exponent - numDigitsAfterDp + numTrailZeros;
}
} else {
if (exponent + numTrailZeros < INT_MIN + numDigitsAfterDp) {
exponent = INT_MIN;
} else {
exponent = exponent + numTrailZeros - numDigitsAfterDp;
}
}
/*
* The desired number is now significandWide * 10**exponent
|
| ︙ | | | ︙ | |
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
|
*/
static double
MakeLowPrecisionDouble(
int signum, /* 1 if the number is negative, 0 otherwise */
Tcl_WideUInt significand, /* Significand of the number */
int numSigDigs, /* Number of digits in the significand */
long exponent) /* Power of ten */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
mp_int significandBig; /* Significand expressed as a bignum. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
|
|
|
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
|
*/
static double
MakeLowPrecisionDouble(
int signum, /* 1 if the number is negative, 0 otherwise */
Tcl_WideUInt significand, /* Significand of the number */
int numSigDigs, /* Number of digits in the significand */
int exponent) /* Power of ten */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
mp_int significandBig; /* Significand expressed as a bignum. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
|
| ︙ | | | ︙ | |
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
|
if (exponent <= mmaxpow) {
/*
* The significand is an exact integer, and so is
* 10**exponent. The product will be correct to within 1/2 ulp
* without special handling.
*/
retval = (double)
((Tcl_WideInt)significand * pow10vals[exponent]);
goto returnValue;
} else {
int diff = QUICK_MAX - numSigDigs;
if (exponent-diff <= mmaxpow) {
/*
* 10**exponent is not an exact integer, but
* 10**(exponent-diff) is exact, and so is
* significand*10**diff, so we can still compute the value
* with only one roundoff.
*/
volatile double factor = (double)
((Tcl_WideInt)significand * pow10vals[diff]);
retval = factor * pow10vals[exponent-diff];
goto returnValue;
}
}
} else {
if (exponent >= -mmaxpow) {
/*
* 10**-exponent is an exact integer, and so is the
* significand. Compute the result by one division, again with
* only one rounding.
*/
retval = (double)
((Tcl_WideInt)significand / pow10vals[-exponent]);
goto returnValue;
}
}
}
/*
* All the easy cases have failed. Promote the significand to bignum and
|
|
|
|
|
|
|
|
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
|
if (exponent <= mmaxpow) {
/*
* The significand is an exact integer, and so is
* 10**exponent. The product will be correct to within 1/2 ulp
* without special handling.
*/
retval =
((double)significand * pow10vals[exponent]);
goto returnValue;
} else {
int diff = QUICK_MAX - numSigDigs;
if (exponent-diff <= mmaxpow) {
/*
* 10**exponent is not an exact integer, but
* 10**(exponent-diff) is exact, and so is
* significand*10**diff, so we can still compute the value
* with only one roundoff.
*/
volatile double factor =
((double)significand * pow10vals[diff]);
retval = factor * pow10vals[exponent-diff];
goto returnValue;
}
}
} else {
if (exponent >= -mmaxpow) {
/*
* 10**-exponent is an exact integer, and so is the
* significand. Compute the result by one division, again with
* only one rounding.
*/
retval =
((double)significand / pow10vals[-exponent]);
goto returnValue;
}
}
}
/*
* All the easy cases have failed. Promote the significand to bignum and
|
| ︙ | | | ︙ | |
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
|
*/
static double
MakeHighPrecisionDouble(
int signum, /* 1=negative, 0=nonnegative */
mp_int *significand, /* Exact significand of the number */
int numSigDigs, /* Number of significant digits */
long exponent) /* Power of 10 by which to multiply */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
int machexp = 0; /* Machine exponent of a power of 10. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
|
|
|
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
|
*/
static double
MakeHighPrecisionDouble(
int signum, /* 1=negative, 0=nonnegative */
mp_int *significand, /* Exact significand of the number */
int numSigDigs, /* Number of significant digits */
int exponent) /* Power of 10 by which to multiply */
{
TCL_IEEE_DOUBLE_ROUNDING_DECL
int machexp = 0; /* Machine exponent of a power of 10. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
|
| ︙ | | | ︙ | |
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
|
retval = BignumToBiasedFrExp(significand, &machexp);
retval = Pow10TimesFrExp(exponent, retval, &machexp);
if (machexp > DBL_MAX_EXP*log2FLT_RADIX) {
retval = HUGE_VAL;
goto returnValue;
}
retval = SafeLdExp(retval, machexp);
if (tiny == 0.0) {
tiny = SafeLdExp(1.0, DBL_MIN_EXP * log2FLT_RADIX - mantBits);
}
if (retval < tiny) {
retval = tiny;
}
/*
* Refine the result twice. (The second refinement should be necessary
* only if the best approximation is a power of 2 minus 1/2 ulp).
|
|
|
|
|
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
|
retval = BignumToBiasedFrExp(significand, &machexp);
retval = Pow10TimesFrExp(exponent, retval, &machexp);
if (machexp > DBL_MAX_EXP*log2FLT_RADIX) {
retval = HUGE_VAL;
goto returnValue;
}
retval = SafeLdExp(retval, machexp);
if (tiny == 0.0) {
tiny = SafeLdExp(1.0, DBL_MIN_EXP * log2FLT_RADIX - mantBits);
}
if (retval < tiny) {
retval = tiny;
}
/*
* Refine the result twice. (The second refinement should be necessary
* only if the best approximation is a power of 2 minus 1/2 ulp).
|
| ︙ | | | ︙ | |
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
|
*/
static int
RequiredPrecision(
Tcl_WideUInt w) /* Number to interrogate. */
{
int rv;
unsigned long wi;
if (w == 0) {
return 0;
}
if (sizeof(Tcl_WideUInt) <= sizeof(size_t)) {
return 1 + TclMSB(w);
}
if (w & ((Tcl_WideUInt) 0xFFFFFFFF << 32)) {
wi = (unsigned long) (w >> 32); rv = 32;
} else {
wi = (unsigned long) w; rv = 0;
}
if (wi & 0xFFFF0000) {
wi >>= 16; rv += 16;
}
if (wi & 0xFF00) {
wi >>= 8; rv += 8;
}
|
|
|
|
|
|
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
|
*/
static int
RequiredPrecision(
Tcl_WideUInt w) /* Number to interrogate. */
{
int rv;
unsigned int wi;
if (w == 0) {
return 0;
}
if (sizeof(Tcl_WideUInt) <= sizeof(size_t)) {
return 1 + TclMSB(w);
}
if (w & ((Tcl_WideUInt)0xFFFFFFFF << 32)) {
wi = (unsigned int)(w >> 32); rv = 32;
} else {
wi = (unsigned int)w; rv = 0;
}
if (wi & 0xFFFF0000) {
wi >>= 16; rv += 16;
}
if (wi & 0xFF00) {
wi >>= 8; rv += 8;
}
|
| ︙ | | | ︙ | |
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
|
for (;;) {
/*
* Convert a digit.
*/
digit = (int) d;
d -= digit;
*s++ = '0' + digit;
/*
* Truncate the conversion if the string of digits is within 1/2 ulp
* of the actual value.
*/
if (d < eps) {
|
|
|
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
|
for (;;) {
/*
* Convert a digit.
*/
digit = (int) d;
d -= digit;
*s++ = '0' + (char)digit;
/*
* Truncate the conversion if the string of digits is within 1/2 ulp
* of the actual value.
*/
if (d < eps) {
|
| ︙ | | | ︙ | |
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
|
eps *= tens[ilim-1];
i = 1;
for (;;) {
/*
* Extract a digit.
*/
digit = (int) d;
d -= digit;
if (d == 0.0) {
ilim = i;
}
*s++ = '0' + digit;
/*
* When the given digit count is reached, handle trailing strings of 0
* and 9.
*/
if (i == ilim) {
|
|
|
|
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
|
eps *= tens[ilim-1];
i = 1;
for (;;) {
/*
* Extract a digit.
*/
digit = (int)d;
d -= digit;
if (d == 0.0) {
ilim = i;
}
*s++ = '0' + (char)digit;
/*
* When the given digit count is reached, handle trailing strings of 0
* and 9.
*/
if (i == ilim) {
|
| ︙ | | | ︙ | |
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
|
}
}
/*
* Stash the current digit.
*/
*s++ = '0' + digit;
break;
}
/*
* Does one plus the current digit put us within roundoff of the
* number?
*/
if (b > S - mminus || (b == S - mminus
&& (dPtr->w.word1 & 1) == 0)) {
if (digit == 9) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
++digit;
*s++ = '0' + digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
|
|
|
|
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
|
}
}
/*
* Stash the current digit.
*/
*s++ = '0' + (char)digit;
break;
}
/*
* Does one plus the current digit put us within roundoff of the
* number?
*/
if (b > S - mminus || (b == S - mminus
&& (dPtr->w.word1 & 1) == 0)) {
if (digit == 9) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
++digit;
*s++ = '0' + (char)digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + (char)digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
| ︙ | | | ︙ | |
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
|
}
b = b % S;
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
} else {
while (*--s == '0') {
/* do nothing */
}
|
|
|
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
|
}
b = b % S;
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + (char)digit;
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
} else {
while (*--s == '0') {
/* do nothing */
}
|
| ︙ | | | ︙ | |
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
|
}
}
/*
* Stash the last digit.
*/
*s++ = '0' + digit;
break;
}
/*
* Does one plus the current digit put us within roundoff of the
* number?
*/
if (ShouldBankerRoundUpToNextPowD(&b, &mminus, sd,
dPtr->w.word1 & 1, &temp)) {
if (digit == 9) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
++digit;
*s++ = '0' + digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
|
|
|
|
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
|
}
}
/*
* Stash the last digit.
*/
*s++ = '0' + (char)digit;
break;
}
/*
* Does one plus the current digit put us within roundoff of the
* number?
*/
if (ShouldBankerRoundUpToNextPowD(&b, &mminus, sd,
dPtr->w.word1 & 1, &temp)) {
if (digit == 9) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
++digit;
*s++ = '0' + (char)digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + (char)digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
| ︙ | | | ︙ | |
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
|
mp_clamp(&b);
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
}
while (*--s == '0') {
/* do nothing */
}
|
|
|
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
|
mp_clamp(&b);
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + (char)digit;
if (i == ilim) {
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
}
while (*--s == '0') {
/* do nothing */
}
|
| ︙ | | | ︙ | |
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
|
}
i = 1;
while (err == MP_OKAY) {
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
/*
* Does the current digit leave us with a remainder small enough to
* round to it?
*/
r1 = mp_cmp_mag(&b, (m2plus > m2minus)? &mplus : &mminus);
if (r1 == MP_LT || (r1 == MP_EQ && (dPtr->w.word1 & 1) == 0)) {
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
++digit;
if (digit == 10) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
}
*s++ = '0' + digit;
break;
}
/*
* Does the current digit leave us with a remainder large enough to
* commit to rounding up to the next higher digit?
*/
if (ShouldBankerRoundUpToNext(&b, &mminus, &S,
dPtr->w.word1 & 1)) {
++digit;
if (digit == 10) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
*s++ = '0' + digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if ((err == MP_OKAY) && (i == ilim)) {
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
|
|
|
|
|
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
|
}
i = 1;
while (err == MP_OKAY) {
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = (int)dig.dp[0];
/*
* Does the current digit leave us with a remainder small enough to
* round to it?
*/
r1 = mp_cmp_mag(&b, (m2plus > m2minus)? &mplus : &mminus);
if (r1 == MP_LT || (r1 == MP_EQ && (dPtr->w.word1 & 1) == 0)) {
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
++digit;
if (digit == 10) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
}
*s++ = '0' + (char)digit;
break;
}
/*
* Does the current digit leave us with a remainder large enough to
* commit to rounding up to the next higher digit?
*/
if (ShouldBankerRoundUpToNext(&b, &mminus, &S,
dPtr->w.word1 & 1)) {
++digit;
if (digit == 10) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
*s++ = '0' + (char)digit;
break;
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + (char)digit;
if ((err == MP_OKAY) && (i == ilim)) {
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
|
| ︙ | | | ︙ | |
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
|
*/
i = 0;
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
/*
* Is a single digit all that was requested?
*/
*s++ = '0' + digit;
if (++i >= ilim) {
if ((mp_mul_2d(&b, 1, &b) == MP_OKAY) && ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
} else {
while (err == MP_OKAY) {
/*
|
|
|
|
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
|
*/
i = 0;
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = (int)dig.dp[0];
/*
* Is a single digit all that was requested?
*/
*s++ = '0' + (char)digit;
if (++i >= ilim) {
if ((mp_mul_2d(&b, 1, &b) == MP_OKAY) && ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
} else {
while (err == MP_OKAY) {
/*
|
| ︙ | | | ︙ | |
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
|
*
* Extract the next group of digits.
*/
if ((err != MP_OKAY) || (mp_div(&b, &S, &dig, &b) != MP_OKAY) || (dig.used > 1)) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
for (j = g-1; j >= 0; --j) {
int t = itens[j];
*s++ = digit / t + '0';
digit %= t;
}
i += g;
/*
* Have we converted all the requested digits?
*/
|
|
|
|
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
|
*
* Extract the next group of digits.
*/
if ((err != MP_OKAY) || (mp_div(&b, &S, &dig, &b) != MP_OKAY) || (dig.used > 1)) {
Tcl_Panic("wrong digit!");
}
digit = (int)dig.dp[0];
for (j = g-1; j >= 0; --j) {
int t = itens[j];
*s++ = (char)(digit / t + '0');
digit %= t;
}
i += g;
/*
* Have we converted all the requested digits?
*/
|
| ︙ | | | ︙ | |
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
|
*/
if (err != MP_OKAY) {
return 0.0;
}
r = 0.0;
for (i = b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
mp_clear(&b);
/*
* Scale the result to the correct number of bits.
*/
|
|
|
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
|
*/
if (err != MP_OKAY) {
return 0.0;
}
r = 0.0;
for (i = b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + (double)b.dp[i];
}
mp_clear(&b);
/*
* Scale the result to the correct number of bits.
*/
|
| ︙ | | | ︙ | |
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
|
if ((err == MP_OKAY) && !exact) {
err = mp_add_d(&b, 1, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
mp_clear(&b);
return r;
}
|
|
|
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
|
if ((err == MP_OKAY) && !exact) {
err = mp_add_d(&b, 1, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + (double)b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
mp_clear(&b);
return r;
}
|
| ︙ | | | ︙ | |
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
|
} else {
err = mp_copy(a, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
mp_clear(&b);
return r;
}
|
|
|
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
|
} else {
err = mp_copy(a, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + (double)b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
mp_clear(&b);
return r;
}
|
| ︙ | | | ︙ | |
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
|
/*
* Accumulate the result, one mp_digit at a time.
*/
r = 0.0;
if (err == MP_OKAY) {
for (i=b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
}
mp_clear(&b);
/*
* Return the result with the appropriate sign.
*/
|
|
|
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
|
/*
* Accumulate the result, one mp_digit at a time.
*/
r = 0.0;
if (err == MP_OKAY) {
for (i=b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + (double)b.dp[i];
}
}
mp_clear(&b);
/*
* Return the result with the appropriate sign.
*/
|
| ︙ | | | ︙ | |