WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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

   

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% in my WWDC23 sale.

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.

Save 50% on all our books and bundles!

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

   

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

   

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

   

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.

   

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

   

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.

   

Filed in the SOLVED section!

1      

Save 50% in my WWDC23 sale.

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.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.