git.delta.rocks / jrsonnet / refs/commits / ff649630ca7e

difftreelog

feat implement builtins for trivial numeric functions

Petr Portnov2022-11-26parent: #5f0f8de.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -77,6 +77,11 @@
 		("member", builtin_member::INST),
 		("count", builtin_count::INST),
 		// Math
+		("abs", builtin_abs::INST),
+		("sign", builtin_sign::INST),
+		("max", builtin_max::INST),
+		("min", builtin_min::INST),
+		("clamp", builtin_clamp::INST),
 		("modulo", builtin_modulo::INST),
 		("floor", builtin_floor::INST),
 		("ceil", builtin_ceil::INST),
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/math.rs
1use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};23#[builtin]4pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {5	Ok(a % b)6}78#[builtin]9pub fn builtin_floor(x: f64) -> Result<f64> {10	Ok(x.floor())11}1213#[builtin]14pub fn builtin_ceil(x: f64) -> Result<f64> {15	Ok(x.ceil())16}1718#[builtin]19pub fn builtin_log(n: f64) -> Result<f64> {20	Ok(n.ln())21}2223#[builtin]24pub fn builtin_pow(x: f64, n: f64) -> Result<f64> {25	Ok(x.powf(n))26}2728#[builtin]29pub fn builtin_sqrt(x: PositiveF64) -> Result<f64> {30	Ok(x.0.sqrt())31}3233#[builtin]34pub fn builtin_sin(x: f64) -> Result<f64> {35	Ok(x.sin())36}3738#[builtin]39pub fn builtin_cos(x: f64) -> Result<f64> {40	Ok(x.cos())41}4243#[builtin]44pub fn builtin_tan(x: f64) -> Result<f64> {45	Ok(x.tan())46}4748#[builtin]49pub fn builtin_asin(x: f64) -> Result<f64> {50	Ok(x.asin())51}5253#[builtin]54pub fn builtin_acos(x: f64) -> Result<f64> {55	Ok(x.acos())56}5758#[builtin]59pub fn builtin_atan(x: f64) -> Result<f64> {60	Ok(x.atan())61}6263#[builtin]64pub fn builtin_exp(x: f64) -> Result<f64> {65	Ok(x.exp())66}6768fn frexp(s: f64) -> (f64, i16) {69	if s == 0.0 {70		(s, 0)71	} else {72		let lg = s.abs().log2();73		let x = (lg - lg.floor() - 1.0).exp2();74		let exp = lg.floor() + 1.0;75		(s.signum() * x, exp as i16)76	}77}7879#[builtin]80pub fn builtin_mantissa(x: f64) -> Result<f64> {81	Ok(frexp(x).0)82}8384#[builtin]85pub fn builtin_exponent(x: f64) -> Result<i16> {86	Ok(frexp(x).1)87}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -77,38 +77,6 @@
     else
       error 'Assertion failed. ' + a + ' != ' + b,
 
-  abs(n)::
-    if !std.isNumber(n) then
-      error 'std.abs expected number, got ' + std.type(n)
-    else
-      if n > 0 then n else -n,
-
-  sign(n)::
-    if !std.isNumber(n) then
-      error 'std.sign expected number, got ' + std.type(n)
-    else
-      if n > 0 then
-        1
-      else if n < 0 then
-        -1
-      else 0,
-
-  max(a, b)::
-    if !std.isNumber(a) then
-      error 'std.max first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.max second param expected number, got ' + std.type(b)
-    else
-      if a > b then a else b,
-
-  min(a, b)::
-    if !std.isNumber(a) then
-      error 'std.min first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.min second param expected number, got ' + std.type(b)
-    else
-      if a < b then a else b,
-
   clamp(x, minVal, maxVal)::
     if x < minVal then minVal
     else if x > maxVal then maxVal