~aasg/nixexprs

ref: 4b5399b1f065b1ba2703e84ac96827ab9b47c5e7 nixexprs/lib/math.nix -rw-r--r-- 740 bytes
4b5399b1 — Aluísio Augusto Silva Gonçalves ci: Don't rely on `meta.platforms` being set 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{ lib, ... }:

rec {
  /* abs :: int -> int[>= 0]
   *
   * Returns the absolute (unsigned) value of n.
   */
  abs = n:
    if n >= 0
    then n
    else n * (-1);

  /* pow :: int -> int[> 0] -> int[]
   *
   * Exponentiation operator, computes n to the e-th (non-negative)
   * power.
   */
  pow = n: e:
    let
      result =
        if e == 0 then 1
        else if e == 1 then n
        else n * (pow n (e - 1));
    in
    assert e >= 0;
    assert e > 0 -> (abs result) >= (abs n);
    result;

  /* rem :: int -> int -> int
   *
   * Computes the remainder of the division of `n` by `d`, such that
   * `rem n d == n - (n / d) * d`.  The sign of the result is that of
   * the dividend `n`.
   */
  rem = n: d: n - d * (n / d);
}