Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.decimal

    RoundingMode.AwayFromZero

    Round inexact values away from zero, including midpoint values.

    RoundingMode.AwayFromZero()
    Receiver
    Not applicable
    Minimum arity
    0
    Maximum arity
    0

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    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.

    Source evidence

    Receiver

    Parameter constraints

    For a requested scale s, rounding selects a multiple of ten raised to negative s. A value already on that grid is unchanged in every rounding mode.

    The requested scale must be an int from 0 through 4294967295 inclusive. A negative value or a value above that range raises TPZ5001 during execution. Scale is never clamped or interpreted as digits to the left of the decimal point.

    When the requested scale is at least the canonical scale, round returns the same value. Otherwise it chooses a grid value according to the supplied mode and normalizes the result.

    RoundingMode.Down() chooses the greatest grid value no greater than the input. It rounds toward negative infinity, including at a midpoint.

    RoundingMode.Up() chooses the least grid value no smaller than the input. It rounds toward positive infinity, including at a midpoint.

    RoundingMode.TowardZero() chooses the grid value with smaller absolute magnitude when the input is between two grid values. RoundingMode.AwayFromZero() chooses the one with larger absolute magnitude. These directions also apply at midpoints.

    RoundingMode.HalfUp() chooses the nearer grid value. At an exact midpoint it chooses the value with greater absolute magnitude for either sign.

    RoundingMode.HalfEven() chooses the nearer grid value. At an exact midpoint it chooses the value whose integer coefficient on the requested grid is even. Determine parity before canonical trailing zeros are removed.

    Omitting the mode selects RoundingMode.HalfEven(). The default is independent of host settings and applies equally to positive and negative values.

    A carry may reduce the canonical scale. A rounded zero has scale zero and no negative sign. Rounding does not pad the result to the requested scale.

    Each RoundingMode spelling constructs a distinct zero payload enum variant and uses parentheses. The modes are immutable values that callers may store and pass to both round and div.

    Result

    The declared result type is RoundingMode.

    Effects

    Failures

    Result and effects

    Link

    Choose the decimal grid value with larger absolute magnitude. Exact grid values remain unchanged.

    Why

    Link

    Increasing absolute magnitude expresses a different bound from rounding toward positive infinity.

    Resolved contracts

    Link

    Related entries

    Link

    std.decimal

    Choose a decimal grid and a rounding direction

    Link
    Ruletopaz.stdlib.contract.decimal-round.rule

    Choose a decimal grid and a rounding direction

    For a requested scale s, rounding selects a multiple of ten raised to negative s. A value already on that grid is unchanged in every rounding mode.

    The requested scale must be an int from 0 through 4294967295 inclusive. A negative value or a value above that range raises TPZ5001 during execution. Scale is never clamped or interpreted as digits to the left of the decimal point.

    When the requested scale is at least the canonical scale, round returns the same value. Otherwise it chooses a grid value according to the supplied mode and normalizes the result.

    RoundingMode.Down() chooses the greatest grid value no greater than the input. It rounds toward negative infinity, including at a midpoint.

    RoundingMode.Up() chooses the least grid value no smaller than the input. It rounds toward positive infinity, including at a midpoint.

    RoundingMode.TowardZero() chooses the grid value with smaller absolute magnitude when the input is between two grid values. RoundingMode.AwayFromZero() chooses the one with larger absolute magnitude. These directions also apply at midpoints.

    RoundingMode.HalfUp() chooses the nearer grid value. At an exact midpoint it chooses the value with greater absolute magnitude for either sign.

    RoundingMode.HalfEven() chooses the nearer grid value. At an exact midpoint it chooses the value whose integer coefficient on the requested grid is even. Determine parity before canonical trailing zeros are removed.

    Omitting the mode selects RoundingMode.HalfEven(). The default is independent of host settings and applies equally to positive and negative values.

    A carry may reduce the canonical scale. A rounded zero has scale zero and no negative sign. Rounding does not pad the result to the requested scale.

    Each RoundingMode spelling constructs a distinct zero payload enum variant and uses parentheses. The modes are immutable values that callers may store and pass to both round and div.

    Why

    A decimal grid states which precision the caller needs. Nearest rounding, an upper or lower bound, and movement toward or away from zero express different numeric intentions. Their results can agree for some inputs without making their meanings interchangeable. HalfEven gives exact ties a stable rule without consistently favoring one magnitude direction.

    Source evidence

    Resolve positive and negative ties in every rounding mode

    Link
    main.tpzResolve positive and negative ties in every rounding modeOpen in Playground
    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 rounded(text: string, mode: RoundingMode) -> string {
    10. let value: Decimal = read(text)
    11. return value.round(0, mode).toString()
    12. }
    13. let modes: Array<RoundingMode> = [RoundingMode.Down(), RoundingMode.Up(), RoundingMode.TowardZero(), RoundingMode.AwayFromZero(), RoundingMode.HalfUp(), RoundingMode.HalfEven()]
    14. for mode in (modes) {
    15. let a = rounded("2.5", mode)
    16. let b = rounded("3.5", mode)
    17. let c = rounded("-2.5", mode)
    18. let d = rounded("-3.5", mode)
    19. io.writeLine("{a} {b} {c} {d}")
    20. }
    Expected outputexit 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 · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    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
    Source evidence

    Distinguish directed rounding from nearest rounding

    Link
    main.tpzDistinguish directed rounding from nearest roundingOpen in Playground
    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 rounded(text: string, mode: RoundingMode) -> string {
    10. let value: Decimal = read(text)
    11. return value.round(0, mode).toString()
    12. }
    13. let modes: Array<RoundingMode> = [RoundingMode.Down(), RoundingMode.Up(), RoundingMode.TowardZero(), RoundingMode.AwayFromZero(), RoundingMode.HalfUp(), RoundingMode.HalfEven()]
    14. for mode in (modes) {
    15. let a = rounded("2.1", mode)
    16. let b = rounded("2.9", mode)
    17. let c = rounded("-2.1", mode)
    18. let d = rounded("-2.9", mode)
    19. io.writeLine("{a} {b} {c} {d}")
    20. }
    Expected outputexit 0
    2 2 -3 -3
    3 3 -2 -2
    2 2 -2 -2
    3 3 -3 -3
    2 3 -2 -3
    2 3 -2 -3
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 2 2 -3 -3
    2. 3 3 -2 -2
    3. 2 2 -2 -2
    4. 3 3 -3 -3
    5. 2 3 -2 -3
    6. 2 3 -2 -3
    Source evidence

    Apply the default mode and normalize a carry or zero

    Link
    main.tpzApply the default mode and normalize a carry or zeroOpen in Playground
    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 value: Decimal = read("1.245")
    10. io.writeLine(value.round(2).toString())
    11. io.writeLine(read("1.255").round(2).toString())
    12. io.writeLine(value.round(5).toString())
    13. let carry: Decimal = read("9.995").round(2)
    14. let zero: Decimal = read("-0.004").round(2)
    15. io.writeLine("{carry.toString()} {carry.scale()}")
    16. io.writeLine("{zero.toString()} {zero.scale()}")
    17. io.writeLine(value.toString())
    Expected outputexit 0
    1.24
    1.26
    1.245
    10 0
    0 0
    1.245
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 1.24
    2. 1.26
    3. 1.245
    4. 10 0
    5. 0 0
    6. 1.245
    Source evidence

    Accept the largest scale without adding stored zeros

    Link
    main.tpzAccept the largest scale without adding stored zerosOpen in Playground
    1. import std.decimal { Decimal }
    2. import std.test
    3. import std.io
    4. let one: Decimal = (1).toDecimal()
    5. let zero: Decimal = (0).toDecimal()
    6. let same: Decimal = one.round(4294967295)
    7. let sameZero: Decimal = zero.round(4294967295)
    8. io.writeLine("{same.toString()} {same.scale()}")
    9. io.writeLine("{sameZero.toString()} {sameZero.scale()}")
    Expected outputexit 0
    1 0
    0 0
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 1 0
    2. 0 0
    Source evidence

    Reject a rounding scale outside the valid range

    Link
    main.tpzReject a rounding scale outside the valid rangeOpen in Playground
    1. import std.decimal { Decimal }
    2. import std.io
    3. let value: Decimal = (1).toDecimal()
    4. value.round(-1)
    5. io.writeLine("after")
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ5001 stdlib-decimal-scale at main.tpz:4:1
    Source evidence

    Reject a rounding scale outside the valid range

    Link
    main.tpzReject a rounding scale outside the valid rangeOpen in Playground
    1. import std.decimal { Decimal }
    2. import std.io
    3. let value: Decimal = (1).toDecimal()
    4. value.round(4294967296)
    5. io.writeLine("after")
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ5001 stdlib-decimal-scale at main.tpz:4:1
    Source evidence

    Diagnostics

    Link

    Runtime value guard

    Menu

    Download