[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 15:11:31 UTC 2010


The following commit has been merged in the master branch:
commit db0096e1b9301390073449a26b0d6a7030eeb5ec
Author: John Goerzen <jgoerzen at complete.org>
Date:   Sat Oct 14 03:32:56 2006 +0100

    Rename to quantity.hs

diff --git a/MissingH/Size.hs b/MissingH/Quantity.hs
similarity index 56%
rename from MissingH/Size.hs
rename to MissingH/Quantity.hs
index fe4ef13..290fe5a 100644
--- a/MissingH/Size.hs
+++ b/MissingH/Quantity.hs
@@ -29,30 +29,42 @@ Tools for rendering sizes
 
 Written by John Goerzen, jgoerzen\@complete.org -}
 
-module MissingH.Size (
+module MissingH.Size (quantifyNum,
+                      renderNum,
+                      SizeOpts(..),
+                      binaryOpts,
+                      siOpts
                      )
 
 where
 import Data.List
+import Text.Printf
 
-data SizeOpts = SizeOpts { base :: Int,
-                           powerIncr :: Int,
-                           firstPower :: Int,
-                           suffixes :: String}
+{- | The options for 'quantifyNum' and 'renderNum' -}
+data SizeOpts = SizeOpts { base :: Int, -- ^ The base from which calculations are made
+                           powerIncr :: Int, -- ^ The increment to the power for each new suffix
+                           firstPower :: Int, -- ^ The first power for which suffixes are given
+                           suffixes :: String -- ^ The suffixes themselves
+                         }
                            
+{- | Predefined definitions for byte measurement in groups of 1024, from 0 to
+2**80 -}
 binaryOpts = SizeOpts {base = 2,
                        firstPower = 0,
                        suffixes = " KMGTPEZY",
                        powerIncr = 10}
 
+{- | Predefined definitions for SI measurement, from 10**-24 to 10**24. -}
 siOpts = SizeOpts {base = 10,
                    firstPower = -24,
                    suffixes = "yzafpnum kMGTPEZY",
                    powerIncr = 3}
 
-renderNum :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> a -> (b, Char)
-renderNum opts 0 = (0, snd $ renderNum opts 1)
-renderNum opts inpnumber 
+{- | Takes a number and returns a new (quantity, suffix) combination.
+The space character is used as the suffix for items around 0. -}
+quantifyNum :: (Ord a, Real a, Floating b, Ord b) => SizeOpts -> a -> (b, Char)
+quantifyNum opts 0 = (0, snd $ quantifyNum opts 1)
+quantifyNum opts inpnumber 
     | inpnumber < 0 = 
         (posres * (-1), possuf)
     | otherwise = (retnum, suffix)
@@ -62,10 +74,23 @@ renderNum opts inpnumber
           idx2pwr i = i * powerIncr opts + firstPower opts
           finderfunc (x, _) = (fromIntegral $ base opts) ** (fromIntegral x) 
                               <= number
+          -- Find the largest item that does not exceed the number given.
+          -- If the number is larger than the larger item in the list,
+          -- that's fine; we'll just write it in terms of what we have.
+
           (usedexp, expidx) =
               case find finderfunc (reverse incrIdxList) of
                   Just x -> x
                   Nothing -> head incrIdxList -- If not found, it's smaller than the first
           suffix = (suffixes opts !! (fromIntegral expidx))
           retnum = number / ((fromIntegral (base opts) ** (fromIntegral usedexp)))
-          (posres, possuf) = renderNum opts (inpnumber * (-1))
\ No newline at end of file
+          (posres, possuf) = quantifyNum opts (inpnumber * (-1))
+
+renderNum :: (Ord a, Real a) => 
+             SizeOpts
+          -> Int                -- ^ Precision of the result
+          -> a                  -- ^ The number to examine
+          -> String
+renderNum opts prec number =
+    (printf ("%." ++ show prec ++ "g") num) ++ [suffix]
+    where (num, suffix) = (quantifyNum opts number)::(Double, Char)
\ No newline at end of file

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list