mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
5f94b77a39
* #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
142 lines
2.0 KiB
Go
142 lines
2.0 KiB
Go
package values
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"math"
|
|
"strconv"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Float float64
|
|
|
|
var ZeroFloat = Float(0.0)
|
|
|
|
func NewFloat(input float64) Float {
|
|
return Float(input)
|
|
}
|
|
|
|
func ParseFloat(input interface{}) (Float, error) {
|
|
if core.IsNil(input) {
|
|
return ZeroFloat, nil
|
|
}
|
|
|
|
i, ok := input.(float64)
|
|
|
|
if ok {
|
|
if i == 0 {
|
|
return ZeroFloat, nil
|
|
}
|
|
|
|
return Float(i), nil
|
|
}
|
|
|
|
// try to cast
|
|
str, ok := input.(string)
|
|
|
|
if ok {
|
|
i, err := strconv.Atoi(str)
|
|
|
|
if err == nil {
|
|
if i == 0 {
|
|
return ZeroFloat, nil
|
|
}
|
|
|
|
return Float(i), nil
|
|
}
|
|
}
|
|
|
|
return ZeroFloat, errors.Wrap(core.ErrInvalidType, "expected 'float'")
|
|
}
|
|
|
|
func ParseFloatP(input interface{}) Float {
|
|
res, err := ParseFloat(input)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func IsNaN(input Float) Boolean {
|
|
return NewBoolean(math.IsNaN(float64(input)))
|
|
}
|
|
|
|
func IsInf(input Float, sign Int) Boolean {
|
|
return NewBoolean(math.IsInf(float64(input), int(sign)))
|
|
}
|
|
|
|
func (t Float) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(float64(t))
|
|
}
|
|
|
|
func (t Float) Type() core.Type {
|
|
return core.FloatType
|
|
}
|
|
|
|
func (t Float) String() string {
|
|
return fmt.Sprintf("%f", t)
|
|
}
|
|
|
|
func (t Float) Compare(other core.Value) int {
|
|
raw := float64(t)
|
|
|
|
switch other.Type() {
|
|
case core.FloatType:
|
|
f := other.Unwrap().(float64)
|
|
|
|
if raw == f {
|
|
return 0
|
|
}
|
|
|
|
if raw < f {
|
|
return -1
|
|
}
|
|
|
|
return +1
|
|
case core.IntType:
|
|
i := other.Unwrap().(int)
|
|
f := float64(i)
|
|
|
|
if raw == f {
|
|
return 0
|
|
}
|
|
|
|
if raw < f {
|
|
return -1
|
|
}
|
|
|
|
return +1
|
|
case core.BooleanType, core.NoneType:
|
|
return 1
|
|
default:
|
|
return -1
|
|
}
|
|
}
|
|
|
|
func (t Float) Unwrap() interface{} {
|
|
return float64(t)
|
|
}
|
|
|
|
func (t Float) Hash() uint64 {
|
|
h := fnv.New64a()
|
|
|
|
h.Write([]byte(t.Type().String()))
|
|
h.Write([]byte(":"))
|
|
|
|
bytes := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(bytes, math.Float64bits(float64(t)))
|
|
h.Write(bytes)
|
|
|
|
return h.Sum64()
|
|
}
|
|
|
|
func (t Float) Copy() core.Value {
|
|
return t
|
|
}
|