什么是值类型BigInteger的在C#中的限制? [英] What is the limit of the Value Type BigInteger in C#?

查看:354
本文介绍了什么是值类型BigInteger的在C#中的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为MSDN描述的BigInteger 是:




这是不可改变的类型,它代表一个任意大的整数,其
值在理论上没有上限或下限




我可以看到的BigInteger是值类型,就像我所知, 。值类型必须有16个字节



A 最大尺寸

MSDN更进一步说:



< BLOCKQUOTE>

一个OutOfMemoryException可以抛出并导致
BigInteger值增长过大的操作。




和更




虽然这个过程是透明的呼叫者,它确实收取
的性能损失。在某些情况下,反复尤其是当
的运算在一个环路上执行非常大的BigInteger值




如何能储存这样大值,大如 double.MaxValue + double.MaxValue
我被告知,它有引用类型里面obejects,但我可以在这里找到其在VisualStudio的定义是值类型。



什么是它真正的限制?即使没有这样的,怎么能作为值类型管理存储数据的全部金额是多少?


解决方案
< BLOCKQUOTE>

我可以看到的BigInteger是值类型,因为就我所知,一个值类型必须有16字节的最大尺寸。




不,这不是真的。这是一个的常规的限制,但它是完全可行的值类型采取不止于此。例如:

 公共结构美孚{
私人只读INT A,B,C,D,E; //看马,20个字节!
}



不过,我强烈怀疑,的BigInteger 实际上包括为一个字节数组的引用:

 公共结构的BigInteger {
私人只读的byte []数据;
//一些其他的领域...
}



穆斯林奔Dhaou的回答显示使用 INT 一个电流实施和 UINT [] ,但当然的详情的这是有意隐藏。)



所以的的的BigInteger 仍然可以很小,但它可以指大内存块 - 如果没有足够的内存来分配的需要什么当你执行一些操作,你会得到一个异常。




如何可以存储这么大的数值,大如double.MaxValue + double.MaxValue?




的BigInteger 的整数,所以我不会特别想用它来做什么用做双击 ...但根本上的限制将是你周围有多少内存已经得到了和阵列的大小在CLR可以应付。在现实中,你会谈论的巨大的实际击中任何具体数量限制之前的数字 - 但如果你有人数较少的gazillions,这显然有大内存的要求太


As described in MSDN BigInteger is :

An immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds.

As I can see BigInteger is a ValueType, as much as I know, a ValueType must have a maximum size of 16 bytes.

MSDN goes further saying :

an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large.

and more :

Although this process is transparent to the caller, it does incur a performance penalty. In some cases, especially when repeated operations are performed in a loop on very large BigInteger values

How could it store such big values, as big as double.MaxValue + double.MaxValue ? I was told that it has ReferenceType obejects inside it, but all I can find here in its definition in VisualStudio is ValueTypes.

What's its real limit ? And even if doesn't have one, how can it "as a value type" manage to store all that amount of data ?

解决方案

As I can see BigInteger is a ValueType, as much as I know, a ValueType must have a maximum size of 16 bytes.

No, that's not true. It's a conventional limit, but it's entirely feasible for a value type to take more than that. For example:

public struct Foo {
    private readonly int a, b, c, d, e; // Look ma, 20 bytes!
}

However, I strongly suspect that BigInteger actually includes a reference to a byte array:

public struct BigInteger {
    private readonly byte[] data;
    // Some other fields...
}

(Moslem Ben Dhaou's answer shows one current implementation using int and uint[], but of course the details of this are intentionally hidden.)

So the value of a BigInteger can still be small, but it can refer to a big chunk of memory - and if there isn't enough memory to allocate what's required when you perform some operation, you'll get an exception.

How could it store such big values, as big as double.MaxValue + double.MaxValue ?

Well BigInteger is for integers, so I wouldn't particularly want to use it for anything to do with double... but fundamentally the limitations are going to be around how much memory you've got and the size of array the CLR can cope with. In reality, you'd be talking about enormous numbers before actually hitting the limit for any specific number - but if you have gazillions of smaller numbers, that obviously has large memory requirements too.

这篇关于什么是值类型BigInteger的在C#中的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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