github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/math/pow.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  // POW returns the base to the exponent value.
    13  // @param {Int | Float} base - The base value.
    14  // @param {Int | Float} exp - The exponent value.
    15  // @return {Float} - The exponentiated value.
    16  func Pow(_ context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 2)
    18  
    19  	if err != nil {
    20  		return values.None, err
    21  	}
    22  
    23  	err = core.ValidateType(args[0], types.Int, types.Float)
    24  
    25  	if err != nil {
    26  		return values.None, err
    27  	}
    28  
    29  	err = core.ValidateType(args[1], types.Int, types.Float)
    30  
    31  	if err != nil {
    32  		return values.None, err
    33  	}
    34  
    35  	return values.NewFloat(math.Pow(toFloat(args[0]), toFloat(args[1]))), nil
    36  }