optimizations
Some checks failed
Build / build (push) Successful in 10m47s
Publish Image / publish (push) Failing after 33s
Test / build (push) Failing after 6m42s

This commit is contained in:
Chuck Smith
2024-04-02 14:54:08 -04:00
parent 88e3330856
commit f08b458325
9 changed files with 313 additions and 355 deletions

View File

@@ -17,6 +17,8 @@ func (f Float) Bool() bool {
func (f Float) Add(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value + float64(obj.Value)}, nil
case Float:
return Float{f.Value + obj.Value}, nil
default:
@@ -26,6 +28,8 @@ func (f Float) Add(other Object) (Object, error) {
func (f Float) Sub(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value - float64(obj.Value)}, nil
case Float:
return Float{f.Value - obj.Value}, nil
default:
@@ -35,6 +39,8 @@ func (f Float) Sub(other Object) (Object, error) {
func (f Float) Mul(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{f.Value * float64(obj.Value)}, nil
case Float:
return Float{f.Value * obj.Value}, nil
case String:
@@ -48,6 +54,11 @@ func (f Float) Mul(other Object) (Object, error) {
func (f Float) Div(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
if obj.Value == 0.0 {
return nil, NewDivisionByZeroError(f)
}
return Float{f.Value / float64(obj.Value)}, nil
case Float:
if obj.Value == 0 {
return nil, NewDivisionByZeroError(f)
@@ -60,6 +71,8 @@ func (f Float) Div(other Object) (Object, error) {
func (f Float) Mod(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Float{math.Mod(f.Value, float64(obj.Value))}, nil
case Float:
return Float{math.Mod(f.Value, obj.Value)}, nil
default:
@@ -95,17 +108,17 @@ func (f Float) String() string {
}
// Copy implements the Copyable interface
func (i Float) Copy() Object {
return Float{Value: i.Value}
func (f Float) Copy() Object {
return Float{Value: f.Value}
}
// Hash implements the Hasher interface
func (i Float) Hash() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
func (f Float) Hash() HashKey {
return HashKey{Type: f.Type(), Value: uint64(f.Value)}
}
// Type returns the type of the object
func (i Float) Type() Type { return FloatType }
func (f Float) Type() Type { return FloatType }
// Inspect returns a stringified version of the object for debugging
func (i Float) Inspect() string { return fmt.Sprintf("%f", i.Value) }
func (f Float) Inspect() string { return fmt.Sprintf("%f", f.Value) }