Haskell program to multiply two floating point numbers

HaskellServer Side ProgrammingProgramming

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.

raja
Updated on 11-Oct-2022 11:21:47

Advertisements