github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/math/stddev_population.go (about)

     1  package math
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // STDDEV_POPULATION returns the population standard deviation of the values in a given array.
    13  // @param {Int[] | Float[]} numbers - Array of numbers.
    14  // @return {Float} - The population standard deviation.
    15  func StandardDeviationPopulation(_ context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 1, 1)
    17  
    18  	if err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	err = core.ValidateType(args[0], types.Array)
    23  
    24  	if err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	arr := args[0].(*values.Array)
    29  
    30  	if arr.Length() == 0 {
    31  		return values.NewFloat(math.NaN()), nil
    32  	}
    33  
    34  	vp := variance(arr, values.NewInt(0))
    35  
    36  	return values.NewFloat(math.Pow(float64(vp), 0.5)), nil
    37  }