| ︙ | | | ︙ | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclTomMath.h"
#include <math.h>
/*
* Older MSVC has no copysign function, but it's available at least since
* MSVC++ 12.0 (that is Visual Studio 2013).
*/
#if (defined(_MSC_VER) && (_MSC_VER < 1800))
inline static double
copysign(double a, double b) {
return _copysign(a, b);
}
#endif
/*
* This code supports (at least hypothetically), IBM, Cray, VAX and IEEE-754
* floating point; of these, only IEEE-754 can represent NaN. IEEE-754 can be
* uniquely determined by radix and by the widths of significand and exponent.
*/
|
>
<
<
<
<
|
<
<
<
|
<
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclTomMath.h"
#include <float.h>
#include <math.h>
#ifdef _WIN32
#define copysign _copysign
#endif
/*
* This code supports (at least hypothetically), IBM, Cray, VAX and IEEE-754
* floating point; of these, only IEEE-754 can represent NaN. IEEE-754 can be
* uniquely determined by radix and by the widths of significand and exponent.
*/
|
| ︙ | | | ︙ | |
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
|
}
break;
case ZERO:
case DECIMAL:
significandOverflow = AccumulateDecimalDigit(0, numTrailZeros-1,
&significandWide, &significandBig, significandOverflow);
if (!significandOverflow && (significandWide > MOST_BITS+signum)){
significandOverflow = 1;
mp_init_u64(&significandBig, significandWide);
}
returnInteger:
if (!significandOverflow) {
if (significandWide > MOST_BITS+signum) {
mp_init_u64(&significandBig,
|
|
|
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
|
}
break;
case ZERO:
case DECIMAL:
significandOverflow = AccumulateDecimalDigit(0, numTrailZeros-1,
&significandWide, &significandBig, significandOverflow);
if (!significandOverflow && (significandWide > MOST_BITS+signum)) {
significandOverflow = 1;
mp_init_u64(&significandBig, significandWide);
}
returnInteger:
if (!significandOverflow) {
if (significandWide > MOST_BITS+signum) {
mp_init_u64(&significandBig,
|
| ︙ | | | ︙ | |
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
|
* to whether 'significandOverflow' is set. The desired floating
* point value is significand * 10**k, where
* k = numTrailZeros+exponent-numDigitsAfterDp.
*/
objPtr->typePtr = &tclDoubleType;
if (exponentSignum) {
/*
* At this point exponent>=0, so the following calculation
* cannot underflow.
*/
exponent = -exponent;
}
/*
* Adjust the exponent for the number of trailing zeros that
* have not been accumulated, and the number of digits after
|
|
|
|
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
|
* to whether 'significandOverflow' is set. The desired floating
* point value is significand * 10**k, where
* k = numTrailZeros+exponent-numDigitsAfterDp.
*/
objPtr->typePtr = &tclDoubleType;
if (exponentSignum) {
/*
* At this point exponent>=0, so the following calculation
* cannot underflow.
*/
exponent = -exponent;
}
/*
* Adjust the exponent for the number of trailing zeros that
* have not been accumulated, and the number of digits after
|
| ︙ | | | ︙ | |
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
if (exponent + numTrailZeros < LONG_MIN + numDigitsAfterDp) {
exponent = LONG_MIN;
} else {
exponent = exponent + numTrailZeros - numDigitsAfterDp;
}
}
/*
* The desired number is now significandWide * 10**exponent
* or significandBig * 10**exponent, depending on whether
* the significand has overflowed a wide int.
*/
if (!significandOverflow) {
objPtr->internalRep.doubleValue = MakeLowPrecisionDouble(
signum, significandWide, numSigDigs, exponent);
|
|
|
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
|
if (exponent + numTrailZeros < LONG_MIN + numDigitsAfterDp) {
exponent = LONG_MIN;
} else {
exponent = exponent + numTrailZeros - numDigitsAfterDp;
}
}
/*
* The desired number is now significandWide * 10**exponent
* or significandBig * 10**exponent, depending on whether
* the significand has overflowed a wide int.
*/
if (!significandOverflow) {
objPtr->internalRep.doubleValue = MakeLowPrecisionDouble(
signum, significandWide, numSigDigs, exponent);
|
| ︙ | | | ︙ | |
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
|
}
objPtr->typePtr = &tclDoubleType;
break;
#ifdef IEEE_FLOATING_POINT
case sNAN:
case sNANFINISH:
objPtr->internalRep.doubleValue = MakeNaN(signum,significandWide);
objPtr->typePtr = &tclDoubleType;
break;
#endif
case INITIAL:
/* This case only to silence compiler warning. */
Tcl_Panic("TclParseNumber: state INITIAL can't happen here");
}
|
|
|
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
|
}
objPtr->typePtr = &tclDoubleType;
break;
#ifdef IEEE_FLOATING_POINT
case sNAN:
case sNANFINISH:
objPtr->internalRep.doubleValue = MakeNaN(signum, significandWide);
objPtr->typePtr = &tclDoubleType;
break;
#endif
case INITIAL:
/* This case only to silence compiler warning. */
Tcl_Panic("TclParseNumber: state INITIAL can't happen here");
}
|
| ︙ | | | ︙ | |
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
|
if (M5 - 1 > M2) {
M2 = M5 - 1;
}
}
/*
* Compute twoMv as 2*M*v, where v is the approximate value.
* This is done by bit-whacking to calculate 2**(M2+1)*significand,
* and then multiplying by 5**M5.
*/
msb = binExponent + M2; /* 1008 */
nDigits = msb / MP_DIGIT_BIT + 1;
mp_init_size(&twoMv, nDigits);
i = (msb % MP_DIGIT_BIT + 1);
|
|
|
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
|
if (M5 - 1 > M2) {
M2 = M5 - 1;
}
}
/*
* Compute twoMv as 2*M*v, where v is the approximate value.
* This is done by bit-whacking to calculate 2**(M2+1)*significand,
* and then multiplying by 5**M5.
*/
msb = binExponent + M2; /* 1008 */
nDigits = msb / MP_DIGIT_BIT + 1;
mp_init_size(&twoMv, nDigits);
i = (msb % MP_DIGIT_BIT + 1);
|
| ︙ | | | ︙ | |
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
|
for (i=0; i<=8; ++i) {
if ((M5 + exponent) & (1 << i)) {
mp_mul(&twoMd, pow5+i, &twoMd);
}
}
mp_mul_2d(&twoMd, M2+exponent+1, &twoMd);
/*
* Now let twoMd = twoMd - twoMv, the difference between the exact and
* approximate values.
*/
mp_sub(&twoMd, &twoMv, &twoMd);
/*
|
|
|
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
|
for (i=0; i<=8; ++i) {
if ((M5 + exponent) & (1 << i)) {
mp_mul(&twoMd, pow5+i, &twoMd);
}
}
mp_mul_2d(&twoMd, M2+exponent+1, &twoMd);
/*
* Now let twoMd = twoMd - twoMv, the difference between the exact and
* approximate values.
*/
mp_sub(&twoMd, &twoMv, &twoMd);
/*
|
| ︙ | | | ︙ | |
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
|
if ((rteSigWide & 1) == 0) {
mp_clear(&twoMd);
mp_clear(&twoMv);
return approxResult;
}
}
/*
* Reduce the numerator and denominator of the corrector term so that
* they will fit in the floating point precision.
*/
shift = mp_count_bits(&twoMv) - FP_PRECISION - 1;
if (shift > 0) {
mp_div_2d(&twoMv, shift, &twoMv, NULL);
mp_div_2d(&twoMd, shift, &twoMd, NULL);
|
|
|
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
|
if ((rteSigWide & 1) == 0) {
mp_clear(&twoMd);
mp_clear(&twoMv);
return approxResult;
}
}
/*
* Reduce the numerator and denominator of the corrector term so that
* they will fit in the floating point precision.
*/
shift = mp_count_bits(&twoMv) - FP_PRECISION - 1;
if (shift > 0) {
mp_div_2d(&twoMv, shift, &twoMv, NULL);
mp_div_2d(&twoMd, shift, &twoMd, NULL);
|
| ︙ | | | ︙ | |
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
|
/*
* Initialize table of powers of 10 expressed as wide integers.
*/
maxpow10_wide = (int)
floor(sizeof(Tcl_WideUInt) * CHAR_BIT * log(2.) / log(10.));
pow10_wide = Tcl_Alloc((maxpow10_wide + 1) * sizeof(Tcl_WideUInt));
u = 1;
for (i = 0; i < maxpow10_wide; ++i) {
pow10_wide[i] = u;
u *= 10;
}
pow10_wide[i] = u;
|
>
|
|
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
|
/*
* Initialize table of powers of 10 expressed as wide integers.
*/
maxpow10_wide = (int)
floor(sizeof(Tcl_WideUInt) * CHAR_BIT * log(2.) / log(10.));
pow10_wide = (Tcl_WideUInt *)
Tcl_Alloc((maxpow10_wide + 1) * sizeof(Tcl_WideUInt));
u = 1;
for (i = 0; i < maxpow10_wide; ++i) {
pow10_wide[i] = u;
u *= 10;
}
pow10_wide[i] = u;
|
| ︙ | | | ︙ | |
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
|
if (shift > 0) {
mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
mp_int d;
mp_init(&d);
mp_div_2d(a, -shift, &b, &d);
exact = d.used == 0;
mp_clear(&d);
} else {
mp_copy(a, &b);
}
if (!exact) {
mp_add_d(&b, 1, &b);
}
|
|
|
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
|
if (shift > 0) {
mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
mp_int d;
mp_init(&d);
mp_div_2d(a, -shift, &b, &d);
exact = mp_iszero(&d);
mp_clear(&d);
} else {
mp_copy(a, &b);
}
if (!exact) {
mp_add_d(&b, 1, &b);
}
|
| ︙ | | | ︙ | |