## 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 `````` 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`````` 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))
 SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
 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`````` 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`````` Mar '23 As i said i don't know how you set up exponentiation operator in your formula. Use `pow()` function instead. 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. Mar '23 Super! That compiles and gives me a number :) I need to chck the numbers but so far so good, thank you @ygeras! :) 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. Mar '23 Filed in the SOLVED section! 1

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.