profile
viewpoint
If you are wondering where the data of this site comes from, please visit https://api.github.com/users/gouravnema/events. GitMemory does not store any data, but only uses NGINX to cache data for a period of time. The idea behind GitMemory is simply to give users a better reading experience.

gouravnema/heap 1

Heap is a simple key value pair store written in PHP. This uses filesystem for storing key values. all keys are files and content of file is value which keys holds.

gouravnema/avatarify 0

Avatars for Zoom, Skype and other video-conferencing apps.

gouravnema/bwip-js 0

Barcode Writer in Pure JavaScript

gouravnema/draw.io 0

Source to www.draw.io

gouravnema/EnvayaSMS 0

Simple SMS and MMS gateway running on Android

gouravnema/generators 0

[ Javascript - ES6 ] Concepts in generators

gouravnema/glup_transpile 0

setting up gulp to transpile es6

gouravnema/hamster 0

GNOME time tracker

gouravnema/laravel-elixir-index-builder 0

laravel elixir task to build index.html for single page application with versioned css and js files

release nodejs/node

v14.17.1

released time in 4 days

pull request commentrazorpay/go-financial

Rate function added

@thsubaku9 Thank you for adding this to go-financial ! 💯 🎉

thsubaku9

comment created time in 14 days

push eventrazorpay/go-financial

Kaustubh J

commit sha 35a25fd8b7c41eb356c3abe862a3c53fa6853e9b

