如何在C ++中找到大数除法的余数? [英] How to find the remainder of large number division in C++?

查看:97
本文介绍了如何在C ++中找到大数除法的余数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++中的模数有疑问.我试图做的是将一个非常大的数相除,例如说M%2,其中M = 54,302,495,302,423.但是,当我进行编译时,它表示该数字对于int是"long"的.然后,当我将其切换为双精度时,它会重复相同的错误消息.有什么办法可以使剩下的这个很大的数字,甚至更大的数字呢?感谢您的帮助,非常感谢.

I have a question regarding modulus in C++. What I was trying to do was divide a very large number, lets say for example, M % 2, where M = 54,302,495,302,423. However, when I go to compile it says that the number is to 'long' for int. Then when I switch it to a double it repeats the same error message. Is there a way I can do this in which I will get the remainder of this very large number or possibly an even larger number? Thanks for your help, much appreciated.

推荐答案

对于C ++中的大量算术,请使用 GMP 图书馆.特别是, mpz_mod 函数可以做到这一点.

For large number arithmetic in C++, use the GMP library. In particular, the mpz_mod function would do this.

对于更自然的C ++包装器, mpz_class 类可以通过为多精度操作提供运算符重载来提供帮助.

For a more natural C++ wrapper, the mpz_class class can help by providing operator overloading for multiprecision operations.

这篇关于如何在C ++中找到大数除法的余数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