scientific 0.3.4.5 → 0.3.4.6
raw patch · 4 files changed
+42/−7 lines, 4 files
Files
- changelog +22/−0
- scientific.cabal +1/−1
- src/Data/Scientific.hs +4/−3
- test/test.hs +15/−3
changelog view
@@ -1,3 +1,25 @@+0.3.4.6+ * Made toDecimalDigits more similar to floatToDigits++ Previously:++ toDecimalDigits 0 == ([0],1)++ Now:++ toDecimalDigits 0 == ([0],0)++ Because:++ Numeric.floatToDigits 10 (0 :: Double) == ([0],0)+++ * Introduce a special case for 0 in fromFloatDigits++ fromFloatDigits 0 = 0++ This should fix https://github.com/bos/aeson/issues/369+ 0.3.4.5 The following are all a courtesy of Oleg Grenrus (phadej):
scientific.cabal view
@@ -1,5 +1,5 @@ name: scientific-version: 0.3.4.5+version: 0.3.4.6 synopsis: Numbers represented using scientific notation description: @Data.Scientific@ provides the number type 'Scientific'. Scientific numbers are
src/Data/Scientific.hs view
@@ -622,14 +622,15 @@ -- algorithm doesn't know in which direction the short decimal representation -- would be rounded and computes more digits fromFloatDigits :: (RealFloat a) => a -> Scientific-fromFloatDigits = positivize fromPositiveRealFloat+fromFloatDigits 0 = 0+fromFloatDigits rf = positivize fromPositiveRealFloat rf where fromPositiveRealFloat r = go digits 0 0 where (digits, e) = Numeric.floatToDigits 10 r go [] !c !n = Scientific c (e - n)- go (d:ds) !c !n = go ds (c * 10 + fromIntegral d) (n + 1)+ go (d:ds) !c !n = go ds (c * 10 + toInteger d) (n + 1) -- | Safely convert a 'Scientific' number into a 'RealFloat' (like a 'Double' or a -- 'Float').@@ -944,7 +945,7 @@ -- The last property means that the coefficient will be normalized, i.e. doesn't -- contain trailing zeros. toDecimalDigits :: Scientific -> ([Int], Int)-toDecimalDigits (Scientific 0 _) = ([0], 1)+toDecimalDigits (Scientific 0 _) = ([0], 0) toDecimalDigits (Scientific c' e') = case normalizePositive c' e' of Scientific c e -> go c 0 []
test/test.hs view
@@ -29,6 +29,7 @@ import qualified Data.Text.Lazy as TL (unpack) import qualified Data.Text.Lazy.Builder as TLB (toLazyText) import qualified Data.Text.Lazy.Builder.Scientific as T+import Numeric ( floatToDigits ) #ifdef BYTESTRING_BUILDER import qualified Data.ByteString.Lazy.Char8 as BLC8@@ -66,10 +67,16 @@ , testGroup "Formatting" [ testProperty "read . show == id" $ \s -> read (show s) === s - , smallQuick "toDecimalDigits_laws"- (SC.over nonNegativeScientificSeries toDecimalDigits_laws)- (QC.forAll nonNegativeScientificGen toDecimalDigits_laws)+ , testGroup "toDecimalDigits"+ [ smallQuick "laws"+ (SC.over nonNegativeScientificSeries toDecimalDigits_laws)+ (QC.forAll nonNegativeScientificGen toDecimalDigits_laws) + , smallQuick "== Numeric.floatToDigits"+ (toDecimalDigits_eq_floatToDigits . SC.getNonNegative)+ (toDecimalDigits_eq_floatToDigits . QC.getNonNegative)+ ]+ , testGroup "Builder" [ testProperty "Text" $ \s -> formatScientific Scientific.Generic Nothing s ==@@ -222,6 +229,11 @@ genericIsFloating :: RealFrac a => a -> Bool genericIsFloating a = fromInteger (floor a :: Integer) /= a++toDecimalDigits_eq_floatToDigits :: Double -> Bool+toDecimalDigits_eq_floatToDigits d =+ Scientific.toDecimalDigits (Scientific.fromFloatDigits d)+ == Numeric.floatToDigits 10 d conversionsProperties :: forall realFloat. ( RealFloat realFloat