- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

# Haskell program to multiply two floating point numbers

This tutorial will discuss writing a program to multiply two floating point numbers in Haskell Programming Language. The computations in Haskell are done through Mathematical functions.

Floating numbers are used to represent fractional numbers i.e numbers having decimal points. Example: 2.23, 0.25. These numbers can be positive, negative, or zero. All integers are floating point numbers but all floating point numbers are not integers.

In this tutorial, we discuss four different ways to compute the multiplication of two floating point numbers in Haskell.

Implementing multiplication computation in the main function.

Implementing multiplication computation in a separate function.

Implementing multiplication using the list product method.

Implementing multiplication using prefix notation.

Implementing multiplication using the section operator.

## Syntax

Following are the syntax rules to compute the multiplication of two floating point numbers.

floatNumber_1 * floatNumber_2

We start with declaring and initializing variables for floating point operands to be multiplied and multiplying them using the infix function (operator) for multiplication (“*”).

## Algorithm steps

Declare two variables for floating point operands (optional).

Initialize variables.

Compute the multiplication computation and load the result into a variable.

Print the result

## Example 1

In this example we are implementing multiplication computation in the main function.

main :: IO() main = do -- declaring and initializing operands let float1 = 1.22 let float2 = 2.22 -- declaring and initializing result with computed value let result = (float1 * float2) -- printing the result print ("The multiplication of two floating point numbers 1.22 and 2.22 is:") print (result)

## Output

"The multiplication of two floating point numbers 1.22 and 2.22 is:" 2.7084

Note − Lines starting with “--” are single line comment which is ignored by the compiler during the execution of the program

In the above program we declared and initialized variable for floating point operands and we computed the multiplication value using infix function “*” (operator). Finally, We loaded the resultant value into a variable and printed the value

## Example 2

In this example we are implementing multiplication computation in a separate function.

-- function declaration for multiplication of two float numbers multiply :: Float->Float->Float -- function definition for multiplication multiply f1 f2 = f1 * f2 main :: IO() main = do -- declaring and initializing operands let float1 = 4.54 let float2 = 2.22 -- invoking function and populating result with return value let result = multiply float1 float2 -- printing the result print ("The multiplication of two floating point numbers 4.54 and 2.22 is:") print (result)

## Output

"The multiplication of two floating point numbers 4.54 and 2.22 is:" 10.0788

In the above program we declared a function which takes two floating point numbers as arguments and returns a floating point number. Next, we defined the function to compute multiplication. We invoked the function from main with argument and loaded the return value into the result variable and printed the result.

## Example 3

In this example we are implementing multiplication using the list product method.

main :: IO() main = do -- declaring and initializing operands let float1 = 7.22 let float2 = 2.22 -- initializing the list variable with operands let list = [float1,float2] -- computing the multiplication using list product method let result = product list -- printing the result print ("The multiplication of two floating point numbers 7.22 and 2.22 is:") print (result)

## Output

"The multiplication of two floating point numbers 7.22 and 2.22 is:" 16.0284

In the above program we loaded the declared floating point variables into a list. We used a built-in method for a list called product which multiplies all the elements in a list and returns the result, here we only loaded two numbers it returns the product of these two numbers. Finally, we printed the returned result.

## Example 4

In this example we are implementing multiplication using prefix notation.

main :: IO() main = do -- declaring and initializing operands let float1 = 1.22 let float2 = 2.22 -- computing the multiplication using prefix notation let result = (*) float1 float2 -- printing the result print ("The multiplication of two floating point numbers is:") print (result)

## Output

The multiplication of two floating point numbers is: 2.7084

In the above program used infix function as a prefix to compute the result. As said all operators in Haskell are functions. We can use them in a functional style by encapsulating them with brackets and passing the operands as arguments. It returns the computed value. Finally, we printed the result.

## Example 5

In this example we are implementing multiplication using the section operator.

main :: IO() main = do -- declaring and initializing operands let float1 = 1.22 let float2 = 2.22 -- computing the multiplication using section operator let result = (*float1) float2 -- printing the result print ("The multiplication of two floating point numbers is:") print (result)

## Output

The multiplication of two floating point numbers is: 2.7084

In the above program we used section operator (*number) which is syntactic sugar for infix functions in Haskell to mimic as a function taking single argument and multiples it with the number inside the encapsulated brackets. We put the first floating point number inside brackets and we passed the other as an argument. This returns the multiplication of these two numbers and Finally, we printed the returned result.

## Conclusion

In this article, we discussed different ways to compute the multiplication of two floating point numbers in Haskell Programming Language.

- Related Questions & Answers
- C Program to Multiply two Floating Point Numbers?
- Java Program to Multiply Two Floating-Point Numbers
- Java program to multiply given floating point numbers
- Signed floating point numbers
- C++ Program to Multiply two Numbers
- Program to find GCD of floating point numbers in C++
- 8051 Program to Multiply two 8 Bit numbers
- 8085 Program to Multiply two 8 bits numbers
- 8085 program to multiply two 8 bit numbers
- 8086 program to multiply two 8-bit numbers
- 8086 program to multiply two 16-bit numbers
- 8085 Program to multiply two 2-digit BCD numbers
- 8085 Program to multiply two 16-bit binary numbers
- Fastest Way to multiply two Numbers
- 8085 Program to Multiply two numbers of size 8 bits
- Program to Multiply two 8 Bit numbers in 8051 Microprocessor