Documentation
¶
Overview ¶
Package frac4 stores fractional values in the range [0.0000, 1.0000] using a compact fixed-point representation with 4 decimal digits.
The value is encoded as an integer scaled by 10,000, so each value is exactly represented with 4 fractional digits.
Index ¶
- Constants
- type Value
- func (v Value) Float64() float64
- func (v Value) MarshalBinary() ([]byte, error)
- func (v Value) MarshalJSON() ([]byte, error)
- func (v Value) MarshalText() ([]byte, error)
- func (v Value) Scaled() uint16
- func (v Value) String() string
- func (v Value) StringFixed4() string
- func (v Value) StringShort() string
- func (v *Value) UnmarshalBinary(data []byte) error
- func (v *Value) UnmarshalJSON(b []byte) error
- func (v *Value) UnmarshalText(text []byte) error
Constants ¶
const ( // Scale turns 0.0000..1.0000 into 0..10000 (inclusive). Scale uint16 = 10000 // Max is the maximum scaled value (representing 1.0000). Max uint16 = 10000 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value uint16
Value stores a number in [0.0000, 1.0000] with exactly 4 decimal places.
It is represented as an integer v where realValue = v / 10000.
func FromFloat64 ¶
FromFloat64 converts an approximate float in [0..1] by rounding to the nearest 1e-4 (four fractional digits).
func FromScaled ¶
FromScaled constructs a Value from its underlying scaled integer. The input must be in [0..10000].
func MustFromScaled ¶
MustFromScaled is FromScaled but panics on invalid input.
func Parse ¶
Parse parses decimals like "0.000", "0.12", "0.1234", "1", "1.0000" (up to 4 fractional digits).
The returned value is always quantized to exactly 4 digits.
func (Value) Float64 ¶
Float64 returns the value as a float64.
Note: floats may not represent the value exactly, so prefer parsing from strings (or using FromScaled) when exact decimal semantics matter.
func (Value) MarshalBinary ¶
MarshalBinary encodes the value as 2 little-endian bytes.
func (Value) MarshalJSON ¶
MarshalJSON encodes as a JSON string to preserve exact 1e-4 precision.
func (Value) MarshalText ¶
MarshalText encodes as the fixed4 decimal string form.