Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека · std.bigint

    BigInt.div

    Return an exact quotient rounded toward zero.

    div(other: BigInt) -> Result<BigInt, string>
    Модуль
    std.bigint →
    Получатель
    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 divisor returns Ok containing the quotient. Zero 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

    Меню

    Загрузка →