大整数乘法
Big Integer Multiplication
本文关键字:整数 更新时间:2023-10-16
你好,我一直在研究一个大的整数类,我已经完成了加法和减法,我现在正在尝试编写一个有效的乘法函数,但是如果我在前 3 位数字之后乘以一个非常大的数字,它会失去精度并且答案是错误的。谁能帮助我在这里做错了什么,我想不出什么?
BigInt operator*(const BigInt& left, const BigInt& right)
{
BigInt temp1 = left;
BigInt temp2 = right;
temp1.sign = 1;
temp2.sign = 1;
BigInt Max = MAX(temp1, temp2), Min = MIN(temp1, temp2);
ArrayList<char> temp_container = ArrayList<char>();
ArrayList<BigInt> nums = ArrayList<BigInt>();
int carry = 0;
int zero_count = 0;
for (int i = Min.Digits.size() - 1; i > -1; i--)
{
for (int j = Max.Digits.size() - 1; j > -1; j--)
{
int temp = (Max.Digits.get(j) * Min.Digits.get(i)) + carry;//Multiply Digits
if (temp < 10)//if it is a digit
{
temp_container.add_to_front(temp + '0');
carry = 0;
}
//else if it isnt a digit
else if (temp >= 10 && j > 0)
{
temp_container.add_to_front((temp / 10) + '0');
carry = temp % 10;
}
else if (temp >= 10 && j < 0)
{
temp_container.add_to_front((temp / 10) + '0');
temp_container.add_to_front((temp % 10) + '0');
}
}
for (int j = 0; j < zero_count; j++)
{
temp_container.add('0');
}
nums.add(BigInt(temp_container));
temp_container.removeAll();
zero_count++;//increase the amount of zeros to add to the next number
}
BigInt result = BigInt("0");
for (int i = 0; i < nums.size(); i++)
{
result += nums.get(i);//add all of the number up
}
//determine if positive or negative
if (left.sign == right.sign)
{
result.sign = 1;
}
else
{
result.sign = -1;
}
return result;
}
您的携带和数字颠倒(当有携带时(。
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- std::当在256字节边界上写入整数时,流的奇怪行为
- 内联程序集printf将整数解释为地址
- 是否基于数组B整数打印数组A中的整数
- 如何在C++中将整数转换为其数字数组