Add std.sign and fix division by zero in stdlib

This commit is contained in:
Stanisław Barzowski 2018-02-21 22:34:58 +01:00 committed by Dave Cunningham
parent bed2cd89bc
commit 44770e75dd
5 changed files with 10061 additions and 8676 deletions

File diff suppressed because it is too large Load Diff

View File

@ -481,7 +481,7 @@ limitations under the License.
local whole = std.floor(n_);
local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
local zp = zero_pad - prec - dot_size;
local str = render_int(n__ / n_ * whole, zp, 0, blank, sign, 10, "");
local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, "");
if prec == 0 then
str + if ensure_pt then "." else ""
else
@ -711,6 +711,16 @@ limitations under the License.
else
if n > 0 then n else -n,
sign(n)::
if std.type(n) != "number" 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.type(a) != "number" then
error "std.max first param expected number, got " + std.type(a)

View File

@ -5,7 +5,7 @@ RUNTIME ERROR: Attempt to use super when there is no super class.
{ x: 5, assert super.x == 5 }
-------------------------------------------------
<std>:1036:29-30 thunk from <thunk <ta> from <function <anonymous>>>
<std>:1046:29-30 thunk from <thunk <ta> from <function <anonymous>>>
local ta = std.type(a);
@ -13,7 +13,7 @@ RUNTIME ERROR: Attempt to use super when there is no super class.
<builtin> builtin function <type>
-------------------------------------------------
<std>:1038:33-35 thunk from <function <anonymous>>
<std>:1048:33-35 thunk from <function <anonymous>>
if !std.primitiveEquals(ta, tb) then
@ -21,7 +21,7 @@ RUNTIME ERROR: Attempt to use super when there is no super class.
<builtin> builtin function <primitiveEquals>
-------------------------------------------------
<std>:1038:12-40 function <anonymous>
<std>:1048:12-40 function <anonymous>
if !std.primitiveEquals(ta, tb) then

1
testdata/percent_format_float.golden vendored Normal file
View File

@ -0,0 +1 @@
"0.000000"

1
testdata/percent_format_float.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
"%f" % 0