返回类型取决于策略:http://www.boost.org/doc/libs/1_58_0/libs/geometry/doc/html/geometry/design.html#geometry.design.return_type。
这让我觉得你可以通过策略明确指定计算类型:
Live On Coliru
#include <iostream>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/algorithms/distance.hpp>
#include <boost/geometry/strategies/distance.hpp>
int main() {
using namespace boost::geometry;
using P = model::d2::point_xy<float>;
using V = traits::coordinate_type<P>::type;
std::cout << "Coordinate type: " << typeid(V).name() << "\n";
P a, b;
std::cout << "Calculation type: " << typeid(default_distance_result<P, P>::calculation_type).name() << "\n";
std::cout << "Result type: " << typeid(default_distance_result<P, P>::type).name() << "\n";
using S = strategy::distance::pythagoras<V>;
std::cout << "Calculation type: " << typeid(distance_result<P, P, S>::calculation_type).name() << "\n";
std::cout << "Result type: " << typeid(distance_result<P, P, S>::type).name() << "\n";
static_assert(boost::is_same<distance_result<P, P, S>::type, float>::value, "oops");
}
打印(通过c++filt -t 传送):
Coordinate type: float
Calculation type: boost::geometry::strategy::distance::pythagoras<void>::calculation_type<boost::geometry::model::d2::point_xy<float, boost::geometry::cs::cartesian>, boost::geometry::model::d2::point_xy<float, boost::geometry::cs::cartesian> >
Result type: double
Calculation type: boost::geometry::strategy::distance::pythagoras<float>::calculation_type<boost::geometry::model::d2::point_xy<float, boost::geometry::cs::cartesian>, boost::geometry::model::d2::point_xy<float, boost::geometry::cs::cartesian> >
Result type: float
请注意,由于精度有限,结果精度可能会被截断。这在很大程度上是精度/存储效率的权衡。
我实际上希望在任何地方使用double(而不是float)都能获得最佳性能,尤其是在对现代 CPU 指令集进行全面优化的情况下。