Code Monkey home page Code Monkey logo

unitsystems's Introduction

UnitSystems

A library that provides strongly typed units of measure. Extremely fast, easy to extend and easy to use. Provides on-the-fly units conversion in magnitude and different representations.

No more runtime exceptions when it comes to units calculation and/or conversion.

Samples

Samples below show some of the possibilities that can be achieved by using the library. They are not limited only to presented cases although some of them may need additional rules implemented according to the law of physics.

R# template is available for quick new units creation. It is available under:

Add -> New from Template -> All Templates -> Unit

Basics

Let's start from simple case:

// Implicit or traditional style declaration
Volt volt0;
Volt volt1 = 1.0;
Volt volt2 = new Volt(1);

// Build-in standard comparison operators present in ordinary types such as int
bool equal = volt1 == volt2;                // true
bool notEqual = volt1 != volt2;             // false
bool greaterThan = volt1 > volt2;           // false
bool smallerThan = volt1 < volt2;           // false
bool graterThanOrEqual = volt1 >= volt2;    // true
bool smallerThanOrEqual = volt1 <= volt2;   // true

// Addition and subtraction
Volt sum = volt1 + volt2;   // sum = 2 V
Volt diff = volt1 - volt2;  // diff = 0 V

Conversions

Conversion in magnitude:

Gram g = 1000;                  // 1000 g
Kilogram kg = (Kilogram)g;      // 1000 g = 1 kg

Metre m = 1;                    // 1 m
Millimetre mm = (Millimetre)m;  // 1 m = 1000 mm

Conversion in representation:

Newton N = 1;
Metre m = 1;

Joule joule = N * m;

Advanced

More complicated cases when on-the-fly conversions comes in:

Metre m = 1;
Ampere I = 4;
Ohm R = 10;

// Metre times Metre is Metre Squared: m · m = m²
SquareOf<Metre> metreSquared = m * m;

// Square metre divided by metre is.. metre!
Metre metre = (m * m) / m;

// Watt unit composition: W = (A²)*R
Watt watt = (I * I) * R;

Not explicitly defined unit in SI representation can also be used to define rules for unit conversions:

Coulomb C = 1;
Joule J = 1;
Volt V = 20;

// Farad in base SI = C²/J
QuotientOf<SquareOf<Coulomb>, Joule> faradSi = (C * C) / J;

// Farad times Volt = Coulomb (even if multiplied in base SI representation)
Coulomb coulomb = faradSi * V;

Simple rules between different units can result in complex dependencies:

Newton N = 1;
Metre m = 1;
Volt V = 20;
Ohm R = 10;

// Nested multiple implicit unit resolution
// Wb = J/A | J = N∙m | A = V/R
Weber weber = (N * m) / (V / R);

Real case scenario

Ohm's law:

// Ohm's Law
Volt v = 20;
Ohm r = 10;
Ampere i = 4;

Volt volt = i * r;      // volt = 40 V
Ohm ohm = v / i;        // ohm = 5 Ω
Ampere ampere = v / r;  // ampere = 2 A

Remarks

Lib icon made by Freepik from www.flaticon.com

unitsystems's People

Contributors

michal-mad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.