基于大整形的运算收录

本文探讨了为何在实际编程中大整数较少见,以及在C++和Python中处理大整数的加减乘除方法。作者详细介绍了大整数的运算原理,并提供了C++代码实现,强调了在特定场景下使用Python处理大整数的天然优势。
摘要由CSDN通过智能技术生成

目录

目录

目录

前言

为什么要大整数

大整形的加/减法

大整形的乘法

大整形除法

大整形开方

代码实现


前言

好久没有更新博客了,hhh。时隔三个月,我又回来了。先来点简单的大整形,虽说简单,但是在编写的时候还是debug了好久。

申明:本文代码为博主自行编写,尚有不足还望海涵,也希望大佬可以指点一二。

为什么要大整数

这是一个令人尴尬的问题。个人看法是除了竞赛大概率碰不上大整形,或者使用c/c++处理大整形的几率很小。我想有的小伙伴要问了,为什么说c/c++处理大整形的几率很小。请看c/c++的语言标准。时至今日,c/c++语言标准都没有明确规定相关生产商必须提供大整形。而在实际的生产中,只有GCC一方提供了“大整形” -- 128位整形。事实上,128位整型并不是内嵌的、官方认定的类型,换句话说只要128位系统没出现,128位类型就不能内嵌,一定是一个认为实现的标准库。在现实生活中,64位整数已经能够处理我们日常生活中的事情。正如有人调侃,微软当初使用64位整型是因为需要64位来存放比尔盖茨的财产,但是32位对于我们普通人来说足够了。由此可见,实际生活没有那么多的大整形。凡事皆有例外,为金融、航天等机密仪器所设计的程序可能会面临大整形、高精度的需求。因此,你发现python在金融称王称霸是合理的。python处理大整形、高精度有着天然优势。但是在精密仪器上应该还是c/cpp开发的程序较多。

总而言之,就是当64位整数不能够满足需求时,就需要按需设计一个存储结构。这个存储结构就是大整形。顺便一提这里不建议使用128位整数,因为可移植性比较差。

大整形的加/减法

大整形的加减法是最为简单的,简单来说就是按位相加减,事后借进位

大整形的乘法

乘法的实现也比较低,相较于加减法难一丢丢。如果我们有如下的式子:

number=a_n*base^{n}+a_{n-1}*base^{n-1}+\dots+a_{0}*base^{0}

那么,现在有number_{1},number_{2}可以像如上式子表达。为了方便表示结果存储在 result ,并且number[i]=a_{i}*base^{i}

于是我们知道number_{1}*number_{2}=\sum_{i=0}^{n_{1}}(number_{1}[i])*\sum_{i=0}^{n_{2}}(number_{2}[i]),进一步推导得到result[k]=\sum_{i}^{k}(number_{1}[i]*number_{2}[k-i])

大整形除法

除法最简单的直观的方式就是位对齐,减试商。这里还可以来一个小优化,就是试商的时候可以不用循环尝试,使用二分搜索尝试,这样会快一点。因为我们的基底不一定是10。如果是大基底循环试商就太慢了,而选用小基底空间上又太浪费。

当然还有数学方法,如果基于数学算法,大整形的运算速度都可以提升到O(nlog^{n})。但是,我太菜了,不能理解。所以就不出来瞎掰扯了,误人子弟了。

大整形开方

这里也是使用了试根法。也有数学方法来着,不过是基于除法实现。so,除法不懂,这里就更不懂了(苦笑ing...)。

代码实现

1000位(十进制)以内目前没发现bug。仍有待测试和完善,乘法计算速度尚可。里面的减法只实现了移位减法供除法使用。一般的减法设计可参看加法 + 移位减法中的检测机制。

里面虽然是无符号类型,但是无符号和有符号的计算效果是一样的。也就是说,如果你需要设计有符号的大整形,可以标注最高位的无符号第1个二进制位就是符号位。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>

class BigInt : public std::vector<unsigned long long> {
public:
  BigInt(unsigned long long n = 0) {
	int i = 0;
	do {
	  push_back(0);
	  at(i++) = n % base;
	  n /= base;
	} while (n);
  };
  BigInt(char* s) {
	int len = strlen(s);
	for (int i = len - 1; i >= 0; i -= log_10_base) {
	  unsigned long long num = 0;
	  for (int j = i - log_10_base + 1 < 0 ? 0 : i - log_10_base + 1; j <= i; ++j)
		num = num * 10 + (s[j] - '0');
	  push_back(num);
	}
  }

