Code Monkey home page Code Monkey logo

cpp-scientific-units's Introduction

Header-only framework which allows using different units and quantities as types

C++ GitHub license

How to use

Example usage:

static_assert(static_cast<q_J>(1.0_q_J).value == 1.0);
constexpr auto grav_acc = 10.0_q_mps / 1.0_q_s;
constexpr auto height = 10.0_q_m;
constexpr auto mass = 10.0_q_g;
static_assert(static_cast<q_J>(1.0_q_J + 2.0_q_N * 2.5_q_m - grav_acc * height * mass).value == 5.0);

When dealing with prefixes, the first prefix appearing in the expression will be assigned to the resulting type:

auto mass =  10.0_q_g + 1.0_q_kg;
static_assert(std::is_same<decltype(mass), q_g>::value);
//******************************************************
auto mass =  1.0_q_kg + 10.0_q_g;
static_assert(std::is_same<decltype(mass), q_kg>::value);

It is possible to cast between different prefixes, as long as the unit is the same:

static_assert(static_cast<q_g>(q_kg{1.0}).value == 1000.0);

As you note, all types are starting with q_ and all user-defined type literals start with _q_. Conversion between equivalent types happen implicitly.

This code will generate a compile-time error.

q_g grams{10.0};
q_J energy = 2.0 * grams;

Selection_086

How it works

The framework consists of 3 building blocks:

  • metaprogramming
  • type_list
  • quantity

Metaprogramming

Metaprogramming is a set of helper templates which allow performing type manipulations on variadic structs.

Terminology: In this framework, a variadic/Variadic is any type declared as follows:

template<typename...T>
struct ExampleVariadic;

This, using the helper IsVariadic<ExampleVariadic<int, double>>::value will return true. IsVariadic<int>::value will return false.

The usage of the metaprogramming library can be easily understood from the unit tests.

Type lists

Type lists are built from putting all together the metaprogramming helpers into one class. Type lists are the building blocks of quantities.

For example, TypeList<kg, m> could represent a type of "kilogram times meter".

TypeList<s, s> could represent a type of "second square". Thus, a quantity will be represented by two type lists, one for the numerator and one for the denominator.

TypeList<> could represent a numerator or denominator of 1. For example, a quantity of meter could be represented by two type lists, TypeList<m> and TypeList<>, for the numerator and denominator respectively.

The metaprogramming helpers are used to define basic algebraic operations with TypeLists.

cpp-scientific-units's People

Contributors

doruirimescu avatar

Watchers

 avatar  avatar  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.