Changes between Initial Version and Version 1 of Ticket #4441


Ignore:
Timestamp:
Oct 26, 2010 1:56:11 PM (5 years ago)
Author:
dterei
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #4441 – Description

    initial v1  
    77module Main where
    88
    9 import Data.Array.ST
    10 import Control.Monad.ST
    11 
    12 import Data.Word
    13 import Data.Char
    149import Numeric
    1510import System.IO
     
    2823   return $ read x
    2924
    30 -----------------------------------------------------------------------------
    31 -- * Floating point conversion
    32 --
    33 
    3425dToStr :: Double -> String
    35 dToStr d
    36  = let bs     = doubleToBytes d
    37        hex d' = case showHex d' "" of
    38                     []    -> error "dToStr: too few hex digits for float"
    39                     [x]   -> ['0',x]
    40                     [x,y] -> [x,y]
    41                     _     -> error "dToStr: too many hex digits for float"
    42        str  = map toUpper $ concat . reverse . (map hex) $ bs
    43    in  "0x" ++ str
     26dToStr d = show d
    4427
    4528fToStr :: Float -> String
    4629fToStr = (dToStr . realToFrac)
    47 
    48 -- -----------------------------------------------------------------------------
    49 -- Converting floating-point literals to integrals for printing
    50 
    51 castDoubleToWord8Array :: STUArray s Int Double -> ST s (STUArray s Int Word8)
    52 castDoubleToWord8Array = castSTUArray
    53 
    54 doubleToBytes :: Double -> [Int]
    55 doubleToBytes d
    56   = runST (do
    57        arr <- newArray_ ((0::Int),7)
    58        writeArray arr 0 d
    59        arr <- castDoubleToWord8Array arr
    60        i0 <- readArray arr 0
    61        i1 <- readArray arr 1
    62        i2 <- readArray arr 2
    63        i3 <- readArray arr 3
    64        i4 <- readArray arr 4
    65        i5 <- readArray arr 5
    66        i6 <- readArray arr 6
    67        i7 <- readArray arr 7
    68        return (map fromIntegral [i0,i1,i2,i3,i4,i5,i6,i7])
    69     )
    7030}}}
    7131
     
    7434$ ./TestCase
    7535  Enter a double: 2.0e-2
    76   Float Version : 0x3F947AE147AE147B
    77   Double Version: 0x3F947AE147AE147B
     36  Float Version : 2.0e-2
     37  Double Version: 2.0e-2
    7838}}}
    7939
     
    8242$ ./TestCase
    8343  Enter a double: 2.0e-2
    84   Float Version : 0x3F947AE140000000
    85   Double Version: 0x3F947AE147AE147B
     44  Float Version : 1.9999999552965164e-2
     45  Double Version: 2.0e-2
    8646}}}