Skip to content

Integer -> Int64 sometimes wrong

Conversion from Integer to Int64 values sometimes
produces the wrong
value.  In particular, it seems to fail on
small-magnitude negative
values whose representation is large.  E.g.

  > (fromIntegral ((2^30 -1 + 2^30) - (2^30 + 2^30 ::
Integer))) :: Data.Int.Int64
  4294967295
  > (fromIntegral ((2^30 - 2 + 2^30) - (2^30 - 1 + 2^30
:: Integer))) :: Data.Int.Int64
  -1

The Haskell-level workaround is to do the conversion as
a non-negative
number then negate if needed:

  > let fis (i::Integer) = (fromIntegral (signum i) *
fromIntegral (abs i)) :: Data.Int.Int64
  > fis ((2^30 -1 + 2^30) - (2^30 + 2^30 :: Integer))
  -1
Trac metadata
Trac field Value
Version 5.04.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution ResolvedDuplicate
Component hslibs/lang
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information