1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
|
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; /* Machine exponent of a power of 10. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
* This causes the result of double-precision calculations to be rounded
* twice: once to the precision of double-extended and then again to the
* precision of double. Double-rounding introduces gratuitous errors of 1
* ulp, so we need to change rounding mode to 53-bits. We also make
|
>
>
|
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
|
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; /* Machine exponent of a power of 10. */
int shift, n;
mp_int bntmp;
/*
* With gcc on x86, the floating point rounding mode is double-extended.
* This causes the result of double-precision calculations to be rounded
* twice: once to the precision of double-extended and then again to the
* precision of double. Double-rounding introduces gratuitous errors of 1
* ulp, so we need to change rounding mode to 53-bits. We also make
|
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
|
* Develop a first approximation to the significand. It is tempting simply
* to force bignum to double, but that will overflow on input numbers like
* 1.[string repeat 0 1000]1; while this is a not terribly likely
* scenario, we still have to deal with it. Use fraction and exponent
* instead. Once we have the significand, multiply by 10**exponent. Test
* for overflow. Convert back to a double, and test for underflow.
*/
retval = BignumToBiasedFrExp(significand, &machexp);
retval = Pow10TimesFrExp(exponent, retval, &machexp);
if (machexp > DBL_MAX_EXP*log2FLT_RADIX) {
retval = HUGE_VAL;
goto returnValue;
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
|
* Develop a first approximation to the significand. It is tempting simply
* to force bignum to double, but that will overflow on input numbers like
* 1.[string repeat 0 1000]1; while this is a not terribly likely
* scenario, we still have to deal with it. Use fraction and exponent
* instead. Once we have the significand, multiply by 10**exponent. Test
* for overflow. Convert back to a double, and test for underflow.
*/
if (exponent < -511) {
mp_init_copy(&bntmp, significand);
shift = -exponent - 511;
exponent += shift;
while (shift > 0) {
n = (shift > 9) ? 9 : shift;
mp_div_d(&bntmp, (mp_digit) pow10_wide[n], &bntmp, NULL);
shift -= n;
}
significand = &bntmp;
} else if (exponent > 511) {
mp_init_copy(&bntmp, significand);
shift = exponent - 511;
exponent -= shift;
while (shift > 0) {
n = (shift > 9) ? 9 : shift;
mp_mul_d(&bntmp, (mp_digit) pow10_wide[n], &bntmp);
shift -= n;
}
significand = &bntmp;
}
retval = BignumToBiasedFrExp(significand, &machexp);
retval = Pow10TimesFrExp(exponent, retval, &machexp);
if (machexp > DBL_MAX_EXP*log2FLT_RADIX) {
retval = HUGE_VAL;
goto returnValue;
}
|
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
|
retval = RefineApproximation(retval, significand, exponent);
/*
* Come here to return the computed value.
*/
returnValue:
if (signum) {
retval = -retval;
}
/*
* On gcc on x86, restore the floating point mode word.
*/
|
>
>
>
|
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
|
retval = RefineApproximation(retval, significand, exponent);
/*
* Come here to return the computed value.
*/
returnValue:
if (significand == &bntmp) {
mp_clear(&bntmp);
}
if (signum) {
retval = -retval;
}
/*
* On gcc on x86, restore the floating point mode word.
*/
|