t27.aiEnglish

Биты, триты и GF16

Вы узнаете

Как число хранится в битах и как сбалансированная троичная система хранит его в тритах.

Железо хранит числа как группы битов фиксированной длины, и t27 заставляет писать ширину: u8 — это 8 беззнаковых битов, i16 — 16 битов со знаком. t27 также говорит на сбалансированной троичной системе, где трит равен -1, 0 или +1. Для дробей есть GF16, 16-битное число с плавающей точкой: 1 бит знака, 6 битов экспоненты и 9 битов мантиссы. Введите число в калькулятор и посмотрите, как оно округляется, поле за полем, и насколько велика ошибка.

Попробуйте

Введите 1.618 и прочитайте биты экспоненты и мантиссы; затем введите 0.1 и посмотрите на ошибку.

Открыть интерактивный урок →

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

Открыть спеку урока в плеере ↗

Все уроки