optimizations
This commit is contained in:
@@ -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) }
|
||||
|
||||
Reference in New Issue
Block a user