formatting icon indicating copy to clipboard operation
formatting copied to clipboard

float fails negative integral-value numbers?

Open iustin opened this issue 3 months ago • 0 comments

Maybe I'm doing something wrong, but:

ghci> sformat float (-1::Int)
"*** Exception: Numeric.showInt: can't show negative numbers
ghci> sformat float (-1::Int)
"*** Exception: Numeric.showInt: can't show negative numbers
ghci> sformat float (-1::Double)
"*** Exception: Numeric.showInt: can't show negative numbers
ghci> sformat float (-100::Double)
"*** Exception: Numeric.showInt: can't show negative numbers
ghci> sformat float (-100.0001::Double)
"-100.0001"

Looks like the code in Data.Text.Formatting, more precisely this:

      -- `showFFloat (Just 0) "" 1.0` gives "1.", but we want "1"
      let intPart = (floor dbl :: Int) in
        if dbl == (fromIntegral intPart)
          then showInt intPart ""
          else showFFloat Nothing dbl ""

Is ignoring the fact that showInt only works on non-negative integrals.

I don't understand the comment about showFFloat behaviour, I see it differently:

ghci> showFFloat Nothing (-1.0::Double) ""
"-1.0"
ghci> showFFloat Nothing (1.0::Double) ""
"1.0"
ghci> showFFloat Nothing (1.05::Double) ""
"1.05"
ghci> showFFloat Nothing (-1.05::Double) ""
"-1.05"
ghci> showFFloat (Just 0) (-1.05::Double) ""
"-1"
ghci> showFFloat (Just 0) (-1.0::Double) ""
"-1"
ghci> showFFloat (Just 0) (1.0::Double) ""
"1"

So maybe with modern base, the showInt call can be removed, and all this simplified to showFFloat?

iustin avatar Sep 21 '25 09:09 iustin