unwitch 1.0.0 → 2.0.0
raw patch · 9 files changed
+48/−50 lines, 9 files
Files
- Changelog.md +7/−0
- LICENSE +1/−1
- src/Unwitch/Convert/Complex.hs +0/−18
- src/Unwitch/Convert/Double.hs +8/−0
- src/Unwitch/Convert/Fixed.hs +7/−0
- test/Test/Convert/ComplexSpec.hs +0/−24
- test/Test/Convert/DoubleSpec.hs +11/−0
- test/Test/Convert/FixedSpec.hs +9/−0
- unwitch.cabal +5/−7
Changelog.md view
@@ -1,5 +1,12 @@ # Change log for unwitch project +## Version 2.0.0 ++ fix cabal file links++ delete complex module which made no sense.++## Version 1.0.0 ++ claude generate most conversion functions, looks good+ ## Version 0.1.0 Implement some of Double.
LICENSE view
@@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Jappie Klooster+Copyright (c) 2026 Jappie Klooster Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
− src/Unwitch/Convert/Complex.hs
@@ -1,18 +0,0 @@-module Unwitch.Convert.Complex- ( fromReal- , toReal- )-where--import Data.Complex (Complex((:+)), imagPart, realPart)---- | Wraps a real number as a complex number with zero imaginary part.-fromReal :: (Num a) => a -> Complex a-fromReal x = x :+ 0---- | Extracts the real part if the imaginary part is zero.-toReal :: (Eq a, Num a) => Complex a -> Maybe a-toReal c = if imagPart c == 0- then Just $ realPart c- else Nothing-
src/Unwitch/Convert/Double.hs view
@@ -1,5 +1,6 @@ module Unwitch.Convert.Double ( toFloat+ , toFixed , toRational , toInteger , toInt8@@ -20,6 +21,7 @@ where import Data.Bifunctor(first)+import Data.Fixed (Fixed, HasResolution) import Unwitch.Constant import qualified GHC.Float as F import Unwitch.Convert.Ratio(unwrapIfDenominatorOne)@@ -34,6 +36,12 @@ -- loses precision?! toFloat :: Double -> Float toFloat = F.double2Float++-- | Converts a Double to a Fixed value. Rejects NaN and infinities.+toFixed :: (HasResolution a) => Double -> Either RationalErrors (Fixed a)+toFixed double = do+ r <- toRational double+ Right $ Prelude.fromRational r data IntegerErrors = IntegerFlow Integer Overflows | RationalConversion RationalErrors
src/Unwitch/Convert/Fixed.hs view
@@ -3,6 +3,7 @@ , toInteger , toRational , toFixed+ , toDouble ) where @@ -37,4 +38,10 @@ in if Prelude.toRational target == r then Just target else Nothing++-- | Converts a Fixed value to Double. Infallible, but may lose precision+-- for values that cannot be exactly represented as a Double.+toDouble :: (HasResolution a) => Fixed a -> Double+toDouble = Prelude.fromRational . Prelude.toRational+
− test/Test/Convert/ComplexSpec.hs
@@ -1,24 +0,0 @@-module Test.Convert.ComplexSpec (spec) where--import Test.Hspec-import Data.Complex (Complex((:+)))-import qualified Unwitch.Convert.Complex as Complex--spec :: Spec-spec = describe "Unwitch.Convert.Complex" $ do-- describe "fromReal" $ do- it "wraps with zero imaginary part" $- Complex.fromReal (3.0 :: Double) `shouldBe` (3.0 :+ 0.0)- it "wraps zero" $- Complex.fromReal (0 :: Int) `shouldBe` (0 :+ 0)-- describe "toReal" $ do- it "succeeds when imaginary is 0" $- Complex.toReal (5.0 :+ 0.0 :: Complex Double) `shouldBe` Just 5.0- it "fails when imaginary is nonzero" $- Complex.toReal (5.0 :+ 1.0 :: Complex Double) `shouldBe` Nothing-- describe "round-trip" $- it "toReal . fromReal == Just" $- Complex.toReal (Complex.fromReal (42.0 :: Double)) `shouldBe` Just 42.0
test/Test/Convert/DoubleSpec.hs view
@@ -1,6 +1,7 @@ module Test.Convert.DoubleSpec (spec) where import Test.Hspec+import Data.Fixed (Fixed, E2, E6) import Data.Int import Data.Word import Numeric.Natural (Natural)@@ -81,6 +82,16 @@ Double.toNatural (-1.0) `shouldSatisfy` isLeft it "rejects fractional" $ Double.toNatural 1.5 `shouldSatisfy` isLeft++ describe "toFixed" $ do+ it "converts finite Double to Fixed" $+ Double.toFixed 1.5 `shouldBe` Right (1.50 :: Fixed E2)+ it "converts zero" $+ Double.toFixed 0.0 `shouldBe` Right (0.0 :: Fixed E6)+ it "rejects NaN" $+ Double.toFixed (0 / 0 :: Double) `shouldSatisfy` (isLeft :: Either Double.RationalErrors (Fixed E2) -> Bool)+ it "rejects Infinity" $+ Double.toFixed (1 / 0 :: Double) `shouldBe` (Left (Double.IsInf Overflow) :: Either Double.RationalErrors (Fixed E2)) isLeft :: Either a b -> Bool isLeft (Left _) = True
test/Test/Convert/FixedSpec.hs view
@@ -36,3 +36,12 @@ -- that exists in E6 but not in E2: -- 1.123 in E6 -> 1.12 in E2 loses precision Fixed.toFixed (1.123000 :: Fixed E6) `shouldBe` (Nothing :: Maybe (Fixed E2))++ describe "toDouble" $ do+ it "converts whole Fixed to Double" $+ Fixed.toDouble (42.0 :: Fixed E2) `shouldBe` 42.0+ it "converts fractional Fixed to Double" $+ Fixed.toDouble (1.50 :: Fixed E2) `shouldBe` 1.5+ it "converts zero" $+ Fixed.toDouble (0.0 :: Fixed E6) `shouldBe` 0.0+
unwitch.cabal view
@@ -1,8 +1,8 @@ cabal-version: 3.0 name: unwitch-version: 1.0.0-homepage: https://github.com/jappeace/haskell-unwitch-project#readme+version: 2.0.0+homepage: https://github.com/jappeace/unwitch#readme synopsis: converts between primitives description: Removes the magic from witch.@@ -17,10 +17,10 @@ 3. No trouble with orphans. 4. Custom errors instead of the prelude based ones allow client code to recover with typesafety even on partial conversions.-bug-reports: https://github.com/jappeace/haskell-unwitch-project/issues+bug-reports: https://github.com/jappeace/unwitch/issues author: Jappie Klooster maintainer: jappieklooster@hotmail.com-copyright: 2020 Jappie Klooster+copyright: 2026 Jappie Klooster license: MIT category: Data license-file: LICENSE@@ -33,7 +33,7 @@ source-repository head type: git- location: https://github.com/jappeace/haskell-unwitch-project+ location: https://github.com/jappeace/unwitch common common-options default-extensions:@@ -83,7 +83,6 @@ exposed-modules: Unwitch.Convert.ByteString Unwitch.Convert.Char- Unwitch.Convert.Complex Unwitch.Convert.Double Unwitch.Convert.Fixed Unwitch.Convert.Float@@ -138,7 +137,6 @@ Test.Convert.PropertySpec Test.Convert.RatioSpec Test.Convert.FixedSpec- Test.Convert.ComplexSpec Test.Convert.CharSpec Test.Convert.ShortByteStringSpec Test.Convert.TextSpec