t27.aiРусский

Bits, trits and GF16

You will learn

How a number is stored in bits, and how balanced ternary stores it in trits.

Hardware stores numbers as fixed groups of bits, and t27 makes you write the width: u8 is 8 unsigned bits, i16 is 16 signed bits. t27 also speaks balanced ternary, where a trit is -1, 0 or +1. For fractions it has GF16, a 16-bit float with 1 sign bit, 6 exponent bits and 9 mantissa bits. Type a number into the calculator and watch how it is rounded, field by field, and how big the error is.

Try it

Type 1.618 and read the exponent and mantissa bits; then type 0.1 and look at the error.

Open the interactive lesson →

GF16 calculator: a number as 16 GoldenFloat bits and as ternary
GF16 calculator: a number as 16 GoldenFloat bits and as ternary ↗

Type a number and see its GF16 sign, exponent and mantissa bit by bit, the exact value it rounds to, the error, and the same number in balanced ternary.

specs/tutorial/01_values_and_types.t27

// SPDX-License-Identifier: Apache-2.0
; 01 — Values and types
; Lesson 1 of the t27 tutorial. Every constant carries an explicit width,
; because a spec has to say what reaches hardware rather than let a compiler
; pick for it. Nothing here is inferred.
; phi^2 + 1/phi^2 = 3 | TRINITY

module tutorial-01-values;

// ============================================================================
// Integers
//
// Width and signedness are part of the name's type, always written out.
// u = unsigned, i = signed, and the number is the bit width.
// ============================================================================

pub const SMALL_UNSIGNED : u8 = 200;
pub const SMALL_SIGNED : i8 = -100;
pub const WIDE_UNSIGNED : u16 = 65535;
pub const WIDER : u32 = 4000000000;
pub const SIGNED_32 : i32 = -2000000;

// ============================================================================
// Other bases
//
// Hex and binary are ordinary integer literals. Binary is worth using wherever
// the value IS a bit pattern -- a mask written as 0b111111111 documents its own
// width in a way that 511 does not.
// ============================================================================

pub const AS_HEX : u16 = 0xFFFF;
pub const AS_BINARY : u8 = 0b1010;
pub const MANTISSA_MASK : u16 = 0b111111111;

// ============================================================================
// Floats and booleans
// ============================================================================

pub const RATIO : f32 = 1.5;
pub const PRECISE : f64 = 2.25;

pub const ENABLED : bool = true;
pub const DISABLED : bool = false;

// ============================================================================
// The trit
//
// This language is built on three states rather than two. A trit is stored in
// an i8 and is only ever -1, 0 or +1; the invariant at the bottom is what
// makes that a claim rather than a comment.
// ============================================================================

pub const TRIT_NEG : i8 = -1;
pub const TRIT_ZERO : i8 = 0;
pub const TRIT_POS : i8 = 1;

pub const TRINITY : i8 = 3;

// ============================================================================
// Invariants
//
// An invariant states something that must hold for the module as a whole. It
// is checked, not decorative.
// ============================================================================

invariant trit_range_is_three_states
    assert TRIT_POS - TRIT_NEG + 1 == TRINITY

invariant masks_match_their_widths
    assert MANTISSA_MASK == 511

Open the lesson's spec in the player ↗

All lessons