Syntax error Haskell Program to convert int type variables to double

Haskell Program to convert int type variables to double



In Haskell, we will convert int type variables into double by using user-defined function, intToDouble along with fromIntegral, realToFrac and realPart functions. In the first example, we are going to use (intToDouble n = fromIntegral n) function and in the second example, we are going to use (intToDouble n = realToFrac n). And in the third example, we are going to use (intToDouble n = realPart (fromIntegral n :+ 0)).

Algorithm

  • Step 1 ? The intToDouble function is defined using fromIntegral function.

  • Step 2 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 3 ? The variable named, "n" is being initialized. It will hold the integer that is to be converted to respective double value.

  • Step 4 ? The function, intToDouble is being called and n is passed to it.

  • Step 5 ? The resultant double value is printed to the console using ?putStrLn' statement after the function is called.

Example 1

In this example, the user-defined function, intToDouble is defined to convert the int variables to double using fromIntegral function.

intToDouble :: Int -> Double
intToDouble n = fromIntegral n

main :: IO ()
main = do
   let n = 42
   putStrLn (show (intToDouble n))

Output

42.0

Example 2

In this example, the user-defined function, intToDouble is defined to convert the int variables to double using fromIntegral function by division.

intToDouble :: Int -> Double
intToDouble n = fromIntegral n / 1.0

main :: IO ()
main = do
   let n = 42
   putStrLn (show (intToDouble n))

Output

42.0

Example 3

In this example, the user-defined function, intToDouble is defined to convert the int variables to double using fromIntegral function by multiplication.

intToDouble :: Int -> Double
intToDouble n = fromIntegral (n * 10^1) / 10.0

main :: IO ()
main = do
   let n = 42
   putStrLn (show (intToDouble n))

Output

42.0

Example 4

In this example, the user-defined function, intToDouble is defined to convert the int variables to double using realToFrac function.

intToDouble :: Int -> Double
intToDouble n = realToFrac n

main :: IO ()
main = do
   let n = 42
   putStrLn (show (intToDouble n))

Output

42.0

Example 5

In this example, the user-defined function, intToDouble is defined to convert the int variables to double using realPart function.

import Data.Complex

intToDouble :: Int -> Double
intToDouble n = realPart (fromIntegral n :+ 0)

main :: IO ()
main = do
   let n = 42
   putStrLn (show (intToDouble n))

Output

42.0

Conclusion

Integer to Double conversion is a process of converting an integer value to a floating-point value of type Double.In Haskell, an Int variable is converted to double using user-defined intToDouble function along with fromIntegral, realToFrac and realPart functions.

Updated on: 2023-04-20T12:02:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements