Numbers

Moth's bounded numeric types name exact widths and precisions. Unconstrained whole-number values default to I32, while decimal/exponent values default to F64. These defaults do not impose arithmetic minimums: equal I8, U8 and F16 operands keep their narrow result type.

Numeric literals retain their origin while body-local constraints resolve them. Operators check or round at their selected result type, including every F16 operation. See the Progress matrix for current compiler and backend support: Progress.

Choose the explanation level: Numeric types

Numeric types

Moth's bounded numeric types name their exact width or precision:

  • Signed integers: I8, I16, I32, I64
  • Unsigned integers: U8, U16, U32, U64
  • Binary floats: F16, F32, F64

An unconstrained whole-number value defaults to I32. An unconstrained decimal/exponent value defaults to F64. These defaults do not set a minimum arithmetic width. For example, I8 + I8 stays I8 and can overflow I8.

small I8 = 12
ratio F16 = 0.5

Binary floats preserve signed zero and subnormals. They never hold NaN or infinity. Moth also has Byte, a distinct octet with no arithmetic, and the exact decimal types Dec, Dec0 and Dec1 through Dec256. Dec and Dec0 share the scale-zero identity.

See Numeric types for ranges and type boundaries.

Moth gives every bounded numeric value an exact width or precision. The numeric inventory has eleven fixed types, plus the separate exact Dec family and the non-arithmetic Byte type.

Exact numeric inventory

  • Signed integers: I8, I16, I32, I64
  • Unsigned integers: U8, U16, U32, U64
  • Binary floats: F16, F32, F64

Each integer name fixes its complete value range:

Type

Range

I8

-128 through 127

I16

-32768 through 32767

I32

-2147483648 through 2147483647

I64

-9223372036854775808 through 9223372036854775807

U8

0 through 255

U16

0 through 65535

U32

0 through 4294967295

U64

0 through 18446744073709551615

F16, F32 and F64 use IEEE-754 binary16, binary32 and binary64 precision. Binary floats keep subnormals and signed zero, but Moth values never include NaN or either infinity. A conversion or operation that produces a non-finite result fails at its selected precision.

Bool and Char remain non-numeric types.

Defaults and numeric families

An unconstrained integer-origin value defaults to I32. An unconstrained binary-float-origin value defaults to F64. Moth has no unsigned literal category and no value-size-based type selection. An unconstrained whole literal outside the I32 range produces a diagnostic rather than retrying as I64, U64 or Dec. An independent exact U64 receiver can materialise the full U64 range directly from the retained spelling.

The default pair chooses otherwise unconstrained origins. It does not impose a minimum width on arithmetic. When inference selects I8, U16 or F16, an eligible same-type operation keeps that type and checks or rounds there. See Operators.

The source grammar has no numeric suffixes or radix forms. Whole spelling introduces an integer origin. Decimal-point or exponent spelling introduces a binary-float origin. Delayed inference can choose a width or precision within that family, but it does not turn a bound integer into a float or a bound binary float into Dec. A mixed collection of raw whole and decimal/exponent literals selects the binary-float family regardless of item order. A named integer value does not change family to make such a collection fit.

Receiving compatibility remains exact outside the operator rules. An operator may select a common arithmetic domain, but that does not grant general assignment, parameter or return widening. Fixed integers and fixed binary floats also require an explicit conversion to mix.

Builtin counts and positions

Builtin lengths, capacities and positional indices use I32, including automatically supplied collection and range positions. Their maximum count reaches 2_147_483_647, subject to stricter target or resource limits. A future change follows a language-wide default-integer change, not an independent builder or API choice. Range counters keep their separately selected exact domain. Physical allocation sizes and address calculations follow the compiler's checked storage policy, not source count arithmetic.

Byte

Byte names one octet and remains distinct from U8, I8 and Char.

raw Byte = 255
  • A whole literal from 0 through 255 can initialise Byte; a negative value cannot.
  • Byte supports equality and unsigned-octet ordering with another Byte.
  • Byte has no arithmetic operators, unary minus or range-loop domain.
  • Byte and U8 convert infallibly in both directions. Other numeric conversions compose through U8.
  • {Byte} remains an ordinary collection, not a buffer source type or text encoding.

Dec and DecN

Dec and Dec0 name the same scale-zero arbitrary-precision integer. Dec1 through Dec256 represent coefficient / 10^scale. Scale belongs to the type identity, so Dec2 and Dec4 remain different types. Dec01 and scales above 256 are invalid.

large Dec = 1000000000000000000000
money Dec2 = 12.34
  • Whole literals materialise exactly at any scale.
  • Decimal-point and exponent literals must fit the receiving scale exactly. money Dec2 = 1.239 fails rather than rounding.
  • Integer conversion and scale widening preserve the value exactly. Scale narrowing and Dec-to-integer conversion fail if removed digits are non-zero or the target range cannot hold the value.
  • Dec arithmetic follows its existing scale and per-operation rounding rules. Binary floats and Byte never mix with Dec implicitly.
  • Dec exponentiation takes an I32 exponent. It keeps the exponent separate from the base scale and preserves the existing negative-exponent failure.
  • Canonical text trims fractional trailing zeroes, uses a leading zero before fractions and never uses exponent notation or negative zero.

Dec remains an immutable value, not a source-visible BigInt or an added numeric trait family. See Numeric literals and Operators for receiving and operation details.

MON data boundary

MON remains schema-directed literal data, not a source-body inference engine. Its parser keeps integer and decimal/exponent categories lossless until the schema selects an exact destination. Rust Integer and Decimal data families do not add Moth source types. See MON format.

The Progress matrix owns current compiler and backend support: Progress.

Choose the explanation level: Numeric literals

Numeric literals

Unconstrained whole-number spelling defaults to I32. Decimal-point and exponent spelling defaults to F64:

score = 42
ratio = 0.5
population = 1e6
temperature = -5

The grammar uses lowercase e, has no suffixes or radix forms and rejects unary + and spaced - count. Small values do not infer narrow types.

An exact receiver materialises a literal directly:

small U8 = 200
large U64 = 18_000_000_000
half F16 = 0.5
price Dec2 = 12.50

An exact receiver can also type unresolved literal arithmetic:

total U8 = 1 + 2
money Dec4 = 0.1 + 0.2

The U8 operation checks its own narrow result. A bound value keeps its type: receiving context can type a raw literal beside it, but cannot retype that named value or a completed operation. See Numeric literals for body-local inference, peer priority and barriers.

Literal spelling establishes a numeric origin. Moth keeps that spelling until the permitted receiving and peer constraints have resolved its exact type.

Spelling and defaults

whole = 42
decimal = 0.5
large = 1e6
small = 1e-6
precise = 1.0e+21
  • Whole-number spelling creates an integer origin. An otherwise unconstrained origin defaults to I32.
  • Decimal-point or exponent spelling creates a binary-float origin. An otherwise unconstrained origin defaults to F64.
  • The grammar has no unsigned literal category, numeric suffixes or radix forms.
  • Value size never chooses a width. An unconstrained whole literal outside I32 diagnoses instead of retrying as I64, U64 or Dec.
  • A concrete receiver can materialise a retained whole spelling directly as U64, including values above the I32 range.
  • Decimal or exponent spelling cannot initialise an integer merely because its mathematical value is integral.
  • Exponents use lowercase e. Uppercase E, unary + and a spaced prefix minus such as - count are invalid.
  • A leading - stays attached to its literal or expression. Signed minimum values materialise directly from their negative spelling.

Body-local constraints

Each runtime function body forms an inference boundary, including nested blocks, branches, loops and handlers. The implicit root runtime body follows the same rule. Each binding has one static type across all writes and control-flow paths. Compile-time declarations resolve at their own declaration boundary, so later runtime uses cannot retype a named constant. Each concrete generic instance gets its own body-local inference state.

Value-producing blocks keep their existing placement and explicit-receiver requirements. Inference operates inside legal constructs and does not invent a missing receiver to legalise a different block form.

Numeric constraints can come from exact annotations, assignments, concrete parameters, return slots, fields, typed container entries and eligible value-producing receivers. Copies connect the same numeric unknown. Mutation constrains one binding type rather than creating a new type for each write. A known generic signature can connect an existing numeric unknown across its parameters and result. The compiler uses that signature alone: it does not inspect a callee body or search traits to choose a type.

Every eligible use contributes before defaulting. Source order cannot pick the winning receiver. Conflicting exact receivers produce MOTH-TYPE-0001 with both receiving witnesses in authored order, independent of traversal order. A trait requirement, overload, failure effect, proven value range, backend capability or optimisation never chooses a numeric type.

