
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Long Type Variables into Int in Haskell
In Haskell, we can use fromIntegral function along with toInteger and truncate function, div and mod functions to convert long type variable into int. In the first example, we are going to use ( let intVar = fromIntegral longVar :: Int) and in the second example, we are going to use (let highPart = fromIntegral (longVar `div` 2^32) :: Int and let lowPart = fromIntegral (longVar `mod` 2^32) :: Int).
Algorithm
Step 1 ? 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 2 ? The variable named, "longVar" is being initialized. It will hold the long type variable that is to be converted to respective integer value.
Step 3 ? The function, fromIntegral is being called and longVar is passed to it.
Step 4 ? The resultant integer value is printed to the console using ?putStrLn' statement after the function is called.
Example 1
In this example, the function is defined with the help of fromIntegral function to convert the long type variables to Int.
main :: IO () main = do let longVar = 123456789123456789 let intVar = fromIntegral longVar :: Int putStrLn $ "The long value is " ++ show longVar ++ "." putStrLn $ "The corresponding integer value is " ++ show intVar ++ "."
Output
The long value is 123456789123456789. The corresponding integer value is 123456789123456789.
Example 2
In this example, the function is defined with the help of fromIntegral and truncate function to convert the long type variables to Int.
main :: IO () main = do let longVar = 123456789123456789 let intVar = truncate (fromIntegral longVar :: Double) :: Int putStrLn $ "The long value is " ++ show longVar ++ "." putStrLn $ "The corresponding integer value is " ++ show intVar ++ "."
Output
The long value is 123456789123456789. The corresponding integer value is 123456789123456789.
Example 3
In this example, the function is defined with the help of fromIntegral and toInteger function to convert the long type variables to Int.
main :: IO () main = do let longVar = 123456789123456789 let intVar = fromIntegral (toInteger longVar) :: Int putStrLn $ "The long value is " ++ show longVar ++ "." putStrLn $ "The corresponding integer value is " ++ show intVar ++ "."
Output
The long value is 123456789123456789. The corresponding integer value is 123456789123456789.
Example 4
In this example, the function is defined with the help of fromIntegral function to convert the long type variables to Int.
main :: IO () main = do let longVar = 123456789123456789 let highPart = fromIntegral (longVar `div` 2^32) :: Int let lowPart = fromIntegral (longVar `mod` 2^32) :: Int let intVar = highPart * 2^32 + lowPart putStrLn $ "The long value is " ++ show longVar ++ "." putStrLn $ "The corresponding integer value is " ++ show intVar ++ "."
Output
The long value is 123456789123456789. The corresponding integer value is 123456789123456789.
Conclusion
In Haskell, a Long type variable represents a 64-bit signed integer, while an Int type variable represents a machine-dependent signed integer, which is typically either 32 or 64 bits depending on the platform. Therefore, converting a Long type variable to an Int type variable may result in a loss of precision if the Long value is too large to fit in an Int. In Haskell, a long type variables is converted to int using fromIntegral function along with truncate and toInteger function and also by using div & mod function.