Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grisu: trailing zero with fixed, precision=0, and showpoint #1417

Closed
planet36 opened this issue Nov 22, 2019 · 4 comments
Closed

Grisu: trailing zero with fixed, precision=0, and showpoint #1417

planet36 opened this issue Nov 22, 2019 · 4 comments

Comments

@planet36
Copy link

When printing in fixed notation with precision=0 and showpoint , there is a trailing zero when Grisu is enabled.

Using g++ 9.2.1 and libfmt master branch (9108b25)

The following code illustrates this

#include <fmt/format.h>
#include <iostream>

int main()
{
	std::cout << "FMT_VERSION=" << FMT_VERSION << '\n';
	std::cout << "FMT_GCC_VERSION=" << FMT_GCC_VERSION << '\n';
#ifdef FMT_USE_GRISU
	std::cout << "FMT_USE_GRISU=" << FMT_USE_GRISU << '\n';
#else
	std::cout << "FMT_USE_GRISU not defined\n";
#endif

	double x = 123.0;
	int precision = 0;

	std::cout.precision(precision);

	std::cout << "cout: " << std::showpoint << std::fixed << x << '\n';
	std::cout << "fmt:  " << fmt::format("{:#.{}f}", x, precision) << '\n';

	return 0;
}

Outputs:

FMT_VERSION=60001
FMT_GCC_VERSION=902
FMT_USE_GRISU=1
cout: 123.
fmt:  123.0

@vitaut
Copy link
Contributor

vitaut commented Nov 24, 2019

Good catch, thanks! Fixed in 28d7191.

@vitaut vitaut closed this as completed Nov 24, 2019
@planet36
Copy link
Author

On a related note...
Running the same example code, but with Grisu disabled, I noticed a change in output introduced in commit ded1e76.

FMT_VERSION=60001
FMT_GCC_VERSION=902
FMT_USE_GRISU=0
cout: 123.
fmt: 123.000000

Using the prior commit (c7edd8e) outputs:

FMT_VERSION=60001
FMT_GCC_VERSION=902
FMT_USE_GRISU=0
cout: 123.
fmt: 123.

@vitaut
Copy link
Contributor

vitaut commented Nov 26, 2019

FP formatting was being refactored =). Should be fixed now.

@planet36
Copy link
Author

planet36 commented Dec 3, 2019

On another related note, when printing scientific notation, there is a missing decimal point.

	std::cout << "cout: " << std::showpoint << std::scientific << x << '\n';
	std::cout << "fmt:  " << fmt::format("{:#.{}e}", x, precision) << '\n';

Outputs

cout: 1.e+02
fmt: 1e+02

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

No branches or pull requests

2 participants