Module
LinkBigInt preserves arbitrary precision values. Text parsing, formatting, checked int conversion, division, and remainder have explicit failure outcomes.
Why
LinkExact arithmetic and checked conversions keep large values distinct from bounded machine integers.
Availability
LinkImport std.bigint in language mode 5.21. The current compiler selects std/bigint.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Exported types
Linkexport type Big
Type members keep the compiler-owned type name shown in their declarations. Importing a module does not create a new type identity.
Public API
LinkPending contracts
LinkRead and write integers in an explicit radix
LinkRead and write integers in an explicit radix
BigInt represents signed integers without a fixed bit width. Its text operations preserve the exact integer value and do not use a locale.
BigInt.parse takes a radix from 2 through 36. Text consists of an optional minus sign followed by at least one ASCII digit or lowercase ASCII letter. Letters a through z have digit values 10 through 35. Every digit must be smaller than the radix.
Leading zero digits are accepted. Negative zero produces the same zero value as 0. Parsing does not trim whitespace, accept a plus sign, accept uppercase letters, accept separators, or interpret a radix prefix. Characters are interpreted as ordinary digits of the supplied radix.
Valid text returns Some containing the exact integer. Empty text, a sign without digits, an invalid digit, any extra character, or a radix outside the allowed range returns None(). These input failures do not raise a runtime fault.
BigInt.toString takes a radix from 2 through 36 and returns lowercase ASCII digits in that radix. A negative nonzero value starts with a minus sign. The output has no radix prefix or leading zero digits. Zero is written as 0.
An invalid BigInt.toString radix raises TPZ5001 during execution. It does not select a default radix or return empty text.
Formatting a value and parsing the result with the same radix recovers that value. Parsing and formatting accepted text produces its canonical spelling in the selected radix.
The receiver and positional arguments are evaluated once in source order before the operation validates their values. An earlier fault prevents later evaluation. The operations do not change their inputs or perform external effects.
Why
An explicit radix gives each digit one value. Strict input spelling makes accidental whitespace and alternate signs visible to the caller. Canonical output gives stored integer text a predictable form without a locale dependency.
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Parse and format exact integers in explicit radices
Linkimport std.bigint { BigInt }import std.testimport std.iolet number = test.assertSome(BigInt.parse("ff", 16))io.writeLine(number.toString(2))io.writeLine(number.toString(10))io.writeLine(number.toString(16))io.writeLine(test.assertSome(BigInt.parse("z", 36)).toString(10))io.writeLine(test.assertSome(BigInt.parse("-00015", 10)).toString(16))io.writeLine(test.assertSome(BigInt.parse("-000", 10)).toString(36))io.writeLine(test.assertSome(BigInt.parse("123456789012345678901234567890", 10)).toString(10))
11111111 255 ff 35 -f 0 123456789012345678901234567890
Expected result
Expected result
Execution completes. Exit code 0.
11111111255ff35-f0123456789012345678901234567890
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Return None for malformed or out of radix digits
Linkimport std.bigint { BigInt }import std.iofor text in (["", "-", "1_0", "0x10", "g", "12", "1\n2"]) {match (BigInt.parse(text, 16)) {case None() => io.writeLine("rejected")case Some(_) => io.writeLine("accepted")}}match (BigInt.parse("2", 2)) {case None() => io.writeLine("digit rejected")case Some(_) => io.writeLine("accepted")}
rejected rejected rejected rejected rejected rejected rejected digit rejected
Expected result
Expected result
Execution completes. Exit code 0.
rejectedrejectedrejectedrejectedrejectedrejectedrejecteddigit rejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject parse radices outside two through thirty six
Linkimport std.bigint { BigInt }import std.iofor radix in ([-1, 0, 1, 37, 9223372036854775807]) {match (BigInt.parse("10", radix)) {case None() => io.writeLine("radix rejected")case Some(_) => io.writeLine("accepted")}}
radix rejected radix rejected radix rejected radix rejected radix rejected
Expected result
Expected result
Execution completes. Exit code 0.
radix rejectedradix rejectedradix rejectedradix rejectedradix rejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject surrounding whitespace in integer text
Linkimport std.bigint { BigInt }import std.iomatch (BigInt.parse(" 10 ", 10)) {case None() => io.writeLine("whitespace rejected")case Some(_) => io.writeLine("accepted")}
whitespace rejected
Expected result
Expected result
Execution completes. Exit code 0.
whitespace rejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject a leading plus sign in integer text
Linkimport std.bigint { BigInt }import std.iomatch (BigInt.parse("+10", 10)) {case None() => io.writeLine("plus rejected")case Some(_) => io.writeLine("accepted")}
plus rejected
Expected result
Expected result
Execution completes. Exit code 0.
plus rejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject uppercase letters in integer text
Linkimport std.bigint { BigInt }import std.iomatch (BigInt.parse("FF", 16)) {case None() => io.writeLine("uppercase rejected")case Some(_) => io.writeLine("accepted")}
uppercase rejected
Expected result
Expected result
Execution completes. Exit code 0.
uppercase rejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Evaluate parse arguments before validating their values
Linkimport std.bigint { BigInt }import std.iofunction text() -> string {io.writeLine("text")return "bad"}function radix() -> int {io.writeLine("radix")return 10}match (BigInt.parse(text(), radix())) {case None() => io.writeLine("rejected")case Some(_) => io.writeLine("accepted")}
text radix rejected
Expected result
Expected result
Execution completes. Exit code 0.
textradixrejected
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject a formatting radix outside the valid range
Linkimport std.bigint { BigInt }import std.testimport std.iolet number: BigInt = test.assertSome(BigInt.parse("1", 10))number.toString(1)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Reject a formatting radix outside the valid range
Linkimport std.bigint { BigInt }import std.testimport std.iolet number: BigInt = test.assertSome(BigInt.parse("1", 10))number.toString(37)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 99 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 124 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 235 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 44 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 53 - compiler/crates/topaz_rt/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bigint-textatworking-tree - topaz.decision.bigint-text-spelling.1 at
topaz.decisi
Diagnostics
LinkConvert exact integers within the int range
LinkConvert exact integers within the int range
BigInt.toInt returns Some containing the same integer when the value is at least -9223372036854775808 and at most 9223372036854775807. Both endpoints are included.
Every value outside that signed 64 bit range returns None(). Conversion does not wrap, truncate, round, or saturate.
The operation evaluates its receiver once and leaves it unchanged. A failed conversion is an Option value and does not raise an arithmetic overflow fault.
BigInt.parse can represent integers beyond the int range. BigInt.toString still formats those values exactly when BigInt.toInt returns None().
Why
A checked conversion preserves the distinction between an exact arbitrary precision value and a bounded machine integer. Option makes failure explicit without inventing a replacement integer.
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree
Include both int endpoints and reject their neighbors
Linkimport std.bigint { BigInt }import std.testimport std.iofunction show(text: string) -> () {let value = test.assertSome(BigInt.parse(text, 10))match (value.toInt()) {case Some(integer) => io.writeLine("{integer}")case None() => io.writeLine("outside int")}}show("-9223372036854775808")show("9223372036854775807")show("-9223372036854775809")show("9223372036854775808")show("0")
-9223372036854775808 9223372036854775807 outside int outside int 0
Expected result
Expected result
Execution completes. Exit code 0.
-92233720368547758089223372036854775807outside intoutside int0
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree
Keep a large value after conversion fails
Linkimport std.bigint { BigInt }import std.testimport std.iolet value = test.assertSome(BigInt.parse("1000000000000000000000000000000", 10))match (value.toInt()) {case None() => io.writeLine("outside int")case Some(_) => io.writeLine("converted")}io.writeLine(value.toString(10))
outside int 1000000000000000000000000000000
Expected result
Expected result
Execution completes. Exit code 0.
outside int1000000000000000000000000000000
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree
Divide exact integers toward zero
LinkDivide exact integers toward zero
For a nonzero divisor, BigInt.div returns Ok containing the exact integer quotient truncated toward zero.
BigInt.mod returns Ok containing the remainder left by that quotient. For dividend a and divisor b, the values satisfy a = quotient * b + remainder. A nonzero remainder has the sign of the dividend and a magnitude smaller than the divisor magnitude.
A zero dividend and nonzero divisor produce a zero quotient and zero remainder. A negative divisor changes the quotient sign and does not choose the remainder sign.
A zero divisor makes either operation return Err containing a descriptive nonempty string. The string has no stable machine readable format. The operation does not raise a division fault for this input.
Quotients and remainders are BigInt values. Division has no signed 64 bit overflow boundary and does not convert an operand to int.
The receiver is evaluated before the divisor and each is evaluated once. An earlier fault prevents later evaluation. The operations leave both values unchanged and have no external effects.
Use div and mod for BigInt division and remainder. The language arithmetic operators on BigInt provide addition, subtraction, and multiplication.
Why
Truncating the quotient toward zero keeps signed division aligned with int arithmetic. Returning Err for a zero divisor gives exact integer calculations a recoverable failure value.
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 86 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 74 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 83 -
topaz.stdlib.contract.bigint-divisionatworking-tree
Keep quotient and remainder signs explicit
Linkimport std.bigint { BigInt }import std.testimport std.iofunction divide(left: string, right: string) -> () {let a = test.assertSome(BigInt.parse(left, 10))let b = test.assertSome(BigInt.parse(right, 10))let quotient = test.assertOk(a.div(b))let remainder = test.assertOk(a.mod(b))io.writeLine(quotient.toString(10) + " " + remainder.toString(10))}divide("7", "3")divide("-7", "3")divide("7", "-3")divide("-7", "-3")divide("2", "7")divide("0", "-3")divide("-9223372036854775808", "-1")
2 1 -2 -1 -2 1 2 -1 0 2 0 0 9223372036854775808 0
Expected result
Expected result
Execution completes. Exit code 0.
2 1-2 -1-2 12 -10 20 09223372036854775808 0
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 86 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 74 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 83 -
topaz.stdlib.contract.bigint-divisionatworking-tree
Handle a zero divisor as a Result error
Linkimport std.bigint { BigInt }import std.testimport std.iolet value = test.assertSome(BigInt.parse("7", 10))let zero = test.assertSome(BigInt.parse("0", 10))match (value.div(zero)) {case Err(_) => io.writeLine("division rejected")case Ok(_) => io.writeLine("accepted")}match (value.mod(zero)) {case Err(_) => io.writeLine("remainder rejected")case Ok(_) => io.writeLine("accepted")}io.writeLine(value.toString(10))io.writeLine(zero.toString(10))
division rejected remainder rejected 7 0
Expected result
Expected result
Execution completes. Exit code 0.
division rejectedremainder rejected70
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 86 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 74 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 83 -
topaz.stdlib.contract.bigint-divisionatworking-tree
Evaluate the receiver before the divisor
Linkimport std.bigint { BigInt }import std.testimport std.iofunction receiver() -> BigInt {io.writeLine("receiver")return test.assertSome(BigInt.parse("7", 10))}function divisor() -> BigInt {io.writeLine("divisor")return test.assertSome(BigInt.parse("3", 10))}let quotient = test.assertOk(receiver().div(divisor()))io.writeLine(quotient.toString(10))
receiver divisor 2
Expected result
Expected result
Execution completes. Exit code 0.
receiverdivisor2
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 86 - compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 74 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 83 -
topaz.stdlib.contract.bigint-divisionatworking-tree