// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include #include "main.h" template void test_conversion() { typedef Array ArrayXFrom; typedef Array ArrayXTo; typedef Array ArrayXDouble; Index size = internal::random(1, EIGEN_TEST_MAX_SIZE); double from_min = static_cast((std::numeric_limits::min)()); double from_range = static_cast((std::numeric_limits::max)()) - from_min; // ArrayXFrom::Random() only generates 32-bit values (#2749), so we generate // doubles and scale to fit the range. ArrayXDouble doubles = (ArrayXDouble::Random(size) + 1.0) * (from_range / 2.0) + from_min; ArrayXFrom from = doubles.template cast(); ArrayXTo to(size); for (Index i = 0; i < size; ++i) { to(i) = static_cast(from(i)); } VERIFY_IS_APPROX(from.template cast(), to); } template void test_conversion_to() { CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); CALL_SUBTEST((test_conversion())); } EIGEN_DECLARE_TEST(float_conversion) { for (int i = 0; i < g_repeat; i++) { CALL_SUBTEST(test_conversion_to()); CALL_SUBTEST(test_conversion_to()); } }