diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-02-11 18:50:25 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-02-11 18:50:25 +0100 |
| commit | 41b5c14e2f5d7140d7d5a1bd7201e70a8a137dcd (patch) | |
| tree | bf43eea1e72b09f5f227763c0dc8d3bb0435a183 /lib/Graphics/Fountainhead/Type.hs | |
| parent | c5f715ac7cdfb663fc84cb9fe841903b5aed99c5 (diff) | |
| download | fountainhead-41b5c14e2f5d7140d7d5a1bd7201e70a8a137dcd.tar.gz | |
Add Fixed32 numeric instances
Diffstat (limited to 'lib/Graphics/Fountainhead/Type.hs')
| -rw-r--r-- | lib/Graphics/Fountainhead/Type.hs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/Graphics/Fountainhead/Type.hs b/lib/Graphics/Fountainhead/Type.hs index e809d9c..c412b74 100644 --- a/lib/Graphics/Fountainhead/Type.hs +++ b/lib/Graphics/Fountainhead/Type.hs @@ -14,14 +14,40 @@ module Graphics.Fountainhead.Type ) where import Data.Bits ((.>>.), (.&.)) -import Data.Int (Int16) +import Data.Int (Int16, Int32) import Data.Word (Word16, Word32) import Data.Time (Day(..)) import Data.Time.Calendar.OrdinalDate (fromOrdinalDate) +import Data.Fixed (HasResolution(..)) newtype Fixed32 = Fixed32 Word32 deriving (Eq, Show) +instance Num Fixed32 + where + (Fixed32 x) + (Fixed32 y) = Fixed32 $ x + y + (Fixed32 x) - (Fixed32 y) = Fixed32 $ x - y + (Fixed32 x) * (Fixed32 y) = Fixed32 $ div (x * y) 65536 + abs (Fixed32 x) = Fixed32 $ fromIntegral $ abs (fromIntegral x :: Int32) + signum (Fixed32 x) + | x == 0 = Fixed32 0 + | (fromIntegral x :: Int32) < 0 = Fixed32 0xffff0000 + | otherwise = Fixed32 0x10000 + fromInteger x = Fixed32 $ fromInteger $ x * 65536 + +instance Ord Fixed32 + where + compare (Fixed32 x) (Fixed32 y) = + compare (fromIntegral x :: Int32) (fromIntegral y) + +instance Real Fixed32 + where + toRational (Fixed32 x) = toRational (fromIntegral x :: Int32) / 65536.0 + +instance HasResolution Fixed32 + where + resolution = const 65536 + newtype F2Dot14 = F2Dot14 Int16 deriving (Eq, Show) |
