Skip to content

rylTianChen/cpp_hpcalc_high-precision

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 

Repository files navigation

hpcalc

🔥 单文件纯 C++ 高精度大整数库 | 有符号整数 | 运算符重载 | Karatsuba 乘法

🔥 Single-file C++ high-precision big integer library with arithmetic, bitwise operations, operator overloading, and Karatsuba multiplication.

hpcalc.h内部提供详细中文使用说明

detailed English instructions provided inside hpcalc.h

GitHub Stars GitHub Forks C++14 Platform Single File Karatsuba License

✨ 项目简介

hpcalc 是纯手写、单文件、无任何依赖的高精度有符号整数计算库,适用于算法竞赛、大数运算、教学与工程项目。 仅需引入 hpcalc.h 即可使用,支持完整运算符重载与位运算。

🎯 核心特性

  • 单文件开箱即用:仅 hpcalc.h,无需编译、无需链接
  • 无第三方依赖:不依赖 GMP、Boost 等任何库
  • 千进制存储:速度优于传统十进制
  • Karatsuba 分治乘法:大数乘法性能大幅提升
  • 完整运算符重载+ - * / % 直接使用
  • 支持位运算& | ^ ~(补码实现)
  • 错误安全:异常场景统一返回 EMPTY
  • 丰富工具函数:反转、回文、末尾补 0、删位、取指定位等

🚀 快速示例

#include<iostream>
#include"hpcalc.h"
using namespace grnum;
HP a, b, c;
char op;
int main(){
	while(1){
		std::cin >> a >> op >> b;
		c.clear();
		if(op == '+') c = a+b;
		if(op == '-') c = a-b;
		if(op == '*') c = a*b;
		if(op == '/') c = a/b;
		if(op == '^') c = HP_pow(a, b);
		if(op == '%') c = a%b;
		if(c.isEMPTY()) puts("Error");
		else putsHP(c);
	}
	return 0;
}

编译命令

g++ test.cpp -O2 -std=c++14 -o test.exe

性能测试

测试项目 Python 内置 hpcalc (C++)
2^200000 计算 0.61 ms 68 ms
10 万位 + 10 万位 加法 0.01 ms 0 ms
100 万位 加法 2.37 ms 51 ms
大数取模(100 万位) 11.73 ms 138 ms

这些是纯计算时间, 可以在库中找到timer.cpp和timer.py

📌 支持运算

数学运算

  • 加减乘除模:+ - * / %
  • 乘方:HP_pow(a, b)
  • 复合赋值:+= -= *= /= %=
  • 自增 / 自减:++ --

位运算

  • 与:a & b
  • 或:a | b
  • 异或:a ^ b
  • 取反:~a
  • 基于 1024 进制补码实现

比较运算 > < >= <= == != 全支持,可与 int/long long 混用

🧩 常用 API

HP x = 123456;
x.length();      // 获取位数
x.sign();        // 获取符号:1 / -1 / 0
x.isEMPTY();     // 是否为错误值
x.clear();       // 清空为 EMPTY
x.reverse();     // 数字反转
x.isPalindrome();// 是否回文数
x.AppendZero(5); // 末尾加 5 个 0
x.RemoveTail(3); // 删除末尾 3 位
x.GetDigit(2);   // 获取倒数第 2 位

⚠️ 注意事项

  1. 仅支持有符号大整数,不支持浮点数
  2. 负数取模规则:余数与被除数同号
  3. 指数超过 8 位时,HP_pow 返回 EMPTY
  4. 错误 / 空值参与运算 → 返回 EMPTY
  5. 最大长度限制:1e8 位

📧 作者

天辰

lyrTianChen09@outlook.com

📄 许可证

开源免费,可自由使用、修改、分发

About

单文件 C++ 高精度大整数库,支持加减乘除模幂、位运算、运算符重载,内置 Karatsuba 快速乘法。Single-file C++ high-precision big integer library with arithmetic, bitwise operations, operator overloading, and Karatsuba multiplication.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors