Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

wrapping<u64> to int

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

A 64-bit word reduced by a remainder, mask, shift or quotient converts to int exactly.

Syntax: (<wrapping<u64>> % m) ↦ int

Category#

arithmetic

Examples#

radix/corpus/reference/operator-group/wrapping-u64-to-int/modular-word-u64-to-int.fab (canonical · operator-group)#

A 64-bit word reduced by a remainder, mask, shift or quotient converts to int exactly.

# =============================================================================
# wrapping<u64> to int — reduce first, convert the exact result
# =============================================================================
#
# What this teaches:
#   • A 64-bit word past 2^53 is still an exact value: the expression
#     `(h % m)` is computed on the full word, and only the final store into
#     an `int` cell is checked
#   • A remainder, a mask, a right shift and a quotient all bring a large word
#     into `int` range, so each converts without trapping
#
# Common mistakes:
#   • Treating the word as a double before the operator runs
#
# See also: wrapping, ↦
# =============================================================================

main {
    let wrapping<u64> h ← 16294208416658607535
    let int r ← h % 200000001 ↦ int
    print r
    let int low ← (h ∧ 255) ↦ int
    print low
    let int top ← (h ⇒ 60) ↦ int
    print top
    let int q ← h / 4000000000000 ↦ int
    print q
    let int total ← (h % 1000 + h % 7) ↦ int
    print total
}

Expected output:

187565860
175
14
4073552
537