This bounded process does not add general type inference. Function signatures, fields, public interfaces and named constants remain concrete at their existing declaration boundaries. No pending numeric unknown crosses a module boundary.

Receiving, peer and default priority

Inference applies this priority before materialisation:

eligible receiving context
    -> eligible local numeric peer context
    -> language default

An exact receiver reaches unresolved literal arithmetic and grouping, including raw literal leaves beside named operands. It never retypes a named binding, constant, field, call result or completed operator result.

base I32 = 10
total I64 = base + 1

The receiver types raw 1 as I64, so the mixed fixed-integer operation produces I64. base stays I32. If both operands name I32 values, the operation stays I32 and an I64 receiver rejects it.

Without an explicit receiver, an eligible typed numeric peer can supply its exact type to the opposite unresolved literal-only arithmetic subtree. The peer never changes its own type. A subtree containing a binding, field, call result or cast result is not literal-only. Call arguments use their own parameter receivers.

small U8 = 10
same_width = small + 1

The local U8 peer types the raw 1 as U8, so same_width has type U8. An explicit wider receiver can type the raw leaf wider without changing small:

wider U16 = small + 1

Here 1 receives U16 and ordinary mixed-integer arithmetic produces U16. small remains U8.

Forward arithmetic only across named values

A later consumer can resolve an initializer that still consists only of unresolved literal arithmetic:

value = 1 + 2
take_u64(value) -- Assume this parameter requires U64.

The initializer becomes U64 before operator selection. A completed operation over named values cannot search backwards for a wider operand type:

base = 1
twice = base + base
take_u64(twice) -- Rejected without an independent U64 constraint on base.

base defaults to I32, so twice has type I32. The receiver cannot make either named operand U64. An independent take_u64(base) can resolve base before forward arithmetic. Its position relative to other eligible uses does not change that result.

Copies preserve the same rule. A copied numeric unknown shares its type constraints with the source, while a copy of a completed value keeps that value's concrete type. Mutation also keeps one type across its initializer and all later writes. A write cannot create a new type version or push a constraint back through a completed named arithmetic expression.

Assume take_u64 accepts a U64 parameter:

source = 1
duplicate = copy source
take_u64(duplicate)

count ~= 1
~count += 1
take_u64(count)

The copy connects source and duplicate to one numeric type. Mutation keeps one type across count's initializer, update and later use. Both examples resolve to U64 from the direct parameter receiver; source order does not choose a different type.

Literal families in collections

A non-empty numeric collection can retain one unresolved numeric element type until an exact collection receiver or another eligible use resolves it. Raw whole and decimal/exponent literals select the binary-float family regardless of order:

values = {1, 2.5}
samples = {2.5, 1}

Both default to F64 unless a receiver selects another binary-float precision. A bound integer never changes family to fit:

count = 1
mixed = {count, 2.5} -- Rejected: count remains an integer.

An empty collection creates no numeric unknown and keeps its existing explicit typing requirements. Arithmetic promotion does not search for a common collection storage type.

Destination-aware materialisation

A literal materialises directly at an immediate concrete numeric receiver. The compiler does not first construct a default value and convert it:

small U8 = 200
large U64 = 18_000_000_000
half F16 = 0.5
money Dec2 = 12.34
raw Byte = 255
  • A whole literal can initialise a fixed integer, binary float, Dec or Byte when it fits.
  • A negative value cannot initialise an unsigned type or Byte. Negative unsigned spellings, including -0, are rejected.
  • Integer literals must fit the receiving range. 4_294_967_296 fits U64 but not U32.
  • Decimal or exponent literals must fit a receiving Dec scale exactly. money Dec2 = 1.239 fails rather than rounding.
  • A binary-float literal rounds directly to its destination using nearest, ties-to-even. Inexact values such as 0.1, subnormals and signed zero remain valid, while a non-finite rounded result fails.

Dec literal context

An explicit Dec receiver reaches every unresolved literal leaf in its arithmetic and grouping. Each leaf materialises directly at the selected scale, without passing through a binary float or a formatted intermediate:

whole Dec4 = 1 + 1
fraction Dec4 = 0.1 + 0.2
grouped Dec4 = (1 + 2) * 3

fraction holds exactly 0.3. Parentheses preserve the expression's grouping but do not add a receiving boundary. A Dec peer can supply its scale to an opposite literal-only arithmetic subtree from either side. A concrete Dec value, call result or cast result does not become a raw literal.

