tvl-depot/tvix
Florian Klink 1facd889bb feat(tvix/eval): use lexical-core to format float
Apparently our naive implementation of float formatting, which simply
used {:.5}, and trimmed trailing "0" strings not sufficient.

It wrongly trimmed numbers with zeroes but no decimal point, like
`10000` got trimmed to `1`.

Nix uses `std::to_string` on the double, which according to
https://en.cppreference.com/w/cpp/string/basic_string/to_string
is equivalent to `std::sprintf(buf, "%f", value)`.

https://en.cppreference.com/w/cpp/io/c/fprintf mentions this is treated
like this:

> Precision specifies the exact number of digits to appear after
> the decimal point character. The default precision is 6. In the
> alternative implementation decimal point character is written even if
> no digits follow it. For infinity and not-a-number conversion style
> see notes.

This doesn't seem to be the case though, and Nix uses scientific
notation in some cases.

There's a whole bunch of strategies to determine which is a more compact
notation, and which notation should be used for a given number.
https://github.com/rust-lang/rust/issues/24556 provides some pointers
into various rabbit holes for those interested.

This gist seems to be that currently a different formatting is not
exposed in rust directly, at least not for public consumption.

There is the
[lexical-core](https://github.com/Alexhuszagh/rust-lexical) crate
though, which provides a way to format floats with various strategies
and formats.

Change our implementation of `TotalDisplay` for the `Value::Float` case
to use that. We still need to do some post-processing, because Nix
always adds the sign in scientific notation (and there's no way to
configure lexical-core to do that), and lexical-core in some cases keeps
the trailing zeros.

Even with all that in place, there as a difference in `eval-okay-
fromjson.nix` (from tvix-tests), which I couldn't get to work. I updated
the fixture to a less problematic number.

With this, the testsuite passes again, and does for the upcoming CL
introducing builtins.fromTOML, and enabling the nix testsuite bits for
it, too.

Change-Id: Ie6fba5619e1d9fd7ce669a51594658b029057acc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7922
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
2023-01-25 07:49:44 +00:00
..
.vscode chore(tvix): fix vscode rust-analyzer recommendation 2022-10-15 16:54:28 +00:00
cli feat(tvix/cli): add errors module with drv construction errors 2023-01-20 15:39:51 +00:00
derivation refactor(tvix/derivation): relax bounds for reference iterator 2023-01-23 15:08:32 +00:00
docs docs(tvix): add reconstructed changelog for the Nix language 2023-01-16 09:32:49 +00:00
eval feat(tvix/eval): use lexical-core to format float 2023-01-25 07:49:44 +00:00
nar docs(tvix/nar): document how to use NAR writer 2022-12-21 14:31:54 +00:00
nix_cli chore(tvix): upgrade to clap 4.0 2022-12-21 13:23:38 +00:00
proto chore(tvix/store): move castore.proto 2022-12-04 10:41:39 +00:00
serde feat(tvix/serde): implement enum deserialisation 2023-01-04 17:21:40 +00:00
store chore(tvix/store): add fastcdc crate 2023-01-21 15:12:45 +00:00
verify-lang-tests test(tvix/eval): add test for builtins parity 2023-01-06 12:00:38 +00:00
.gitignore feat(tvix/): .gitignore target folders 2022-11-11 19:55:12 +00:00
Cargo.lock feat(tvix/eval): use lexical-core to format float 2023-01-25 07:49:44 +00:00
Cargo.nix feat(tvix/eval): use lexical-core to format float 2023-01-25 07:49:44 +00:00
Cargo.toml feat(tvix/serde): initial Nix->serde::Deserialize impl 2023-01-02 22:24:43 +00:00
crate-hashes.json chore(tvix/store): add tonic-mock 2023-01-21 09:34:15 +00:00
default.nix refactor(tvix/store): move protobuf build config one level up 2023-01-06 17:57:06 +00:00
LICENSE chore(tvix): Bootstrap Tvix folder 2021-03-27 00:09:49 +00:00
OWNERS chore(gerrit): migrate OWNERS files to code-owners style 2022-09-19 11:13:28 +00:00
README.md docs(tvix): link to IRC channel and mailing list 2023-01-21 15:08:38 +00:00

Tvix

For more information about Tvix, feel free to reach out. We are interested in people who would like to help us review designs, brainstorm and describe requirements that we may not yet have considered.

Most of the discussion around development happens on our IRC channel, which you can join in several ways documented on tvl.fyi.

There's also some discussion around development on our mailing list.

Building the CLI

If you are in a full checkout of the TVL depot, you can simply run mg build in the cli directory (or mg build //tvix/cli from anywhere in the repo). The mg command is found in /tools/magrathea.

Important note: We only use and test Nix builds of our software against Nix 2.3. There are a variety of bugs and subtle problems in newer Nix versions which we do not have the bandwidth to address, builds in newer Nix versions may or may not work.

The CLI can also be built with standard Rust tooling (i.e. cargo build), as long as you are in a shell with the right dependencies (provided by mg shell //tvix:shell).

Rust projects, crate2nix

Some parts of Tvix are written in Rust. To simplify the dependency management on the Nix side of these builds, we use crate2nix in a single Rust workspace in //tvix to maintain the Nix build configuration.

When making changes to Cargo dependency configuration in any of the Rust projects under //tvix, be sure to run mg run //tvix:crate2nixGenerate -- in //tvix itself and commit the changes to the generated Cargo.nix file.

License structure

All code implemented for Tvix is licensed under the GPL-3.0, with the exception of the protocol buffer definitions used for communication between services which are available under a more permissive license (MIT).

The idea behind this structure is that any direct usage of our code (e.g. linking to it, embedding the evaluator, etc.) will fall under the terms of the GPL3, but users are free to implement their own components speaking these protocols under the terms of the MIT license.