From 7f7f99f3505ba9bc3d330fe32d3e697a2ad45446 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Mon, 10 May 2021 11:47:00 +0200 Subject: [PATCH 1/2] Implement `builtins.floor` and `builtins.ceil` using the C library functions internally Closes #4782 Note: even though the type is internally called `NixFloat`, it's actually a `double`. --- src/libexpr/primops.cc | 40 +++++++++++++++++++++++++++++ tests/lang/eval-okay-floor-ceil.exp | 1 + tests/lang/eval-okay-floor-ceil.nix | 9 +++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/lang/eval-okay-floor-ceil.exp create mode 100644 tests/lang/eval-okay-floor-ceil.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index fc4afaab8..3db9c6478 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -21,6 +21,8 @@ #include #include +#include + namespace nix { @@ -714,6 +716,44 @@ static RegisterPrimOp primop_addErrorContext(RegisterPrimOp::Info { .fun = prim_addErrorContext, }); +static void prim_ceil(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + auto value = state.forceFloat(*args[0], args[0]->determinePos(pos)); + mkInt(v, ceil(value)); +} + +static RegisterPrimOp primop_ceil({ + .name = "__ceil", + .args = {"double"}, + .doc = R"( + Converts an IEEE-754 double-precision floating-point number (*double*) to + the next higher integer. + + If the datatype is neither an integer nor a "float", an evaluation error will be + thrown. + )", + .fun = prim_ceil, +}); + +static void prim_floor(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + auto value = state.forceFloat(*args[0], args[0]->determinePos(pos)); + mkInt(v, floor(value)); +} + +static RegisterPrimOp primop_floor({ + .name = "__floor", + .args = {"double"}, + .doc = R"( + Converts an IEEE-754 double-precision floating-point number (*double*) to + the next lower integer. + + If the datatype is neither an integer nor a "float", an evaluation error will be + thrown. + )", + .fun = prim_floor, +}); + /* Try evaluating the argument. Success => {success=true; value=something;}, * else => {success=false; value=false;} */ static void prim_tryEval(EvalState & state, const Pos & pos, Value * * args, Value & v) diff --git a/tests/lang/eval-okay-floor-ceil.exp b/tests/lang/eval-okay-floor-ceil.exp new file mode 100644 index 000000000..81f80420b --- /dev/null +++ b/tests/lang/eval-okay-floor-ceil.exp @@ -0,0 +1 @@ +"23;24;23;23" diff --git a/tests/lang/eval-okay-floor-ceil.nix b/tests/lang/eval-okay-floor-ceil.nix new file mode 100644 index 000000000..d76a0d86e --- /dev/null +++ b/tests/lang/eval-okay-floor-ceil.nix @@ -0,0 +1,9 @@ +with import ./lib.nix; + +let + n1 = builtins.floor 23.5; + n2 = builtins.ceil 23.5; + n3 = builtins.floor 23; + n4 = builtins.ceil 23; +in + builtins.concatStringsSep ";" (map toString [ n1 n2 n3 n4 ]) From bec33858bbd053404bbf2c4f60e97cad13c365e0 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Mon, 10 May 2021 16:41:10 +0200 Subject: [PATCH 2/2] primops: math.h -> cmath Co-authored-by: Eelco Dolstra --- src/libexpr/primops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 3db9c6478..e8569b654 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -21,7 +21,7 @@ #include #include -#include +#include namespace nix {