ref: ed1d7d4442a02e2c31fad7f98dc7d4e545b54603
parent: 7e3092ebb2672a8cfaf0f2ed95c283fd41bf263d
author: Lennart Augustsson <lennart@augustsson.net>
date: Fri Nov 3 04:50:51 EDT 2023
Add integerToInt
--- a/lib/Data/Integer.hs
+++ b/lib/Data/Integer.hs
@@ -3,6 +3,7 @@
module Data.Integer(
Integer,
intToInteger,
+ integerToInt,
) where
import Primitives
import Control.Error
@@ -16,15 +17,6 @@
import Data.Num
import Data.Ord
import Text.Show
-{--import Prelude hiding(Integer)
-import qualified Prelude as P
-import Data.Char
-import Compat
-import Test.QuickCheck
-import GHC.Stack
-import Debug.Trace
--}
--
-- The Integer is stored in sign-magniture format with digits in base maxD (2^31)
@@ -38,6 +30,9 @@
type Digit = Int
+maxD :: Digit
+maxD = 2147483648 -- 2^31, this is used so multiplication of two digit doesn't overflow a 64 bit Int
+
data Sign = Plus | Minus
--deriving Show
@@ -93,11 +88,22 @@
f 0 = []
f x = rem x maxD : f (quot x maxD)
+integerToInt :: Integer -> Int
+integerToInt (I sign ds) = s * i
+ where
+ i =
+ case ds of
+ [] -> 0
+ [d1] -> d1
+ [d1,d2] -> d1 + maxD * d2
+ [d1,d2,d3] -> d1 + maxD * d2 + (maxD * maxD) * d3
+ s =
+ case sign of
+ Plus -> 1
+ Minus -> -1
+
zeroD :: Digit
zeroD = 0
-
-maxD :: Digit
-maxD = 2147483648 -- 2^31, this is used so multiplication of two digit doesn't overflow a 64 bit Int
addI :: Integer -> Integer -> Integer
addI (I Plus xs) (I Plus ys) = I Plus (add xs ys)
--
⑨