mod(other: Big Int) - > Result<Big Int, string>- Module
- std.
bigint - Receiver
- Big
Int - Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
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
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1120 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 61 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1120 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 468 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 297
Receiver
BigInt
Parameter constraints
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.
Related rule: Divide exact integers toward zeroResult
The declared result type is Result<Big.
Effects
Failures
Result and effects
LinkA nonzero remainder has the dividend sign. A zero divisor returns Err containing diagnostic text.
Resolved contracts
LinkRelated entries
LinkDivide 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