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

difftreelog

Merge pull request #91 from CertainLach/number-intrinsics

Yaroslav Bolyukin2022-11-26parents: #5f0f8de #414a5eb.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
77 ("member", builtin_member::INST),77 ("member", builtin_member::INST),
78 ("count", builtin_count::INST),78 ("count", builtin_count::INST),
79 // Math79 // Math
80 ("abs", builtin_abs::INST),
81 ("sign", builtin_sign::INST),
82 ("max", builtin_max::INST),
83 ("min", builtin_min::INST),
80 ("modulo", builtin_modulo::INST),84 ("modulo", builtin_modulo::INST),
81 ("floor", builtin_floor::INST),85 ("floor", builtin_floor::INST),
82 ("ceil", builtin_ceil::INST),86 ("ceil", builtin_ceil::INST),
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
1use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};1use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};
2
3#[builtin]
4pub fn builtin_abs(x: f64) -> Result<f64> {
5 Ok(x.abs())
6}
7
8#[builtin]
9pub fn builtin_sign(x: f64) -> Result<f64> {
10 Ok(if x == 0. { 0. } else { x.signum() })
11}
12
13#[builtin]
14pub fn builtin_max(x: f64, y: f64) -> Result<f64> {
15 Ok(x.max(y))
16}
17
18#[builtin]
19pub fn builtin_min(x: f64, y: f64) -> Result<f64> {
20 Ok(x.min(y))
21}
222
3#[builtin]23#[builtin]
4pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {24pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
77 else77 else
78 error 'Assertion failed. ' + a + ' != ' + b,78 error 'Assertion failed. ' + a + ' != ' + b,
7979
80 abs(n)::
81 if !std.isNumber(n) then
82 error 'std.abs expected number, got ' + std.type(n)
83 else
84 if n > 0 then n else -n,
85
86 sign(n)::
87 if !std.isNumber(n) then
88 error 'std.sign expected number, got ' + std.type(n)
89 else
90 if n > 0 then
91 1
92 else if n < 0 then
93 -1
94 else 0,
95
96 max(a, b)::
97 if !std.isNumber(a) then
98 error 'std.max first param expected number, got ' + std.type(a)
99 else if !std.isNumber(b) then
100 error 'std.max second param expected number, got ' + std.type(b)
101 else
102 if a > b then a else b,
103
104 min(a, b)::
105 if !std.isNumber(a) then
106 error 'std.min first param expected number, got ' + std.type(a)
107 else if !std.isNumber(b) then
108 error 'std.min second param expected number, got ' + std.type(b)
109 else
110 if a < b then a else b,
111
112 clamp(x, minVal, maxVal)::80 clamp(x, minVal, maxVal)::
113 if x < minVal then minVal81 if x < minVal then minVal
114 else if x > maxVal then maxVal82 else if x > maxVal then maxVal