Numbat Documentation

repository·main·Indexed 25 days ago

https://github.com/sharkdp/numbat

Numbat is a statically typed programming language designed for scientific computations, featuring first-class support for physical dimensions and units. It provides dimensional analysis via a static type system, a comprehensive standard library of SI and other unit systems, an interactive REPL, and a dedicated VS Code extension for syntax highlighting and language support.

Tokens
38.8K
Snippets
149
Records
266
Agent score
80%

What's inside Numbat

  1. Overview of Numbat programming language

    main
    Numbat is a statically typed programming language designed for scientific computations. Its primary feature is first-class support for physical dimensions and units, allowing for complex mathematical and unit-based calculations.
  2. Overview of Numbat

    main
    Numbat is a statically typed programming language designed specifically for scientific computations. Its primary feature is first-class support for physical units, allowing developers to perform complex scientific calculations with built-in dimensional analysis.
  3. Compare Numbat with other scientific tools

    main

    Numbat is a functional/declarative, strongly typed programming language designed for scientific calculations. Use the following comparison to determine if Numbat fits your needs compared to other tools:

    FeatureNumbatQalculateKalkerGNU UnitsFrinkWolfram Alpha
    FOSS LicenseMIT, Apache-2.0GPL-2.0MITGPL-3.0
    InterfacesCLI, WebCLI, GUICLICLICLI, GUICLI, Web, GUI
    UnitsComprehensive, Custom, Physical Dimensions, Currency, Date/TimeComprehensive, Custom, Currency, Date/TimeBasicComprehensive, CustomComprehensive, Currency, Date/TimeComprehensive, Currency, Date/Time
    LanguageCustom functions, Real programming language, Strongly typedCustom functionsCustom functionsN/ACustom functions, Real programming languageN/A
    CalculatorHex/Oct/BinHex/Oct/Bin, Symbolic, Complex, Vectors/MatricesHex/Oct/Bin, Complex, Vectors/MatricesHex/Oct/BinHex/Oct/Bin, Symbolic, Complex, Vectors/MatricesHex/Oct/Bin, Symbolic, Complex, Vectors/Matrices

    Key Differentiators:

    • Numbat vs Qalculate: Qalculate is more feature-complete if you do not require a full programming language.
    • Numbat vs Frink: Frink is an imperative/OOP language and is more powerful, but lacks Numbat's static type system. Frink is not open-source.
    • Numbat vs GNU Units: GNU Units has more pre-defined units, but Numbat offers easier custom unit definitions.
    • Numbat vs Wolfram Alpha: Wolfram Alpha is optimized for single-line queries rather than long computations and lacks a strict syntax.
  4. Understand the Numbat Type System

    main
    Numbat uses a static type system where physical dimensions are treated as types. This ensures dimensional correctness (and thus unit correctness) before the program executes. For example, 2 meter and 3 inch both have the type Length, making 2 meter + 3 inch well-typed, whereas 2 meter + 3 second is ill-typed because 3 second is of type Time.
  5. Understand the Numbat VS Code Extension structure

    main

    The extension directory contains the following key files for defining language support:

    • package.json: The manifest file where you declare language support and define the location of the grammar file.
    • syntaxes/numbat.tmLanguage.json: The TextMate grammar file used for syntax tokenization.
    • language-configuration.json: Defines language-specific settings such as comment tokens and bracket matching.
  6. Understand Numbat core features

    main

    Numbat is a statically typed programming language designed for scientific computations with first-class support for physical dimensions and units.

    Key Capabilities:

    • Physical dimensions as types: Dimensions like Length and Time act as static types. Types can be explicitly annotated or inferred.
    • First-class physical units: Units are treated as primary citizens. They support various notations (e.g., km/h, GiB, ) and can be converted using the -> operator (e.g., 30 km/h -> mph).
    • Comprehensive standard library: Includes a wide range of SI, US Customary, Imperial, Nautical, Astronomical, Atomic, and Nuclear units, along with mathematical and physical constants.
    • Interactive REPL: An interactive terminal with readline features like command history, Ctrl-R search, and tab completion.
    • Assertions: Use assert_eq for exact or approximate equality checks at runtime (e.g., assert_eq(c, 300_000 km/s, 1% × c)).
    • Extensibility: The unit system is written in Numbat itself and can be customized by providing an init.nbt file or writing a custom prelude module.
  7. Perform date and time arithmetic

    main

    Numbat supports arithmetic operations on DateTime objects.

    Supported Operations:

    • DateTime - DateTime: Returns a Time (duration) between the two dates. By default, the result is in seconds.
    • DateTime + Time: Returns a new DateTime by adding a duration to the date.
    • DateTime - Time: Returns a new DateTime by subtracting a duration from the date.
    • DateTime -> tz("..."): Converts the datetime to the specified time zone.

    Important Note on Calendar Arithmetic: Directly adding/subtracting days, months, or years (e.g., now() + 3 months) uses fixed definitions (e.g., 1 day = 24 hours) which may not account for Daylight Saving Time or varying month lengths. For accurate calendar-aware arithmetic, use calendar_add or calendar_sub instead.

  8. Add type annotations to variable definitions

    main

    You can add type annotations to variable definitions using the syntax let <identifier>: <Type> = <value>. Type annotations are verified by the Numbat type checker. Using them can improve code readability and help catch errors early in complex definitions.

    let Δp: Pressure = 0.1 bar
    let μ_water: DynamicViscosity = 1 mPa·s
  9. Perform unit conversions using the arrow operator

    main

    You can convert the result of an expression to a specific unit by appending -> followed by the target unit at the end of the expression. This also helps Numbat validate that the calculation is dimensionally correct.

    let radioactivity = N_A * occurrence * decay_rate / molar_mass -> Bq / g
  10. Serve the Numbat WASM web application

    main

    After building, you can serve the generated web assets located in the www folder using a local web server. Using Python's built-in HTTP server is a quick way to do this. Once the server is running, access the application at http://0.0.0.0:8000/ in your browser.

    cd www
    python -m http.server