58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package attribute
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
//基准倍数
|
|
const CARDINAL_NUMBER int = 10000
|
|
|
|
func NewFixedPoint(value float32) *FixedPoint {
|
|
return &FixedPoint{
|
|
rawValue: int(math.Round(float64(value * float32(CARDINAL_NUMBER)))),
|
|
}
|
|
}
|
|
|
|
type FixedPoint struct {
|
|
rawValue int
|
|
}
|
|
|
|
func (this *FixedPoint) Scalar() float32 {
|
|
return float32(this.rawValue) * 0.0001
|
|
}
|
|
|
|
func (this *FixedPoint) ToInt() int {
|
|
return this.rawValue / CARDINAL_NUMBER
|
|
}
|
|
func (this *FixedPoint) ToFloat() float32 {
|
|
return this.Scalar()
|
|
}
|
|
func (this *FixedPoint) ToString() string {
|
|
return fmt.Sprintf("%f", this.Scalar())
|
|
}
|
|
|
|
///+
|
|
func (this *FixedPoint) Add(y *FixedPoint) *FixedPoint {
|
|
this.rawValue += y.rawValue
|
|
return this
|
|
}
|
|
|
|
///-
|
|
func (this *FixedPoint) Reduce(y *FixedPoint) *FixedPoint {
|
|
this.rawValue -= y.rawValue
|
|
return this
|
|
}
|
|
|
|
///*
|
|
func (this *FixedPoint) Multiply(y *FixedPoint) *FixedPoint {
|
|
this.rawValue = (this.rawValue * y.rawValue) / CARDINAL_NUMBER
|
|
return this
|
|
}
|
|
|
|
/// /
|
|
func (this *FixedPoint) Divide(y *FixedPoint) *FixedPoint {
|
|
this.rawValue = (this.rawValue * CARDINAL_NUMBER) / y.rawValue
|
|
return this
|
|
}
|