-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #7 Added ABS * #7 Added ACOS * #7 Added ASIN * #7 Added ATAN * #7 Added ATAN2 * #7 Added AVERAGE * #7 Added CEIL * #7 Added COS * #7 Added DEGREES * #7 Added EXP * #7 Added EXP2 * #7 Added FLOOR * #7 Added LOG * #7 Added LOG2 * #7 Added LOG10 * #7 Added MAX * #7 Added MEDIAN * #7 Added MIN * #7 Added PERCENTILE * #7 Added PI * #7 Added POW * #7 Added RADIANS * #7 Added RAND * #7 Added RANGE * #7 Added ROUND * #7 Added SIN * #7 Added SQRT * #7 Added TAN * #7 Added SUM * #7 Added STDDEV_POPULATION * #7 Added STDDEV_SAMPLE, VARIANCE_POPULATION, VARIANCE_SAMPLE
- Loading branch information
Showing
74 changed files
with
2,466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package math | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"math" | ||
) | ||
|
||
/* | ||
* Returns the absolute value of a given number. | ||
* @param number (Int|Float) - Input number. | ||
* @returns (Float) - The absolute value of a given number. | ||
*/ | ||
func Abs(_ context.Context, args ...core.Value) (core.Value, error) { | ||
err := core.ValidateArgs(args, 1, 1) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return values.NewFloat(math.Abs(toFloat(args[0]))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package math_test | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"github.com/MontFerret/ferret/pkg/stdlib/math" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestAbs(t *testing.T) { | ||
Convey("Should return absolute value", t, func() { | ||
Convey("When value is int", func() { | ||
out, err := math.Abs(context.Background(), values.NewInt(-5)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 5) | ||
|
||
out, err = math.Abs(context.Background(), values.NewInt(3)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 3) | ||
}) | ||
|
||
Convey("When value is float", func() { | ||
out, err := math.Abs(context.Background(), values.NewFloat(-5)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 5) | ||
|
||
out, err = math.Abs(context.Background(), values.NewFloat(5.1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 5.1) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package math | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"math" | ||
) | ||
|
||
/* | ||
* Returns the arccosine, in radians, of a given number. | ||
* @param number (Int|Float) - Input number. | ||
* @returns (Float) - The arccosine, in radians, of a given number. | ||
*/ | ||
func Acos(_ context.Context, args ...core.Value) (core.Value, error) { | ||
err := core.ValidateArgs(args, 1, 1) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return values.NewFloat(math.Acos(toFloat(args[0]))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package math_test | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"github.com/MontFerret/ferret/pkg/stdlib/math" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestAcos(t *testing.T) { | ||
Convey("Should return arccosine", t, func() { | ||
out, err := math.Acos(context.Background(), values.NewInt(-1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 3.141592653589793) | ||
|
||
out, err = math.Acos(context.Background(), values.NewInt(0)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 1.5707963267948966) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package math | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"math" | ||
) | ||
|
||
/* | ||
* Returns the arcsine, in radians, of a given number. | ||
* @param number (Int|Float) - Input number. | ||
* @returns (Float) - The arcsine, in radians, of a given number. | ||
*/ | ||
func Asin(_ context.Context, args ...core.Value) (core.Value, error) { | ||
err := core.ValidateArgs(args, 1, 1) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return values.NewFloat(math.Asin(toFloat(args[0]))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package math_test | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"github.com/MontFerret/ferret/pkg/stdlib/math" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestAsin(t *testing.T) { | ||
Convey("Should return arcsine value", t, func() { | ||
out, err := math.Asin(context.Background(), values.NewInt(1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 1.5707963267948966) | ||
|
||
out, err = math.Asin(context.Background(), values.NewInt(0)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 0) | ||
|
||
out, err = math.Asin(context.Background(), values.NewInt(-1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, -1.5707963267948966) | ||
|
||
out, err = math.Asin(context.Background(), values.NewInt(2)) | ||
|
||
So(err, ShouldBeNil) | ||
So(values.IsNaN(out.(values.Float)), ShouldEqual, true) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package math | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"math" | ||
) | ||
|
||
/* | ||
* Returns the arctangent, in radians, of a given number. | ||
* @param number (Int|Float) - Input number. | ||
* @returns (Float) - The arctangent, in radians, of a given number. | ||
*/ | ||
func Atan(_ context.Context, args ...core.Value) (core.Value, error) { | ||
err := core.ValidateArgs(args, 1, 1) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return values.NewFloat(math.Atan(toFloat(args[0]))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package math | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"math" | ||
) | ||
|
||
/* | ||
* Returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value. | ||
* @param number1 (Int|Float) - Input number. | ||
* @param number2 (Int|Float) - Input number. | ||
* @returns (Float) - The arc tangent of y/x, using the signs of the two to determine the quadrant of the return value. | ||
*/ | ||
func Atan2(_ context.Context, args ...core.Value) (core.Value, error) { | ||
err := core.ValidateArgs(args, 2, 2) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[1], core.IntType, core.FloatType) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
arg1 := toFloat(args[0]) | ||
arg2 := toFloat(args[1]) | ||
|
||
return values.NewFloat(math.Atan2(arg1, arg2)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package math_test | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"github.com/MontFerret/ferret/pkg/stdlib/math" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestAtan2(t *testing.T) { | ||
Convey("Should return tangent value", t, func() { | ||
out, err := math.Atan2(context.Background(), values.NewInt(0), values.NewInt(0)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 0) | ||
|
||
out, err = math.Atan2(context.Background(), values.NewInt(1), values.NewInt(0)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 1.5707963267948966) | ||
|
||
out, err = math.Atan2(context.Background(), values.NewInt(1), values.NewInt(1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out.Unwrap(), ShouldEqual, 0.7853981633974483) | ||
|
||
out, err = math.Atan2(context.Background(), values.NewInt(-10), values.NewInt(20)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out.Unwrap(), ShouldEqual, -0.4636476090008061) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package math_test | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"github.com/MontFerret/ferret/pkg/stdlib/math" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestAtan(t *testing.T) { | ||
Convey("Should return arctangent value", t, func() { | ||
out, err := math.Atan(context.Background(), values.NewInt(-1)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, -0.7853981633974483) | ||
|
||
out, err = math.Atan(context.Background(), values.NewInt(0)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out, ShouldEqual, 0) | ||
|
||
out, err = math.Atan(context.Background(), values.NewInt(10)) | ||
|
||
So(err, ShouldBeNil) | ||
So(out.Unwrap(), ShouldEqual, 1.4711276743037345) | ||
}) | ||
} |
Oops, something went wrong.