-- |
-- Module      :  ELynx.Tools.Numeric
-- Copyright   :  (c) Dominik Schrempf 2021
-- License     :  GPL-3.0-or-later
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Creation date: Thu Feb 14 13:28:37 2019.
--
-- Numerical functions.
module ELynx.Tools.Numeric
  ( -- * Numeric
    harmonic,
    roundN,
  )
where

-- | Calculate the nth harmonic number.
harmonic :: Int -> Double
harmonic :: Int -> Double
harmonic Int
1 = Double
1.0
harmonic Int
n = Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int -> Double
harmonic (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

-- | Round double to a given precision.
roundN :: Int -> Double -> Double
roundN :: Int -> Double -> Double
roundN Int
n Double
v = Integer -> Double
forall a. Num a => Integer -> a
fromInteger (Double -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Integer) -> Double -> Integer
forall a b. (a -> b) -> a -> b
$ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
10 Double -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
n)) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
10.0 Double -> Int -> Double
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Int
n)