query
On this page

sub

lib.sub

Primop
Docs pulled from | This Revision | about 23 hours ago


Nix manual

Takes 2 arguments

e1, e2

Return the difference between the numbers e1 and e2.

Noogle detected

Aliases

Detected Type
sub :: Number -> Number -> Number

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:4487

static void prim_sub(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceValue(*args[0], pos);
    state.forceValue(*args[1], pos);
    if (args[0]->type() == nFloat || args[1]->type() == nFloat)
        v.mkFloat(
            state.forceFloat(*args[0], pos, "while evaluating the first argument of the subtraction")
            - state.forceFloat(*args[1], pos, "while evaluating the second argument of the subtraction"));
    else {
        auto i1 = state.forceInt(*args[0], pos, "while evaluating the first argument of the subtraction");
        auto i2 = state.forceInt(*args[1], pos, "while evaluating the second argument of the subtraction");

        auto result_ = i1 - i2;

        if (auto result = result_.valueChecked(); result.has_value()) {
            v.mkInt(*result);
        } else {
            state.error<EvalError>("integer overflow in subtracting %1% - %2%", i1, i2).atPos(pos).debugThrow();
        }
    }
}