C++ C++;boost rational类,楼层功能
我不熟悉使用boost 除了简单地执行x.numerator()/x.demonator外,在使用boost rational库时,是否有一种调用floor(x)函数(即向零截断)的适当的“boost类型”方法C++ C++;boost rational类,楼层功能,c++,boost,floor,C++,Boost,Floor,我不熟悉使用boost 除了简单地执行x.numerator()/x.demonator外,在使用boost rational库时,是否有一种调用floor(x)函数(即向零截断)的适当的“boost类型”方法 谢谢,没有了。我仔细研究了它,唯一一个处理转换为连分数表示的函数是从完全相同的操作开始的(格式化为易读): 现场演示 #include <boost/rational.hpp> namespace boost { template <typename Int
谢谢,没有了。我仔细研究了它,唯一一个处理转换为连分数表示的函数是从完全相同的操作开始的(格式化为易读): 现场演示
#include <boost/rational.hpp>
namespace boost {
template <typename IntType>
constexpr IntType floor(rational<IntType> const& r) {
return static_cast<IntType>(r.numerator() / r.denominator());
}
}
#include <iostream>
template <typename IntType> void test()
{
boost::rational<IntType> a(230,7), b(222*111111,-777777);
std::cout << "a: " << a << " -> " << floor(a) << "\n";
std::cout << "b: " << b << " -> " << floor(b) << "\n";
}
#include <boost/multiprecision/cpp_int.hpp>
int main() {
test<int>();
test<boost::multiprecision::cpp_int>();
}
➣在声明整数类型的命名空间中声明它,或在命名空间内声明它::boost,以便让它生效。感谢您的回复。我想我会做一些类似于你建议的事情。Boost库中没有包含楼层功能,这有什么原因吗?对我来说,不包括地板和天花板功能似乎有点过头了。
namespace boost {
template <typename IntType>
constexpr IntType floor(rational<IntType> const& r) {
return static_cast<IntType>(r.numerator() / r.denominator());
}
}
#include <boost/rational.hpp>
namespace boost {
template <typename IntType>
constexpr IntType floor(rational<IntType> const& r) {
return static_cast<IntType>(r.numerator() / r.denominator());
}
}
#include <iostream>
template <typename IntType> void test()
{
boost::rational<IntType> a(230,7), b(222*111111,-777777);
std::cout << "a: " << a << " -> " << floor(a) << "\n";
std::cout << "b: " << b << " -> " << floor(b) << "\n";
}
#include <boost/multiprecision/cpp_int.hpp>
int main() {
test<int>();
test<boost::multiprecision::cpp_int>();
}
a: 230/7 -> 32
b: -222/7 -> -31
a: 230/7 -> 32
b: -222/7 -> -31