Repository navigation
Fix rational and double on exponents beyond Int and on integer mantissas - #725
Conversation
The exponent was read as an Int and wrapped: "1e18446744073709551616" gave 1.0, and "1e9223372036854775808" threw "Negative exponent", since the exponent became minBound. Read it as an Integer and clamp it to the Int range; a larger exponent gives zero or infinity anyway. Without a fractional part, floaty computed `fromInteger real * 10 ^^ power` in the result type, so rational lost its exact path: "0e400" gave NaN and "5e-324" gave 0.0. Pass these to the reader's own function too.
|
What's the rationale for clamping instead of returning an error? |
|
Could a compromise be to go through the exact path only if |
Computing the numerator and denominator as integers and making the Rational once avoids the gcd at every step of 10 ^^ power in Rational. ReadNumbers: RationalText 52.9 -> 31.3 ms, RationalLazyText 70.7 -> 48.5 ms against master.
|
On the shortcut: it isn't exact for small exponents either. For random |
|
Yes, let's make it an error when the exponent overflows an |
|
I'm thinking we should even reject exponents > 1,000,000 because for I'm not sure whether (1) there are uses of |
|
I don't think there exists a robust way to make usage of untrusted It is a responsibility of a user to choose appropriate data types. If they want to be safe from DDoS, they should not be using |
An exponent with more significant digits than maxBound is not converted, so a long exponent is still read in linear time. rational compares the exponent with two comparisons instead of abs.
|
Done in 487e53d: an exponent outside the |
An exponent with more than (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 significant digits is out of range, so the digits that are converted always fit in an Int, and toExponent's Integer bounds and minBound clamp are no longer needed. A longer exponent is read as maxBound and rejected after the perhaps around the exponent, which would otherwise turn the error into "no exponent".
|
Thanks! Applied in a0a479e: the exponent is now read as an One difference from the suggestion: |
Use catchOrBind so that only a missing "e" falls back to no exponent, go back to before the "e" when no digits follow it, and fail directly when there are too many significant digits, instead of reading them as maxBound and checking that afterwards.
Leading zeros don't change the value, so the digits can be folded as they are; the zeros are only dropped for the length check.
…ents On 32-bit platforms an exponent may have at most 9 significant digits, so the large exponent tests now use 1.1e100000000. The long exponent tests timed out under emulation (0.12 s against 0.1 s) with 100,000 digits; 10,000 is still far out of range.
|
The i386, armv7 and emulated jobs failed on the tests, fixed in 8a61f2f:
|
Two problems in
floaty, in bothData.Text.ReadandData.Text.Lazy.Read.1. The exponent is read as an
Intand wraps.The last one throws from a pure function: the exponent wraps to
minBound,abs minBoundis still negative, and10 ^^ powerraises. An exponent with more significant digits than always fit in anInt(18 on 64-bit) is now an error (Left "exponent out of range"). Such digits aren't converted, so an exponent like1efollowed by 100,000 digits is rejected in linear time.2. Without a fractional part,
rationalskipped its exact path.floatycomputedfromInteger real * (10 ^^ power)in the result type, while1.0e…goes throughf:rationalread0e4001000e-3101000.0e-310already gives this)5e-32417976931348623157e292These now go through
ftoo.doublegives the same results as before for them (it adds0 / 1);double "0e400"is stillNaN, as0 * InfinityinDouble, and I left that alone.Tests: a property comparing
rationalwithreadon<integer>e<exponent>(the existing ones useshow, which always includes a.), and cases in the DDoS group for the largest exponents accepted and the smallest rejected (built fromfiniteBitSize, so they also hold on 32-bit) and for 100,000-digit exponents.The new tests fail on master (8 of them); the rest of the suite is unchanged (
t714fails on my Windows machine on master too).Benchmarks (
ReadNumbersagainst master, on a localnumbers.txtof 100,000 mixed numbers, sincetext-test-dataisn't in the repo):doubleand the integer readers are unchanged;rationalis 42% faster (lazy: 29%), as it now builds the exact value fromIntegers once instead of withRationalarithmetic in10 ^^ power.