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