Skip to content

Commit a5fefdb

Browse files
authored
feat: Add more math functions (#1091)
1 parent fe3ce9d commit a5fefdb

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ local html = import 'html.libsonnet';
110110
<ul><code>std.acos(x)</code></ul>
111111
<ul><code>std.atan(x)</code></ul>
112112
<ul><code>std.round(x)</code></ul>
113+
<ul><code>std.isEven(x)</code></ul>
114+
<ul><code>std.isOdd(x)</code></ul>
115+
<ul><code>std.isInteger(x)</code></ul>
116+
<ul><code>std.isDecimal(x)</code></ul>
113117
</ul>
114118
<p>
115119
The function <code>std.mod(a, b)</code> is what the % operator is desugared to. It performs
116120
modulo arithmetic if the left hand side is a number, or if the left hand side is a string,
117121
it does Python-style string formatting with <code>std.format()</code>.
118122
</p>
123+
<p>
124+
The functions <code>std.isEven(x)</code> and <code>std.isOdd(x)</code> use integral part of a
125+
floating number to test for even or odd.
126+
</p>
119127
|||,
120128
],
121129
fields: [

stdlib/std.jsonnet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,11 @@ limitations under the License.
17521752
contains(arr, elem):: std.any([e == elem for e in arr]),
17531753

17541754
equalsIgnoreCase(str1, str2):: std.asciiLower(str1) == std.asciiLower(str2),
1755+
1756+
isEven(x):: std.round(x) % 2 == 0,
1757+
isOdd(x):: std.round(x) % 2 != 0,
1758+
isInteger(x):: std.round(x) == x,
1759+
isDecimal(x):: std.round(x) != x,
17551760

17561761
removeAt(arr, at):: [
17571762
arr[i],

test_suite/stdlib.jsonnet

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,15 @@ std.assertEqual(std.contains([1, 2, 3], 'foo'), false) &&
15791579
std.assertEqual(std.equalsIgnoreCase('foo', 'FOO'), true) &&
15801580
std.assertEqual(std.equalsIgnoreCase('foo', 'bar'), false) &&
15811581

1582+
std.assertEqual(std.isEven(10), true) &&
1583+
std.assertEqual(std.isEven(5), false) &&
1584+
std.assertEqual(std.isOdd(5), true) &&
1585+
std.assertEqual(std.isOdd(10), false) &&
1586+
std.assertEqual(std.isInteger(1), true) &&
1587+
std.assertEqual(std.isInteger(1.1), false) &&
1588+
std.assertEqual(std.isDecimal(1.1), true) &&
1589+
std.assertEqual(std.isDecimal(1), false) &&
1590+
15821591
std.assertEqual(std.remove([1, 2, 3], 2), [1, 3]) &&
15831592
std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&
15841593

0 commit comments

Comments
 (0)