Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mjendrusch/metric
Dimensionful types for Nim.
https://github.com/mjendrusch/metric
dimensional-analysis nim si-units units-of-measure units-of-measurement
Last synced: 3 months ago
JSON representation
Dimensionful types for Nim.
- Host: GitHub
- URL: https://github.com/mjendrusch/metric
- Owner: mjendrusch
- License: mit
- Created: 2018-02-25T21:46:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-07T13:21:57.000Z (over 6 years ago)
- Last Synced: 2024-05-18T14:33:57.269Z (6 months ago)
- Topics: dimensional-analysis, nim, si-units, units-of-measure, units-of-measurement
- Language: Nim
- Size: 9.77 KB
- Stars: 18
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - metric - A small library providing type-level dimensional analysis. (Science / Embedded)
README
# metric - bringing SI to Nim
[![nimble](https://raw.githubusercontent.com/yglukhov/nimble-tag/master/nimble_js.png)](https://github.com/yglukhov/nimble-tag)
`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
```nim
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:```nim
type
MyDimension = object of BaseDimensionproc `$`(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.