tvl-depot/users/sterni/nix/float/default.nix
sterni 55a3b3eb81 feat(sterni/nix): add trivial float library
Only implements the different conversion types from and to ints for now.
Unfortunately very reliant on builtins.{floor,ceil} which can't be
implemented purely except very inefficiently (to my knowledge), so it
only really works for C++ Nix >= 2.4. Tests are thus skipped for
C++ Nix 2.3.

Change-Id: Idcb1a11df11e214cdba3f2a0715472b370daa7dc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9008
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
2023-08-09 12:12:47 +00:00

23 lines
522 B
Nix

{ depot, ... }:
let
inherit (depot.users.sterni.nix)
num
;
in
rec {
# In C++ Nix, the required builtins have been added in version 2.4
ceil = builtins.ceil or (throw "Nix implementation is missing builtins.ceil");
floor = builtins.floor or (throw "Nix implementation is missing builtins.floor");
truncate = f: if f >= 0 then floor f else ceil f;
round = f:
let
s = num.sign f;
a = s * f;
in
s * (if a >= floor a + 0.5 then ceil a else floor a);
intToFloat = i: i * 1.0;
}