metric

Dimensionful types and dimensional analysis.

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-22 05:28

Installation

nimble install metric
choosenim install metric
git clone https://github.com/mjendrusch/metric

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
metric - - - - - - -

Source

Repository https://github.com/mjendrusch/metric
Homepage https://github.com/mjendrusch/metric
Registry Source nimble_official

README

metric - bringing SI to Nim

nimble

metric is a small library providing type-level dimensional analysis. It allows you to keep track of the physical units of your programs, and can be useful for writing scientific software.

Installation

metric has no non-standard dependencies. Just type nimble install metric and you're good to go.

A small usage example

import metric, strformat

## Let's open a block with a lot of miscellaneous units predefinded:
withUnits:
  var
    v0 = 40.0 * mile / hour # Some non-SI units of velocity.
  echo "v0 in miles per hour: ", fmt"{v0 as mile / hour} [mph]"
  echo "v0 in decimeters per fortnight: ",
    fmt"{v0 as dm / (2.0 * week)} [dm / fortnight]"
  echo "v0 in SI units: ", v0

As you can see, metric makes it easy to handle units, and never have to worry about keeping track of strange conversions.

Custom dimensions

metric provides the means to handle all dimensionful quantities under the SI system. Dimensions taken into consideration are:

  • Length
  • Time
  • Mass
  • Amount
  • Temperature
  • Current
  • Intensity

Should you need different units, you can do that, by just defining them as object of BaseDimension, with the following procs and constants:

type
  MyDimension = object of BaseDimension

proc `$`(x: typedesc[MyDimension]): string = "mydim" ## \
  ## Define how you want your dimension's base unit to be pretty-printed.

const
  myUnitValue = Unit[MyDimension](val: 1.0) ## The name does not matter here.

With those things defined, you are good to use your units in the same way, as the built-ins.