mod(other: Big Int) - > Result<Big Int, string>- 수신자
- Big
Int - 최소 인자 수
- 1
- 최대 인자 수
- 1
Declaration and parameters
링크사용 조건
기본 포함 · 미정
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.
수신자
BigInt
매개변수 제약
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.
관련 규칙 · Divide exact integers toward zero →결과
The declared result type is Result<Big.
효과
실패
Result and effects
링크A nonzero remainder has the dividend sign. A zero divisor returns Err containing diagnostic text.
Resolved contracts
링크Related entries
링크Divide exact integers toward zero
링크Divide 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.
이유
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.
Keep quotient and remainder signs explicit
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
2 1-2 -1-2 12 -10 20 09223372036854775808 0
Handle a zero divisor as a Result error
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
division rejectedremainder rejected70
Evaluate the receiver before the divisor
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
receiverdivisor2