1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00
golearn/base/float.go

131 lines
3.6 KiB
Go
Raw Normal View History

2014-08-02 16:22:14 +01:00
package base
import (
"encoding/json"
2014-08-22 06:55:20 +00:00
"fmt"
"strconv"
2014-08-02 16:22:14 +01:00
)
// FloatAttribute is an implementation which stores floating point
// representations of numbers.
type FloatAttribute struct {
2014-08-22 06:55:20 +00:00
Name string
Precision int
2014-08-02 16:22:14 +01:00
}
// MarshalJSON returns a JSON representation of this Attribute
// for serialisation.
func (f *FloatAttribute) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "float",
"name": f.Name,
"attr": map[string]interface{}{
"precision": f.Precision,
},
})
}
// UnmarshalJSON reads a JSON representation of this Attribute.
func (f *FloatAttribute) UnmarshalJSON(data []byte) error {
var d map[string]interface{}
err := json.Unmarshal(data, &d)
if err != nil {
return err
}
if precision, ok := d["precision"]; ok {
f.Precision = int(precision.(float64))
return nil
}
return fmt.Errorf("Precision must be specified")
}
2014-08-02 16:22:14 +01:00
// NewFloatAttribute returns a new FloatAttribute with a default
// precision of 2 decimal places
func NewFloatAttribute(name string) *FloatAttribute {
2014-08-22 06:55:20 +00:00
return &FloatAttribute{name, 2}
2014-08-02 16:22:14 +01:00
}
2014-09-28 01:34:21 +02:00
// Compatible checks whether this FloatAttribute can be ponded with another
2014-08-02 16:22:14 +01:00
// Attribute (checks if they're both FloatAttributes)
2014-09-28 01:34:21 +02:00
func (Attr *FloatAttribute) Compatible(other Attribute) bool {
2014-08-22 06:55:20 +00:00
_, ok := other.(*FloatAttribute)
return ok
2014-08-02 16:22:14 +01:00
}
// Equals tests a FloatAttribute for equality with another Attribute.
//
// Returns false if the other Attribute has a different name
// or if the other Attribute is not a FloatAttribute.
func (Attr *FloatAttribute) Equals(other Attribute) bool {
2014-08-22 06:55:20 +00:00
// Check whether this FloatAttribute is equal to another
_, ok := other.(*FloatAttribute)
if !ok {
// Not the same type, so can't be equal
return false
}
if Attr.GetName() != other.GetName() {
return false
}
return true
2014-08-02 16:22:14 +01:00
}
// GetName returns this FloatAttribute's human-readable name.
func (Attr *FloatAttribute) GetName() string {
2014-08-22 06:55:20 +00:00
return Attr.Name
2014-08-02 16:22:14 +01:00
}
// SetName sets this FloatAttribute's human-readable name.
func (Attr *FloatAttribute) SetName(name string) {
2014-08-22 06:55:20 +00:00
Attr.Name = name
2014-08-02 16:22:14 +01:00
}
// GetType returns Float64Type.
func (Attr *FloatAttribute) GetType() int {
2014-08-22 06:55:20 +00:00
return Float64Type
2014-08-02 16:22:14 +01:00
}
// String returns a human-readable summary of this Attribute.
// e.g. "FloatAttribute(Sepal Width)"
func (Attr *FloatAttribute) String() string {
2014-08-22 06:55:20 +00:00
return fmt.Sprintf("FloatAttribute(%s)", Attr.Name)
2014-08-02 16:22:14 +01:00
}
// CheckSysValFromString confirms whether a given rawVal can
// be converted into a valid system representation. If it can't,
// the returned value is nil.
func (Attr *FloatAttribute) CheckSysValFromString(rawVal string) ([]byte, error) {
2014-08-22 06:55:20 +00:00
f, err := strconv.ParseFloat(rawVal, 64)
if err != nil {
return nil, err
}
2014-08-02 16:22:14 +01:00
2014-08-22 06:55:20 +00:00
ret := PackFloatToBytes(f)
return ret, nil
2014-08-02 16:22:14 +01:00
}
// GetSysValFromString parses the given rawVal string to a float64 and returns it.
//
// float64 happens to be a 1-to-1 mapping to the system representation.
// IMPORTANT: This function panic()s if rawVal is not a valid float.
// Use CheckSysValFromString to confirm.
func (Attr *FloatAttribute) GetSysValFromString(rawVal string) []byte {
2014-08-22 06:55:20 +00:00
f, err := Attr.CheckSysValFromString(rawVal)
if err != nil {
panic(err)
}
return f
2014-08-02 16:22:14 +01:00
}
// GetFloatFromSysVal converts a given system value to a float
func (Attr *FloatAttribute) GetFloatFromSysVal(rawVal []byte) float64 {
2014-08-22 06:55:20 +00:00
return UnpackBytesToFloat(rawVal)
2014-08-02 16:22:14 +01:00
}
// GetStringFromSysVal converts a given system value to to a string with two decimal
// places of precision.
func (Attr *FloatAttribute) GetStringFromSysVal(rawVal []byte) string {
2014-08-22 06:55:20 +00:00
f := UnpackBytesToFloat(rawVal)
formatString := fmt.Sprintf("%%.%df", Attr.Precision)
return fmt.Sprintf(formatString, f)
2014-08-02 16:22:14 +01:00
}