高精大大大大大模板
LawrenceSivan · · 个人记录
高精度加减法
包含整型与浮点数
const int maxn=1e5+5;
struct Biginteger{
int data[maxn];
int len;
bool neg;
Biginteger(){
neg=0;len=0;
memset(data,0,sizeof(data));
}
inline void clear(){
neg=0;len=0;
memset(data,0,sizeof(data));
}
Biginteger operator = (char *s) {
memset(data,0,sizeof(data));
int n=strlen(s+1);
for (re int i=n;i;i--) {
data[++len]=s[i]-'0';
}
neg=false;
return *this;
}
friend Biginteger operator + (const Biginteger &a,const Biginteger &b){
Biginteger res;
int len=max(a.len,b.len);
for(re int i=1;i<=len;i++){
res.data[i]+=(a.data[i]+b.data[i]);
res.data[i+1]+=(res.data[i]/10);
res.data[i]%=10;
}
res.len=len+(res.data[len+1]^0);
return res;
}
friend Biginteger operator + (const Biginteger & a, const int & b) {
Biginteger res = a;
res.data[1] += b;
for (int i = 1; i <= res.len; i++) {
if (res.data[i] < 10) return res;
res.data[i+1] += res.data[i] / 10;
res.data[i] %= 10;
}
return res;
}
friend Biginteger operator - (Biginteger & a, Biginteger & b){
Biginteger res;
int len=max(a.len,b.len);
if(a<b)swap(a,b),res.neg=true;
for(int i=1;i<=len;i++){
res.data[i]+=a.data[i]-b.data[i];
if(res.data[i]<0)res.data[i]+=10,res.data[i+1]--;
}
res.len =len-(res.data[len]==0);
if(res.neg==true)swap(a,b);
for(re int i=res.len;i>1;i--){
if(res.data[i]!=0)break;
res.len--;
}
return res;
}
friend bool operator < (const Biginteger &a,const Biginteger &b){
if(a.len<b.len)return true;
if(a.len>b.len)return false;
for(re int i=a.len;i;i--){
if(a.data[i]<b.data[i])return true;
}
return false;
}
friend bool operator > (const Biginteger &a,const Biginteger &b){
if(a.len>b.len)return true;
if(a.len<b.len)return false;
for(re int i=a.len;i;i--){
if(a.data[i]>b.data[i])return true;
}
return false;
}
friend bool operator == (const Biginteger &a,const Biginteger &b){
if(a.len!=b.len)return false;
for(re int i=a.len;i;i--){
if(a.data[i]!=b.data[i])return false;
}
return true;
}
}ita,itb;
istream& operator >> (istream& in, Biginteger &x){
char s[maxn];
if (!(in>>(s+1)))return in;
x=s;
return in;
}
ostream& operator << (ostream& out, const Biginteger &x){
if(x.neg)out<<'-';
out<<x.data[x.len];
for (re int i=x.len-1;i;i--) {
char buf[20];
sprintf(buf, "%d", x.data[i]);
for (re ull j=0;j<strlen(buf);j++)out<<buf[j];
}
return out;
}
/*
123123.123123
*/
struct Decimal{
Biginteger integer,real;
bool neg;
int rlen;
Decimal(){
neg=0;
rlen=0;
memset(integer.data,0,sizeof(integer.data));
memset(real.data,0,sizeof(real.data));
}
inline void clear(){
neg=0;rlen=0;
integer.clear();
real.clear();
}
Decimal operator = (char *s) {
neg=0;
rlen=0;
memset(integer.data,0,sizeof(integer.data));
memset(real.data,0,sizeof(real.data));
int n=strlen(s+1);
for(re int i=1;i<=n;i++){
if(s[i]=='.'){
for(re int j=i-1;j;j--){
integer.data[++integer.len]=s[j]-'0';
}
for(re int j=i+1;j<=n;j++){
real.len++;
real.data[maxn-real.len-1]=s[j]-'0';
}
rlen=real.len;
real.len=maxn-2;
break;
}
}
return *this;
}
/* 3212.123123 231.1254 */
/* 2123 00..00321321 132 0..004521 */
/*
123123
1254
-------
248523
*/
friend Decimal operator + (const Decimal & a, const Decimal & b){
Decimal res;
int pos=max(a.rlen,b.rlen);
for(re int i=maxn-pos-1;i<=maxn-2;i++){
res.real.data[i]+=(a.real.data[i]+b.real.data[i]);
res.real.data[i+1]+=(res.real.data[i]/10);
res.real.data[i]%=10;
}
// res.real=a.real+b.real;
res.rlen=pos;
if(res.real.data[maxn-1]){
res.integer.data[1]+=(res.real.data[maxn-1]);
res.real.data[maxn-1]=0;
}
int len=max(a.integer.len,b.integer.len);
for(re int i=1;i<=len;i++){
res.integer.data[i]+=(a.integer.data[i]+b.integer.data[i]);
res.integer.data[i+1]+=(res.integer.data[i]/10);
res.integer.data[i]%=10;
}
res.integer.len=len+(res.integer.data[len+1]^0);
return res;
}
/*
123.123
4795.144
*/
/* 3212.123123 231.1254 */
/* 2123 00..00321321 132 0..004521 */
/*
123123
1254
-------
248523
*/
friend Decimal operator - (Decimal & a,Decimal & b){
Decimal res;
if(a<b)swap(a,b),res.neg=true;
int pos=max(a.rlen,b.rlen);res.rlen=pos;
for(re int i=maxn-pos-1;i<=maxn-2;i++){
res.real.data[i]+=a.real.data[i]-b.real.data[i];
if(res.real.data[i]<0)res.real.data[i+1]--,res.real.data[i]+=10;
}
if(res.real.data[maxn-1]<0){
res.integer.data[1]+=res.real.data[maxn-1];
res.real.data[maxn-1]=0;
}
int len=max(a.integer.len,b.integer.len);
for(re int i=1;i<=len;i++){
res.integer.data[i]+=a.integer.data[i]-b.integer.data[i];
if(res.integer.data[i]<0)res.integer.data[i+1]--,res.integer.data[i]+=10;
}
res.integer.len=len-(res.integer.data[len]==0);
if(res.neg)swap(a,b);
for(re int i=res.integer.len;i>1;i--){
if(res.integer.data[i]!=0)break;
res.integer.len--;
}
return res;
}
friend bool operator < (const Decimal & a, const Decimal & b){
if(a.integer.len<b.integer.len)return true;
if(a.integer.len>b.integer.len)return false;
for(re int i=a.integer.len;i;i--){
if(a.integer.data[i]<b.integer.data[i])return true;
if(a.integer.data[i]>b.integer.data[i])return false;
}
int pos=max(a.rlen,b.rlen);
for(re int i=maxn-2;i>=maxn-pos-1;i--){
if(a.real.data[i]<b.real.data[i])return true;
if(a.real.data[i]>b.real.data[i])return false;
}
return false;
}
friend bool operator > (const Decimal & a, const Decimal & b){
if(a.integer.len>b.integer.len)return true;
if(a.integer.len<b.integer.len)return false;
for(re int i=a.integer.len;i;i--){
if(a.integer.data[i]>b.integer.data[i])return true;
if(a.integer.data[i]<b.integer.data[i])return false;
}
int pos=max(a.rlen,b.rlen);
for(re int i=maxn-2;i>=maxn-pos-1;i--){
if(a.real.data[i]>b.real.data[i])return true;
if(a.real.data[i]<b.real.data[i])return false;
}
return false;
}
friend bool operator == (const Decimal & a, const Decimal & b){
if(a.integer.len!=b.integer.len||a.rlen!=b.rlen)return false;
for(re int i=a.integer.len;i;i--){
if(a.integer.data[i]!=b.integer.data[i])return false;
}
int pos=max(a.rlen,b.rlen);
for(re int i=maxn-2;i>=maxn-pos-1;i--){
if(a.real.data[i]!=b.real.data[i])return false;
}
return true;
}
}n,s;
istream& operator >> (istream& in, Decimal &x){
char s[maxn];
if (!(in>>(s+1)))return in;
x=s;
return in;
}
ostream& operator << (ostream& out, const Decimal &x){
if(x.neg)out<<'-';
for(re int i=x.integer.len;i;i--){
char buf[20];
sprintf(buf, "%d", x.integer.data[i]);
for (re ull j=0;j<strlen(buf);j++)out<<buf[j];
}
out<<'.';
for(re int i=maxn-2;i>=maxn-x.rlen-1;i--){
char buf[20];
sprintf(buf, "%d", x.real.data[i]);
for (re ull j=0;j<strlen(buf);j++)out<<buf[j];
}
return out;
}
struct Biginteger {
static const int BASE = 10000; // 当只使用加减法时可以节省空间,把
static const int WIDTH = 4; // BASE 改成 100000000,WIDTH 改成 8
std::vector<int> s;
Biginteger(long long num = 0) { *this = num; }
Biginteger(const std::string& str) { *this = str; }
Biginteger operator = (long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while (num > 0);
return *this;
}
Biginteger operator = (const std::string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len; i++) {
int end = str.length() - i * WIDTH;
int start = std::max(0, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
}
bool operator < (const Biginteger& b) const {
if (s.size() != b.s.size()) return s.size() < b.s.size();
for (int i = s.size() - 1; i >= 0; i--)
if (s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
bool operator > (const Biginteger& b) const { return b < *this; }
bool operator <= (const Biginteger& b) const { return !(b < *this); }
bool operator >= (const Biginteger& b) const { return !(*this < b); }
bool operator != (const Biginteger& b) const { return b < *this || *this < b; }
bool operator == (const Biginteger& b) const { return !(b < *this) && !(*this < b); }
Biginteger operator + (const Biginteger& b) const {
Biginteger c;
c.s.clear();
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size()) x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x % BASE);
g = x / BASE;
}
return c;
}
Biginteger operator - (const Biginteger& b) const {
Biginteger c;
c.s.clear();
if (*this == b) {
c.s.push_back(0);
} else if (*this < b) {
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= b.s.size() && i >= s.size()) {
c.s[i - 1] = -c.s[i - 1]; break;
}
int x = g;
if (i < b.s.size()) x += b.s[i];
if (i < s.size()) x -= s[i];
if (x < 0) {
c.s.push_back(BASE + x % BASE);
g = x / BASE - 1;
} else {
c.s.push_back(x % BASE);
g = x / BASE;
}
}
} else {
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size()) x += s[i];
if (i < b.s.size()) x -= b.s[i];
if (x < 0) {
c.s.push_back(BASE + x % BASE);
g = x / BASE - 1;
} else {
c.s.push_back(x % BASE);
g = x / BASE;
}
}
}
return c;
}
Biginteger operator * (const Biginteger& b) const {
Biginteger c; int len = s.size() + b.s.size(); bool flag = false;
c.s.clear();
if (*this == 0 || b == 0) {c.s.push_back(0); return 0;}
if (*this < 0 && b > 0) flag = true;
if (*this > 0 && b < 0) flag = true;
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= len) break;
int x = g;
for (int u = 0, v = i, temp; v >= 0; u++, v--)
if (u < s.size() && v < b.s.size()) {
temp = s[u] * b.s[v];
if (temp < 0) temp = -temp;
x += temp;
}
c.s.push_back(x % BASE);
g = x / BASE;
}
for (int i = c.s.size() - 1; i >= 0 && c.s[i] == 0; i--)
c.s.pop_back();
if (flag) c.s[c.s.size() - 1] = -c.s[c.s.size() - 1];
return c;
}
inline void killzero() {
while (s.back() == 0 && s.size() > 1) s.pop_back();
}
inline void reverse() {
int len = s.size();
for (int i = 0; i < len >> 1; ++i) swap(s[i], s[len - i - 1]);
}
Biginteger operator / (const Biginteger &b) const {
Biginteger c, t;
c.s.clear(); t.s.clear();
for (int i = s.size() - 1; i >= 0; --i) {
t.s.push_back(s[i]);
int x = 0;
while (b <= t) { t -= b; x++; }
c.s.push_back(x);
}
c.reverse();
c.killzero();
return c;
}
Biginteger operator += (const Biginteger& b) {
*this = *this + b; return *this;
}
Biginteger operator ++ (int) {
*this = *this + 1; return *this;
}
Biginteger operator ++ () {
*this = *this + 1; return *this;
}
Biginteger operator -= (const Biginteger& b) {
*this = *this - b; return *this;
}
Biginteger operator -- (int) {
*this = *this - 1; return *this;
}
Biginteger operator -- () {
*this = *this - 1; return *this;
}
Biginteger operator *= (const Biginteger& b) {
*this = *this * b; return *this;
}
Biginteger operator /= (const Biginteger& b) {
*this = *this / b; return *this;
}
// Biginteger operator %= (const Biginteger& b) {
// *this = *this % b; return *this;
// }
};
std::ostream& operator << (std::ostream& out, const Biginteger& x) {
out << x.s.back();
for (int i = x.s.size() - 2; i >= 0; i--) {
char buf[20];
sprintf(buf, "%04d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) out << buf[j];
}
return out;
}
std::istream& operator >> (std::istream& in, Biginteger& x) {
std::string s;
if (!(in >> s)) return in;
x = s;
return in;
}
// This is a test program with decimal lib
#include <cstdlib>
#include <cstring>
#include <string>
// ---------- decimal lib start ----------
const int PREC = 2100;
class Decimal {
public:
Decimal();
Decimal(const std::string &s);
Decimal(const char *s);
Decimal(int x);
Decimal(long long x);
Decimal(double x);
bool is_zero() const;
// p (p > 0) is the number of digits after the decimal point
std::string to_string(int p) const;
double to_double() const;
friend Decimal operator + (const Decimal &a, const Decimal &b);
friend Decimal operator + (const Decimal &a, int x);
friend Decimal operator + (int x, const Decimal &a);
friend Decimal operator + (const Decimal &a, long long x);
friend Decimal operator + (long long x, const Decimal &a);
friend Decimal operator + (const Decimal &a, double x);
friend Decimal operator + (double x, const Decimal &a);
friend Decimal operator - (const Decimal &a, const Decimal &b);
friend Decimal operator - (const Decimal &a, int x);
friend Decimal operator - (int x, const Decimal &a);
friend Decimal operator - (const Decimal &a, long long x);
friend Decimal operator - (long long x, const Decimal &a);
friend Decimal operator - (const Decimal &a, double x);
friend Decimal operator - (double x, const Decimal &a);
friend Decimal operator * (const Decimal &a, int x);
friend Decimal operator * (int x, const Decimal &a);
friend Decimal operator / (const Decimal &a, int x);
friend bool operator < (const Decimal &a, const Decimal &b);
friend bool operator > (const Decimal &a, const Decimal &b);
friend bool operator <= (const Decimal &a, const Decimal &b);
friend bool operator >= (const Decimal &a, const Decimal &b);
friend bool operator == (const Decimal &a, const Decimal &b);
friend bool operator != (const Decimal &a, const Decimal &b);
Decimal & operator += (int x);
Decimal & operator += (long long x);
Decimal & operator += (double x);
Decimal & operator += (const Decimal &b);
Decimal & operator -= (int x);
Decimal & operator -= (long long x);
Decimal & operator -= (double x);
Decimal & operator -= (const Decimal &b);
Decimal & operator *= (int x);
Decimal & operator /= (int x);
friend Decimal operator - (const Decimal &a);
// These can't be called
friend Decimal operator * (const Decimal &a, double x);
friend Decimal operator * (double x, const Decimal &a);
friend Decimal operator / (const Decimal &a, double x);
Decimal & operator *= (double x);
Decimal & operator /= (double x);
private:
static const int len = PREC / 9 + 1;
static const int mo = 1000000000;
static void append_to_string(std::string &s, long long x);
bool is_neg;
long long integer;
int data[len];
void init_zero();
void init(const char *s);
};
Decimal::Decimal() {
this->init_zero();
}
Decimal::Decimal(const char *s) {
this->init(s);
}
Decimal::Decimal(const std::string &s) {
this->init(s.c_str());
}
Decimal::Decimal(int x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = x;
}
Decimal::Decimal(long long x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = x;
}
Decimal::Decimal(double x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = (long long)x;
x -= integer;
for (int i = 0; i < len; i++) {
x *= mo;
if (x < 0) x = 0;
data[i] = (int)x;
x -= data[i];
}
}
void Decimal::init_zero() {
is_neg = false;
integer = 0;
memset(data, 0, len * sizeof(int));
}
bool Decimal::is_zero() const {
if (integer) return false;
for (int i = 0; i < len; i++) {
if (data[i]) return false;
}
return true;
}
void Decimal::init(const char *s) {
this->init_zero();
is_neg = false;
integer = 0;
// find the first digit or the negative sign
while (*s != 0) {
if (*s == '-') {
is_neg = true;
++s;
break;
} else if (*s >= 48 && *s <= 57) {
break;
}
++s;
}
// read the integer part
while (*s >= 48 && *s <= 57) {
integer = integer * 10 + *s - 48;
++s;
}
// read the decimal part
if (*s == '.') {
int pos = 0;
int x = mo / 10;
++s;
while (pos < len && *s >= 48 && *s <= 57) {
data[pos] += (*s - 48) * x;
++s;
x /= 10;
if (x == 0) {
++pos;
x = mo / 10;
}
}
}
}
void Decimal::append_to_string(std::string &s, long long x) {
if (x == 0) {
s.append(1, 48);
return;
}
char _[30];
int cnt = 0;
while (x) {
_[cnt++] = x % 10;
x /= 10;
}
while (cnt--) {
s.append(1, _[cnt] + 48);
}
}
std::string Decimal::to_string(int p) const {
std::string ret;
if (is_neg && !this->is_zero()) {
ret = "-";
}
append_to_string(ret, this->integer);
ret.append(1, '.');
for (int i = 0; i < len; i++) {
// append data[i] as "%09d"
int x = mo / 10;
int tmp = data[i];
while (x) {
ret.append(1, 48 + tmp / x);
tmp %= x;
x /= 10;
if (--p == 0) {
break;
}
}
if (p == 0) break;
}
if (p > 0) {
ret.append(p, '0');
}
return ret;
}
double Decimal::to_double() const {
double ret = integer;
double k = 1.0;
for (int i = 0; i < len; i++) {
k /= mo;
ret += k * data[i];
}
if (is_neg) {
ret = -ret;
}
return ret;
}
bool operator < (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return a.is_neg && (!a.is_zero() || !b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return false;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return false;
}
}
bool operator > (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return !a.is_neg && (!a.is_zero() || !b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return false;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return false;
}
}
bool operator <= (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return a.is_neg || (a.is_zero() && b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return true;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return true;
}
}
bool operator >= (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return !a.is_neg || (a.is_zero() && b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return true;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return true;
}
}
bool operator == (const Decimal &a, const Decimal &b) {
if (a.is_zero() && b.is_zero()) return true;
if (a.is_neg != b.is_neg) return false;
if (a.integer != b.integer) return false;
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) return false;
}
return true;
}
bool operator != (const Decimal &a, const Decimal &b) {
return !(a == b);
}
Decimal & Decimal::operator += (long long x) {
if (!is_neg) {
if (integer + x >= 0) {
integer += x;
} else {
bool last = false;
for (int i = len - 1; i >= 0; i--) {
if (last || data[i]) {
data[i] = mo - data[i] - last;
last = true;
} else {
last = false;
}
}
integer = -x - integer - last;
is_neg = true;
}
} else {
if (integer - x >= 0) {
integer -= x;
} else {
bool last = false;
for (int i = len - 1; i >= 0; i--) {
if (last || data[i]) {
data[i] = mo - data[i] - last;
last = true;
} else {
last = false;
}
}
integer = x - integer - last;
is_neg = false;
}
}
return *this;
}
Decimal & Decimal::operator += (int x) {
return *this += (long long)x;
}
Decimal & Decimal::operator -= (int x) {
return *this += (long long)-x;
}
Decimal & Decimal::operator -= (long long x) {
return *this += -x;
}
Decimal & Decimal::operator /= (int x) {
if (x < 0) {
is_neg ^= 1;
x = -x;
}
int last = integer % x;
integer /= x;
for (int i = 0; i < len; i++) {
long long tmp = 1LL * last * mo + data[i];
data[i] = tmp / x;
last = tmp - 1LL * data[i] * x;
}
if (is_neg && integer == 0) {
int i;
for (i = 0; i < len; i++) {
if (data[i] != 0) {
break;
}
}
if (i == len) {
is_neg = false;
}
}
return *this;
}
Decimal & Decimal::operator *= (int x) {
if (x < 0) {
is_neg ^= 1;
x = -x;
} else if (x == 0) {
init_zero();
return *this;
}
int last = 0;
for (int i = len - 1; i >= 0; i--) {
long long tmp = 1LL * data[i] * x + last;
last = tmp / mo;
data[i] = tmp - 1LL * last * mo;
}
integer = integer * x + last;
return *this;
}
Decimal operator - (const Decimal &a) {
Decimal ret = a;
// -0 = 0
if (!ret.is_neg && ret.integer == 0) {
int i;
for (i = 0; i < Decimal::len; i++) {
if (ret.data[i] != 0) break;
}
if (i < Decimal::len) {
ret.is_neg = true;
}
} else {
ret.is_neg ^= 1;
}
return ret;
}
Decimal operator + (const Decimal &a, int x) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (int x, const Decimal &a) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (const Decimal &a, long long x) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (long long x, const Decimal &a) {
Decimal ret = a;
return ret += x;
}
Decimal operator - (const Decimal &a, int x) {
Decimal ret = a;
return ret -= x;
}
Decimal operator - (int x, const Decimal &a) {
return -(a - x);
}
Decimal operator - (const Decimal &a, long long x) {
Decimal ret = a;
return ret -= x;
}
Decimal operator - (long long x, const Decimal &a) {
return -(a - x);
}
Decimal operator * (const Decimal &a, int x) {
Decimal ret = a;
return ret *= x;
}
Decimal operator * (int x, const Decimal &a) {
Decimal ret = a;
return ret *= x;
}
Decimal operator / (const Decimal &a, int x) {
Decimal ret = a;
return ret /= x;
}
Decimal operator + (const Decimal &a, const Decimal &b) {
if (a.is_neg == b.is_neg) {
Decimal ret = a;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] += b.data[i] + last;
if (ret.data[i] >= Decimal::mo) {
ret.data[i] -= Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer += b.integer + last;
return ret;
} else if (!a.is_neg) {
// a - |b|
return a - -b;
} else {
// b - |a|
return b - -a;
}
}
Decimal operator - (const Decimal &a, const Decimal &b) {
if (!a.is_neg && !b.is_neg) {
if (a >= b) {
Decimal ret = a;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] -= b.data[i] + last;
if (ret.data[i] < 0) {
ret.data[i] += Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer -= b.integer + last;
return ret;
} else {
Decimal ret = b;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] -= a.data[i] + last;
if (ret.data[i] < 0) {
ret.data[i] += Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer -= a.integer + last;
ret.is_neg = true;
return ret;
}
} else if (a.is_neg && b.is_neg) {
// a - b = (-b) - (-a)
return -b - -a;
} else if (a.is_neg) {
// -|a| - b
return -(-a + b);
} else {
// a - -|b|
return a + -b;
}
}
Decimal operator + (const Decimal &a, double x) {
return a + Decimal(x);
}
Decimal operator + (double x, const Decimal &a) {
return Decimal(x) + a;
}
Decimal operator - (const Decimal &a, double x) {
return a - Decimal(x);
}
Decimal operator - (double x, const Decimal &a) {
return Decimal(x) - a;
}
Decimal & Decimal::operator += (double x) {
*this = *this + Decimal(x);
return *this;
}
Decimal & Decimal::operator -= (double x) {
*this = *this - Decimal(x);
return *this;
}
Decimal & Decimal::operator += (const Decimal &b) {
*this = *this + b;
return *this;
}
Decimal & Decimal::operator -= (const Decimal &b) {
*this = *this - b;
return *this;
}
// ---------- decimal lib end ----------
#include <iostream>
#include <iomanip>
#include <string>
std::string to_TF(bool x) {
return x ? "true" : "false";
}
int main() {
// input output test
std::cout << "Testing IO ...\n";
Decimal a(std::string("-233.123454270204572407233464200700"));
std::cout << a.to_string(50) << "\n";
a = Decimal("-0");
std::cout << a.to_string(50) << "\n";
// += -= *= /= < test
std::cout << "Testing += -= *= /= < ...\n";
a = 100;
std::cout << "a = " << a.to_string(50) << "\n";
a /= 24;
std::cout << "a/24 = " << a.to_string(50) << "\n";
a += 123;
std::cout << "a/24+123 = " << a.to_string(50) << "\n";
a *= 234;
std::cout << "(a/24+123)*234 = " << a.to_string(50) << "\n";
std::cout << "(a/24+123)*234 < 29756.9999999999999999 : ";
std::cout << (a < "29756.9999999999999999" ? "true" : "false") << "\n";
// double <-> decimal test
std::cout << "Testing double <-> Decimal ...\n";
std::cout << std::fixed << std::setprecision(15);
std::cout << "a.to_double() = " << a.to_double() << "\n";
a = 0.123456789;
std::cout << "0.123456789 = " << a.to_double() << "\n";
std::cout << "0.123456789 = " << a.to_string(50) << "\n";
a *= 23333333;
std::cout << "0.123456789*23333333 = " << a.to_double() << "\n";
std::cout << "0.123456789*23333333 = " << a.to_string(50) << "\n";
// < > <= >= == != test
std::cout << "Testing < > <= >= == != ...\n";
std::cout << "1234.567 < 1234.568 : ";
std::cout << to_TF(Decimal("1234.567") < Decimal("1234.568")) << "\n";
std::cout << "1234.567 < -1234.568 : ";
std::cout << to_TF(Decimal("1234.567") < Decimal("-1234.568")) << "\n";
std::cout << "-1234.567 < 1234.568 : ";
std::cout << to_TF(Decimal("-1234.567") < Decimal("1234.568")) << "\n";
std::cout << "-1234.567 < -1234.568 : ";
std::cout << to_TF(Decimal("-1234.567") < Decimal("-1234.568")) << "\n";
std::cout << "1234.567 < 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") < Decimal("1234.567")) << "\n";
std::cout << "1234.567 <= 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") <= Decimal("1234.567")) << "\n";
std::cout << "1234.567 > 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") > Decimal("1234.567")) << "\n";
std::cout << "1234.567 >= 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") >= Decimal("1234.567")) << "\n";
std::cout << "-1234.567 < -1234.567 : ";
std::cout << to_TF(Decimal("-1234.567") < Decimal("-1234.567")) << "\n";
std::cout << "-1234.567 <= -1234.567 : ";
std::cout << to_TF(Decimal("-1234.567") <= Decimal("-1234.567")) << "\n";
std::cout << "-1234.567 > -1234.567 : ";
std::cout << to_TF(Decimal("-1234.567") > Decimal("-1234.567")) << "\n";
std::cout << "-1234.567 >= -1234.567 : ";
std::cout << to_TF(Decimal("-1234.567") >= Decimal("-1234.567")) << "\n";
std::cout << "1234.567 == 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") == Decimal("1234.567")) << "\n";
std::cout << "1234.567 == 1234.568 : ";
std::cout << to_TF(Decimal("1234.567") == Decimal("1234.568")) << "\n";
std::cout << "1234.567 == -1234.567 : ";
std::cout << to_TF(Decimal("1234.567") == Decimal("-1234.567")) << "\n";
std::cout << "1234.567 != 1234.567 : ";
std::cout << to_TF(Decimal("1234.567") != Decimal("1234.567")) << "\n";
std::cout << "1234.567 != 1234.568 : ";
std::cout << to_TF(Decimal("1234.567") != Decimal("1234.568")) << "\n";
std::cout << "1234.567 != -1234.567 : ";
std::cout << to_TF(Decimal("1234.567") != Decimal("-1234.567")) << "\n";
std::cout << "-0 == 0 : ";
std::cout << to_TF(Decimal("-0") == Decimal("0")) << "\n";
std::cout << "-0 != 0 : ";
std::cout << to_TF(Decimal("-0") != Decimal("0")) << "\n";
std::cout << "-0 < 0 : ";
std::cout << to_TF(Decimal("-0") < Decimal("0")) << "\n";
std::cout << "-0 > 0 : ";
std::cout << to_TF(Decimal("-0") > Decimal("0")) << "\n";
std::cout << "-0 <= 0 : ";
std::cout << to_TF(Decimal("-0") <= Decimal("0")) << "\n";
std::cout << "-0 >= 0 : ";
std::cout << to_TF(Decimal("-0") >= Decimal("0")) << "\n";
// += - test
std::cout << "Testing += - ...\n";
a = 1.0 / 3.0;
std::cout << "1/3 = " << a.to_string(50) << "\n";
a += 233;
std::cout << "1/3+233 = " << a.to_string(50) << "\n";
a -= 235;
std::cout << "1/3-2 = " << a.to_string(50) << "\n";
a += 1;
std::cout << "1/3-1 = " << a.to_string(50) << "\n";
a += 1;
std::cout << "1/3 = " << a.to_string(50) << "\n";
std::cout << "-1/3 = " << (-a).to_string(50) << "\n";
a = 0.0;
std::cout << "-0 = " << (-a).to_string(50) << "\n";
a -= 10;
std::cout << "0 - 10 = " << a.to_string(50) << "\n";
// *= /= negative test
std::cout << "Testing *= /= with negative numbers ...\n";
a = -1.0 / 6.0;
std::cout << "-1/6 = " << a.to_string(50) << "\n";
a *= 7;
std::cout << "-1/6*7 = " << a.to_string(50) << "\n";
a *= -3;
std::cout << "-1/6*7*(-3) = " << a.to_string(50) << "\n";
a /= -6;
std::cout << "-1/6*7*(-3)/(-6) = " << a.to_string(50) << "\n";
a *= 0;
std::cout << "-1/6*7*(-3)/(-6)*0 = " << a.to_string(50) << "\n";
// + - * / test
std::cout << "Testing + - * / ...\n";
std::cout << "1/3+2 = " << (Decimal(1.0 / 3.0) + 2).to_string(50) << "\n";
std::cout << "1/3+2 = " << (Decimal(1.0) / 3 + 2).to_string(50) << "\n";
std::cout << "1/3+2 = " << (Decimal(1.0) / 3 + Decimal(2.0)).to_string(50) << "\n";
std::cout << "1/3+(-2) = " << (Decimal(1.0 / 3.0) + -2).to_string(50) << "\n";
std::cout << "1/3+(-2) = " << (Decimal(1.0) / 3 + -2).to_string(50) << "\n";
std::cout << "1/3+(-2) = " << (Decimal(1.0) / 3 + Decimal(-2.0)).to_string(50) << "\n";
std::cout << "-1/3+2 = " << (Decimal(-1.0 / 3.0) + 2).to_string(50) << "\n";
std::cout << "-1/3+2 = " << (Decimal(-1.0) / 3 + 2).to_string(50) << "\n";
std::cout << "-1/3+2 = " << (Decimal(-1.0) / 3 + Decimal(2.0)).to_string(50) << "\n";
std::cout << "1/3-2 = " << (Decimal(1.0 / 3.0) - 2).to_string(50) << "\n";
std::cout << "1/3-2 = " << (Decimal(1.0) / 3 - 2).to_string(50) << "\n";
std::cout << "1/3-2 = " << (Decimal(1.0) / 3 - Decimal(2.0)).to_string(50) << "\n";
std::cout << "-1/3-2 = " << (-Decimal(1.0 / 3.0) - 2).to_string(50) << "\n";
std::cout << "-1/3-2 = " << (-Decimal(1.0) / 3 - 2).to_string(50) << "\n";
std::cout << "-1/3-2 = " << (-Decimal(1.0) / 3 - Decimal(2.0)).to_string(50) << "\n";
std::cout << "1/-3+2 = " << (Decimal(1.0 / -3.0) + 2).to_string(50) << "\n";
std::cout << "1/-3+2 = " << (Decimal(1.0) / -3 + 2).to_string(50) << "\n";
std::cout << "1/-3+2 = " << (Decimal(1.0) / -3 + Decimal(2.0)).to_string(50) << "\n";
std::cout << "-1/-3+2 = " << (Decimal(-1.0 / -3.0) + 2).to_string(50) << "\n";
std::cout << "-1/-3+2 = " << (Decimal(-1.0) / -3 + 2).to_string(50) << "\n";
std::cout << "-1/-3+2 = " << (Decimal(-1.0) / -3 + Decimal(2.0)).to_string(50) << "\n";
std::cout << "1/6*11 = " << (Decimal(1.0) / 6 * 11).to_string(50) << "\n";
std::cout << "1/6*-11 = " << (Decimal(1.0) / 6 * -11).to_string(50) << "\n";
std::cout << "1/-6*-11 = " << (Decimal(1.0) / -6 * -11).to_string(50) << "\n";
// std::cout << "[can't compile] 1/1.5 = " << (Decimal(1.0) / 1.5).to_string(50) << "\n";
std::cout << "1/7-1/6 = " << (Decimal(1.0) / 7 - Decimal(1.0) / 6).to_string(50) << "\n";
// Calculate e = 1 / 0! + 1 / 1! + 1 / 2! + ...
Decimal ans = 0;
Decimal tmp = 1;
for (int i = 1; i <= 1000; i++) {
ans += tmp;
tmp /= i;
}
std::cout << "e = " << ans.to_string(2000) << "\n";
}