Haskell Program to get the remainder of float numbers using library function


Haskell has internal functions like divMod, mod and properFraction to get the remainder of the float number. In the first example we are going to use (divMod' (x) (y)) function and in the second example, we are going to use (mod' x y) function. And in third example, we are going to use (properFraction $ x / y) function.

Algorithm

  • Step 1 − The Data.Fixed module is imported to use divMod function.

  • Step 2 − The variables named, “x” and “y” are being initialized. It will hold the floating point numbers whose remainder value is to be found after division.

  • Step 3 − Use internal functions to get the remainder of the float number

  • Step 4 − Program execution will be started from main function. The main() function has whole control of the program. It calls the divMod function and prints the result.

  • Step 5 − The resultant remainder value is printed to the console using ‘print’ function after the function is called.

Example 1

In this example, we are going to see that how we can get the remainder of floating point numbers by using divMod function.

import Data.Fixed

x = 5.5
y = 2.3

(quotient, remainder) = divMod' (x) (y)

main = print remainder

Output

0.9000000000000004

Example 2

In this example, we are going to see that how we can get the remainder of floating point numbers by using mod function.

import Data.Fixed

x = 5.5
y = 2.3

remainder = mod' x y

main = print remainder

Output

0.9000000000000004

Example 3

In this example, we are going to see that how we can get the remainder of floating point numbers by using mod function.

import Data.Ratio

x = 4.3
y = 2.3
(quotient, remainder) = properFraction $ x / y

main = do
   print remainder

Output

0.8695652173913044

Conclusion

The remainder of floating point numbers is the value left over after the division of one floating point number by another. In other words, it is the amount that remains after the division process is complete. It can also be thought of as the remainder of a floating point number when it is divided by another floating point number. The remainder is typically represented as a decimal number.

In Haskell, to get the remainder of float numbers, we can use the divMod, mod or properFraction functions.

Updated on: 13-Mar-2023

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements