Ticket #104 (closed bug: Duplicate)

Opened 10 years ago

Last modified 43 years ago

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: Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

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

Changed 10 years ago by simonmar

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