
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
Multiply Two Floating Point Numbers in Haskell
This tutorial will help us in multiplying two floating point numbers. The multiplication is simply an arithmetic mathematical operation. In Haskell, floating point numbers are represented as values of the Float or Double type. You can use the (*) operator or the multiply() function to multiply two floating point numbers in Haskell. Alternatively, we can also use (/) operator to multiply two floating point numbers by dividing 1 by the reciprocal of the second number.
Method 1: Using Multiply Function
This method uses multiply() function to multiply two floating point numbers. The function is defined before the main function, as multiply x y = x * y where x and y are two floating point numbers. Once this function is called under main, the two floating point numbers are passed to it. These two floating point numbers are multiplied. And the final resultant multiplication value is printed using the putStrLn function and the show function.
Algorithm
Step 1 ? Multiply() function is defined over floating point numbers as , multiply x y = x * y.
Step 2 ? 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 ? A variable named, "result" is being initialized. It will contain the resultant value after multiply() function will compute the multiplication of the floating numbers passed, once the function is being called.
Step 4 ? Once the two floating point numbers are multiplied, the final resultant multiplication value is displayed by using ?putStrLn' statement.
Example
In the following example, we will multiply two floating point numbers using multiply function.
multiply :: Float -> Float -> Float multiply x y = x * y main :: IO () main = do let result = multiply 3.14 15.9 putStrLn (show result)
Output
49.926
Method 2: Using * operator
This method uses (*) operator to multiply two floating point numbers. (*) is an arithmetic operator. The two floating point numbers are passed with (*) operator in between them. And Once the two floating point numbers are multiplied, the final resultant multiplication value is printed using the putStrLn function and the show function.
Algorithm
Step 1 ? Program execution will be started from main function. The main() function has whole control of the program. It is called as main = do.
Step 2 ? A variable named, "result" is being initialized. It will have the resultant value that is to be displayed after the multiplication of two floating point numbers. The two floating point numbers are simply passed by using * operator in between to compute the multiplication. The final multiplication is computed and result is assigned.
Step 3 ? Once the two floating point numbers are multiplied, the final resultant multiplication value is displayed by using ?putStrLn' statement.
Example
In the following example, we are going to multiply two floating point numbers using * operator
main :: IO () main = do let result = 3.14 * 15.9 putStrLn (show result)
Output
49.926
Method 3: Using / operator
This method uses (/) operator to multiply two floating point numbers. (/) is an arithmetic operator. The two floating point numbers and (/) operator is passed to multiply two floating point numbers by dividing 1 by the reciprocal of the second number. And once the two floating point numbers are multiplied, the final resultant multiplication value is printed using the putStrLn function and the show function.
Algorithm
Step 1 ? 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 ? A variable named, "result" is being initialized. It will have the resultant value that is to be displayed after the multiplication of two floating point numbers. The two floating point numbers are simply passed by using / operator. The / operator is used to multiply two floating point numbers by dividing 1 by the reciprocal of the second number. The final multiplication is computed and result is assigned.
Step 3 ? Once the two floating point numbers are multiplied, the final resultant multiplication value is displayed by using ?putStrLn' statement.
Example
In the following example, we are going to multiply two floating point numbers using / operator
main :: IO () main = do let result = 3.14 / (1/15.9) putStrLn (show result)
Output
49.926
Conclusion
The multiplication is simply an arithmetic mathematical operation. In Haskell, the two floating point numbers can be multiplied by various ways. These ways might include by using multiply() function which to be defined as multiply x y = x * y, or by using (*) and (/) operator. The (*) operator can directly be passed between two floating point numbers. The (/) operator is used to multiply two floating point numbers by dividing 1 by the reciprocal of the second number.
In every approach, two floating point numbers are passed that needs to be multiplied.