Opened 13 years ago

Closed 13 years ago

Last modified 46 years ago

#104 closed bug (Duplicate)

Integer -> Int64 sometimes wrong

Reported by: magunter Owned by: nobody
Priority: normal Milestone:
Component: hslibs/lang Version: 5.04.2
Keywords: Cc:
Operating System: Architecture:
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

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

Change History (1)

comment:1 Changed 13 years ago by simonmar

  • Status changed from assigned to closed
Note: See TracTickets for help on using tickets.