Every decimal leaf must fit the selected scale. small Dec2 = 0.005 + 0.005 fails even though the mathematical sum fits because neither leaf can materialise at scale two. Different Dec scales never rescale silently. A completed operation over named operands also keeps its own type rather than changing to satisfy a wider decimal receiver.

Dec context reaches a power base but not its exponent. The exponent uses the separate I32 rule below.

Cast and operator barriers

A cast destination never chooses the source type. Resolve the source from its own eligible constraints or its language default, then check the complete source-target pair:

value = 1
take_u32(cast value)
take_i64(value)

The later I64 parameter resolves value as I64; the U32 cast target does not. The cast then uses the I64-to-U32 policy. A cast also forms a barrier to incoming arithmetic receiver context. An untyped declaration such as converted = cast source remains invalid because no immediate target exists.

Already concrete operator results never change type to satisfy a wider receiver. Comparison and logical results stop numeric context and remain Bool. Member access, propagation and completed catch results also stop later peer context. Explicit cast operands resolve independently.

Dec exponentiation has one separate boundary:

Base

Exponent type

Result

DecN

I32

DecN

An explicit Dec receiver reaches the power base but not the exponent. A raw exponent receives the default I32; an already typed other-width integer needs an explicit cast at a legal receiving site. Negative exponents keep their existing failure behaviour.

MON data boundary

MON literal data shares numeric spelling and materialisation facts but keeps its independent schema-directed parser. Its integer and decimal/exponent categories remain lossless until the schema selects a destination. See MON format.

Choose the explanation level: Operators

Operators

Put spaces around symbolic operators:

total = left + right
~count += 1

Use words for equality and logic:

same = left is right
ready = loaded and valid
retry = timed_out or disconnected
blocked = not ready

/ performs real division and produces F64 for fixed integer operands. // performs integer division in the selected common integer type:

real_result = 5 / 2
whole_result = 5 // 2

Fixed integer operations preserve an equal operand type and use the wider operand when signedness matches. Mixed signedness selects the smallest signed type that holds both complete input ranges:

Operands

Result type

I8 + I8

I8

U8 + U8

U8

I8 + U8

I16

I8 + U16

I32

I64 + U64

invalid

No 32-bit minimum applies. Narrow operations can overflow in their selected result type. Fixed floats also preserve equal precision, so F16 + F16 stays F16 and rounds after that operation. Unary minus preserves signed integer and binary-float types. It rejects every unsigned type, including known zero.

I64 and U64 still compare exactly even though they have no common arithmetic type. Byte has no arithmetic. Dec keeps its scale and per-operation rounding rules; its exponent type is I32.

Compound assignment uses the ordinary arithmetic domain and checks any required conversion back to the target. It evaluates the place and right operand once and writes only after success. A catch on the right-hand expression does not catch later update checks.

Use templates to join text. + adds numbers only. See Operators for the complete domain table and Checked arithmetic for failure delivery.

Moth uses word forms for equality and logic:

same = left is right
different = left is not right
ready = has_input and is_valid
should_retry = timed_out or disconnected
blocked = not ready
  • and and or require Bool operands and short-circuit.
  • not requires one Bool after it. It is prefix-only, so write not ready rather than ready not.
  • Symbolic equality and symbolic logical-not forms are not language operators.

Fixed integer arithmetic

Fixed integer +, -, *, //, % and ^ choose one common operand domain from the complete declared ranges:

  • Equal types keep that type.
  • Different widths with the same signedness use the wider width.
  • Mixed signedness uses the smallest available signed type that contains both complete ranges.
  • No 32-bit minimum applies. Reject a pair when no supported type contains both ranges.

Operands

Result type

I8 + I8

I8

U8 + U8

U8

I8 + I16

I16

U8 + U16

U16

I8 + U8

I16

I16 + U8

I16

I8 + U16

I32

I16 + U16

I32

I32 + U32

I64

I64 + U32

I64

I64 + U64

invalid

I32 + U64

invalid

The common type must contain every input value, not every possible result. U8 + U8 stays U8 and can overflow. U8 - U8 stays U8 and can underflow. Known operand values never select a wider type to avoid failure. The result uses a supported fixed type, never an invented width or an arbitrary-precision fallback. Mixed-domain selection stays symmetric in operand order.

