boost rational有理数 tcy
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: test sy on
- 下一篇: Mac笔记本电脑关闭fn功能的方法