Circa comparison of fractional numbers


/ Published in: Haskell
Save to your folder(s)

This module provides a couple of functions and an operator to compare approximately fractional numbers.


Copy this code and paste it in your HTML
  1. module Circa (
  2. circaEq,
  3. picoEq, nanoEq, microEq, milliEq, centiEq, deciEq, uniEq,
  4. (~=)
  5. ) where
  6.  
  7. circaEq :: (Fractional t, Ord t) => t -> t -> t -> Bool
  8. circaEq t x y = abs (x - y) < t
  9.  
  10. picoEq :: (Fractional t, Ord t) => t -> t -> Bool
  11. picoEq = circaEq 1e-12
  12. infix 4 `picoEq`
  13.  
  14. nanoEq :: (Fractional t, Ord t) => t -> t -> Bool
  15. nanoEq = circaEq 1e-9
  16. infix 4 `nanoEq`
  17.  
  18. microEq :: (Fractional t, Ord t) => t -> t -> Bool
  19. microEq = circaEq 1e-6
  20. infix 4 `microEq`
  21.  
  22. milliEq :: (Fractional t, Ord t) => t -> t -> Bool
  23. milliEq = circaEq 1e-3
  24. infix 4 `milliEq`
  25.  
  26. centiEq :: (Fractional t, Ord t) => t -> t -> Bool
  27. centiEq = circaEq 1e-2
  28. infix 4 `centiEq`
  29.  
  30. deciEq :: (Fractional t, Ord t) => t -> t -> Bool
  31. deciEq = circaEq 1e-1
  32. infix 4 `deciEq`
  33.  
  34. uniEq :: (Fractional t, Ord t) => t -> t -> Bool
  35. uniEq = circaEq 1
  36. infix 4 `uniEq`
  37.  
  38. (~=) :: (Fractional t, Ord t) => t -> t -> Bool
  39. (~=) = picoEq
  40. infix 4 ~=

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.