Fixed integer / first requires the same common-integer eligibility as the other binary operators, then converts the operands and divides as F64. Every eligible integer / produces F64, including I8 / I8. A receiving context cannot narrow this completed result.

// truncates towards zero. % follows the dividend's sign. ^ means exponentiation, not XOR. Preserve the established zero-divisor and invalid-exponent failures, including the exponent-zero convention 0 ^ 0 is 1. Signed minimum // -1 overflows its selected integer domain, while its % -1 result remains zero.

Fixed integers compare exactly across all widths and signedness. I64 and U64 need no arithmetic common type to compare. Comparisons use neither a binary-float conversion nor subtraction, so they preserve the order of every value.

Unary minus

Unary - preserves the type of a fixed signed integer or binary float:

Negated operand type

Result type

-I8

I8

-I16

I16

-I32

I32

-I64

I64

-F16

F16

-F32

F32

-F64

F64

Negating a signed integer minimum overflows in that same type. Unary minus on a fixed unsigned integer remains invalid, including a known zero. It does not insert a signed conversion. Floating negation reverses the sign of zero: positive zero becomes negative zero and negative zero becomes positive zero.

Fixed binary-float arithmetic

Fixed binary-float +, -, *, /, % and ^ select the wider operand precision. Equal precisions retain their type; F16 has no minimum-F32 domain:

Operands

Result type

F16 + F16

F16

F16 + F32

F32

F32 + F64

F64

Binary floats do not support //. Each operation completes at its selected semantic precision. F16 rounds after every language-level operation and checks finiteness there. An F32 carrier may implement an F16 operation, but cannot defer the F16 rounding or finite check until a later store. For concrete F16 operands, (2048 + 1) - 2048 produces zero because the first addition rounds to F16.

Binary floats remain finite. Subnormals and signed zero remain valid and positive and negative zero compare equal. Existing operation-specific numerical contracts still apply; this rule adds no new correctly-rounded Core Math promise.

Dec arithmetic

  • Same-scale + and - and unary minus are exact.
  • Multiplication computes the exact product then rounds half-even to the result scale.
  • Positive-scale / checks zero and rounds half-even to the result scale. Scale-zero / is invalid.
  • Scale-zero // truncates towards zero and % gives the integer remainder. Positive-scale // is invalid and % gives a decimal remainder at that scale.
  • DecN ^ I32 takes an I32 exponent. Negative exponents keep their existing failure. Power rounds half-even where the result scale requires it.

Each decimal operation applies its own scale and rounding rule. A later operation cannot defer a required rounding step. Dec and Dec0 share the scale-zero identity.

Fixed integers mix with a Dec operand by exact scaling at the decimal operand's scale, including the full U64 range. Different Dec scales need an explicit conversion. Fixed binary floats and Byte never mix with Dec implicitly. An integer operation never falls back to Dec on overflow.

These examples show why decimal operations round at their own result scale:

chain Dec4 = 1 / 3 * 3 -- 0.9999: division rounds before multiplication
cube Dec2 = 1.07 ^ 3 -- 1.23: power rounds its exact result once
repeated Dec2 = (1.07 * 1.07) * 1.07 -- 1.22: each multiplication rounds

Comparisons

  • Fixed integers compare exactly across all widths and signedness, including I64 with U64.
  • Fixed binary floats compare by numeric value. Positive and negative zero compare equal.
  • A Dec compares with the same-scale Dec type and with fixed integers through the exact scaling rule. Different Dec scales need an explicit conversion.
  • Byte supports equality and unsigned ordering with another Byte.
  • Char supports equality and ordering. Bool supports equality only.
  • String supports is and is not, not ordering.
  • Choices and options have additional equality rules in their own references.

+ adds numbers only. Use templates for string concatenation and interpolation. See Strings and characters.

Compound assignment

+=, -=, *=, /=, //=, %= and ^= use the ordinary operator domain, then perform a checked conversion back when the result type differs from the left-hand type. Same-type updates add no artificial promotion and narrowing pair, but their arithmetic still checks and the store commits only on success.

  • Evaluate the place and right operand once.
  • Write only after arithmetic and any write-back conversion succeed.
  • A catch on the right-hand expression protects that expression only. Later arithmetic and write-back can still fail.
  • Integer /= uses its F64 result and the checked conversion back. Use //= for integer division.