  const int theBase() const { return base; };

  // 赋值号
  BigInt& operator = (const BigInt& other) {
	for (int i = 0, j = 0; i < other.size(); ++i, ++j) {
	  if (i >= size()) push_back(0);
	  at(j) = other[i];
	}
	for (int i = other.size(); i < size(); ++i) pop_back();
	return *this;
  };
  // 加法类
  BigInt operator+ (const BigInt& other) {
	int min_digital = std::min(size(), other.size());
	int max_digital = std::max(size(), other.size());
	BigInt ret;
	for (int i = max_digital - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < min_digital; ++i) ret[i] = at(i) + other[i];
	for (int i = min_digital; i < size(); ++i) ret[i] = at(i);
	for (int i = min_digital; i < other.size(); ++i) ret[i] = other[i];
	ret.process();
	return ret;
  };
  BigInt& operator+= (const BigInt& other) {
	int min_digital = std::min(size(), other.size());
	int max_digital = std::max(size(), other.size());
	for (int i = 0; i < min_digital; ++i) at(i) += other[i];
	for (int i = min_digital; i < other.size(); ++i) {
	  push_back(0);
	  at(i) = other[i];
	}
	process();
	return *this;
  };
  // 乘法类
  BigInt operator* (const BigInt& other) {
	BigInt ret;
	for (int i = size() + other.size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  for (int j = 0; j < other.size(); ++j)
		ret[i + j] += at(i) * other[j];

	ret.process();
	return ret;
  };
  BigInt operator* (const long long num) {
	BigInt ret;
	for (int i = size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  ret[i] = at(i) * num;

	ret.process();
	return ret;
  };
  BigInt& operator*= (const int num) {
	for (int i = 0; i < size(); ++i)
	  at(i) *= num;

	process();
	return *this;
  };
  BigInt operator*= (const BigInt& other) {
	BigInt ret;
	for (int i = size() + other.size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  for (int j = 0; j < other.size(); ++j)
		ret[i + j] += at(i) * other[j];

	ret.process();
	for (int i = 0; i < ret.size(); ++i) {
	  if (i >= size()) push_back(0);
	  at(i) = ret[i];
	}
	return *this;
  };
  // 减法类
  // *this - (num << shl) the base is class_base 
  void sub_with_shl(const BigInt& num, int shl) {
	for (int i = 0; i < num.size(); ++i) {
	  unsigned long long check = at(i + shl);
	  at(i + shl) -= num[i];
	  if (check - num[i] > check) { // 如果发生结尾
		at(i + shl) += base;
		at(i + shl + 1) -= 1;
		int higher = i + shl + 1;
		while (at(higher) >= base) { // 是否会产生连续借位
		  at(higher) += base;
		  higher += 1;
		  at(higher) -= 1;
		} // 
	  } // 
	}
	process();
  };
  // 除法类
  // 最简单的方法就是一直循环减
  // 实际上还有数学方法,本人能力有限无法实现,涉及到多项式环快速逆,Crypto的知识。
  BigInt operator/ (BigInt& divisor) {
	if (*this < divisor) return BigInt((unsigned long long) 0);
	BigInt quotiend;
	BigInt remainder = *this;
	int shl = size() - divisor.size(); // 移位
	if (!divisor.less_equal_with_shl(*this, shl)) shl -= 1;
	while (divisor <= remainder) {
	  unsigned long long q = remainder.search_quotient(divisor, shl);
	  remainder.sub_with_shl(divisor * (q - 1), shl);
	  quotiend[quotiend.size() - 1] = q - 1;
	  quotiend.push_back(0);
	  if (shl) shl -= 1;
	}
	quotiend.pop_back();
	quotiend.reverse();
	quotiend.process();
	return quotiend;
  }
  BigInt operator% (BigInt& divisor) {
	if (*this < divisor) return *this;
	BigInt quotiend;
	BigInt remainder = *this;
	int shl = size() - divisor.size(); // 移位
	if (!divisor.less_equal_with_shl(*this, shl)) shl -= 1;
	while (divisor <= remainder) {
	  unsigned long long q = remainder.search_quotient(divisor, shl);
	  remainder.sub_with_shl(divisor * (q - 1), shl);
	  quotiend[quotiend.size() - 1] = q - 1;
	  quotiend.push_back(0);
	  if (shl) shl -= 1;
	}
	return remainder;
  }
  int operator% (int divisor) {
	int r = 0;
	for (int i = size() - 1; i >= 0; --i) {
	  // r = r * base + at(i);
	  r = (r * (base % divisor) + at(i) % divisor) % divisor;
	}
	return r;
  }

  // 开方运算 -- 1000位精确
  BigInt sqrt() {
	BigInt ret;
	int sz = 0;
	if (size() % 2 == 0) ret.resize(sz = size() >> 1);
	else ret.resize(sz = (size() >> 1) + 1);
	for (int i = sz - 1; i >= 0; --i) {
	  search_root_for_ith_digital(ret, i);
	}
	ret.process();
	return ret;
  }

  // 比较类
  bool operator<(const BigInt& other) const {
	if (size() != other.size()) return size() < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i]) return at(i) < other[i];
	return false;
  }
  bool operator<=(const BigInt& other) const {
	if (size() != other.size()) return size() < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i]) return at(i) < other[i];
	return true;
  }
  // *this << shl <= other
  bool less_equal_with_shl(const BigInt& other, int shl) const {
	if (size() + shl != other.size()) return size() + shl < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i + shl]) return at(i) < other[i + shl];
	return true;
  }


  // 输出
  void output() {
	printf("%Id", at(size() - 1));
	for (int i = size() - 2; i >= 0; --i) {
	  for (int j = base / 10; j > 0; j /= 10)
		printf("%Id", at(i) % (j * 10) / j);
	}
	puts("");
  }
