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

문서 검색

명세 6.0

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

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

    라이브러리 · std.decimal

    Decimal.div

    Divide at the requested decimal grid and return a recoverable zero divisor error.

    div(other: Decimal, scale: int, mode: RoundingMode = RoundingMode.HalfEven()) -> Result<Decimal, string>
    수신자
    Decimal
    최소 인자 수
    2
    최대 인자 수
    3

    Declaration and parameters

    링크

    사용 조건

    기본 포함 · 미정

    Import std.decimal in language mode 5.21.

    The current compiler selects std/decimal.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    수신자

    Decimal

    매개변수 제약

    Decimal.div evaluates the exact quotient of its receiver and the divisor. For a nonzero divisor it rounds that quotient once to the requested decimal grid and returns Ok containing the normalized Decimal.

    The requested scale and mode have the same meaning as in Decimal.round. Omitting the mode selects RoundingMode.HalfEven(). Input scales and coefficient sizes do not introduce another precision limit or an intermediate rounding step.

    A nonterminating decimal quotient is rounded to the requested grid. A terminating quotient that is already on the grid remains exact. The canonical result scale can be smaller than the requested scale.

    Division signs follow ordinary numeric division. All six rounding modes apply to the signed exact quotient, including negative ties and results that round to zero.

    A zero divisor returns Err with the string Decimal.div: division by zero. This includes zero divided by zero and a divisor parsed from a negative zero spelling. This failure is a Result value and does not raise TPZ4002.

    A zero dividend and a nonzero divisor return Ok containing zero for every valid requested scale. An internal sum of operand and requested scales must not turn a nonzero divisor into a division by zero error.

    An invalid requested scale raises TPZ5001 during execution before the zero divisor is classified. The operation never replaces an invalid scale with zero or reports that scale error as a successful Result.

    The receiver, divisor, scale and explicitly supplied mode expressions evaluate once in that order before their values are validated. The operation preserves both operands.

    결과

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

    효과

    실패

    Result and effects

    링크

    Round the exact quotient once with the supplied mode and normalize the result. The default mode is HalfEven. Invalid scales raise TPZ5001 during execution before zero divisor classification.

    Why

    링크

    An explicit result grid gives a nonterminating quotient a defined finite result while Result keeps a zero divisor recoverable.

    Resolved contracts

    링크

    Related entries

    링크

    std.decimal →

    Divide with an explicit result scale

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

    Divide with an explicit result scale

    Decimal.div evaluates the exact quotient of its receiver and the divisor. For a nonzero divisor it rounds that quotient once to the requested decimal grid and returns Ok containing the normalized Decimal.

    The requested scale and mode have the same meaning as in Decimal.round. Omitting the mode selects RoundingMode.HalfEven(). Input scales and coefficient sizes do not introduce another precision limit or an intermediate rounding step.

    A nonterminating decimal quotient is rounded to the requested grid. A terminating quotient that is already on the grid remains exact. The canonical result scale can be smaller than the requested scale.

    Division signs follow ordinary numeric division. All six rounding modes apply to the signed exact quotient, including negative ties and results that round to zero.

    A zero divisor returns Err with the string Decimal.div: division by zero. This includes zero divided by zero and a divisor parsed from a negative zero spelling. This failure is a Result value and does not raise TPZ4002.

    A zero dividend and a nonzero divisor return Ok containing zero for every valid requested scale. An internal sum of operand and requested scales must not turn a nonzero divisor into a division by zero error.

    An invalid requested scale raises TPZ5001 during execution before the zero divisor is classified. The operation never replaces an invalid scale with zero or reports that scale error as a successful Result.

    The receiver, divisor, scale and explicitly supplied mode expressions evaluate once in that order before their values are validated. The operation preserves both operands.

    이유

    A division result needs an explicit decimal grid because a finite pair of decimals can have a nonterminating quotient. Rounding the exact quotient once avoids hidden precision loss. A recoverable zero divisor stays distinct from invalid configuration and from internal scale arithmetic.

    Distinguish exact and repeating quotients at the requested grid

    링크
    main.tpzDistinguish exact and repeating quotients at the requested grid플레이그라운드 열기 →
    1. import std.decimal { Decimal }
    2. import std.test
    3. import std.io
    4. function show(result: Result<Decimal, string>) -> () {
    5. let value: Decimal = test.assertOk(result)
    6. io.writeLine("{value.toString()} {value.scale()}")
    7. }
    8. let one: Decimal = (1).toDecimal()
    9. let third: Result<Decimal, string> = one.div((3).toDecimal(), 6)
    10. let eighth: Result<Decimal, string> = one.div((8).toDecimal(), 2)
    11. let negativeTie: Result<Decimal, string> = (-1).toDecimal().div((8).toDecimal(), 2)
    12. let exact: Result<Decimal, string> = (100).toDecimal().div((4).toDecimal(), 4)
    13. let integerGrid: Result<Decimal, string> = (2).toDecimal().div((3).toDecimal(), 0)
    14. show(third)
    15. show(eighth)
    16. show(negativeTie)
    17. show(exact)
    18. show(integerGrid)
    기대 출력종료 코드 0
    0.333333 6
    0.12 2
    -0.12 2
    25 0
    1 0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. 0.333333 6
    2. 0.12 2
    3. -0.12 2
    4. 25 0
    5. 1 0

    Apply every rounding mode to signed quotient ties

    링크
    main.tpzApply every rounding mode to signed quotient ties플레이그라운드 열기 →
    1. import std.decimal { Decimal, RoundingMode }
    2. import std.test
    3. import std.io
    4. function read(text: string) -> Decimal {
    5. let parsed: Option<Decimal> = Decimal.parse(text)
    6. let value: Decimal = test.assertSome(parsed)
    7. return value
    8. }
    9. function quotient(text: string, mode: RoundingMode) -> string {
    10. let value: Decimal = read(text)
    11. let result: Result<Decimal, string> = value.div((2).toDecimal(), 0, mode)
    12. let rounded: Decimal = test.assertOk(result)
    13. return rounded.toString()
    14. }
    15. let modes: Array<RoundingMode> = [RoundingMode.Down(), RoundingMode.Up(), RoundingMode.TowardZero(), RoundingMode.AwayFromZero(), RoundingMode.HalfUp(), RoundingMode.HalfEven()]
    16. for mode in (modes) {
    17. let a = quotient("5", mode)
    18. let b = quotient("7", mode)
    19. let c = quotient("-5", mode)
    20. let d = quotient("-7", mode)
    21. io.writeLine("{a} {b} {c} {d}")
    22. }
    기대 출력종료 코드 0
    2 3 -3 -4
    3 4 -2 -3
    2 3 -2 -3
    3 4 -3 -4
    3 4 -3 -4
    2 4 -2 -4
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

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

    Return a recoverable error for every zero divisor

    링크
    main.tpzReturn a recoverable error for every zero divisor플레이그라운드 열기 →
    1. import std.decimal { Decimal }
    2. import std.test
    3. import std.io
    4. function read(text: string) -> Decimal {
    5. let parsed: Option<Decimal> = Decimal.parse(text)
    6. let value: Decimal = test.assertSome(parsed)
    7. return value
    8. }
    9. let divisor: Decimal = read("-0.000")
    10. let numerators: Array<Decimal> = [(0).toDecimal(), (1).toDecimal()]
    11. for numerator in (numerators) {
    12. let result: Result<Decimal, string> = numerator.div(divisor, 2)
    13. match (result) {
    14. case Err(message) => io.writeLine(message)
    15. case Ok(_) => io.writeLine("accepted")
    16. }
    17. }
    18. io.writeLine(divisor.toString())
    기대 출력종료 코드 0
    Decimal.div: division by zero
    Decimal.div: division by zero
    0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. Decimal.div: division by zero
    2. Decimal.div: division by zero
    3. 0

    Keep a zero quotient at the largest valid scale

    링크
    main.tpzKeep a zero quotient at the largest valid scale플레이그라운드 열기 →
    1. import std.decimal { Decimal }
    2. import std.test
    3. import std.io
    4. function read(text: string) -> Decimal {
    5. let parsed: Option<Decimal> = Decimal.parse(text)
    6. let value: Decimal = test.assertSome(parsed)
    7. return value
    8. }
    9. let divisor: Decimal = read("0.1")
    10. let result: Result<Decimal, string> = (0).toDecimal().div(divisor, 4294967295)
    11. match (result) {
    12. case Ok(value) => io.writeLine(value.toString())
    13. case Err(_) => io.writeLine("unexpected error")
    14. }
    기대 출력종료 코드 0
    0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. 0

    Evaluate the receiver and division arguments once in order

    링크
    main.tpzEvaluate the receiver and division arguments once in order플레이그라운드 열기 →
    1. import std.decimal { Decimal, RoundingMode }
    2. import std.test
    3. import std.io
    4. function receiver() -> Decimal {
    5. io.writeLine("receiver")
    6. return (6).toDecimal()
    7. }
    8. function divisor() -> Decimal {
    9. io.writeLine("divisor")
    10. return (4).toDecimal()
    11. }
    12. function scale() -> int {
    13. io.writeLine("scale")
    14. return 1
    15. }
    16. function mode() -> RoundingMode {
    17. io.writeLine("mode")
    18. return RoundingMode.HalfEven()
    19. }
    20. let result: Result<Decimal, string> = receiver().div(divisor(), scale(), mode())
    21. let value: Decimal = test.assertOk(result)
    22. io.writeLine(value.toString())
    기대 출력종료 코드 0
    receiver
    divisor
    scale
    mode
    1.5
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. receiver
    2. divisor
    3. scale
    4. mode
    5. 1.5

    Reject an invalid scale before classifying a zero divisor

    링크
    main.tpzReject an invalid scale before classifying a zero divisor플레이그라운드 열기 →
    1. import std.decimal { Decimal }
    2. import std.io
    3. let value: Decimal = (1).toDecimal()
    4. value.div((0).toDecimal(), -1)
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Reject an invalid scale before classifying a zero divisor

    링크
    main.tpzReject an invalid scale before classifying a zero divisor플레이그라운드 열기 →
    1. import std.decimal { Decimal }
    2. import std.io
    3. let value: Decimal = (1).toDecimal()
    4. value.div((0).toDecimal(), 4294967296)
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    Diagnostics

    링크

    Runtime value guard →

    메뉴

    다운로드 →