diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-11 20:58:46 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-11 20:58:46 +0300 |
| commit | 4efc8d2c03e91ea330cbb9366a60e7405ebca8de (patch) | |
| tree | 538259a91f7c87971cda78cd881e6c74343238ee /internal/rpn/number.go | |
| parent | 4465f7abdc72887a422b2ddd9afc43ee811b2e9b (diff) | |
Refactor number.go Float64() to return errors
- Float64() now returns (float64, error) instead of just float64
- All arithmetic methods (Add, Sub, Mul, Div, Pow, Mod, Compare) return errors
- IsString() and IsSymbol() added to Number interface
- Float and Rat now implement IsString() and IsSymbol()
- StringNum and Symbol updated to implement complete Number interface
- IsZero() updated to use val, _ := Float64() pattern
- ToFloat() updated to ignore error from Float64()}
Diffstat (limited to 'internal/rpn/number.go')
| -rw-r--r-- | internal/rpn/number.go | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/internal/rpn/number.go b/internal/rpn/number.go index bb6f765..2915a2a 100644 --- a/internal/rpn/number.go +++ b/internal/rpn/number.go @@ -186,7 +186,8 @@ func (f *Float) Mod(other Number) (Number, error) { // IsZero returns true if the float is zero. // For boolean values, false (0) is zero, true (1) is not zero. func (f *Float) IsZero() bool { - return f.Float64() == 0 + val, _ := f.Float64() + return val == 0 } // IsNegative returns true if the float is negative. @@ -194,6 +195,16 @@ func (f *Float) IsNegative() bool { return f.n < 0 } +// IsString returns true if this number represents a string value. +func (f *Float) IsString() bool { + return false +} + +// IsSymbol returns true if this number represents a symbol. +func (f *Float) IsSymbol() bool { + return false +} + // Compare returns -1, 0, or 1 if this float is less than, equal to, or greater than another. func (f *Float) Compare(other Number) (int, error) { otherF, err := other.Float64() @@ -373,6 +384,16 @@ func (r *Rat) IsNegative() bool { return r.n.Sign() < 0 } +// IsString returns true if this number represents a string value. +func (r *Rat) IsString() bool { + return false +} + +// IsSymbol returns true if this number represents a symbol. +func (r *Rat) IsSymbol() bool { + return false +} + // Compare returns -1, 0, or 1 if this rational is less than, equal to, or greater than another. func (r *Rat) Compare(other Number) (int, error) { otherRat, ok := other.(*Rat) @@ -393,7 +414,8 @@ func ToRat(n Number) *big.Rat { // ToFloat converts a Number to float64. func ToFloat(n Number) float64 { - return n.Float64() + val, _ := n.Float64() + return val } // StringNum represents a string value on the stack for variable names. @@ -433,6 +455,7 @@ func (s *StringNum) IsNegative() bool { return false } func (s *StringNum) Compare(other Number) (int, error) { return 0, fmt.Errorf("string not supported for comparison") } func (s *StringNum) Bool() (bool, error) { return false, fmt.Errorf("string not supported for Bool()") } func (s *StringNum) IsBool() bool { return false } +func (s *StringNum) IsSymbol() bool { return false } // Symbol represents a variable symbol on the stack. // Symbols are created when: @@ -493,6 +516,9 @@ func (s *Symbol) IsZero() bool { func (s *Symbol) IsNegative() bool { return false } +func (s *Symbol) IsString() bool { + return false +} func (s *Symbol) Compare(other Number) (int, error) { return 0, fmt.Errorf("symbol not supported for comparison") } |
