Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.bigint

    BigInt.toInt

    Convert an exact integer when it fits the signed int range.

    toInt() -> Option<int>
    Receiver
    BigInt
    Minimum arity
    0
    Maximum arity
    0

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import 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.

    Source evidence

    Receiver

    BigInt

    Parameter constraints

    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().

    Result

    The declared result type is Option<int>.

    Effects

    Failures

    Result and effects

    Link

    Both signed 64 bit endpoints return Some. Values outside the range return None without wrapping or saturation.

    Resolved contracts

    Link

    Related entries

    Link

    std.bigint

    Convert exact integers within the int range

    Link
    Ruletopaz.stdlib.contract.bigint-to-int.rule

    Convert 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

    Include both int endpoints and reject their neighbors

    Link
    main.tpzInclude both int endpoints and reject their neighborsOpen in Playground
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. function show(text: string) -> () {
    5. let value = test.assertSome(BigInt.parse(text, 10))
    6. match (value.toInt()) {
    7. case Some(integer) => io.writeLine("{integer}")
    8. case None() => io.writeLine("outside int")
    9. }
    10. }
    11. show("-9223372036854775808")
    12. show("9223372036854775807")
    13. show("-9223372036854775809")
    14. show("9223372036854775808")
    15. show("0")
    Expected outputexit 0
    -9223372036854775808
    9223372036854775807
    outside int
    outside int
    0
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. -9223372036854775808
    2. 9223372036854775807
    3. outside int
    4. outside int
    5. 0
    Source evidence

    Keep a large value after conversion fails

    Link
    main.tpzKeep a large value after conversion failsOpen in Playground
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. let value = test.assertSome(BigInt.parse("1000000000000000000000000000000", 10))
    5. match (value.toInt()) {
    6. case None() => io.writeLine("outside int")
    7. case Some(_) => io.writeLine("converted")
    8. }
    9. io.writeLine(value.toString(10))
    Expected outputexit 0
    outside int
    1000000000000000000000000000000
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. outside int
    2. 1000000000000000000000000000000
    Source evidence

    Menu

    Download