Re: double_ parser is less precise than atof

jfaust <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
I think the "raw" tags in Nabble stripped the code from the email. Trying
again.

{
	using namespace boost::spirit;
	using namespace boost::spirit::qi;

	const double expected = -0.9022464718061531;
	const char* input = "-0.9022464718061531";

	// Both atof and lexical_cast give the same result
	double fromAtof = atof(input);   // -0.90224647180615314
	double fromLexCast = boost::lexical_cast<double>(input);  //
-0.90224647180615314

	// qi::parse gives a slightly different answer
	char const* f(input);
	char const* l(f + strlen(f));
	double fromSpirit = 0.0;
	parse(f, l, double_, fromSpirit); // -0.90224647180615325

	// The cause is the calculation in boost::spirit::traits::scale:
	//    n = T(acc_n) / pow10<T>(-exp);
	// I've expanded the code a bit here:
	unsigned long long acc_n = 9022464718061531ul;  // 9022464718061531
	double d_acc_n = double(acc_n);                  // 9022464718061532.0  !
	double fromInternalCalc = d_acc_n / -10000000000000000.; //
-0.90224647180615325
}



--
View this message in context: http://boost.2283326.n4.nabble.com/double-parser-is-less-precise-than-atof-tp4691900p4691921.html
Sent from the spirit-general mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.