- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我运行这段代码时,我得到了运行时异常
OverflowException: BigInteger cannot represent infinity.
BigInteger sum=0;
for (double i=1 ; i<=1000 ;i++ )
sum += (BigInteger) Math.Pow(i,i);
Console.WriteLine(sum);
据我所知,BigInteger
值应该没有限制。那么为什么会抛出 OverflowException
呢?
最佳答案
发生这种情况是因为您超出了 double
的限制。
Math.Pow
在 double
中计算,因此有限结果只能与 1.7e308 一样大,您超过 i = 144
的数字。所以它导致 double.PositiveInfinity
,无法转换为 BigInteger
。 BigInteger
没有像 double
那样对无穷大进行特殊表示,它只能存储整数而无穷大不是整数 - 即使 BigInteger
没有极限,它永远不会达到无穷大。 BigInteger
实际上也有一个限制,当它用来存储数字的内部数组达到其最大大小时(您可能会更快地耗尽内存)。
在这种情况下,您可以使用 BigInteger.Pow
来避免这种情况,例如
BigInteger sum = 0;
for (int i = 1; i <= 1000; i++)
sum += BigInteger.Pow(i, i);
结果很大,符合预期。
关于c# - BigInteger 不能表示无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31754769/
我正在尝试计算一个大数,这需要 BigInteger.Pow(),但我需要指数也是 BigInteger 而不是 整数。 即 BigInteger.Pow(BigInteger) 我怎样才能做到这一点
在 Java 中,大多数基本类型都是有符号的(一位用于表示 +/-),因此当我超出类型的限制时,我会得到意想不到的结果,比如负数。 有没有比使用 BigInteger 更好的解决方案,因为 BigIn
如何选择随机元素 α ∈ Z*p? P 是一个随机的 1024 位素数 BigInteger。 这是我找到 BigInteger p 的方法: Random rand = new Random(new
我正在尝试使用 BigInteger 类在 Java 中实现 Fermat、Miller-Rabin 或 AKS 算法。 我想我有Fermat test已实现,但 BigInteger 类不允许将 B
我知道 BigInteger 有一个构造函数,您可以在其中通过传递新 BigInteger 的最大 bitLength 和一个随机参数来生成随机 BigInteger: BigInteger(int
我正在考虑为自己存储公钥的想法。为此,我需要将 BigInteger 转换为某种变量,然后从该值重新创建 BigInteger。 我在 Stackoverflow 中进行了搜索,找到了执行此操作的最佳
目前我正在使用 Long 整数类型。我使用以下方法从/到二进制/数字进行转换: Convert.ToInt64(BinaryString, 2); //Convert binary string of
我有以下代码: public static BigInteger[] Cubes (int m){ Set result = new HashSet(); for (int i = 1
因此,在我的计算机科学课上,我们一直在学习如何使用 Java 编码,并且我遇到了编码本身的第一个局限性。问题是 long 只允许您存储最多 64 位或接近该位的数字。所以我们已经开始用 16 位数字进
你好我想计算 2^(256bit number) 在 java 中,但 biginteger 的 pow 函数只能处理 int。 如何计算更大的数字? 有图书馆吗? 我想计算来自的所有数字 2^0 2
我正在尝试构建一个 BigInteger 数组,但似乎该数组需要由整数本身索引(如果是这样,那对我来说似乎非常愚蠢,但我希望我只是误解了一些东西) .我正在尝试的基本上是以下内容: BigIntege
我在 .Net 项目中使用 BigInteger ( link ) 的单声道实现在 Java 中我使用 java.math.BigInteger。 相同的代码在 Java 中产生不同的结果。 .Net
我正在构建 DSA 算法。但是在将 BigInteger 数字与其他 BigInteger 数字进行排名时遇到了问题。这是我要使用的公式: v = ((g^u1 * y^u2) mod p) mod
我正在尝试用Java实现Schnorr签名算法。我遇到了计算大指数幂(例如 MD5 哈希值)的问题。 有什么方法可以让BigInteger获得BigInteger的权力吗? 我需要计算 (a^x*b^
我正在编写斐波那契程序。 public class ImperativeFibonacci implements Fibonacci { public BigInteger fibonacci(i
我原以为 BigInteger 类中的两个构造函数 BigInteger(String) 和 BigInteger(byte[]) 的行为相似,但事实并非如此。 为什么两个 BigInteger 不相
在 Java 中,要将 String 转换为 BigInteger,您可以使用构造函数 new BigInteger(String),但要将 int/long 转换为工厂函数,您可以使用 BigInt
我正在做一些大整数计算,我需要对一个 BigInteger 求另一个 BigInteger 的幂。 .pow() 方法执行我想要的操作,但将 int 值作为参数。 .modPow 方法采用 BigIn
Java 7 方法的复杂性是什么pow和 isProbablePrime在BigInteger类(class)? 我知道 Rabin 测试的简单实现具有 O(k(log(n))^3) 复杂度,可以通过
.NET 4.0 为任意大的整数提供了 System.Numerics.BigInteger 类型。我需要计算 BigInteger 的平方根(或合理的近似值——例如整数平方根)。这样我就不必重新实现
我是一名优秀的程序员,十分优秀!