private:
  void process() {
	for (int i = 0; i < size(); ++i) {
	  if (at(i) < base) continue;
	  if (i + 1 == size()) push_back(0);
	  at(i + 1) += at(i) / base;
	  at(i) %= base;
	}
	for (int i = size() - 1; at(i) == 0 && i > 0; --i) pop_back();
  }
  // 为除法设计 -- 小优化
  long long search_quotient(BigInt& divisor, int shl) {
	long long l = 1, r = base;
	while (l < r) {
	  long long mid = l + (r - l >> 1);
	  BigInt tmp = divisor * mid;
	  if (tmp.less_equal_with_shl(*this, shl)) l = mid + 1;
	  else r = mid;
	}
	return l;
  }
  BigInt& reverse() {
	for (int i = 0, j = size() - 1; i < j; ++i, --j) {
	  unsigned long long tmp = at(i);
	  at(i) = at(j);
	  at(j) = tmp;
	}
	return *this;
  }
  void search_root_for_ith_digital(BigInt& ret, int i) {
	long long l = 1, r = base;
	while (l < r) {
	  ret[i] = l + (r - l >> 1);
	  BigInt tmp = ret * ret;
	  if (tmp.less_equal_with_shl(*this, 0)) l = ret[i] + 1;
	  else r = ret[i];
	}
	ret[i] = l;
	unsigned long long check = ret[i];
	ret[i] -= 1;
	if (ret[i] > check) {
	  int higner = i + 1;
	  ret[i] += base;
	  ret[higner] -= 1;
	  while (ret[higner] > base) {
		ret[higner] += base;
		higner += 1;
		ret[higner] -= 1;
	  }
	}
	return;
  }
private:
  const static int base = 1000000000;
  const static int log_10_base = 9;
};

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
武汉理工大学基于Verilog的运算器设计是指在该大学进行的一项基于Verilog语言的电子运算器设计研究。运算器是计算机中的一个重要组成部分,用于进行各种数学运算和逻辑运算。 在武汉理工大学的研究中,运算器是通过使用Verilog硬件描述语言进行设计和实现的。Verilog是一种用于电子设计自动化的硬件描述语言,常用于设计硬件逻辑电路。通过使用Verilog语言,可以对运算器进行逻辑设计、功能实现以及性能优化。 基于Verilog的运算器设计中,研究人员会首先分析运算器的功能需求和性能要求。然后,他们会使用Verilog语言进行逻辑设计,包括运算器的各个模块、信号传输和控制逻辑等。接下来,他们会使用仿真软件对设计进行验证,确保设计在各种情况下都能正确运行。 在设计验证完成后,研究人员会将Verilog代码进行综合,转换成物理硬件电路。然后,他们会进行布局和布线,将电路内部的各个模块和线路进行物理连接。最后,他们会制造出实际的芯片,并进行测试和调试,确保运算器能够正常工作。 通过基于Verilog的运算器设计,武汉理工大学的研究人员可以探索和优化各种电子运算器的设计,提高计算机的性能和功能。他们的研究成果可以应用于计算机、通信、人工智能等领域,为我们的科技发展提供支持和推动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值