mirror of
https://github.com/boostorg/compute.git
synced 2026-02-18 01:52:20 +00:00
make_literal is losing some precision when making literal floating point values because it uses digits10, but that only gives us the number of decimal digits that will survive a decimal->native>decimal conversion; what we are doing is a native->decimal->native conversion, which requires the user of ::max_digits10 (which is 2 (double) or 3 (float) larger than ::digits10). max_digits10 is a c++11 feature, however, so this commit uses digits10 + 3 when the c++11 numeric_limits isn't available.
61 lines
1.9 KiB
C++
61 lines
1.9 KiB
C++
//---------------------------------------------------------------------------//
|
|
// Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca>
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0
|
|
// See accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt
|
|
//
|
|
// See http://boostorg.github.com/compute for more information.
|
|
//---------------------------------------------------------------------------//
|
|
|
|
#define BOOST_TEST_MODULE TestLiteralConversion
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
#include <boost/compute/detail/literal.hpp>
|
|
|
|
BOOST_AUTO_TEST_CASE(literal_conversion_float)
|
|
{
|
|
std::vector<float> values, roundtrip;
|
|
values.push_back(1.2345679f);
|
|
values.push_back(1.2345680f);
|
|
values.push_back(1.2345681f);
|
|
for (int i = 0; i < values.size(); i++) {
|
|
std::istringstream iss(boost::compute::detail::make_literal(values[i]));
|
|
float x;
|
|
BOOST_CHECK(iss >> x);
|
|
BOOST_CHECK_EQUAL(char(iss.get()), 'f');
|
|
// Make sure we're at the end:
|
|
iss.peek();
|
|
BOOST_CHECK(iss.eof());
|
|
|
|
roundtrip.push_back(x);
|
|
}
|
|
BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
|
|
BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
|
|
BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(literal_conversion_double)
|
|
{
|
|
std::vector<double> values, roundtrip;
|
|
values.push_back(1.2345678901234567);
|
|
values.push_back(1.2345678901234569);
|
|
values.push_back(1.2345678901234571);
|
|
for (int i = 0; i < values.size(); i++) {
|
|
std::istringstream iss(boost::compute::detail::make_literal(values[i]));
|
|
double x;
|
|
BOOST_CHECK(iss >> x);
|
|
// Make sure we're at the end:
|
|
iss.peek();
|
|
BOOST_CHECK(iss.eof());
|
|
roundtrip.push_back(x);
|
|
}
|
|
BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
|
|
BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
|
|
BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
|
|
}
|