To recover a whole update locally, catch a private helper call or compute and check the replacement before assignment. See Checked arithmetic.

Precedence and associativity

From highest to lowest:

  • level 6: unary not, unary -
  • level 5: ^
  • level 4: *, /, //, %
  • level 3: +, -
  • level 2: is, is not, <, <=, >, >=
  • level 1: and
  • level 0: or

^ associates right. Other binary operators associate left. Parentheses override normal precedence.

Spacing

Symbolic binary operators require spaces on both sides. Assignment = follows the same outer-spacing rule but is not a binary operator.

total = left + right
count = 1

These forms are invalid:

-- INVALID: missing required spacing
total=left+right
count=1

Compound forms follow the same rule:

~count += 1
~count //= 2

~count+=1, ~count +=1 and ~count+= 1 are invalid.

The mutable declaration spelling ~= stays adjacent. It needs whitespace before ~ and after =:

count ~= 1

count~= 1, count ~=1 and count ~ = 1 are invalid.

Operator overloading remains outside Moth's language design. Operators stay compiler-owned and predictable.

Choose the explanation level: Checked arithmetic

Checked arithmetic

Moth checks each numeric operation in its selected result type. Overflow, division by zero and non-finite binary-float results produce recoverable failure rather than wrapping or implicitly panicking.

Narrow same-type operations can fail:

Operands

Selected result type

I8 + I8

I8

U8 + U8

U8

F16 + F16

F16

For example, 127 + 1 overflows when both operands and the result use I8. An F16 operation rounds and checks at F16 precision, even if a wider temporary could hold its intermediate.

A function can return implicit failure through builtin Error!:

divide |left I32, right I32| -> F64, Error!:
    return left / right
;

Private functions can carry implicit failure through internal calls. Custom error slots need local recovery or explicit mapping. Use catch for a local fallback:

total = left * right + offset catch then 0

The handler protects the complete selected expression, including arithmetic and nested call arguments. Authored arithmetic can make a safety catch eligible even when the operation proves infallible. That does not create a failure effect. Known invalid numeric work still diagnoses during compilation.

See Checked arithmetic for result domains, narrow failure witnesses and operand widening before arithmetic.

Runtime numeric operations use their selected semantic result type. A failed check produces implicit built-in failure for the enclosing handler or function contract. Moth does not wrap the value, saturate it or implicitly panic.

Checked results

A common arithmetic type must contain every operand, but need not contain every possible result. Equal narrow operands keep their type, so those operations can overflow or underflow in that narrow domain:

Operands

Selected result type

I8 + I8

I8

U8 + U8

U8

F16 + F16

F16

A wider register or scratch value may compute or validate the result. It cannot expose a wider semantic type, silently wrap or postpone a required check until assignment.

-- INVALID: known overflow in I8
overflow I8 = 127 + 1

This known I8 overflow diagnoses at compile time. Runtime operations use the same selected-width check:

add_i8 |left I8, right I8| -> I8, Error!:
    return left + right
;

value I8 = add_i8(127, 1) catch then 0

Negating a signed minimum also fails in its original narrow type:

-- INVALID: negating the signed minimum
minimum #I8 = -128
negative #I8 = -minimum

negative diagnoses during compilation. A runtime operand uses the same check:

negate_i8 |value I8| -> I8, Error!:
    return -value
;

result I8 = negate_i8(-128) catch then 0

U8 + U8 can overflow at 255 + 1, while U8 - U8 can underflow at 0 - 1. Known values never choose a wider type to avoid either failure. A later cast or compound write-back forms a separate check.

Widen the operands before arithmetic when the operation needs a wider domain. Casting the result cannot widen an operation that has already completed:

add_wide |left I8, right I8| -> I16:
    wide_left I16 = cast left
    wide_right I16 = cast right
    return wide_left + wide_right
;

cast_after |left I8, right I8| -> I16, Error!:
    return cast left + right
;

add_wide(127, 1) produces 128. cast_after(127, 1) fails in the I8 addition before the result conversion can run.

Failures include:

  • fixed-integer overflow or unsigned underflow in the selected result type
  • division or remainder by zero
  • signed minimum // -1 overflow
  • invalid integer exponents
  • a non-finite binary-float result at the selected precision
  • a Dec operation with a zero divisor or invalid negative exponent
  • a compound update whose result cannot convert back to its target type