Rate function added (#17) * Added PV an NPV computation * testing for pv and npv functions * missing examples added * comment line violation * gofumpt linting * indentation oopsie * new play.golang links * variable naming * better example * Rate calculation * modified as per decimal values * minor modifications * required modifications * required modifications 2 * required fixes and comment modifications * gofumpt * README changes * error printing

view details

push time in 14 days

PR merged razorpay/go-financial

Rate function added

This PR adds the Rate calculation function to go-financial (using Newton Rapson for the root calculation)

+217 -3

8 comments

4 changed files

thsubaku9

pr closed time in 14 days

pull request commentrazorpay/go-financial

Rate function added

Hey @thsubaku9, I will review this over the coming weekend.

thsubaku9

comment created time in 16 days

pull request commentrazorpay/go-financial

Rate function added

Any updates here ? @krantideep95 @gyanesh-m @ra0van

thsubaku9

comment created time in 23 days

created repositoryvinaysshenoy/R8Test

Project to show bug with R8 ignoring configuration

created time in 23 days

pull request commentrazorpay/go-financial

Rate function added

lets gooooo pt.2 @gyanesh-m

thsubaku9

comment created time in a month

startedelectronicarts/kara

started time in 2 months

startedJakeWharton/diffuse

started time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func main() { 	tolerance := decimal.NewFromFloat(1e-6) 	initialGuess := decimal.NewFromFloat(0.1), -	rate, ok := gofinancial.Rate(pv, fv, pmt, nper, when, maxIter, tolerance, initialGuess)-	if ok {-		fmt.Printf("rate:%v ", rate)-	} else {+	rate, err := gofinancial.Rate(pv, fv, pmt, nper, when, maxIter, tolerance, initialGuess)+	if err != nil { 		fmt.Printf("NaN")

print error.

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, bool) {

Alright, errors are a better way to go about it

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, bool) {+	var nextIterRate, currentIterRate decimal.Decimal = initialGuess, initialGuess++	for iter := int64(0); iter < maxIter; iter++ {+		currentIterRate = nextIterRate+		nextIterRate = currentIterRate.Sub(getRateRatio(pv, fv, pmt, currentIterRate, nper, when))+	}++	if nextIterRate.Sub(currentIterRate).Abs().GreaterThan(tolerance) {+		return nextIterRate, false

also, make it GreaterThanOrEqual

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, bool) {+	var nextIterRate, currentIterRate decimal.Decimal = initialGuess, initialGuess++	for iter := int64(0); iter < maxIter; iter++ {+		currentIterRate = nextIterRate+		nextIterRate = currentIterRate.Sub(getRateRatio(pv, fv, pmt, currentIterRate, nper, when))+	}++	if nextIterRate.Sub(currentIterRate).Abs().GreaterThan(tolerance) {+		return nextIterRate, false

return decimal.Zero with custom error code.

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, bool) {+	var nextIterRate, currentIterRate decimal.Decimal = initialGuess, initialGuess++	for iter := int64(0); iter < maxIter; iter++ {

add tolerance check(https://github.com/numpy/numpy-financial/blob/master/numpy_financial/_financial.py#L661) along with iter<maxIter condition. Refer: https://github.com/numpy/numpy-financial/blob/master/numpy_financial/_financial.py#L661

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt

fix indentation around : Run godoc -http=:80 and go to http://localhost/pkg/github.com/razorpay/go-financial/#Rate to view the mismatched indentations.

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/+

no newline before a function comment. Otherwise it gets skipped.

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ maxIter : total number of iterations to perform calculation+ tolerance : accept result only if the difference in iteration values is less than the tolerance provided+ initialGuess : an initial point to start approximating from+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, bool) {
func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, error) {

also add error code for nan output in go-financial/error_codes.go

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero
This function computes the ratio that is used to find a single value that sets the non-liner equation to zero
thsubaku9

comment created time in 2 months

pull request commentrazorpay/go-financial

Rate function added

Lets go ? @gyanesh-m

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Test_Nper(t *testing.T) { 		}) 	} }++func Test_rate(t *testing.T) {+	type args struct {+		pv           decimal.Decimal+		fv           decimal.Decimal+		pmt          decimal.Decimal+		nper         int64+		when         paymentperiod.Type+		maxIter      int64+		tolerance    decimal.Decimal+		initialGuess decimal.Decimal+	}+	tests := []struct {+		name string+		args args+		want decimal.Decimal+	}{+		{+			name: "success", args: args{+				pv:           decimal.NewFromInt(2000),+				fv:           decimal.NewFromInt(-3000),+				pmt:          decimal.NewFromInt(100),+				nper:         4,+				when:         paymentperiod.BEGINNING,+				maxIter:      100,+				tolerance:    decimal.NewFromFloat(1e-7),+				initialGuess: decimal.NewFromFloat(0.1),+			},+			want: decimal.NewFromFloat(0.06106257989825202),+		}, {+			name: "success", args: args{

so nan basically requires your validity to be false, so I've changed the test to handle that case as well

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Test_Nper(t *testing.T) { 		}) 	} }++func Test_rate(t *testing.T) {+	type args struct {+		pv           decimal.Decimal+		fv           decimal.Decimal+		pmt          decimal.Decimal+		nper         int64+		when         paymentperiod.Type+		maxIter      int64+		tolerance    decimal.Decimal+		initialGuess decimal.Decimal+	}+	tests := []struct {+		name string+		args args+		want decimal.Decimal+	}{+		{+			name: "success", args: args{+				pv:           decimal.NewFromInt(2000),+				fv:           decimal.NewFromInt(-3000),+				pmt:          decimal.NewFromInt(100),+				nper:         4,+				when:         paymentperiod.BEGINNING,+				maxIter:      100,+				tolerance:    decimal.NewFromFloat(1e-7),+				initialGuess: decimal.NewFromFloat(0.1),+			},+			want: decimal.NewFromFloat(0.06106257989825202),+		}, {+			name: "success", args: args{

add nan test case.

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func main() { } ``` [Run on go-playground](https://play.golang.org/p/hm77MTPBGYg)++## Rate  ++```go  +func Rate(pv, fv, pmt decimal, nper int64, when paymentperiod.Type, params ...float64) (decimal, bool)

update.

thsubaku9

comment created time in 2 months

pull request commentrazorpay/go-financial

Rate function added

lets gooooooo @gyanesh-m

thsubaku9

comment created time in 2 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ params	: optional parameters for maxIter, tolerance, and initialGuess+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, params ...decimal.Decimal) (decimal.Decimal, bool) {

Sorry about that. Didn't know whether to keep it strict or variadic. Changes have been reflected in code, readme and play ground link as well

thsubaku9

comment created time in 3 months

issue commentrazorpay/go-financial

Financial Functions enhancement

ok. We can pick few of those which you mentioned to be added. Rest can be taken up as and when there is a requirement.

thsubaku9

comment created time in 3 months

Pull request review commentrazorpay/go-financial

Rate function added

 func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal { 	} 	return internalNpv }++/*+This function computs the ratio that is used to find a single value that sets the non-liner equation to zero++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ curRate: the rate compounded once per period rate+*/+func getRateRatio(pv, fv, pmt, curRate decimal.Decimal, nper int64, when paymentperiod.Type) decimal.Decimal {+	oneInDecimal := decimal.NewFromInt(1)+	whenInDecimal := decimal.NewFromInt(when.Value())+	nperInDecimal := decimal.NewFromInt(nper)++	f0 := curRate.Add(oneInDecimal).Pow(decimal.NewFromInt(nper)) // f0 := math.Pow((1 + curRate), float64(nper))+	f1 := f0.Div(curRate.Add(oneInDecimal))                       // f1 := f0 / (1 + curRate)++	yP0 := pv.Mul(f0)+	yP1 := pmt.Mul(oneInDecimal.Add(curRate.Mul(whenInDecimal))).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	y := fv.Add(yP0).Add(yP1) // y := fv + pv*f0 + pmt*(1.0+curRate*when.Value())*(f0-1)/curRate++	derivativeP0 := nperInDecimal.Mul(f1).Mul(pv)+	derivativeP1 := pmt.Mul(whenInDecimal).Mul(f0.Sub(oneInDecimal)).Div(curRate)+	derivativeP2s0 := oneInDecimal.Add(curRate.Mul(whenInDecimal))+	derivativeP2s1 := ((curRate.Mul((nperInDecimal)).Mul(f1)).Sub(f0).Add(oneInDecimal)).Div(curRate.Mul(curRate))+	derivativeP2 := derivativeP2s0.Mul(derivativeP2s1)+	derivative := derivativeP0.Add(derivativeP1).Add(derivativeP2)+	// derivative := (float64(nper) * f1 * pv) + (pmt * ((when.Value() * (f0 - 1) / curRate) + ((1.0 + curRate*when.Value()) * ((curRate*float64(nper)*f1 - f0 + 1) / (curRate * curRate)))))++	return y.Div(derivative)+}++/*+Rate computes the Interest rate per period by running Newton Rapson to find an approximate value for:++y = fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1)+(0 - y_previous) /(rate - rate_previous) = dy/drate {derivative of y w.r.t. rate}+++Params:+ nper 	: number of compounding periods+ pmt	: a (fixed) payment, paid either+	  	  at the beginning (when = 1) or the end (when = 0) of each period+ pv		: a present value+ fv		: a future value+ when 	: specification of whether payment is made+		  at the beginning (when = 1) or the end (when = 0) of each period+ params	: optional parameters for maxIter, tolerance, and initialGuess+References:+	Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document+    Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated+    Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12.+    Organization for the Advancement of Structured Information Standards+    (OASIS). Billerica, MA, USA. [ODT Document]. Available:+    http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula+    OpenDocument-formula-20090508.odt+*/++func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, params ...decimal.Decimal) (decimal.Decimal, bool) {

keep maxIter, initialGuess and tolerance as regular params and not variadic.

thsubaku9

comment created time in 3 months