double_ parser is less precise than atof

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

double_ parser is less precise than atof

jfaust
The following code demonstrates a small loss of precision using qi. I'm using VS 2013 and boost 1.60.0. The problem is shown near the end where an int64 is converted to double.

The main problem for me is that I'm trying to replace an existing parser with qi. The small differences in precision are causing differences in our regression test suite.

{
	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
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: double_ parser is less precise than atof

sehe
On Tue, Feb 28, 2017, at 04:14 PM, jfaust wrote:

> The following code demonstrates a small loss of precision using qi. I'm
> using
> VS 2013 and boost 1.60.0. The problem is shown near the end where an
> int64
> is converted to double.
>
> The main problem for me is that I'm trying to replace an existing parser
> with qi. The small differences in precision are causing differences in
> our
> regression test suite.
>
Did You forget to include the reproducing test code? I can't see it

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: double_ parser is less precise than atof

jfaust
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
}
Loading...