query
On this page

mul

builtins.mul

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


Nix manual

Takes 2 arguments

e1, e2

Return the product of the numbers e1 and e2.

Noogle detected

Aliases

Detected Type
mul :: Number -> Number -> Number

Implementation

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

src/libexpr/primops.cc:4538

static void prim_mul(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 of the multiplication")
            * state.forceFloat(*args[1], pos, "while evaluating the second argument of the multiplication"));
    else {
        auto i1 = state.forceInt(*args[0], pos, "while evaluating the first argument of the multiplication");
        auto i2 = state.forceInt(*args[1], pos, "while evaluating the second argument of the multiplication");

        auto result_ = i1 * i2;

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