Skip to content

Commit

Permalink
FIx floating point evaluation
Browse files Browse the repository at this point in the history
Fixes #2361.
  • Loading branch information
edolstra committed Aug 19, 2018
1 parent d277442 commit 9b1bdf2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libexpr/primops.cc
Expand Up @@ -1680,6 +1680,8 @@ static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, V

static void prim_add(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0], pos);
state.forceValue(*args[1], pos);
if (args[0]->type == tFloat || args[1]->type == tFloat)
mkFloat(v, state.forceFloat(*args[0], pos) + state.forceFloat(*args[1], pos));
else
Expand All @@ -1689,6 +1691,8 @@ static void prim_add(EvalState & state, const Pos & pos, Value * * args, Value &

static void prim_sub(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0], pos);
state.forceValue(*args[1], pos);
if (args[0]->type == tFloat || args[1]->type == tFloat)
mkFloat(v, state.forceFloat(*args[0], pos) - state.forceFloat(*args[1], pos));
else
Expand All @@ -1698,6 +1702,8 @@ static void prim_sub(EvalState & state, const Pos & pos, Value * * args, Value &

static void prim_mul(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0], pos);
state.forceValue(*args[1], pos);
if (args[0]->type == tFloat || args[1]->type == tFloat)
mkFloat(v, state.forceFloat(*args[0], pos) * state.forceFloat(*args[1], pos));
else
Expand All @@ -1707,6 +1713,9 @@ static void prim_mul(EvalState & state, const Pos & pos, Value * * args, Value &

static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0], pos);
state.forceValue(*args[1], pos);

NixFloat f2 = state.forceFloat(*args[1], pos);
if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos);

Expand Down
1 change: 1 addition & 0 deletions tests/lang/eval-okay-float.exp
@@ -0,0 +1 @@
[ 3.4 3.5 2.5 1.5 ]
6 changes: 6 additions & 0 deletions tests/lang/eval-okay-float.nix
@@ -0,0 +1,6 @@
[
(1.1 + 2.3)
(builtins.add (0.5 + 0.5) (2.0 + 0.5))
((0.5 + 0.5) * (2.0 + 0.5))
((1.5 + 1.5) / (0.5 * 4.0))
]

0 comments on commit 9b1bdf2

Please sign in to comment.