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

 Mar '23 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 Mar '23 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 Mar '23 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
 SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge. Sponsor Hacking with Swift and reach the world's largest Swift community!
 Mar '23 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 Mar '23 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 Mar '23 As i said i don't know how you set up exponentiation operator in your formula. Use `pow()` function instead. 1 Mar '23 ``````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 Mar '23 Super! That compiles and gives me a number :) I need to chck the numbers but so far so good, thank you @ygeras! :) 1 Mar '23 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 Mar '23 Filed in the SOLVED section! 2

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

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.

You are not logged in