docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#Big integers and factorials

本页内容

Compute 100!, the 300th Fibonacci number and powers beyond 128 bits with the arbitrary-precision int, and find where i64 overflows.

#Code

#Walkthrough

  • int is an integer with no size limit. Small values are held in a single i64 and switch to a digit array only when they overflow, so for small numbers it is about as fast as a fixed-width integer (2.1.2 int, Int).
  • int and i64 don't mix in arithmetic. You convert explicitly, as in int::from(k), so the code shows at a glance which operations can overflow (2.1.6 arithmetic result types).
  • On the i64 side, checked_mul returns none on overflow. The trailing ? returns that none straight out of the function, so factorial_i64 reports its result as i64 | none (Sum types).
  • Integer literals beyond the i64 range can be written directly in an int slot (let big: int = 123_456_...).
  • int implements Display, so f"{x}" gives its decimal string, and the string slices text[:20] and text[-10:] show just the beginning and end (Strings).

Found a mistake? Edit this page on GitHub. Last updated