2018-08-19 05:24
浏览 182

golang中的math / big软件包支持的最大价值是多少?

I'm reading the documentation to the math/big package here:


I am trying to understand how large a number is too big for math.big, and this looked like a constant I could interrogate.

I see on my machine:



How does this relate to the largest integer possible on my machine, for the purpose of calculation? What are the units of this number? Is this bytes, or decimal places or something other than the number itself?

图片转代码服务由CSDN问答提供 功能建议

我正在此处阅读有关math / big软件包的文档:




 <  p>出于计算目的,这与我的机器上可能的最大整数有何关系? 这个数字的单位是什么? 是字节,小数位还是数字本身以外的其他内容? 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwu5318 2018-08-19 05:37

    bignum libraries usually store big numbers as a sequence of digits (e.g. in base 264). Their limitation is related to the memory available. So the largest number you could represent is tied to the limitation of your virtual address space. You can safely assume that a number even as large as 1010000 is representable in bignum. Of course, a googolplex is not representable as a bignum (because it has more bits than the number of particles in the universe).

    Another limitation is the complexity of arithmetic operations. But there exist very efficient bignum algorithms.

    FWIW, the GMPlib (a C library for bignums) can deal with numbers as long as there is memory for them. However, it is rumored than when malloc fails, GMPlib is aborting.

    I don't know what happens inside Go bignums when a number is too big to be representable (and that limit varies from one machine to the next and could be different from one run to the next). For example, Go's Int.Mul gives a product whose size is the sum of the size of the arguments, and the "out of memory" error is undocumented (but obviously can happen).

    When using bignums, prefer iterative algorithms to recursive ones. For example, a naive recursive factorial might overflow the call stack with large enough bignums, so you want to code it iteratively.

    解决 无用
    打赏 举报

相关推荐 更多相似问题