Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add evaluable functor: atan2 #170

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions engine/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ var DefaultEvaluableFunctors = EvaluableFunctors{
`/\`: BitwiseAnd,
`\/`: BitwiseOr,

`div`: IntFloorDiv,
`max`: Max,
`min`: Min,
`^`: IntegerPower,
`div`: IntFloorDiv,
`max`: Max,
`min`: Min,
`^`: IntegerPower,
`atan2`: Atan2,
},
}

Expand Down Expand Up @@ -960,6 +961,35 @@ func Acos(x Number) (Number, error) {
return Float(math.Acos(vx)), nil
}

// Atan2 returns the arc tangent of y/x.
func Atan2(y, x Number) (Number, error) {
var vy float64
switch y := y.(type) {
case Integer:
vy = float64(y)
case Float:
vy = float64(y)
default:
return nil, ErrUndefined
}

var vx float64
switch x := x.(type) {
case Integer:
vx = float64(x)
case Float:
vx = float64(x)
default:
return nil, ErrUndefined
}

if vx == 0 && vy == 0 {
return nil, ErrUndefined
}

return Float(math.Atan2(vy, vx)), nil
}

// Comparison

func eqF(x, y Float) bool {
Expand Down
50 changes: 50 additions & 0 deletions engine/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,56 @@ func TestAcos(t *testing.T) {
})
}

func TestAtan2(t *testing.T) {
t.Run("integer", func(t *testing.T) {
t.Run("integer", func(t *testing.T) {
r, err := Atan2(Integer(0), Integer(1))
assert.NoError(t, err)
assert.Equal(t, Float(0), r)
})

t.Run("float", func(t *testing.T) {
r, err := Atan2(Integer(0), Float(1))
assert.NoError(t, err)
assert.Equal(t, Float(0), r)
})

t.Run("not a number", func(t *testing.T) {
_, err := Atan2(Integer(0), mockNumber{})
assert.Equal(t, ErrUndefined, err)
})
})

t.Run("float", func(t *testing.T) {
t.Run("integer", func(t *testing.T) {
r, err := Atan2(Float(0), Integer(1))
assert.NoError(t, err)
assert.Equal(t, Float(0), r)
})

t.Run("float", func(t *testing.T) {
r, err := Atan2(Float(0), Float(1))
assert.NoError(t, err)
assert.Equal(t, Float(0), r)
})

t.Run("not a number", func(t *testing.T) {
_, err := Atan2(Float(0), mockNumber{})
assert.Equal(t, ErrUndefined, err)
})
})

t.Run("not a number", func(t *testing.T) {
_, err := Atan2(mockNumber{}, Integer(1))
assert.Equal(t, ErrUndefined, err)
})

t.Run("x and y both equal to 0", func(t *testing.T) {
_, err := Atan2(Integer(0), Integer(0))
assert.Equal(t, ErrUndefined, err)
})
}

type mockNumber struct {
mock.Mock
}
Expand Down