Opened 9 years ago

Last modified 9 years ago

#9545 new Bugs

program_options description printout bug

Reported by: Torquil Sørensen <torquil@…> Owned by: Vladimir Prus
Milestone: To Be Determined Component: program_options
Version: Boost 1.55.0 Severity: Problem
Keywords: Cc: torquil@…

Description

When setting the default value of a float option to 0.05, the help description that is printed by the program states that the default value is 0.0500000007. For double, it states "0.050000000000000003". The value of the variable within the program is correct, i.e. 0.05.

I'm compiling with:

$ g++ --version g++ (GCC) 4.8.3 20131226 (prerelease)

Here is a test program that reproduces the problem when run with the option "--help":

#include "boost/program_options.hpp"

int main(int argc, char ** argv)
{
	float dt;

	boost::program_options::variables_map vm;
	boost::program_options::options_description desc("Description");
	desc.add_options()
		("help", "Print help message")
		("dt", boost::program_options::value<float>(&dt)->default_value(0.05), "Time step")
	;

	boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
	boost::program_options::notify(vm);

	if(vm.count("help")) desc.print(std::cout);

	std::cout << "dt = " << dt << "\n";

	return 0;
}

Change History (2)

comment:1 by Vladimir Prus, 9 years ago

I think the printed value is the correct one. 0.05 likely cannot be represented by IEEE floating point types. If you want to show "0.05" to user, please use the second argument to 'default_value' to specify the presentation in help output.

comment:2 by torquil@…, 9 years ago

Thanks, that seems very likely. So it is the intentional lower default precision of std::cout that makes it print 0.05, which is an approximation to the actual variable value. And it would in fact add those zeroes and the final nonzero digit if its precision was increased sufficiently using setprecision().

But I guess std::cout uses a lower default precision intentionally, and perhaps the same should at least be considered for the program_options printout.

So it seems that this is just an esthetic problem, and can even be changed using the second argument as you stated, so I will simply let it print the value that it does at the moment. The extra digits don't bother me much.

Note: See TracTickets for help on using tickets.