{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Numeric.Rounded.Hardware.Internal.Show where
import Data.Bifunctor (first)
import Data.Bits
import Data.Char (intToDigit)
import Numeric.Floating.IEEE.Internal (countTrailingZerosInteger)
import Numeric.Rounded.Hardware.Internal.Rounding
binaryFloatToDecimalDigitsRn :: forall a. RealFloat a
=> RoundingMode
-> Int
-> a
-> ([Int], Int)
binaryFloatToDecimalDigitsRn :: RoundingMode -> Int -> a -> ([Int], Int)
binaryFloatToDecimalDigitsRn RoundingMode
_rm Int
_prec a
0 = ([], Int
0)
binaryFloatToDecimalDigitsRn RoundingMode
_rm Int
_prec a
x | a -> Integer
forall a. RealFloat a => a -> Integer
floatRadix a
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
2 = [Char] -> ([Int], Int)
forall a. HasCallStack => [Char] -> a
error [Char]
"radix must be 2"
binaryFloatToDecimalDigitsRn RoundingMode
rm Int
prec a
x =
let m :: Integer
n, d, e0 :: Int
(Integer
m,Int
n) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
d :: Int
d = a -> Int
forall a. RealFloat a => a -> Int
floatDigits a
x
e0 :: Int
e0 = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a -> a
forall a. Floating a => a -> a -> a
logBase a
10 a
2 :: a) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
prec
s, t :: Integer
(Integer
s,Integer
t) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0, Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
e0 = (Integer
m, Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
n) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
e0)
| Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
e0 = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n, Integer
10Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
e0)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
e0), Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
n))
| Bool
otherwise = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
e0), Integer
1)
q, r :: Integer
(Integer
q,Integer
r) = Integer
s Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
t
q', r', t' :: Integer
e' :: Int
(Integer
q',Integer
r',Integer
t',Int
e') | Integer
10Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
q = case Integer
q Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q'',Integer
r'') -> (Integer
q'', Integer
r''Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
tInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
r, Integer
10Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
t, Int
e0Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = (Integer
q,Integer
r,Integer
t,Int
e0)
in if Integer
r' Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
then
Int -> Integer -> ([Int], Int)
loop0 Int
e' Integer
q'
else
case RoundingMode
rm of
RoundingMode
TowardNegInf -> Int -> Integer -> ([Int], Int)
loop0 Int
e' Integer
q'
RoundingMode
TowardZero -> Int -> Integer -> ([Int], Int)
loop0 Int
e' Integer
q'
RoundingMode
TowardInf -> Int -> Integer -> ([Int], Int)
loop0 Int
e' (Integer
q' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
RoundingMode
ToNearest -> case Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Integer
2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
r') Integer
t' of
Ordering
LT -> Int -> Integer -> ([Int], Int)
loop0 Int
e' Integer
q'
Ordering
EQ | Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
q' -> Int -> Integer -> ([Int], Int)
loop0 Int
e' Integer
q'
| Bool
otherwise -> Int -> Integer -> ([Int], Int)
loop0 Int
e' (Integer
q' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
Ordering
GT -> Int -> Integer -> ([Int], Int)
loop0 Int
e' (Integer
q' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
where
loop0 :: Int -> Integer -> ([Int], Int)
loop0 :: Int -> Integer -> ([Int], Int)
loop0 !Int
_ Integer
0 = ([], Int
0)
loop0 !Int
e Integer
a = case Integer
a Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q,Integer
0) -> Int -> Integer -> ([Int], Int)
loop0 (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Integer
q
(Integer
q,Integer
r) -> Int -> [Int] -> Integer -> ([Int], Int)
loop (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r] Integer
q
loop :: Int -> [Int] -> Integer -> ([Int], Int)
loop :: Int -> [Int] -> Integer -> ([Int], Int)
loop !Int
e [Int]
acc Integer
0 = ([Int]
acc, Int
e)
loop !Int
e [Int]
acc Integer
a = case Integer
a Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q,Integer
r) -> Int -> [Int] -> Integer -> ([Int], Int)
loop (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
acc) Integer
q
{-# SPECIALIZE binaryFloatToDecimalDigitsRn :: RoundingMode -> Int -> Double -> ([Int], Int) #-}
binaryFloatToFixedDecimalDigitsRn :: forall a. RealFloat a
=> RoundingMode
-> Int
-> a
-> [Int]
binaryFloatToFixedDecimalDigitsRn :: RoundingMode -> Int -> a -> [Int]
binaryFloatToFixedDecimalDigitsRn RoundingMode
_rm Int
_prec a
x | a -> Integer
forall a. RealFloat a => a -> Integer
floatRadix a
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
2 = [Char] -> [Int]
forall a. HasCallStack => [Char] -> a
error [Char]
"radix must be 2"
binaryFloatToFixedDecimalDigitsRn RoundingMode
rm Int
prec a
x =
let m, s, t, q, r :: Integer
e :: Int
(Integer
m,Int
e) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
(Integer
s,Integer
t) | Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0, Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
prec) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
5Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
prec, Integer
1)
| Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
5Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
prec, Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
prec))
| Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = (Integer
m Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
prec), Integer
5Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
prec))
| Bool
otherwise = (Integer
m, Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
prec) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
5Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
prec))
(Integer
q,Integer
r) = Integer
s Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
t
in if Integer
r Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
then
[Int] -> Integer -> [Int]
loop [] Integer
q
else
case RoundingMode
rm of
RoundingMode
TowardNegInf -> [Int] -> Integer -> [Int]
loop [] Integer
q
RoundingMode
TowardZero -> [Int] -> Integer -> [Int]
loop [] Integer
q
RoundingMode
TowardInf -> [Int] -> Integer -> [Int]
loop [] (Integer
q Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
RoundingMode
ToNearest -> case Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Integer
2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
r) Integer
t of
Ordering
LT -> [Int] -> Integer -> [Int]
loop [] Integer
q
Ordering
EQ | Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
q -> [Int] -> Integer -> [Int]
loop [] Integer
q
| Bool
otherwise -> [Int] -> Integer -> [Int]
loop [] (Integer
q Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
Ordering
GT -> [Int] -> Integer -> [Int]
loop [] (Integer
q Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
where
loop :: [Int] -> Integer -> [Int]
loop :: [Int] -> Integer -> [Int]
loop [Int]
acc Integer
0 = [Int]
acc
loop [Int]
acc Integer
a = case Integer
a Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q,Integer
r) -> [Int] -> Integer -> [Int]
loop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
acc) Integer
q
{-# SPECIALIZE binaryFloatToFixedDecimalDigitsRn :: RoundingMode -> Int -> Double -> [Int] #-}
binaryFloatToDecimalDigits :: RealFloat a
=> a
-> ([Int], Int)
binaryFloatToDecimalDigits :: a -> ([Int], Int)
binaryFloatToDecimalDigits a
0 = ([], Int
0)
binaryFloatToDecimalDigits a
x | a -> Integer
forall a. RealFloat a => a -> Integer
floatRadix a
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
2 = [Char] -> ([Int], Int)
forall a. HasCallStack => [Char] -> a
error [Char]
"radix must be 2"
binaryFloatToDecimalDigits a
x =
let m, m', m'' :: Integer
n, z, n', e :: Int
(Integer
m,Int
n) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
z :: Int
z = Integer -> Int
countTrailingZerosInteger Integer
m
(Integer
m',Int
n') = (Integer
m Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` Int
z, Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
z)
(Integer
m'',Int
e) | Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = (Integer
m' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
5Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(-Int
n'), Int
n')
| Bool
otherwise = (Integer
m' Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n', Int
0)
in Int -> Integer -> ([Int], Int)
loop0 Int
e Integer
m''
where
loop0 :: Int -> Integer -> ([Int], Int)
loop0 :: Int -> Integer -> ([Int], Int)
loop0 !Int
_ Integer
0 = ([Int
0], Int
0)
loop0 !Int
e Integer
a = case Integer
a Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q,Integer
0) -> Int -> Integer -> ([Int], Int)
loop0 (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Integer
q
(Integer
q,Integer
r) -> Int -> [Int] -> Integer -> ([Int], Int)
loop (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r] Integer
q
loop :: Int -> [Int] -> Integer -> ([Int], Int)
loop :: Int -> [Int] -> Integer -> ([Int], Int)
loop !Int
e [Int]
acc Integer
0 = ([Int]
acc, Int
e)
loop !Int
e [Int]
acc Integer
n = case Integer
n Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
10 of
(Integer
q,Integer
r) -> Int -> [Int] -> Integer -> ([Int], Int)
loop (Int
eInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
acc) Integer
q
{-# SPECIALIZE binaryFloatToDecimalDigits :: Double -> ([Int], Int) #-}
showEFloatRn :: RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showEFloatRn :: RoundingMode -> Maybe Int -> a -> ShowS
showEFloatRn RoundingMode
r Maybe Int
mprec a
x
| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
x = [Char] -> ShowS
showString [Char]
"NaN"
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
x = Char -> ShowS
showChar Char
'-' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RoundingMode -> Maybe Int -> a -> ShowS
forall a. RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showEFloatRn (RoundingMode -> RoundingMode
oppositeRoundingMode RoundingMode
r) Maybe Int
mprec (-a
x)
| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
x = [Char] -> ShowS
showString [Char]
"Infinity"
| Bool
otherwise = let ([Int]
xs,Int
e) = case Maybe Int
mprec of
Maybe Int
Nothing -> a -> ([Int], Int)
forall a. RealFloat a => a -> ([Int], Int)
binaryFloatToDecimalDigits a
x
Just Int
prec -> let !prec' :: Int
prec' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
prec Int
0
in ([Int] -> [Int]) -> ([Int], Int) -> ([Int], Int)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Int -> [Int] -> [Int]
padRight0 (Int
prec' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (([Int], Int) -> ([Int], Int)) -> ([Int], Int) -> ([Int], Int)
forall a b. (a -> b) -> a -> b
$ RoundingMode -> Int -> a -> ([Int], Int)
forall a. RealFloat a => RoundingMode -> Int -> a -> ([Int], Int)
binaryFloatToDecimalDigitsRn RoundingMode
r Int
prec' a
x
e' :: Int
e' | (Int -> Bool) -> [Int] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) [Int]
xs = Int
0
| Bool
otherwise = Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
in case [Int]
xs of
[] -> [Char] -> ShowS
showString [Char]
"0.0e0"
[Int
0] -> [Char] -> ShowS
showString [Char]
"0e0"
[Int
d] -> case Maybe Int
mprec of
Maybe Int
Nothing -> [Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ Int -> Char
intToDigit Int
d Char -> ShowS
forall a. a -> [a] -> [a]
: Char
'.' Char -> ShowS
forall a. a -> [a] -> [a]
: Char
'0' Char -> ShowS
forall a. a -> [a] -> [a]
: Char
'e' Char -> ShowS
forall a. a -> [a] -> [a]
: Int -> [Char]
forall a. Show a => a -> [Char]
show Int
e'
Maybe Int
_ -> [Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ Int -> Char
intToDigit Int
d Char -> ShowS
forall a. a -> [a] -> [a]
: Char
'e' Char -> ShowS
forall a. a -> [a] -> [a]
: Int -> [Char]
forall a. Show a => a -> [Char]
show Int
e'
(Int
d:[Int]
ds) -> [Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ (Int -> Char
intToDigit Int
d Char -> ShowS
forall a. a -> [a] -> [a]
: Char
'.' Char -> ShowS
forall a. a -> [a] -> [a]
: (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
ds) [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Char
'e' Char -> ShowS
forall a. a -> [a] -> [a]
: Int -> [Char]
forall a. Show a => a -> [Char]
show Int
e')
where
padRight0 :: Int -> [Int] -> [Int]
padRight0 :: Int -> [Int] -> [Int]
padRight0 Int
0 [Int]
ys = [Int]
ys
padRight0 !Int
n [] = Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
n Int
0
padRight0 !Int
n (Int
y:[Int]
ys) = Int
y Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int -> [Int] -> [Int]
padRight0 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Int]
ys
{-# SPECIALIZE showEFloatRn :: RoundingMode -> Maybe Int -> Double -> ShowS #-}
showFFloatRn :: RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showFFloatRn :: RoundingMode -> Maybe Int -> a -> ShowS
showFFloatRn RoundingMode
r Maybe Int
mprec a
x
| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
x = [Char] -> ShowS
showString [Char]
"NaN"
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
x = Char -> ShowS
showChar Char
'-' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RoundingMode -> Maybe Int -> a -> ShowS
forall a. RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showFFloatRn (RoundingMode -> RoundingMode
oppositeRoundingMode RoundingMode
r) Maybe Int
mprec (-a
x)
| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
x = [Char] -> ShowS
showString [Char]
"Infinity"
| Bool
otherwise = case Maybe Int
mprec of
Maybe Int
Nothing -> let ([Int]
xs,Int
e) = a -> ([Int], Int)
forall a. RealFloat a => a -> ([Int], Int)
binaryFloatToDecimalDigits a
x
l :: Int
l = [Int] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs
in if Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l then
if [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
xs then
[Char] -> ShowS
showString [Char]
"0.0"
else
[Char] -> ShowS
showString ((Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
xs [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Char
'0' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
".0")
else
if Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then
if Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
e then
[Char] -> ShowS
showString ((Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
xs [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
".0")
else
let ([Int]
ys,[Int]
zs) = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
e [Int]
xs
ys' :: [Int]
ys' = if [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
ys then [Int
0] else [Int]
ys
in [Char] -> ShowS
showString ((Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
ys' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"." [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
zs)
else
[Char] -> ShowS
showString ([Char]
"0." [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (-Int
e) Char
'0' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
xs)
Just Int
prec -> let prec' :: Int
prec' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
prec Int
0
xs :: [Int]
xs = RoundingMode -> Int -> a -> [Int]
forall a. RealFloat a => RoundingMode -> Int -> a -> [Int]
binaryFloatToFixedDecimalDigitsRn RoundingMode
r Int
prec' a
x
l :: Int
l = [Int] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs
in if Int
prec' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then
if [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
xs then
[Char] -> ShowS
showString [Char]
"0"
else
[Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
xs
else
if Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
prec' then
[Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ [Char]
"0." [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
prec' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Char
'0' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
xs
else
let ([Int]
ys,[Int]
zs) = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
prec') [Int]
xs
ys' :: [Int]
ys' = if [Int] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
ys then [Int
0] else [Int]
ys
in [Char] -> ShowS
showString ([Char] -> ShowS) -> [Char] -> ShowS
forall a b. (a -> b) -> a -> b
$ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
ys' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"." [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Int -> Char) -> [Int] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit [Int]
zs
{-# SPECIALIZE showFFloatRn :: RoundingMode -> Maybe Int -> Double -> ShowS #-}
showGFloatRn :: RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showGFloatRn :: RoundingMode -> Maybe Int -> a -> ShowS
showGFloatRn RoundingMode
r Maybe Int
mprec a
x | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
|| (a
0.1 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a -> a
forall a. Num a => a -> a
abs a
x Bool -> Bool -> Bool
&& a -> a
forall a. Num a => a -> a
abs a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1e7) = RoundingMode -> Maybe Int -> a -> ShowS
forall a. RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showFFloatRn RoundingMode
r Maybe Int
mprec a
x
| Bool
otherwise = RoundingMode -> Maybe Int -> a -> ShowS
forall a. RealFloat a => RoundingMode -> Maybe Int -> a -> ShowS
showEFloatRn RoundingMode
r Maybe Int
mprec a
x
{-# SPECIALIZE showGFloatRn :: RoundingMode -> Maybe Int -> Double -> ShowS #-}