UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Using the standard Compounding Monthly Growth Rate Formula (CMGR) but getting a weird compile error.

Forums > SwiftUI

Hi! I'm trying to calculate the Compounding Monthly Growth Rate Formula (CMGR). The formula for this is: CAGR = (Ending Value ÷ Beginning Value) ^ (1 ÷ Number of Periods) – 1.

So i've done the following but getting a comple error.

any ideas?

eaRevenue is an array of doubles.

let monthlyVarianceRevenue = (ea.eaRevenue[11]/ea.eaRevenue[0]) ^ (1/12) - 1

and i'm getting the compile error on the line above.

/Users/j/Library/CloudStorage/drive/MobDEV/mob/Revenue.swift:69:25 The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

1      

Well I got this to compile, but Swift is return NaN. Not a Number. So what next?

let startValue = Int(ea.eaRevenue[11])
let endValue = Int(ea.eaRevenue[0])
let monthlyVarianceRevenue = (startValue / endValue) ^ (1 / 12) - 1

1      

If you use double continue use double. Not sure how your exponentiation operator set up. But if you use Ints in (1/12) it will return 0. Try to convert them as to double as well. (Double(1) / Double(12))

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Hi there. if you mean like this I get the same compile error as before

let startValue = ea.eaRevenue[11]
let endValue = ea.eaRevenue[0]
let monthlyVarianceRevenue = (startValue / endValue) ^ (Double(1) / Double(12)) - 1

1      

Same error with this too

              let startValue = ea.eaRevenue[11]
                    let endValue = ea.eaRevenue[0]
                    let startMonth = 1.0
                    let endMonth = 12.0
                    let monthlyVarianceRevenue = (startValue / endValue) ^ (startMonth / endMonth) - 1

1      

As i said i don't know how you set up exponentiation operator in your formula. Use pow() function instead.

1      

let startValue = ea.eaRevenue[11]
let endValue = ea.eaRevenue[0]
let exponent = Double(1) / Double (12)
let monthlyVarianceRevenue = pow((startValue / endValue), exponent) - 1

Something like this should work , but not sure if second parameter can handle doubles. Cannot check it right now.

1      

Super! That compiles and gives me a number :) I need to chck the numbers but so far so good, thank you @ygeras! :)

1      

To be on the safe side check the numbers as there are maybe imprecisions, if you need one of course. If you work with money you never now ))). It is recommended to use Decimals for them.

1      

Filed in the SOLVED section!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.