Skip to content

Commit

Permalink
joker.math/hypot
Browse files Browse the repository at this point in the history
  • Loading branch information
candid82 committed Feb 5, 2018
1 parent 42bf89d commit 207838a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions std/math.joke
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
{:added "1.0"
:go "cos(x)"}
[^Number x])

(defn ^Double hypot
"Returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow."
{:added "1.0"
:go "hypot(p, q)"}
[^Number p, ^Number q])
21 changes: 21 additions & 0 deletions std/math/a_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ var cos_ Proc = func(args []Object) Object {
return NIL
}

var hypot_ Proc = func(args []Object) Object {
c := len(args)
switch {
case c == 2:

p := ExtractNumber(args, 0)
q := ExtractNumber(args, 1)
res := hypot(p, q)
return MakeDouble(res)

default:
PanicArity(c)
}
return NIL
}

var sin_ Proc = func(args []Object) Object {
c := len(args)
switch {
Expand All @@ -49,6 +65,11 @@ mathNamespace.InternVar("cos", cos_,
NewListFrom(NewVectorFrom(MakeSymbol("x"))),
`Returns the cosine of the radian argument x.`, "1.0"))

mathNamespace.InternVar("hypot", hypot_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("p"), MakeSymbol("q"))),
`Returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.`, "1.0"))

mathNamespace.InternVar("sin", sin_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("x"))),
Expand Down
4 changes: 4 additions & 0 deletions std/math/math_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ func sin(x Number) float64 {
func cos(x Number) float64 {
return math.Cos(x.Double().D)
}

func hypot(p Number, q Number) float64 {
return math.Hypot(p.Double().D, q.Double().D)
}

0 comments on commit 207838a

Please sign in to comment.