@@ -896,7 +896,7 @@ BigInt_ShiftLeft(BigInt *result, npy_uint32 shift)
896
896
if (shiftBits == 0 ) {
897
897
npy_uint32 i ;
898
898
899
- /* copy blcoks from high to low */
899
+ /* copy blocks from high to low */
900
900
for (pInCur = result -> blocks + result -> length ,
901
901
pOutCur = pInCur + shiftBlocks ;
902
902
pInCur >= pInBlocks ;
@@ -1002,7 +1002,7 @@ BigInt_ShiftLeft(BigInt *result, npy_uint32 shift)
1002
1002
* * exponent - value exponent in base 2
1003
1003
* * mantissaBit - index of the highest set mantissa bit
1004
1004
* * hasUnequalMargins - is the high margin twice as large as the low margin
1005
- * * cutoffMode - how to intepret cutoffNumber: fractional or total digits?
1005
+ * * cutoffMode - how to interpret cutoffNumber: fractional or total digits?
1006
1006
* * cutoffNumber - cut off printing after this many digits. -1 for no cutoff
1007
1007
* * pOutBuffer - buffer to output into
1008
1008
* * bufferSize - maximum characters that can be printed to pOutBuffer
@@ -1381,7 +1381,7 @@ Dragon4(const npy_uint64 mantissa, const npy_int32 exponent,
1381
1381
1382
1382
/*
1383
1383
* if we are directly in the middle, round towards the even digit (i.e.
1384
- * IEEE rouding rules)
1384
+ * IEEE rounding rules)
1385
1385
*/
1386
1386
if (compare == 0 ) {
1387
1387
roundDown = (outputDigit & 1 ) == 0 ;
@@ -1588,12 +1588,12 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1588
1588
npy_int32 digits_right )
1589
1589
{
1590
1590
npy_int32 printExponent ;
1591
- npy_int32 numDigits , numWholeDigits , has_sign = 0 ;
1591
+ npy_int32 numDigits , numWholeDigits = 0 , has_sign = 0 ;
1592
1592
1593
- npy_int32 maxPrintLen = bufferSize - 1 , pos = 0 ;
1593
+ npy_int32 maxPrintLen = ( npy_int32 ) bufferSize - <
8000
span class="pl-c1">1, pos = 0 ;
1594
1594
1595
1595
/* track the # of digits past the decimal point that have been printed */
1596
- npy_int32 numFractionDigits = 0 ;
1596
+ npy_int32 numFractionDigits = 0 , desiredFractionalDigits ;
1597
1597
1598
1598
DEBUG_ASSERT (bufferSize > 0 );
1599
1599
@@ -1637,11 +1637,11 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1637
1637
}
1638
1638
}
1639
1639
/* insert the decimal point prior to the fraction */
1640
- else if (numDigits > ( npy_uint32 ) numWholeDigits ) {
1641
- npy_uint32 maxFractionDigits ;
1640
+ else if (numDigits > numWholeDigits ) {
1641
+ npy_int32 maxFractionDigits ;
1642
1642
1643
1643
numFractionDigits = numDigits - numWholeDigits ;
1644
- maxFractionDigits = maxPrintLen - numWholeDigits - 1 - pos ;
1644
+ maxFractionDigits = maxPrintLen - numWholeDigits - 1 - pos ;
1645
1645
if (numFractionDigits > maxFractionDigits ) {
1646
1646
numFractionDigits = maxFractionDigits ;
1647
1647
}
@@ -1656,19 +1656,20 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1656
1656
}
1657
1657
else {
1658
1658
/* shift out the fraction to make room for the leading zeros */
1659
- npy_uint32 numFractionZeros = 0 ;
1659
+ npy_int32 numFractionZeros = 0 ;
1660
1660
if (pos + 2 < maxPrintLen ) {
1661
- npy_uint32 maxFractionZeros , digitsStartIdx , maxFractionDigits , i ;
1661
+ npy_int32 maxFractionZeros , digitsStartIdx , maxFractionDigits , i ;
1662
1662
1663
1663
maxFractionZeros = maxPrintLen - 2 - pos ;
1664
- numFractionZeros = ( npy_uint32 ) - printExponent - 1 ;
1664
+ numFractionZeros = - ( printExponent + 1 ) ;
1665
1665
if (numFractionZeros > maxFractionZeros ) {
1666
1666
numFractionZeros = maxFractionZeros ;
1667
1667
}
1668
1668
1669
1669
digitsStartIdx = 2 + numFractionZeros ;
1670
1670
1671
- /* shift the significant digits right such that there is room for
1671
+ /*
1672
+ * shift the significant digits right such that there is room for
1672
1673
* leading zeros
1673
1674
*/
1674
1675
numFractionDigits = numDigits ;
@@ -1710,6 +1711,11 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1710
1711
buffer [pos ++ ] = '.' ;
1711
1712
}
1712
1713
1714
+ desiredFractionalDigits = precision ;
1715
+ if (cutoff_mode == CutoffMode_TotalLength && precision >= 0 ) {
1716
+ desiredFractionalDigits = precision - numWholeDigits ;
1717
+ }
1718
+
1713
1719
if (trim_mode == TrimMode_LeaveOneZero ) {
1714
1720
/* if we didn't print any fractional digits, add a trailing 0 */
1715
1721
if (numFractionDigits == 0 && pos < maxPrintLen ) {
@@ -1718,11 +1724,12 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1718
1724
}
1719
1725
}
1720
1726
else if (trim_mode == TrimMode_None &&
1721
- digit_mode != DigitMode_Unique &&
1722
- precision > (npy_int32 )numFractionDigits && pos < maxPrintLen ) {
1727
+ digit_mode != DigitMode_Unique &&
1728
+ desiredFractionalDigits > numFractionDigits &&
1729
+ pos < maxPrintLen ) {
1723
1730
/* add trailing zeros up to precision length */
1724
1731
/* compute the number of trailing zeros needed */
1725
- npy_uint32 count = precision - numFractionDigits ;
1732
+ npy_int32 count = desiredFractionalDigits - numFractionDigits ;
1726
1733
if (pos + count > maxPrintLen ) {
1727
1734
count = maxPrintLen - pos ;
1728
1735
}
@@ -1751,7 +1758,7 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1751
1758
1752
1759
/* add any whitespace padding to right side */
1753
1760
if (digits_right >= numFractionDigits ) {
1754
- npy_uint32 count = digits_right - numFractionDigits ;
1761
+ npy_int32 count = digits_right - numFractionDigits ;
1755
1762
1756
1763
/* in trim_mode DptZeros, if right padding, add a space for the . */
1757
1764
if (trim_mode == TrimMode_DptZeros && numFractionDigits == 0
@@ -1769,8 +1776,8 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1769
1776
}
1770
1777
/* add any whitespace padding to left side */
1771
1778
if (digits_left > numWholeDigits + has_sign ) {
1772
- npy_uint32 shift = digits_left - (numWholeDigits + has_sign );
1773
- npy_uint32 count = pos ;
1779
+ npy_int32 shift = digits_left - (numWholeDigits + has_sign );
1780
+ npy_int32 count = pos ;
1774
1781
1775
1782
if (count + shift > maxPrintLen ){
1776
1783
count = maxPrintLen - shift ;
@@ -1781,7 +1788,7 @@ FormatPositional(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1781
1788
}
1782
1789
pos = shift + count ;
1783
1790
for ( ; shift > 0 ; shift -- ) {
1784
- buffer [shift - 1 ] = ' ' ;
1791
+ buffer [shift - 1 ] = ' ' ;
1785
1792
}
1786
1793
}
1787
1794
@@ -1871,7 +1878,8 @@ FormatScientific (char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1871
1878
/* insert the decimal point prior to the fractional number */
1872
1879
numFractionDigits = numDigits - 1 ;
1873
1880
if (numFractionDigits > 0 && bufferSize > 1 ) {
1874
- npy_uint32 maxFractionDigits = bufferSize - 2 ;
1881
+ npy_int32 maxFractionDigits = (npy_int32 )bufferSize - 2 ;
1882
+
1875
1883
if (numFractionDigits > maxFractionDigits ) {
1876
1884
numFractionDigits = maxFractionDigits ;
1877
1885
}
@@ -1905,9 +1913,10 @@ FormatScientific (char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1905
1913
if (precision > (npy_int32 )numFractionDigits ) {
1906
1914
char * pEnd ;
1907
1915
/* compute the number of trailing zeros needed */
1908
- npy_uint32 numZeros = (precision - numFractionDigits );
1909
- if (numZeros > bufferSize - 1 ) {
1910
- numZeros = bufferSize - 1 ;
1916
+ npy_int32 numZeros = (precision - numFractionDigits );
1917
+
1918
+ if (numZeros > (npy_int32 )bufferSize - 1 ) {
1919
+ numZeros = (npy_int32 )bufferSize - 1 ;
1911
1920
}
1912
1921
1913
1922
for (pEnd = pCurOut + numZeros ; pCurOut < pEnd ; ++ pCurOut ) {
@@ -1941,7 +1950,7 @@ FormatScientific (char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1941
1950
/* print the exponent into a local buffer and copy into output buffer */
1942
1951
if (bufferSize > 1 ) {
1943
1952
char exponentBuffer [7 ];
1944
- npy_uint32 digits [5 ];
1953
+ npy_int32 digits [5 ];
1945
1954
npy_int32 i , exp_size , count ;
1946
1955
1947
1956
if (exp_digits > 5 ) {
@@ -1978,8 +1987,8 @@ FormatScientific (char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa,
1978
1987
1979
1988
/* copy the exponent buffer into the output */
1980
1989
count = exp_size + 2 ;
1981
- if (count > bufferSize - 1 ) {
1982
- count = bufferSize - 1 ;
1990
+ if (count > ( npy_int32 ) bufferSize - 1 ) {
1991
+ count = ( npy_int32 ) bufferSize - 1 ;
1983
1992
}
1984
1993
memcpy (pCurOut , exponentBuffer , count );
1985
1994
pCurOut += count ;
0 commit comments