0

I am converting list of float values from python to C++ and the values are not converted properly. Eg. If the list in python contains the following values values = [4.2,2.5,3.6,1,0,6.3] when converted to c++ vector using Boost, I am getting values like below, [4.2000004,2.49999998,5.29999998,6.0999998,1,0,6.30000004], though I expect the exact values to be converted to C++ object

I am using the following C++ code to convert the values

struct iterable_converter
{

template <typename Container>
iterable_converter&
    from_python()
{
    boost::python::converter::registry::push_back(
        &iterable_converter::convertible,
        &iterable_converter::construct<Container>,
        boost::python::type_id<Container>());
    return *this;
}

static void* convertible(PyObject* object)
{
    return PyObject_GetIter(object) ? object : NULL;
}

template <typename Container>
static void construct(
    PyObject* object,
    boost::python::converter::rvalue_from_python_stage1_data* data)
{
    namespace python = boost::python;
    // Object is a borrowed reference, so create a handle indicting it is
    // borrowed for proper reference counting.
    python::handle<> handle(python::borrowed(object));

    // Obtain a handle to the memory block that the converter has allocated
    // for the C++ type.
    typedef python::converter::rvalue_from_python_storage<Container>
        storage_type;
    void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;

    typedef python::stl_input_iterator<typename Container::value_type>
        iterator;

    // Allocate the C++ type into the converter's memory block, and assign
    // its handle to the converter's convertible variable.  The C++
    // container is populated by passing the begin and end iterators of
    // the python object to the container's constructor.
    new (storage)Container(
        iterator(python::object(handle)), // begin
        iterator());                      // end
    data->convertible = storage;
}};

and declaring the class like below,

iterable_converter()
    .from_python<std::vector<float> >();

In python am setting the values like below,

a.f_values = [1,2,3,4.2]

But the converted C++ vector value is [1,2,3,4.19999981]

Please help me solve this issue

Hemanath
  • 19
  • 4

0 Answers0