Fixed numerous bugs

This commit is contained in:
Christan Klein 2025-05-01 16:49:41 +02:00
parent 97b480ccfb
commit c4c26d9ebf

View File

@ -11,6 +11,8 @@ type Exponent = [Bool]
type Charakteristik = [Bool] type Charakteristik = [Bool]
data IEEE = ISingle | IDouble data IEEE = ISingle | IDouble
version = "v1.1"
bin2nachkomma :: Nachkomma -> Double bin2nachkomma :: Nachkomma -> Double
bin2nachkomma = bin2nachkomma =
@ -83,7 +85,9 @@ denormalisierterWert (mbin, mexponent) =
-- Reverse SequenceA: fromMaybe [] $ map (pure :: Int -> Maybe Int) <$> Just [1,0,1] -- Reverse SequenceA: fromMaybe [] $ map (pure :: Int -> Maybe Int) <$> Just [1,0,1]
-- -------------------------------------------------------------------------------------------------------------------------
-- IO -- IO
-- -------------------------------------------------------------------------------------------------------------------------
bin2dez :: String -> Double bin2dez :: String -> Double
bin2dez l = bin2dez l =
@ -170,17 +174,12 @@ destructIEEE Nothing = Nothing
-- -------------------------------------------------------------------------------------------------------------------------
-- Main Programm -- Main Programm
-- -------------------------------------------------------------------------------------------------------------------------
printHelp :: String
printHelp = unlines
[ "Hello World"
, "This is the Help"
]
printError :: String printError :: String
printError = "Invalid Arguments" printError = "Invalid Arguments\nTry ieee-helper --help to get help"
printWithDefault :: String -> (String -> String) -> [String] -> String printWithDefault :: String -> (String -> String) -> [String] -> String
printWithDefault def _ [] = def printWithDefault def _ [] = def
@ -209,17 +208,29 @@ printToIEEE (mode:value_in:_) =
] ]
printToIEEE _ = printError printToIEEE _ = printError
printIEEEToFp :: [String] -> String printDecToBin :: [String] -> String
printIEEEToFp (mode:value_in:_) = printDecToBin (p:num:_) =
fromMaybe printError $ fromMaybe printError $ printPretty <$> (dez2bin <$> readMaybe p <*> readMaybe num)
printPretty <$> where
case map toLower mode of printPretty :: String -> String
"hex" -> showPretty <$> destructIEEE (hex2Bin value_in) printPretty v = unlines
"h" -> showPretty <$> destructIEEE (hex2Bin value_in) [ "Your Decimal number \"" ++ (show $ (read num :: Double)) ++ "\" is the following in binary:"
"bin" -> showPretty <$> destructIEEE (pure value_in) , v
"b" -> showPretty <$> destructIEEE (pure value_in) , "You could also just write " ++ bin2Hex v ++ " if that is easier."
_ -> Nothing ]
printDecToBin _ = printError
printIEEEToFp :: [String] -> String
printIEEEToFp (value_in:_) =
let
(hexIdent, hVal) = splitAt 2 value_in
in
fromMaybe "That didn't work :(" $
printPretty <$>
if hexIdent == "0x" then
showPretty <$> destructIEEE (hex2Bin value_in)
else
showPretty <$> destructIEEE (pure value_in)
where where
printPretty s = unlines printPretty s = unlines
[ "After reading your IEEE754 input the value is:" [ "After reading your IEEE754 input the value is:"
@ -238,11 +249,17 @@ mainInteract (mode:args) =
"HexToBin" -> printWithDefault "HexToBin" -> printWithDefault
printError (fromMaybe printError . hex2Bin) args printError (fromMaybe printError . hex2Bin) args
"DecToBin" -> printDecToBin args
"BinToDec" -> printWithDefault
printError (("Your Number is: " ++) . show . bin2dez) args
"FpToIEEE" -> printToIEEE args "DecToIEEE" -> printToIEEE args
"IEEEToFp" -> printIEEEToFp args "IEEEToDec" -> printIEEEToFp args
"version" -> "v1.0" "-v" -> version
_ -> printHelp "--version" -> version
"-h" -> printHelp
"--help" -> printHelp
_ -> printError
mainInteract _ = printHelp mainInteract _ = printHelp
main :: IO () main :: IO ()
@ -250,7 +267,52 @@ main = do
args <- getArgs args <- getArgs
putStr $ mainInteract args putStr $ mainInteract args
printHelp :: String
printHelp = unlines
[ "IEEE Helper Tool" ,""
, "Usage:"
, " ieee-helper BinToHex <binNumber>"
, " ieee-helper HexToBin <hexNumber>"
, " ieee-helper DecToBin <precision> <decNumber>"
, " ieee-helper BinToDec <binNumber>"
, " ieee-helper DecToIEEE [(Single|s)|(Double|d)] <decNumber>"
, " ieee-helper IEEEToDec <ieee754BinNumber>"
, " ieee-helper -h | --help"
, " ieee-helper -v | --version"
, ""
, "Options:"
, " -h --help\t\tShow this help screen"
, " -v --version\t\tShow current Version"
, ""
, "Command Descriptions:"
, " BinToHex <binNumber>\t\t\t\tConvert a binary number to a hexadecimal number."
, " HexToBin <hexNumber>\t\t\t\tConvert a hexadecimal number to a binary number."
, " DecToBin <precision> <decNumber>\t\tConvert a decimal number to a binary number."
, " \t\t\t\t\t\t Precision is the number of bits after the point."
, " BinToDec <binNumber>\t\t\t\tConvert binary floating number to decimal number."
, " DecToIEEE [(Single|s)|(Double|d)] <decNumber>\tConvert a decimal number to a IEEE754 compliant number."
, " IEEEToDec <ieee754BinNumber>\t\t\tInput a IEEE754 binary number and get the decimal representation."
, ""
, "Argument Descriptions:"
, " binaryNumber\t\tBinary number. Don't seperate by spaces. E.g. 0100101101"
, " hexNumber\t\tHexadecimal number. E.g. 0x42DF"
, " precision\t\tHow many digits after the point should be calculated? E.g. 3"
, " decNumber\t\tFloating point decimal number. E.g. 3.14"
, " ieee754BinNumber\tIEEE754 compliant bin number. E.g. 01000000010010001111010111000010"
, ""
, "Examples:"
, " ieee-helper HexToBin 0x43D.54"
, " ieee-helper DecToBin 23 3.1416"
, " ieee-helper DecToIEEE Single 42.743"
, " ieee-helper IEEEToDec 0x42b844dd"
, " ieee-helper BinToHex 110110.1011001"
]
-- -------------------------------------------------------------------------------------------------------------------------
-- Helpers -- Helpers
-- -------------------------------------------------------------------------------------------------------------------------
splitOn :: Eq e => e -> [e] -> ([e], [e]) splitOn :: Eq e => e -> [e] -> ([e], [e])
splitOn e l = splitOn e l =
@ -271,9 +333,27 @@ splitFillWith index def ls =
splitAt index ls splitAt index ls
bin2Hex :: String -> String bin2Hex :: String -> String
bin2Hex = bin2Hex str =
showHex . convert . reverse . makeInt
if '.' `elem` str then
let
(vorkomma, nachkomma) = splitOn '.' str
in
"0x" ++ doConversion vorkomma
++ "." ++ doConversionR nachkomma
else if ',' `elem` str then
let
(vorkomma, nachkomma) = splitOn ',' str
in
"0x" ++ doConversion vorkomma
++ "." ++ doConversionR nachkomma
else
"0x" ++ doConversion str
where where
doConversion = showHex . convert . reverse . makeInt
doConversionR = showHex . reverse . convertR . makeInt
makeInt = map (\c -> if c == '1' then 1 else 0) makeInt = map (\c -> if c == '1' then 1 else 0)
convert [] = [] convert [] = []
@ -282,7 +362,14 @@ bin2Hex =
convert (a:b:xs) = b*2 +a : convert xs convert (a:b:xs) = b*2 +a : convert xs
convert (a:xs) = a : convert xs convert (a:xs) = a : convert xs
showHex [] = "0x" convertR [] = []
convertR (d:c:b:a:xs) = d*8 + c*4 + b*2 + a : convertR xs
convertR (d:c:b:xs) = d*8 + c*4 + b*2 : convertR xs
convertR (d:c:xs) = d*8 + c*4 : convertR xs
convertR (d:xs) = d*8 : convertR xs
showHex :: [Int] -> String
showHex [] = ""
showHex (x:xs) showHex (x:xs)
| x <= 9 = showHex xs ++ show x | x <= 9 = showHex xs ++ show x
| x == 10 = showHex xs ++ "A" | x == 10 = showHex xs ++ "A"