duanrong0738
2018-08-19 05:24 阅读 85
已采纳

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

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

https://golang.org/pkg/math/big/#pkg-constants

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:

fmt.Println(math.MaxUint32)

4294967295

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongwu5318 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.

    点赞 评论 复制链接分享

相关推荐