Skip to content

Commit

Permalink
Test out float128
Browse files Browse the repository at this point in the history
  • Loading branch information
eacousineau committed May 11, 2019
1 parent 3852e05 commit b04db11
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/pybind11/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ cc_binary(
deps = [":include"],
)

cc_binary(
name = "test_numpy_issue1785",
srcs = ["custom_tests/test_numpy_issue1785.cc"],
deps = [":include"],
)

cc_binary(
name = "test_pybind_issue_repro",
srcs = ["custom_tests/test_pybind_issue_repro.cc"],
Expand Down
60 changes: 60 additions & 0 deletions python/pybind11/custom_tests/test_numpy_issue1785.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// https://github.com/pybind/pybind11/issues/1785
#include <Eigen/Dense>

#include <pybind11/embed.h>
#include <pybind11/eval.h>
// Using Eigen, 'cause I don't want to worry about ownership with capsules
// or buffers.
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

using float128 = long double;
static_assert(sizeof(float128) == 16, "Bad size");

template <typename T, int Dim>
using Vector = Eigen::Matrix<T, Dim, 1>;

void init_module(py::module m) {
m.def("make_array", []() {
return Vector<float128, 3>(1, 2, 3);
});
m.def("sum_array", [](Vector<float128, -1> x) {
// Return as 1x1 - I don't know how to easily return NumPy scalars...
return Vector<float128, 1>(x.array().sum());
});
}

int main(int, char**) {
py::scoped_interpreter guard{};

py::module m("test_module");
init_module(m);
py::globals()["m"] = m;

py::print("[ Eval ]");
py::exec(R"""(
import numpy as np
def main():
x = np.array([1, 2], dtype=np.float128)
print(repr(m.sum_array(x)))
print(repr(m.make_array()))
main()
)""");

py::print("[ Done ]");

return 0;
}

/* Output:
[ Eval ]
array([ 3.0], dtype=float128)
array([ 1.0, 2.0, 3.0], dtype=float128)
[ Done ]
*/

0 comments on commit b04db11

Please sign in to comment.