Skip to content

Conversation

@crertel
Copy link

@crertel crertel commented Dec 13, 2025

Motivation

Fixes #3077 .

Context

Tracking down the weird behavior of current nix (per the issue), we see:

image

I looked around for a while and realized this was probably happening over in the repl code, so I looked for CYAN and found printFloat over in src/libexpr/print.cc.

The original code:

    void printFloat(Value & v)
    {
        if (options.ansiColors)
            output << ANSI_CYAN;
        output << v.fpoint();
        if (options.ansiColors)
            output << ANSI_NORMAL;
    }

Now, iostreams by default don't show the decimal point, so we have to force it (after saving the flags):

    void printFloat(Value & v)
    {
        const std::ios_base::fmtflags flags = output.flags();

        if (options.ansiColors)
            output << ANSI_CYAN;

        output.setf(std::ios::showpoint);
        output << v.fpoint();
        output.flags(flags);

        if (options.ansiColors)
            output << ANSI_NORMAL;
    }

This has the desired effect, mostly:

image

Unfortunately, the question of "how do we get this to show the correct decimal precision?" is a bit open-ended.

The open question is:

  • Should we always, for floats with no fractional parts, just do a single decimal point? E.g., 4.0000 renders as 4.0.
  • Should floats always render with default precision? E.g., 4.0 renders as 4.00000...but so does 4.000001.
  • Should floats try to display as much as they can out to machine precision? E.g., 4.00000000000004 displays as 4.00000000000004 but some larger one eventually rounds. If it rounds to all zeros, should that be truncated as well?

There's some test problems as well, but those are less important than this core formatting question.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@crertel crertel force-pushed the 3077-fix-float-repl branch from c5063fc to d7270da Compare December 13, 2025 04:38
@crertel crertel force-pushed the 3077-fix-float-repl branch from fdb2f9a to bf949f5 Compare December 14, 2025 19:43
@crertel crertel marked this pull request as ready for review December 14, 2025 21:52
@crertel crertel requested a review from edolstra as a code owner December 14, 2025 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some floats are printed like ints

1 participant