# Kelvin: Typesafe Dimensional Analysis

**URL:** https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198
**Category:** Community Showcase
**Created:** [April 3, 2025, 4:21am UTC](https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198 "2025-04-03T04:21:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)
#### Post date: [April 3, 2025, 4:21am UTC](https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198/1 "2025-04-03T04:21:17Z")

</div>

I’ve managed to apply my learnings from throwing together [`ChronoFlare`](https://forum.modular.com/t/chronoflare-a-type-safe-time-interval-library/1155) and generalize it further to enable a typesafe dimensional analysis library. It turns out you can (ab)use `IntLiteral` to very nicely encode dimensional information into the type system. Allowing you to not only bring type safety to your API, but also represent _arbitrary_ dimensions. Couple that with Mojo’s powerful compile time programming, and users can easily add custom units to their codebase with a single `alias`

```auto
# Quantity.D is the `Dimensions` of the type
alias kg = Kilogram.D
alias m = Meter.D
alias s = Second.D
alias Newton = Quantity[kg * m * (s ** -2)]
print(Newton(100)) # 100.0 m^1 kg^1 s^-2
print(Newton(10) + Newton(30)) # 40.0 m^1 kg^1 s^-2

alias MetersPerSecond = Quantity[m / s]
# Length / Time produces MetersPerSecond
var mps: MetersPerSecond = Meter(20) / Second(10)

```

I am still very much in the process of polishing this to make it better and more useful, but I wanted to share it in case anyone who might have use for it has any feedback, or design input.

Also special thanks to @owenhilyard for pointing me towards [the uom Rust library](https://docs.rs/uom/latest/uom/index.html) as it this is heavily inspired by that work, and @martinvuyk for already taking the time to contribute!

> **[GitHub - bgreni/Kelvin](https://github.com/bgreni/Kelvin)**
>
> Contribute to bgreni/Kelvin development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)
#### Post date: [April 16, 2025, 9:21pm UTC](https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198/2 "2025-04-16T21:21:56Z")

</div>

Quick teaser, with some new experimental changes that introduce the ability to encode the scale of your units into each dimension you can do things like this now.

```mojo
    alias speed_of_light = MetersPerSecond(299792458.0)
    print(MilesPerHour(cast_from=speed_of_light)) # 670616629.3843951 mile^1 h^-1

```

---

<div class="post-metadata">

### Author: ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)
#### Post date: [June 17, 2025, 10:13pm UTC](https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198/3 "2025-06-17T22:13:49Z")

</div>

Kelvin is now officially available from the modular community channel!

> **[Kelvin Package | MAX Builds](https://builds.modular.com/packages/kelvin)**
>
> A showcase of state-of-the-art generative AI models demonstrating the power of MAX.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)
#### Post date: [December 14, 2025, 10:13pm UTC](https://forum.modular.com/t/kelvin-typesafe-dimensional-analysis/1198/4 "2025-12-14T22:13:51Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
