Floats with integral mantissa are mangled by printing #487

Closed
opened 2024-08-25 10:06:06 +00:00 by alois31 · 1 comment
Member

Describe the bug

Commands that can print out Nix language expressions (nix-instantiate --parse), or things that look like such (nix-instantiate --eval, nix eval, nix repl), do not show a decimal point when printing floats whose mantissa (in decimal notation) happens to be an integer. The result is misinterpreted as an integer (when there is no exponent), or as a sequence of an integer, the variable e, a plus or minus sign, and another integer (when there is an exponent, since the parser does not accept floats without decimal point and allows termination of integer literals by random letters).

Steps To Reproduce

  1. nix-instantiate --parse --expr 1.0 prints 1
  2. nix-instantiate --parse --expr 1.0e10 prints 1e+10
  3. nix-instantiate --parse --expr 1e+10 prints:
error: undefined variable 'e'
       at «string»:1:2:
            1| 1e+10
             |  ^
  1. Similar results can be observed with the other mentioned commands.

Expected behavior

Floats are printed in a way that allows Lix to parse them as floats again.

nix --version output

nix (Lix, like Nix) 2.92.0-dev-pref2e7f8bab875

Additional context

Some cppnix bugs complaining about this behaviour:
https://github.com/NixOS/nix/issues/3077
https://github.com/NixOS/nix/issues/5063
https://github.com/NixOS/nix/issues/7554
That this happens even for nix-instantiate --parse is not mentioned there, but I think actually most severe, since one would expect this command to print a Nix expression equivalent to the original one. The other commands have no such guarantee (and do occasionally print out other unparseable things, such as «repeated» or subnormals), but it's still a footgun.

## Describe the bug Commands that can print out Nix language expressions (`nix-instantiate --parse`), or things that look like such (`nix-instantiate --eval`, `nix eval`, `nix repl`), do not show a decimal point when printing floats whose mantissa (in decimal notation) happens to be an integer. The result is misinterpreted as an integer (when there is no exponent), or as a sequence of an integer, the variable `e`, a plus or minus sign, and another integer (when there is an exponent, since the parser does not accept floats without decimal point and allows termination of integer literals by random letters). ## Steps To Reproduce 1. `nix-instantiate --parse --expr 1.0` prints `1` 2. `nix-instantiate --parse --expr 1.0e10` prints `1e+10` 3. `nix-instantiate --parse --expr 1e+10` prints: ``` error: undefined variable 'e' at «string»:1:2: 1| 1e+10 | ^ ``` 4. Similar results can be observed with the other mentioned commands. ## Expected behavior Floats are printed in a way that allows Lix to parse them as floats again. ## `nix --version` output nix (Lix, like Nix) 2.92.0-dev-pref2e7f8bab875 ## Additional context Some cppnix bugs complaining about this behaviour: https://github.com/NixOS/nix/issues/3077 https://github.com/NixOS/nix/issues/5063 https://github.com/NixOS/nix/issues/7554 That this happens even for `nix-instantiate --parse` is not mentioned there, but I think actually most severe, since one would expect this command to print a Nix expression equivalent to the original one. The other commands have no such guarantee (and do occasionally print out other unparseable things, such as `«repeated»` or subnormals), but it's still a footgun.
Member

This issue was mentioned on Gerrit on the following CLs:

  • commit message in cl/2189 ("libexpr: Remove Expr::show, add JSON expression serialization")
  • commit message in cl/2190 ("libexpr: Remove Expr::show, add JSON expression serialization")
<!-- GERRIT_LINKBOT: {"cls": [{"backlink": "https://gerrit.lix.systems/c/lix/+/2189", "number": 2189, "kind": "commit message"}, {"backlink": "https://gerrit.lix.systems/c/lix/+/2190", "number": 2190, "kind": "commit message"}], "cl_meta": {"2189": {"change_title": "libexpr: Remove `Expr::show`, add JSON expression serialization"}, "2190": {"change_title": "libexpr: Remove `Expr::show`, add JSON expression serialization"}}} --> This issue was mentioned on Gerrit on the following CLs: * commit message in [cl/2189](https://gerrit.lix.systems/c/lix/+/2189) ("libexpr: Remove `Expr::show`, add JSON expression serialization") * commit message in [cl/2190](https://gerrit.lix.systems/c/lix/+/2190) ("libexpr: Remove `Expr::show`, add JSON expression serialization")
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#487
No description provided.