% -1 for a signed minimum remains zero and does not inherit the // -1 overflow. Integer real division / returns F64 and does not inherit the narrow integer quotient-overflow rule.

Binary-float completion

Each binary-float operation finishes at its selected precision. In particular, F16 rounds and checks finiteness after every language-level operation. F32 may act as an implementation carrier for an F16 operation, but cannot replace its rounding and finite check:

-- INVALID: non-finite result at F16 precision
too_large F16 = 65504.0 + 65504.0

The constant result exceeds finite F16 and diagnoses even though F32 can hold the intermediate. Runtime code must check the same boundary:

double_f16 |value F16| -> F16, Error!:
    return value + value
;

result F16 = double_f16(65504.0) catch then 0.0

F16 completion also rounds between operations. With concrete F16 operands, (2048 + 1) - 2048 produces 0, not 1, because the first addition rounds to F16:

large #F16 = 2048
one #F16 = 1
constant_round #F16 = (large + one) - large

A runtime function must finish the same operations at F16 precision:

round_f16 |large F16| -> F16, Error!:
    one F16 = 1
    return (large + one) - large
;

runtime_round F16 = round_f16(2048) catch then 0

Preserve direct literal and conversion rounding, signed zero and subnormals. Keep operation-specific numerical contracts rather than adding a new correctly-rounded Core Math promise.

Failure delivery

A function with builtin Error! as its error slot materialises escaping implicit failure as Error:

divide |left I32, right I32| -> F64, Error!:
    return left / right
;

ratio = divide(10, 0) catch then 0.0
  • A private function with no error slot carries implicit failure through the existing same-module failure lane.
  • A custom E! slot does not automatically convert implicit failure. Recover locally or map it to E.
  • An exported callable without an error slot must discharge failure that could escape its public contract.
  • Entry-selected top-level runtime code runs in the compiler-owned entry with builtin Error!.

Numeric operators remain ordinary source expressions. See Error returns for function contracts.

Expression recovery

A catch protects the complete selected expression, including arithmetic, receivers, arguments, nested calls and cast operands. Parentheses are optional:

total = left * right + offset catch then 0

Catch eligibility can come from authored arithmetic even when a particular operation cannot fail. That eligibility does not claim a failure effect or create an error producer. See Catch and recovery for eligibility and handler rules.

Implicit failure and builtin Error! may share one handler. A bound error has type Error. A typed Error keeps its message, code and identity, including code 0. A custom E! cannot mix with implicit failure or another typed error.

Catch does not turn a statically invalid operation into runtime recovery. A known overflow, invalid operator pair, out-of-range literal or other known numeric failure remains a compile-time diagnostic.

Compound updates

A compound update checks the revised arithmetic domain, then any required conversion back. The target changes only after both steps succeed. A catch on the right-hand expression protects that expression only; later arithmetic and write-back keep their own failure obligations. Same-type updates have no artificial widening and narrowing pair. /= uses the F64 real-division result and the checked conversion back, while //= remains integer division.

Optimisation

Two conservative proofs remove work, and neither changes source acceptance, side effects, failure order or results. Both behave the same across build profiles and backends.

Failure discharge decides whether an operation can fail at all. It uses only the complete operand and result types, never known values or backend carrier widths. It covers fixed-integer Add, Subtract, Multiply and Negate when every input pair fits the output range. For example, U8 + I8 -> I16 fits every operand pair, but I8 + I8 -> I8 does not. A discharged operation contributes no implicit failure. Discharge never covers binary-float or Dec failure, nor Divide, IntegerDivide, Remainder or Power.

Check elision is an optional backend optimisation over the finished program. It may also use literal values and the results of earlier proven operations within a block, so it can prove a particular integer // or % safe when the divisor range excludes zero and the quotient fits. It only omits a runtime predicate that can never fire, so it never removes a failure path that discharge kept. A wider carrier never proves semantic safety.

An authored catch remains eligible if a proof removes its failure path, but that catch creates no runtime error edge. Folding and runtime execution use the same result types, rounding and failure rules.

Related behaviour

Range candidates are checked against the endpoint before commit, so a counter update never overflows. Loop ordinals keep their checked integer updates. Assertions remain unrecoverable and checked in release. Allocation and stack exhaustion and fatal foreign-value integrity guards stay outside catch.

Conversions that may lose information belong to Casts. Compound write-back remains the receiving-context exception described in Operators.