Hi

I've been learning nim and playing around with decimal from : 
<https://github.com/status-im/nim-decimal>

I'm written some sample code below to calulate a finance thing called weighted 
average cost of capital.

In the example below I wanted to round decimal to 0.733 and then convert that 
to 7.3%. I did try and convert to a float but had no joy there. It seems once 
with a Decimal we're stuck with it.

Questions I have :

  1. Is this the correct way to use Decimal and Round values i.e. using 
quantize and reduce. I was hoping there would be a round(7.333333,3)
  2. Also, I tried using a func for this but compiler complained it wasn't pure 
- any ideas why that isn't pure? Can I make it pure?
  3. It seems like a lot of typing to write DecimalType and newDecimal - is it 
possible in NIM to reduce typing with Dec / newDec
  4. I found it bit weird to pass in newDecimal("0.05") i.e. why do I have to 
pass a string in for this value? I tried with newDecimal(0.05) but got errors.



I wrote this code mostly by looking at the tests in repo.

Market value of equity (e): 1000000 Market value of debt (d): 2000000 Cost of 
equity (rE): 0.15 Cost of debt (rD): 0.05 Tax rate (t): 0.3
    
    
    proc wacc(e: DecimalType, d: DecimalType, rE: DecimalType, rD: DecimalType, 
t:DecimalType ): DecimalType  =
      let v = e + d
      let eV = e / v
      let dV = d / v
      let adjRD = rD * (1 - t)
      return eV * rE + dV * adjRD
    
    let e = newDecimal(1000000)
    let d = newDecimal(2000000)
    let rE = newDecimal("0.15")
    let rD = newDecimal("0.05")
    let t = newDecimal("0.3")
    
    let w = wacc(e, d, rE, rD, t)
    let y = quantize(w,newDecimal("1e-3"))
    echo reduce(y * 100),"%"
    
    
    Run

Thanks in advance

Nelly

Reply via email to