본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.bigint

    BigInt.mod

    Return the remainder associated with division toward zero.

    mod(other: BigInt) -> Result<BigInt, string>
    수신자
    BigInt
    최소 인자 수
    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.

    결과

    The declared result type is Result<BigInt, string>.

    효과

    실패

    Result and effects

    링크

    A nonzero remainder has the dividend sign. A zero divisor returns Err containing diagnostic text.

    Resolved contracts

    링크

    Related entries

    링크

    std.bigint →

    Divide exact integers toward zero

    링크
    규칙topaz.stdlib.contract.bigint-division.rule

    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

    링크
    main.tpzKeep quotient and remainder signs explicit플레이그라운드 열기 →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. function divide(left: string, right: string) -> () {
    5. let a = test.assertSome(BigInt.parse(left, 10))
    6. let b = test.assertSome(BigInt.parse(right, 10))
    7. let quotient = test.assertOk(a.div(b))
    8. let remainder = test.assertOk(a.mod(b))
    9. io.writeLine(quotient.toString(10) + " " + remainder.toString(10))
    10. }
    11. divide("7", "3")
    12. divide("-7", "3")
    13. divide("7", "-3")
    14. divide("-7", "-3")
    15. divide("2", "7")
    16. divide("0", "-3")
    17. divide("-9223372036854775808", "-1")
    기대 출력종료 코드 0
    2 1
    -2 -1
    -2 1
    2 -1
    0 2
    0 0
    9223372036854775808 0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 2 1
    2. -2 -1
    3. -2 1
    4. 2 -1
    5. 0 2
    6. 0 0
    7. 9223372036854775808 0

    Handle a zero divisor as a Result error

    링크
    main.tpzHandle a zero divisor as a Result error플레이그라운드 열기 →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. let value = test.assertSome(BigInt.parse("7", 10))
    5. let zero = test.assertSome(BigInt.parse("0", 10))
    6. match (value.div(zero)) {
    7. case Err(_) => io.writeLine("division rejected")
    8. case Ok(_) => io.writeLine("accepted")
    9. }
    10. match (value.mod(zero)) {
    11. case Err(_) => io.writeLine("remainder rejected")
    12. case Ok(_) => io.writeLine("accepted")
    13. }
    14. io.writeLine(value.toString(10))
    15. io.writeLine(zero.toString(10))
    기대 출력종료 코드 0
    division rejected
    remainder rejected
    7
    0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. division rejected
    2. remainder rejected
    3. 7
    4. 0

    Evaluate the receiver before the divisor

    링크
    main.tpzEvaluate the receiver before the divisor플레이그라운드 열기 →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. function receiver() -> BigInt {
    5. io.writeLine("receiver")
    6. return test.assertSome(BigInt.parse("7", 10))
    7. }
    8. function divisor() -> BigInt {
    9. io.writeLine("divisor")
    10. return test.assertSome(BigInt.parse("3", 10))
    11. }
    12. let quotient = test.assertOk(receiver().div(divisor()))
    13. io.writeLine(quotient.toString(10))
    기대 출력종료 코드 0
    receiver
    divisor
    2
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. receiver
    2. divisor
    3. 2

    메뉴

    다운로드 →