decimal_gda_checked tutorial
This tutorial shows how to run a General Decimal Arithmetic (GDA) calculation
as a pipeline that honours GDA traps: you choose a GdaContext with the traps
your application needs, start a GdaDecimalChecked, chain operations, detect
the step that trapped, inspect the sticky flags, and decide explicitly whether
to continue. This is the model of Python’s decimal module and of the GDA test
suites: flags flags are sticky, and an enabled trap stops the computation.
The operations come from decimal_gda; the
design page proves the flags and trap laws;
the API reference lists every method.
| I want to | Use |
|---|---|
| Start a pipeline from text or a value | GdaDecimalChecked::to_number, from_decimal |
| Decide which conditions stop the calculation | GdaContext::trap |
| See what the latest step raised and what happened so far | raised(), status() |
| Find the step that trapped | is_err(), error(), result() |
| Continue with the defined result after a trap | resume_defined() |
Run a function that returns no number, or a try_ form | value() and context(), from_outcome |
Quick start
Add the module:
moon add Luna-Flow/floating@0.11.0
Import both packages in your moon.pkg:
import {
"Luna-Flow/floating/decimal_gda",
"Luna-Flow/floating/decimal_gda_checked",
}
Divide under the GDA basic context, which traps division by zero:
///|
test "quick start: a trapped division" {
let ctx = @decimal_gda.GdaContext::basic_default()
let d = fn(v : @decimal_gda.Decimal) {
@decimal_gda_checked.GdaDecimalChecked::from_decimal(v, ctx)
}
let r = @decimal_gda_checked.GdaDecimalChecked::to_number("1", ctx).divide(d(@decimal_gda.Decimal::zero()),
)
inspect(r.is_err(), content="true")
inspect(
r.error() == Some(@decimal_gda.GdaSignal::DivisionByZero),
content="true",
)
}
Everyday tasks
The examples list flags with this helper:
///|
fn gda_flags(f : @decimal_gda.GdaFlags) -> String {
let named = [
("inexact", f.inexact()),
("rounded", f.rounded()),
("invalid_operation", f.invalid_operation()),
("division_by_zero", f.division_by_zero()),
("overflow", f.overflow()),
("conversion_syntax", f.conversion_syntax()),
("invalid_context", f.invalid_context()),
]
[ for p in named if p.1 => p.0 ].join(",")
}
Choose the traps
A GdaContext carries a trap set. The predefined contexts differ:
GdaContext::basic_default() (the GDA basic context: precision 9, HalfUp) traps
DivisionByZero, InvalidOperation, Overflow, Underflow and Clamped;
decimal32(), decimal64(), decimal128() and new(...) trap nothing. Add
or remove traps with trap:
///|
test "trap inexact results" {
let exact_only = @decimal_gda.GdaContext::decimal64().trap(
@decimal_gda.GdaSignal::Inexact,
)
let d = fn(v : @decimal_gda.Decimal) {
@decimal_gda_checked.GdaDecimalChecked::from_decimal(v, exact_only)
}
let ok = @decimal_gda_checked.GdaDecimalChecked::to_number("10", exact_only).divide(d(@decimal_gda.Decimal::from_int(4)),
)
inspect(ok.is_err(), content="false")
inspect(ok.value().to_string(), content="2.5")
let stopped = @decimal_gda_checked.GdaDecimalChecked::to_number("10", exact_only).divide(d(@decimal_gda.Decimal::from_int(3)),
)
inspect(stopped.is_err(), content="true")
inspect(
stopped.error() == Some(@decimal_gda.GdaSignal::Inexact),
content="true",
)
}
Read the sticky flags
raised() holds the signals of the latest operation, status() every signal
since the context was created or its flags was last cleared:
///|
test "flags is sticky" {
let ctx = @decimal_gda.GdaContext::new(precision=5)
let d = fn(v : @decimal_gda.Decimal) {
@decimal_gda_checked.GdaDecimalChecked::from_decimal(v, ctx)
}
let r = @decimal_gda_checked.GdaDecimalChecked::to_number("1.234567", ctx)
.add(d(@decimal_gda.Decimal::one()))
.multiply(d(@decimal_gda.Decimal::from_int(2)))
inspect(r.value().to_string(), content="4.4692")
inspect(gda_flags(r.raised()), content="")
inspect(gda_flags(r.flags()), content="inexact,rounded")
}
Nothing runs after a trap
Once trapped, every further operation returns the pipeline unchanged; the value is the defined result of the trapped step:
///|
test "short circuit after a trap" {
let ctx = @decimal_gda.GdaContext::basic_default()
let d = fn(v : @decimal_gda.Decimal) {
@decimal_gda_checked.GdaDecimalChecked::from_decimal(v, ctx)
}
let r = @decimal_gda_checked.GdaDecimalChecked::to_number("1", ctx)
.divide(d(@decimal_gda.Decimal::zero()))
.add(d(@decimal_gda.Decimal::one()))
.multiply(d(@decimal_gda.Decimal::from_int(5)))
inspect(r.is_err(), content="true")
inspect(r.value().to_string(), content="inf")
}
Resume deliberately
resume_defined() accepts the defined result and continues. The flags still
records the trapped signal, so the decision is visible afterwards:
///|
test "resume with the defined result" {
let ctx = @decimal_gda.GdaContext::basic_default()
let d = fn(v : @decimal_gda.Decimal) {
@decimal_gda_checked.GdaDecimalChecked::from_decimal(v, ctx)
}
let trapped = @decimal_gda_checked.GdaDecimalChecked::to_number("1", ctx).divide(
d(@decimal_gda.Decimal::zero()),
)
let resumed = trapped.resume_defined().minus()
inspect(resumed.is_err(), content="false")
inspect(resumed.value().to_string(), content="-inf")
inspect(gda_flags(resumed.flags()), content="division_by_zero")
}
The traps are still enabled after resuming: dividing by zero again traps again.
Going further
Syntax errors are invalid operations
GDA classifies conversion syntax, impossible and undefined divisions and invalid
contexts as invalid-operation conditions. A context that traps
InvalidOperation therefore stops on a malformed string, and the flags gains
invalid_operation next to the specific condition:
///|
test "a malformed literal" {
let r = @decimal_gda_checked.GdaDecimalChecked::to_number(
"12,5",
@decimal_gda.GdaContext::basic_default(),
)
inspect(r.is_err(), content="true")
inspect(
r.error() == Some(@decimal_gda.GdaSignal::InvalidOperation),
content="true",
)
inspect(gda_flags(r.flags()), content="invalid_operation,conversion_syntax")
}
Mathematical functions need bounded contexts
exp, ln, log10 and power keep decNumber’s restriction (an implementation
limit that decTest requires) to precision and
exponent limits within . The interchange contexts satisfy it;
GdaContext::new with its default unbounded range does not, and the functions
return NaN with invalid_context (which traps under InvalidOperation):
///|
test "exp needs a bounded context" {
let good = @decimal_gda_checked.GdaDecimalChecked::to_number(
"2",
@decimal_gda.GdaContext::decimal64(),
).exp()
inspect(good.value().to_string(), content="7.389056098930650")
let bad = @decimal_gda_checked.GdaDecimalChecked::to_number(
"2",
@decimal_gda.GdaContext::new(precision=16),
).exp()
inspect(bad.value().to_string(), content="nan")
inspect(gda_flags(bad.raised()), content="invalid_context")
}
Combine with the plain GDA functions
Every decimal_gda operation that returns a number has a pipeline method. The
functions that return a string, a boolean or an integer (class_name,
is_normal, is_subnormal, compare_total) and the try_ forms do not: run
them on value() and context(), and wrap the outcome of a try_ form with
from_outcome:
///|
test "use a function that returns no number" {
let ctx = @decimal_gda.GdaContext::decimal64()
let start = @decimal_gda_checked.GdaDecimalChecked::to_number("7.5", ctx)
inspect(@decimal_gda.is_normal(start.value(), start.context()).value(), content="true")
inspect(
@decimal_gda.class_name(start.value(), start.context()).value(),
content="+Normal",
)
let exp = @decimal_gda_checked.GdaDecimalChecked::from_outcome(
@decimal_gda.try_exp(start.value(), start.context()).unwrap(),
)
inspect(exp.is_ok(), content="true")
}
Do this only on a pipeline that is not trapped; from_outcome does not check
the previous state.
Common pitfalls
basic_default()traps,new()does not. Choose the context deliberately.raised()versusstatus(). The first is the latest step, the second the sticky history.resume_definedkeeps the flags and the traps. It clears only the trap marker andraised(). Clear the flags withGdaContext::clear_flagson a context and start a new pipeline if needed.- No errors. Traps are not converted to
ArithmeticError; checkis_err(). - Two decimal packages.
decimal_gda.Decimalis notdecimal.Decimal; IEEE-style flag accumulation without traps isdecimal_checked.
Next steps
decimal_gda_checkeddesign: sticky flags as a monoid, the trap predicate and the short-circuit proof.decimal_gda_checkedAPI: every method.decimal_gdatutorial: contexts, rounding modes and the full GDA operation set.