fork download
  1. import Data.Ratio
  2. import Data.List
  3.  
  4. readFloat x = let
  5. ( sigS, exS' ) = span ( /= 'e' ) x
  6. exS = if exS' == "" then "0" else tail exS'
  7. ( hS, lS' ) = span ( /= '.' ) sigS
  8. lS = if lS' == "" then "0" else tail lS'
  9. rInt = toRational . ( read :: String -> Integer )
  10. h = rInt hS
  11. l = ( rInt lS ) * 0.1^( length lS )
  12. exI = ( read exS ) :: Integer
  13. in (h + l ) * 10^exI
  14.  
  15. x ^^^ y = if y<0 then 1/x^(-y) else x^y
  16.  
  17. rat125 x | x >= 10 || x < 1 = let
  18. e = 10 ^^ (floor $ logBase 10 $ fromRational $ x )
  19. in e * ( rat125 $ x/e)
  20. rat125 x | x >= 5 = 5
  21. rat125 x | x >= 2 = 2
  22. rat125 x = 1
  23.  
  24. fl125 = fromRational . rat125 . readFloat
  25.  
  26. main = do
  27. mapM_ print $ map (\x -> (x, fl125 x)) [
  28. show pi,
  29. show $ sin 1,
  30. show $ sqrt 2023 ,
  31. show $ 10^23,
  32. "1e23" ]
  33.  
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
("3.141592653589793",2.0)
("0.8414709848078965",0.5)
("44.97777228809804",20.0)
("100000000000000000000000",9.999999999999999e22)
("1e23",9.999999999999999e22)