Numbat Documentation
repository·main·Indexed 25 days ago
https://github.com/sharkdp/numbatNumbat 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.
What's inside Numbat
- 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.
Overview of Numbat
mainNumbat 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.Understand Numbat's type system relationship with F#
mainNumbat's type system is heavily inspired by F#. While F# uses physical units on the type level, Numbat uses physical dimensions. Both languages support full type inference.Compare Numbat with other scientific tools
mainNumbat 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:
Feature Numbat Qalculate Kalker GNU Units Frink Wolfram Alpha FOSS License MIT, Apache-2.0 GPL-2.0 MIT GPL-3.0 ❌ ❌ Interfaces CLI, Web CLI, GUI CLI CLI CLI, GUI CLI, Web, GUI Units Comprehensive, Custom, Physical Dimensions, Currency, Date/Time Comprehensive, Custom, Currency, Date/Time Basic Comprehensive, Custom Comprehensive, Currency, Date/Time Comprehensive, Currency, Date/Time Language Custom functions, Real programming language, Strongly typed Custom functions Custom functions N/A Custom functions, Real programming language N/A Calculator Hex/Oct/Bin Hex/Oct/Bin, Symbolic, Complex, Vectors/Matrices Hex/Oct/Bin, Complex, Vectors/Matrices Hex/Oct/Bin Hex/Oct/Bin, Symbolic, Complex, Vectors/Matrices Hex/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.
Understand the Numbat Type System
mainNumbat 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 meterand3 inchboth have the typeLength, making2 meter + 3 inchwell-typed, whereas2 meter + 3 secondis ill-typed because3 secondis of typeTime.Understand the Numbat VS Code Extension structure
mainThe 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.
Understand Numbat core features
mainNumbat 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
LengthandTimeact 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,m²) 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_eqfor 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.nbtfile or writing a custompreludemodule.
- Physical dimensions as types: Dimensions like
Features of the Numbat VS Code extension
mainThe Numbat extension provides the following language support features for the Numbat programming language:
- Syntax highlighting
- Code block folding
- Comment toggling
- Autosurrounding brackets
Perform date and time arithmetic
mainNumbat supports arithmetic operations on
DateTimeobjects.Supported Operations:
DateTime - DateTime: Returns aTime(duration) between the two dates. By default, the result is inseconds.DateTime + Time: Returns a newDateTimeby adding a duration to the date.DateTime - Time: Returns a newDateTimeby 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, oryears(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, usecalendar_addorcalendar_subinstead.Add type annotations to variable definitions
mainYou 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·sPerform unit conversions using the arrow operator
mainYou 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 / gServe the Numbat WASM web application
mainAfter building, you can serve the generated web assets located in the
wwwfolder 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 athttp://0.0.0.0:8000/in your browser.cd www python -m http.server