-- ---------------------------------------------------------------------------
-- |
-- Module      : Text.Show.ByteString.Float
-- Copyright   : (c) 2008 Dan Doel
-- Maintainer  : Dan Doel
-- Stability   : Experimental
-- Portability : Non-portable (magic hash)
--
-- Putting floating point values.
--
-- The code in this module is heavily based on GHC.Float

module Text.Show.ByteString.Float where

import GHC.Float

import Control.Monad

import Data.Binary

import Text.Show.ByteString.Util
import Text.Show.ByteString.Int

-- | Show a signed RealFloat value using decimal notation when the
-- absolute value lies between 0.1 and 9,999,999, and scientific
-- notation otherwise. The optional integer can be used to specify
-- precision.
showpGFloat :: RealFloat a => Maybe Int -> a -> Put
showpGFloat :: Maybe Int -> a -> Put
showpGFloat = FFFormat -> Maybe Int -> a -> Put
forall a. RealFloat a => FFFormat -> Maybe Int -> a -> Put
putFormattedFloat FFFormat
FFGeneric

-- | Show a signed RealFloat value using decimal notation. The optional
-- integer can be used to specify precision.
showpFFloat :: RealFloat a => Maybe Int -> a -> Put
showpFFloat :: Maybe Int -> a -> Put
showpFFloat = FFFormat -> Maybe Int -> a -> Put
forall a. RealFloat a => FFFormat -> Maybe Int -> a -> Put
putFormattedFloat FFFormat
FFFixed

-- | Show a signed RealFloat value using scientific (exponential) notation.
-- The optional integer can be used to specify precision.
showpEFloat :: RealFloat a => Maybe Int -> a -> Put
showpEFloat :: Maybe Int -> a -> Put
showpEFloat = FFFormat -> Maybe Int -> a -> Put
forall a. RealFloat a => FFFormat -> Maybe Int -> a -> Put
putFormattedFloat FFFormat
FFExponent

putFormattedFloat :: RealFloat a => FFFormat -> Maybe Int -> a -> Put
putFormattedFloat :: FFFormat -> Maybe Int -> a -> Put
putFormattedFloat fmt :: FFFormat
fmt decs :: Maybe Int
decs f :: a
f
  | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
f                   = Char -> Put
putAscii 'N' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii 'a' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii 'N'
  | a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
f              = String -> Put
putAsciiStr (if a
f a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< 0 then "-Infinity" else "Infinity")
  | a
f a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< 0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
f = Char -> Put
putAscii '-' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> FFFormat -> ([Int], Int) -> Put
go FFFormat
fmt (Integer -> a -> ([Int], Int)
forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
base) (-a
f))
  | Bool
otherwise                 = FFFormat -> ([Int], Int) -> Put
go FFFormat
fmt (Integer -> a -> ([Int], Int)
forall a. RealFloat a => Integer -> a -> ([Int], Int)
floatToDigits (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
base) a
f)
 where
 base :: Int
base = 10

 go :: FFFormat -> ([Int], Int) -> Put
go FFGeneric p :: ([Int], Int)
p@(_,e :: Int
e)
   | Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 Bool -> Bool -> Bool
|| Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 7 = FFFormat -> ([Int], Int) -> Put
go FFFormat
FFExponent ([Int], Int)
p
   | Bool
otherwise      = FFFormat -> ([Int], Int) -> Put
go FFFormat
FFFixed    ([Int], Int)
p
 go FFExponent (is :: [Int]
is, e :: Int
e) =
   case Maybe Int
decs of
     Nothing -> case [Int]
is of
       []     -> String -> Put
forall a. HasCallStack => String -> a
error "putFormattedFloat"
       [0]    -> String -> Put
putAsciiStr "0.0e0"
       [d :: Int
d]    -> Int -> Put
unsafePutDigit Int
d Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> Put
putAsciiStr ".0e" Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put
showpInt (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)
       (d :: Int
d:ds :: [Int]
ds) -> Int -> Put
unsafePutDigit Int
d Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
ds
                                  Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii 'e' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put
showpInt (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)
     Just dec :: Int
dec ->
       let dec' :: Int
dec' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
dec 1 in
       case [Int]
is of
         [0] -> Char -> Put
putAscii '0' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put -> Put
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
dec' (Char -> Put
putAscii '0')
                  Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii 'e' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii '0'
         _   ->
           let (ei :: Int
ei, is' :: [Int]
is') = Int -> Int -> [Int] -> (Int, [Int])
roundTo Int
base (Int
dec'Int -> Int -> Int
forall a. Num a => a -> a -> a
+1) [Int]
is
               (d :: Int
d:ds :: [Int]
ds)    = if Int
ei Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 then [Int] -> [Int]
forall a. [a] -> [a]
init [Int]
is' else [Int]
is'
           in Int -> Put
unsafePutDigit Int
d Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
ds
                Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii 'e' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put
showpInt (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
ei)
 go FFFixed (is :: [Int]
is, e :: Int
e) = case Maybe Int
decs of
   Nothing
     | Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0    -> Char -> Put
putAscii '0' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Put -> Put
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ (-Int
e) (Char -> Put
putAscii '0')
                      Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
is
     | Bool
otherwise -> let g :: a -> [Int] -> Put
g 0 rs :: [Int]
rs     = Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Int] -> Put
mk0 [Int]
rs
                        g n :: a
n []     = Char -> Put
putAscii '0' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> [Int] -> Put
g (a
na -> a -> a
forall a. Num a => a -> a -> a
-1) []
                        g n :: a
n (r :: Int
r:rs :: [Int]
rs) = Int -> Put
unsafePutDigit Int
r Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> [Int] -> Put
g (a
na -> a -> a
forall a. Num a => a -> a -> a
-1) [Int]
rs
                    in Int -> [Int] -> Put
forall a. (Eq a, Num a) => a -> [Int] -> Put
g Int
e [Int]
is
   Just dec :: Int
dec ->
     let dec' :: Int
dec' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
dec 0 in
     if Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0 then
       let (ei :: Int
ei, is' :: [Int]
is') = Int -> Int -> [Int] -> (Int, [Int])
roundTo Int
base (Int
dec' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
e) [Int]
is
           (ls :: [Int]
ls,rs :: [Int]
rs)   = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
ei) [Int]
is'
       in [Int] -> Put
mk0 [Int]
ls Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Put -> Put
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
rs) (Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
rs)
     else
       let (ei :: Int
ei, is' :: [Int]
is') = Int -> Int -> [Int] -> (Int, [Int])
roundTo Int
base Int
dec' (Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate (-Int
e) 0 [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int]
is)
           d :: Int
d:ds :: [Int]
ds      = if Int
ei Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 then [Int]
is' else 0Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
is'
       in Int -> Put
unsafePutDigit Int
d Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Put -> Put
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
ds) (Char -> Put
putAscii '.' Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
ds)

 mk0 :: [Int] -> Put
mk0 [] = Char -> Put
putAscii '0'
 mk0 rs :: [Int]
rs = (Int -> Put) -> [Int] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> Put
unsafePutDigit [Int]
rs