Skip to content

Commit

Permalink
Merge pull request #170 from ichiban/atan2
Browse files Browse the repository at this point in the history
add evaluable functor: atan2
  • Loading branch information
ichiban authored Mar 19, 2022
2 parents e605e4c + fa82d26 commit 81e3f6e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
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

0 comments on commit 81e3f6e

Please sign in to comment.