本文主要是介绍boost rational有理数 tcy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.1.下载 boost 库地址:https://www.boost.org/ 压缩文件boost_1_75_0.7z或下载boost_1_75_0-msvc-14.1-64.exe https://sourceforge.net/projects/boost/files/boost-binaries/1.75.0/
1.2.说明:大多数的 boost 库仅需要包含头文件 hpp 即可,不需要再链接其他的 lib 文件,但是有些 boost 下的库是需要包含 lib 文件的 1.2.我下载的是第一个,有理数库不需要编译只做简单路径设置将 boost 库的路径添加到附加包含目录查看:https://blog.csdn.net/weixin_38102771/article/details/88410617VS2019 prew我的设置:C/C++-->包含目录:C:\boost_1_75_0;(首先你应解压拷贝到C:\)链接器-->附加库目录:C:\boost_1_75_0\libs;
2.实例
#include <boost\rational.hpp>
#include <iostream>using namespace std;
using namespace boost;template <typename T>
bool isOdd(T value) { return (value & 1) == 1; }template <typename T>
bool isEven(T value) { return (value & 1) == 0; }
template <typename T>
int sgn(T value) {if (value == 0) return 0;else if (value > 0) return 1;elsereturn -1;
}
template<typename T>
bool isIntFlower(T value, T now) {return sgn(value)!=sgn(now);
}
template<typename T>
rational<T> swapNumeratorDenominator(const rational<T>& v) {return rational<T>(v.denominator(),v.numerator());
}
实现自定义有理数Power函数
要求有理数数据类型为整数类型。
template<typename T>
rational<T> Power(const rational<T>& base, int exponent) {int exp = exponent;rational<T> result(0);if (result == base)return result;if (exp == 0)return rational<T>(1);if (exp>0)result = base;else{result = swapNumeratorDenominator(base);exp = abs(exp);}if (base < 0 && isEven(exp))result = abs(base);//开始计算T numerator= result.numerator(); //分子T denominator = result.denominator();//分母T old_num = numerator;T old_deno = denominator;T res1 = 1,res2=1;while (exp != 0){if ((exp & 1) == 1){res1 *= numerator;res2 *= denominator;}numerator *= numerator; // 翻倍denominator *= denominator; // 翻倍exp >>= 1; // 右移一位if (isIntFlower(old_num, res1) || isIntFlower(old_deno, res2))throw "int flower!"; }result = { res1,res2 };return result;
}
void test_Power() {cout << "10^4=" << Power<long long>(10, 4) << endl;//=10000 / 1cout << "10^-4=" << Power<long long>(10, -4) << endl;//= 1 / 10000cout << "(-10)^-4=" << Power<long long>(-10, 4) << endl;//= 10000 / 1cout << "(-10)^-4=" << Power<long long>(-10, -4) << endl;//= 10000 / 1cout << "(3/2)^2=" << Power<long long>(rational<long long>(3, 2), 2) << endl;//= 9 / 4cout << "(-3/2)^-2=" << Power<long long>(rational<long long>(3, 2), -2) << endl;// = 4 / 9cout << "(3/-2)^2=" << Power<long long>(rational<long long>(-3, 2), 2) << endl;//= 9 / 4cout << "(3/2)^2=" << Power<long long>(rational<long long>(3, -2), 2) << endl;//= 9 / 4cout << "(3/-2)^0=" << Power<long long>(rational<long long>(3, -2), 0) << endl;//= 1 / 1
}
void test_rational() {using std::cout;//创建有理数rational<int> a1(0); //=0/1// 输出一个既约分数的形式cout << "4/2=" << rational<int>(4, 2) << endl;//=2/1cout << "分子=" << a1.numerator() << endl; //分子=0cout << "分母=" << a1.denominator() << endl; //分母=1//修改值:a1 = { 2,4 }; //1/2a1 = 10; //=10/1a1.assign(3, 5); //=3/5//四则运算:const rational<int> x1{ rational<int>(-3, 2) };const rational<int> x2 = rational<int>(4, 2);cout << "x1+x2=" << x1 + x2 << endl;//=7/2cout << "x1-x2=" << x1 - x2 << endl;//-1/2cout << "x1*x2=" << x1 * x2 << endl;//=3/1cout << "x1/x2=" << x1 / x2 << endl;//=3/4cout << "|x1|=" << abs(x1) << std::endl;//转浮点数:cout << "double=" << rational_cast<double>(x1) << endl;cout << "最大公约数=" << gcd(x1, x2) << endl;//=1/2cout << "最小公倍数" << lcm(x1, x2) << endl; //=6/1//需要转化为double才可以用于pow/cos/sqrt等cout << pow(rational_cast<double>(x2), 2) << endl;cout << sqrt(rational_cast<double>(x2)) << endl;cout << cos(rational_cast<double>(x2)) << endl;//测试异常:rational<int64_t> b(1, 2);try {cout << b << endl;cout << "b/0=" << b / 0 << endl;}catch (bad_rational& e) {cout << e.what() << endl;//b/0=bad rational: zero denominator}
}
int main() {test_Power();test_rational();
}
3.备注:Power实现原理
参考https://blog.csdn.net/qq_41822235/article/details/81777291非递归版本1.全面考察指数的正负、底数是否为零等情况。
2.写出指数的二进制表达,例如13表达为二进制1101。
3.举例:10^1101 = 10^0001*10^0*10^0100*10^1000。
4.通过&1和>>1来逐位读取1101,为1时将该位代表的乘数累乘到最终结果。
实例:
double Power(double base, int exponent) {bool isPositiveNum = true;double res = 1;if(exponent > 0);else if(exponent < 0){isPositiveNum = false;if(base==0)return 0;exponent = -exponent; //变成正数}else{if(base==0)return 0;return 1;}while(exponent!=0){if((exponent&1)==1)res*=base;base*=base; // 翻倍exponent>>=1;// 右移一位}return isPositiveNum == true ? res:(1 / res); }
这篇关于boost rational有理数 tcy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!