Skip to content

numberToRangedRational: fix edge cases for exp ≈ (maxBound :: Int)

Currently a negative exponent less than minBound :: Int results in Infinity, which is very surprising and obviously wrong.

λ> read "1e-9223372036854775808" :: Double                          
0.0                                                                 
λ> read "1e-9223372036854775809" :: Double                          
Infinity

There is a further edge case where the exponent can overflow when increased by the number of tens places in the integer part, or underflow when decreased by the number of leading zeros in the fractional part if the integer part is zero:

λ> read "10e9223372036854775807" :: Double                          
0.0                                                                 
λ> read "0.01e-9223372036854775808" :: Double                       
Infinity

To resolve both of these issues, perform all arithmetic and comparisons involving the exponent in type Integer. This approach also eliminates the need to explicitly check the exponent against maxBound :: Int and minBound :: Int, because the allowed range of the exponent (i.e. the result of floatRange for the target floating point type) is certainly within those bounds.


  • if your MR may break existing programs (e.g. touches base or causes the compiler to reject programs), please describe the expected breakage and add the ~"user facing" label. This will run ghc/head.hackage> to characterise the effect of your change on Hackage.
  • ensure that your commits are either individually buildable or squashed
  • ensure that your commit messages describe what they do (referring to tickets using #NNNN syntax when appropriate)
  • have added source comments describing your change. For larger changes you likely should add a Note and cross-reference it from the relevant places.
  • add a testcase to the testsuite.
  • updates the users guide if applicable
  • mentions new features in the release notes for the next release

If you have any questions don't hesitate to open your merge request and inquire in a comment. If your patch isn't quite done yet please do add prefix your MR title with WIP:.

Once your change is ready please remove the WIP: tag and wait for review. If no one has offerred review in a few days then please leave a comment mentioning @triagers and apply the Blocked on Review label.

Edited by Fraser Tweedale

Merge request reports