Cumulative Sum

The cumulative sum function is frequently used for calculating cumulative gains or losses over a field of numbers. Various applications use this to compute running totals for their applications logic. The formal definition is provided below.

To implement the CumSum function, see the method signature below. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to vals and output.

function CumSum(int256[] calldata vals, uint8 precision) external returns (int256[] memory);

Under the hood, the following functions are utilized directly in the Neural client.

func (con *NeuralMath) CumSum(gas *big.Int, vals []*big.Int, precision uint8) ([]*big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}

	f, err := toFloats(vals, mul)
	if err != nil {
		return nil, err, nil
	}
	dst := make([]float64, len(vals))
	floats.CumSum(dst, f)
	return toBigArray(dst, mul), nil, nil
}

Last updated