summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorMichael Daines <michael@mdaines.com>2013-09-11 20:22:56 -0400
committerMichael Daines <michael@mdaines.com>2013-09-11 20:24:41 -0400
commit82d8253c19fd5cc8f24b732a99cfaa7e5b8d96db (patch)
tree36aef6c399131acd7f87c21c122254f55a487439 /builtin.c
parentd25341478381063d1c76e81b3a52e0592a7c997f (diff)
Add sqrt operator
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 0bdb9356..a19855c0 100644
--- a/builtin.c
+++ b/builtin.c
@@ -76,6 +76,15 @@ static jv f_floor(jv input) {
return ret;
}
+static jv f_sqrt(jv input) {
+ if (jv_get_kind(input) != JV_KIND_NUMBER) {
+ return type_error(input, "has no square root");
+ }
+ jv ret = jv_number(sqrt(jv_number_value(input)));
+ jv_free(input);
+ return ret;
+}
+
static jv f_negate(jv input) {
if (jv_get_kind(input) != JV_KIND_NUMBER) {
return type_error(input, "cannot be negated");
@@ -491,6 +500,7 @@ static jv f_error(jv input, jv msg) {
static const struct cfunction function_list[] = {
{(cfunction_ptr)f_floor, "_floor", 1},
+ {(cfunction_ptr)f_sqrt, "_sqrt", 1},
{(cfunction_ptr)f_plus, "_plus", 3},
{(cfunction_ptr)f_negate, "_negate", 1},
{(cfunction_ptr)f_minus, "_minus", 3},
@@ -576,6 +586,7 @@ static const char* const jq_builtins[] = {
"def max_by(f): _max_by_impl(map([f]));",
"def min_by(f): _min_by_impl(map([f]));",
"def floor: _floor;",
+ "def sqrt: _sqrt;",
"def add: reduce .[] as $x (null; . + $x);",
"def del(f): delpaths([path(f)]);",
"def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));",