summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBasVanDijk <>2017-07-06 22:27:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-07-06 22:27:00 (GMT)
commit390ef63ca6f2a22f858c1d691a1714c0aa3966e5 (patch)
treef5fd8b9607113476d59bc0d0ee9bbd4674ea692e
parent1a16a9b96358b05bb2c7d2ade21bbf1a466ad11b (diff)
version 0.3.5.00.3.5.0
-rw-r--r--changelog4
-rw-r--r--scientific.cabal4
-rw-r--r--src/Data/Scientific.hs28
3 files changed, 33 insertions, 3 deletions
diff --git a/changelog b/changelog
index 1b1472b..cc10ff1 100644
--- a/changelog
+++ b/changelog
@@ -1,3 +1,7 @@
+0.3.5.0
+ * Export scientificP :: ReadP Scientific
+ (Courtesy of Shlok Datye @shlok)
+
0.3.4.15
* Fix build for base < 4.8.
diff --git a/scientific.cabal b/scientific.cabal
index 8f00b2e..b40d3c2 100644
--- a/scientific.cabal
+++ b/scientific.cabal
@@ -1,5 +1,5 @@
name: scientific
-version: 0.3.4.15
+version: 0.3.5.0
synopsis: Numbers represented using scientific notation
description:
@Data.Scientific@ provides the number type 'Scientific'. Scientific numbers are
@@ -106,7 +106,7 @@ test-suite test-scientific
, tasty-smallcheck >= 0.2 && < 0.9
, tasty-quickcheck >= 0.8 && < 0.9
, smallcheck >= 1.0 && < 1.2
- , QuickCheck >= 2.5 && < 2.10
+ , QuickCheck >= 2.5 && < 2.11
, text >= 0.8 && < 1.3
if flag(bytestring-builder)
diff --git a/src/Data/Scientific.hs b/src/Data/Scientific.hs
index a7c126e..e0563cc 100644
--- a/src/Data/Scientific.hs
+++ b/src/Data/Scientific.hs
@@ -74,6 +74,9 @@ module Data.Scientific
, toBoundedInteger
, fromFloatDigits
+ -- * Parsing
+ , scientificP
+
-- * Pretty printing
, formatScientific
, FPFormat(..)
@@ -104,7 +107,7 @@ import Data.Ratio ((%), numerator, denominator)
import Data.Typeable (Typeable)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
-import Data.Word (Word, Word8, Word16, Word32, Word64)
+import Data.Word (Word8, Word16, Word32, Word64)
import Math.NumberTheory.Logarithms (integerLog10')
import qualified Numeric (floatToDigits)
import qualified Text.Read as Read
@@ -116,6 +119,7 @@ import Data.Text.Lazy.Builder.RealFloat (FPFormat(..))
#if !MIN_VERSION_base(4,8,0)
import Data.Functor ((<$>))
+import Data.Word (Word)
import Control.Applicative ((<*>))
#endif
@@ -786,12 +790,34 @@ isInteger s = base10Exponent s >= 0 ||
-- Parsing
----------------------------------------------------------------------
+-- | Supports the skipping of parentheses and whitespaces. Example:
+--
+-- > > read " ( (( -1.0e+3 ) ))" :: Scientific
+-- > -1000.0
+--
+-- (Note: This @Read@ instance makes internal use of
+-- 'scientificP' to parse the floating-point number.)
instance Read Scientific where
readPrec = Read.parens $ ReadPrec.lift (ReadP.skipSpaces >> scientificP)
-- A strict pair
data SP = SP !Integer {-# UNPACK #-}!Int
+-- | A parser for parsing a floating-point
+-- number into a 'Scientific' value. Example:
+--
+-- > > import Text.ParserCombinators.ReadP (readP_to_S)
+-- > > readP_to_S scientificP "3"
+-- > [(3.0,"")]
+-- > > readP_to_S scientificP "3.0e2"
+-- > [(3.0,"e2"),(300.0,"")]
+-- > > readP_to_S scientificP "+3.0e+2"
+-- > [(3.0,"e+2"),(300.0,"")]
+-- > > readP_to_S scientificP "-3.0e-2"
+-- > [(-3.0,"e-2"),(-3.0e-2,"")]
+--
+-- Note: This parser only parses the number itself; it does
+-- not parse any surrounding parentheses or whitespaces.
scientificP :: ReadP Scientific
scientificP = do
let positive = (('+' ==) <$> ReadP.satisfy isSign) `mplus` return True