版本比较
高精度四则运算:从入门到入土
标题
增加 1 行删除 1 行
1高精度:从入门到入土1高精度四则运算:从入门到入土
正文
增加 29 行删除 27 行
9 行未改动101112131415161718112 行未改动全部展开13113213313413513613713842 行未改动181182183184185186187188189190191192193194195196197198199200201202176 行未改动全部展开37938038138238338438538638738838951 行未改动44144244344444544644744868 行未改动5175185195205215225235245255265275285295305315325338 行未改动54254354454554654754854955055155255355458 行未改动613614615616617618619620621622623230 行未改动全部展开854855856857858859860861862137 行未改动全部展开1000100110021003100410051006100717 行未改动1025102610271028102910301031103210331034103510361037103810391040104110421043104410451046234 行未改动全部展开9 行未改动本教程**不是封装教程**,提供的模板代码安全性和正确性**不能保证**。如果你 hack 了我的模板,你可以私信我,我会尝试 debug。文中代码前后可能不一样(因为后期发现一些 bug 可能会更改前期的函数,有时候没有及时更新),请以文末的模板为准。本教程仅介绍**较为常用**的高精度知识(以及前置知识),不常用的(比如 $O(n^{\log_2^3})$ 的 Karatsuba 分治乘法)不做介绍。本教程仅作为**算法介绍**,**不保证代码实现的效率**。如果你追求效率和实用性,你可以看看[masonxiong.《目前最高效的高精度整数模板发布》(2025-09-01)](https://www.luogu.com.cn/article/c70gmlxt)。:::112 行未改动全部展开```cppbool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}42 行未改动参考代码:```cppBIGINT simple_add( const BIGINT& b ) const{BIGINT c;c.len = max( len, b.len );c.num[c.len] = 0; //防止 c.num 没清空for( int i = 0; i < c.len; ++ i ){c.num[i] = 0;if( len > i ) c.num[i] += num[i];if( b.len > i ) c.num[i] += b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;}if( c.num[c.len] ) ++ c.len; //判断最高位return c;}```## 4.2 简单减法176 行未改动全部展开exit( 32 );}if( abs_less( b ) ) return make_pair( BIGINT( 0 ), *this );BIGINT c( *this ), d; //c模 d商d.len = c.len - b.len + 1;for( int i = 0; i < d.len; ++ i ) d.num[i] = 0;for( int i = c.len - b.len + 1; i >= 0; -- i ){while( 1 ){ //循环减直到 c < b * 10^ibool _great = 1; //c >= b * 10^i 判断if( c.len <= i + b.len ) //注意多一位也是大for( int j = b.len - 1; j >= 0; -- j ){ //c >= b * 10^i 判断51 行未改动```cpp#include<iostream>#include<string>#include<algorithm>using std::ostream;using std::istream;using std::string;68 行未改动}bool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}return 0;}bool operator==( const BIGINT& b ) const{ //相等的if( f != b.f ) return 0;if( len < b.len ) return 0;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] != num[i] ) return 0;}return 1;8 行未改动return !( *this < b ) && !( *this == b );}BIGINT simple_add( const BIGINT& b ) const{BIGINT c;c.len = max( len, b.len );c.num[c.len] = 0; //防止 c.num 没清空for( int i = 0; i < c.len; ++ i ){c.num[i] = num[i] + b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;58 行未改动exit( 32 );}if( abs_less( b ) ) return make_pair( BIGINT( 0 ), *this );BIGINT c( *this ), d; //c模 d商d.len = c.len - b.len + 1;for( int i = 0; i < d.len; ++ i ) d.num[i] = 0;for( int i = c.len - b.len + 1; i >= 0; -- i ){while( 1 ){ //循环减直到 c < b * 10^ibool _great = 1; //c >= b * 10^i 判断if( c.len <= i + b.len ) //注意多一位也是大for( int j = b.len - 1; j >= 0; -- j ){ //c >= b * 10^i 判断230 行未改动全部展开注意在牛顿迭代的时候要限制精度,不然会爆内存。牛顿迭代初始值需要是一个极小值。最后记得也加一个极小值,不然有误差```cppBIGFLOAT calc1_b( int n, const BIGFLOAT& b ){auto x0 = BIGFLOAT( BIGINT( int( 1 ) ) ) >> b.a.len; //初始值不能取太大,10^(-len)137 行未改动全部展开}bool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}17 行未改动return !( *this < b ) && !( *this == b );}BIGINT simple_add( const BIGINT& b ) const{BIGINT c;c.len = max( len, b.len );c.num[c.len] = 0; //防止 c.num 没清空for( int i = 0; i < c.len; ++ i ){c.num[i] = 0;if( len > i ) c.num[i] += num[i];if( b.len > i ) c.num[i] += b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;}if( c.num[c.len] ) ++ c.len; //判断最高位return c;}BIGINT operator-() const{ return BIGINT( *this, 1 ); }BIGINT simple_minus( const BIGINT& b ) const{if( *this == b ) return BIGINT( 0 ); //相等234 行未改动全部展开9 行未改动101112131415161718112 行未改动全部展开13113213313413513613713813942 行未改动182183184185186187188189190191192193194195196197198199200201176 行未改动全部展开37837938038138238338438538638738838951 行未改动44144244344444544644744844968 行未改动5185195205215225235245255265275285295305315325335345358 行未改动54454554654754854955055155255355455555658 行未改动615616617618619620621622623624625626230 行未改动全部展开857858859860861862863864865137 行未改动全部展开10031004100510061007100810091010101117 行未改动10291030103110321033103410351036103710381039104010411042104310441045104610471048234 行未改动全部展开9 行未改动本教程**不是封装教程**,提供的模板代码安全性和正确性**不能保证**。如果你 hack 了我的模板,你可以私信我,我会尝试 debug。文中代码前后可能不一样(因为后期发现一些 bug 可能会更改前期的函数,有时候没有及时更新),请以文末的模板为准。本教程仅介绍**较为常用**的高精度四则运算知识(以及前置知识),不常用的(比如 $O(n^{\log_2^3})$ 的 Karatsuba 分治乘法)不做介绍。本教程仅作为**算法介绍**,**不保证代码实现的效率**。如果你追求效率和实用性,你可以看看[masonxiong.《目前最高效的高精度整数模板发布》(2025-09-01)](https://www.luogu.com.cn/article/c70gmlxt)。:::112 行未改动全部展开```cppbool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;if( len > b.len ) return 0;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}42 行未改动参考代码:```cppBIGINT simple_add( const BIGINT& b ) const{BIGINT c( *this );c.len = max( len, b.len );for( int i = len; i <= c.len; ++ i ) c.num[i]=0; //防止 c.num 没清空,多清一位是要进位for( int i = 0; i < b.len; ++ i ){c.num[i] += b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;}if( c.num[c.len] ) ++ c.len; //判断最高位return c;}```## 4.2 简单减法176 行未改动全部展开exit( 32 );}if( abs_less( b ) ) return make_pair( BIGINT( 0 ), *this );BIGINT c( *this ), d; //c模 d商d.len = c.len - b.len + 1;//if( d.len <= 0 ) return make_pair( BIGINT( 0 ), *this );for( int i = 0; i <= d.len; ++ i ) d.num[i] = 0;for( int i = c.len - b.len; i >= 0; -- i ){while( 1 ){ //循环减直到 c < b * 10^ibool _great = 1; //c >= b * 10^i 判断if( c.len <= i + b.len ) //注意多一位也是大for( int j = b.len - 1; j >= 0; -- j ){ //c >= b * 10^i 判断51 行未改动```cpp#include<iostream>#include<string>#include<algorithm>#include<cstring>using std::ostream;using std::istream;using std::string;68 行未改动}bool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;if( len > b.len ) return 0;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}return 0;}bool operator==( const BIGINT& b ) const{ //相等的if( f != b.f ) return 0;if( len != b.len ) return 0;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] != num[i] ) return 0;}return 1;8 行未改动return !( *this < b ) && !( *this == b );}BIGINT simple_add( const BIGINT& b ) const{BIGINT c( *this );c.len = max( len, b.len );for( int i = len; i <= c.len; ++ i ) c.num[i]=0; //防止 c.num 没清空,多清一位是要进位for( int i = 0; i < b.len; ++ i ){c.num[i] += b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;58 行未改动exit( 32 );}if( abs_less( b ) ) return make_pair( BIGINT( 0 ), *this );BIGINT c( *this ), d; //c模 d商d.len = c.len - b.len + 1;//if( d.len <= 0 ) return make_pair( BIGINT( 0 ), *this );for( int i = 0; i <= d.len; ++ i ) d.num[i] = 0;for( int i = c.len - b.len; i >= 0; -- i ){while( 1 ){ //循环减直到 c < b * 10^ibool _great = 1; //c >= b * 10^i 判断if( c.len <= i + b.len ) //注意多一位也是大for( int j = b.len - 1; j >= 0; -- j ){ //c >= b * 10^i 判断230 行未改动全部展开注意在牛顿迭代的时候要限制精度,不然会爆内存。牛顿迭代初始值需要是一个极小值。最后记得也加一个极小值,不然有误差。```cppBIGFLOAT calc1_b( int n, const BIGFLOAT& b ){auto x0 = BIGFLOAT( BIGINT( int( 1 ) ) ) >> b.a.len; //初始值不能取太大,10^(-len)137 行未改动全部展开}bool abs_less( const BIGINT& b ) const{ //绝对值更小的if( len < b.len ) return 1;if( len > b.len ) return 0;for( int i = len - 1; i >= 0; -- i ){if( b.num[i] < num[i] ) return 0;if( num[i] < b.num[i] ) return 1;}17 行未改动return !( *this < b ) && !( *this == b );}BIGINT simple_add( const BIGINT& b ) const{BIGINT c( *this );c.len = max( len, b.len );for( int i = len; i <= c.len; ++ i ) c.num[i]=0; //防止 c.num 没清空,多清一位是要进位for( int i = 0; i < b.len; ++ i ){c.num[i] += b.num[i]; //加法}for( int i = 0; i < c.len; ++ i ){ //进位c.num[i + 1] += c.num[i] / 10;c.num[i] %= 10;}if( c.num[c.len] ) ++ c.len; //判断最高位return c;}BIGINT operator-() const{ return BIGINT( *this, 1 ); }BIGINT simple_minus( const BIGINT& b ) const{if( *this == b ) return BIGINT( 0 ); //相等234 行未改动全部展开