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

Program options default has unreasonable precision #85

Open
phma opened this issue Jun 28, 2019 · 2 comments
Open

Program options default has unreasonable precision #85

phma opened this issue Jun 28, 2019 · 2 comments

Comments

@phma
Copy link

phma commented Jun 28, 2019

Source code:

  time_t now,then;
  double tolerance,stageTolerance,areadone,rmsadj;
  bool done=false;
  string inputFile,outputFile;
  bool validArgs,validCmd=true;
  po::options_description generic("Options");
  po::options_description hidden("Hidden options");
  po::options_description cmdline_options;
  po::positional_options_description p;
  po::variables_map vm;
  generic.add_options()
    ("tolerance,t",po::value<double>(&tolerance)->default_value(0.1),"Vertical tolerance")
    ("output,o",po::value<string>(&outputFile),"Output file");
  hidden.add_options()
    ("input",po::value<string>(&inputFile),"Input file");
  p.add("input",1);
  cmdline_options.add(generic).add(hidden);
//...
      cout<<generic;

Output:

  -t [ --tolerance ] arg (=0.10000000000000001)
                                        Vertical tolerance
  -o [ --output ] arg                   Output file

Output should be:

  -t [ --tolerance ] arg (=0.1)         Vertical tolerance
  -o [ --output ] arg                   Output file

I've tried this on Boost 1.65.1 on Ubuntu Beaver, and Boost 1.69.0 on DragonFly BSD 5.5, and got the same result.

For an idea on how to fix it, see ldecimal.cpp in https://github.com/phma/bezitopo, but that would output ".1", not "0.1".

@e-kwsm
Copy link

e-kwsm commented Nov 15, 2019

-    ("tolerance,t",po::value<double>(&tolerance)->default_value(0.1),"Vertical tolerance")
+    ("tolerance,t",po::value<double>(&tolerance)->default_value(0.1, "0.1"),"Vertical tolerance")

@mattgruenke
Copy link

-    ("tolerance,t",po::value<double>(&tolerance)->default_value(0.1),"Vertical tolerance")
+    ("tolerance,t",po::value<double>(&tolerance)->default_value(0.1, "0.1"),"Vertical tolerance")

Thanks for mentioning that, but it adds verbosity (and quite a lot, if you want to do it in a way that avoids redundancy) when the default precision really should be lower.

Further, I wonder if a better solution wouldn't be to have an overload of default_value() that takes an arbitrary iomanip. That would enable the entire range of formatting customizations that std::ostream supports.

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

3 participants