Rounding Mode.Away From Zero()- Module
- std.
decimal - Receiver
- Not applicable
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
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
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1919 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 62 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1919 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973
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.
Related rule: Choose a decimal grid and a rounding directionResult
The declared result type is Rounding.
Effects
Failures
Result and effects
LinkChoose the decimal grid value with larger absolute magnitude. Exact grid values remain unchanged.
Why
LinkIncreasing absolute magnitude expresses a different bound from rounding toward positive infinity.
Resolved contracts
LinkRelated entries
LinkChoose a decimal grid and a rounding direction
LinkChoose 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
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Resolve positive and negative ties in every rounding mode
Linkimport std.decimal { Decimal, RoundingMode }import std.testimport std.iofunction read(text: string) -> Decimal {let parsed: Option<Decimal> = Decimal.parse(text)let value: Decimal = test.assertSome(parsed)return value}function rounded(text: string, mode: RoundingMode) -> string {let value: Decimal = read(text)return value.round(0, mode).toString()}let modes: Array<RoundingMode> = [RoundingMode.Down(), RoundingMode.Up(), RoundingMode.TowardZero(), RoundingMode.AwayFromZero(), RoundingMode.HalfUp(), RoundingMode.HalfEven()]for mode in (modes) {let a = rounded("2.5", mode)let b = rounded("3.5", mode)let c = rounded("-2.5", mode)let d = rounded("-3.5", mode)io.writeLine("{a} {b} {c} {d}")}
2 3 -3 -4 3 4 -2 -3 2 3 -2 -3 3 4 -3 -4 3 4 -3 -4 2 4 -2 -4
Expected result
Expected result
Execution completes. Exit code 0.
2 3 -3 -43 4 -2 -32 3 -2 -33 4 -3 -43 4 -3 -42 4 -2 -4
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Distinguish directed rounding from nearest rounding
Linkimport std.decimal { Decimal, RoundingMode }import std.testimport std.iofunction read(text: string) -> Decimal {let parsed: Option<Decimal> = Decimal.parse(text)let value: Decimal = test.assertSome(parsed)return value}function rounded(text: string, mode: RoundingMode) -> string {let value: Decimal = read(text)return value.round(0, mode).toString()}let modes: Array<RoundingMode> = [RoundingMode.Down(), RoundingMode.Up(), RoundingMode.TowardZero(), RoundingMode.AwayFromZero(), RoundingMode.HalfUp(), RoundingMode.HalfEven()]for mode in (modes) {let a = rounded("2.1", mode)let b = rounded("2.9", mode)let c = rounded("-2.1", mode)let d = rounded("-2.9", mode)io.writeLine("{a} {b} {c} {d}")}
2 2 -3 -3 3 3 -2 -2 2 2 -2 -2 3 3 -3 -3 2 3 -2 -3 2 3 -2 -3
Expected result
Expected result
Execution completes. Exit code 0.
2 2 -3 -33 3 -2 -22 2 -2 -23 3 -3 -32 3 -2 -32 3 -2 -3
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Apply the default mode and normalize a carry or zero
Linkimport std.decimal { Decimal }import std.testimport std.iofunction read(text: string) -> Decimal {let parsed: Option<Decimal> = Decimal.parse(text)let value: Decimal = test.assertSome(parsed)return value}let value: Decimal = read("1.245")io.writeLine(value.round(2).toString())io.writeLine(read("1.255").round(2).toString())io.writeLine(value.round(5).toString())let carry: Decimal = read("9.995").round(2)let zero: Decimal = read("-0.004").round(2)io.writeLine("{carry.toString()} {carry.scale()}")io.writeLine("{zero.toString()} {zero.scale()}")io.writeLine(value.toString())
1.24 1.26 1.245 10 0 0 0 1.245
Expected result
Expected result
Execution completes. Exit code 0.
1.241.261.24510 00 01.245
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Accept the largest scale without adding stored zeros
Linkimport std.decimal { Decimal }import std.testimport std.iolet one: Decimal = (1).toDecimal()let zero: Decimal = (0).toDecimal()let same: Decimal = one.round(4294967295)let sameZero: Decimal = zero.round(4294967295)io.writeLine("{same.toString()} {same.scale()}")io.writeLine("{sameZero.toString()} {sameZero.scale()}")
1 0 0 0
Expected result
Expected result
Execution completes. Exit code 0.
1 00 0
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Reject a rounding scale outside the valid range
Linkimport std.decimal { Decimal }import std.iolet value: Decimal = (1).toDecimal()value.round(-1)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi
Reject a rounding scale outside the valid range
Linkimport std.decimal { Decimal }import std.iolet value: Decimal = (1).toDecimal()value.round(4294967296)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 118 - compiler/crates/topaz_value/src/value/decimal.rs at
693a4cb69c39· 147 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 108 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
693a4cb69c39· 204 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1916 - compiler/crates/topaz_self_frontend/topaz/checker/call_complete.tpz at
b48527d0a174· 1319 -
topaz.stdlib.contract.decimal-roundatworking-tree - topaz.decision.decimal-round.1 at
topaz.decisi