Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

810 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Lib of Type-safe Units

A units library to apply math with physical units correct, convenient and with high precision.

  • Strong type-safety for datatypes, constants, formulas, and literals (can't add a mass to a length).
  • High precision: fully based on SI base units, 64-bit floating points, and CODATA 2022 constants.
  • Blazing fast due to no runtime overhead, it just compiles to simple doubles.
  • Supports convenient literals (e.g. 24_h) and SI units, Imperial units, Astronomical units, and more.
  • Modern C++ 17 lib: header only, own namespace 'SI', no external dependencies.
  • Cross-platform support for Linux (clang/gcc, x86/arm) and Windows (VS2017-VS2026).

CMake on multiple platforms

🧱 Building Blocks

  1. Datatypes such as SI::length or SI::time in SI/datatypes.h
  2. Units such as SI::meters or SI::seconds in SI/units.h
  3. Literals such as 100_m or 60_s in SI/literals.h
  4. Constants such as SI::constant::speed_of_light in SI/constants.h
  5. Formulas such as SI::formula::wavelength() in SI/formulas.h
  6. Conversions in SI/from_string.h, SI/to_string.h, and in SI/to_equivalent.h
  7. Tests at compile-time in SI/unit_tests.h and continuously by GitHub Actions
  8. Datasets such as chemical elements in 📂datasets

🔎 Quick Example

#include <SI/all.h>
using namespace SI;

int main() {	
    mass m = 1_oz;
    velocity c = constant::speed_of_light;
    energy E = m * c * c;
    println("The potential energy of a single ounce is: ", E);
}

Executing: cmake . && make && ./examples writes to the console:

The potential energy of a single ounce is: 2.55PJ

42 further examples can be found at: examples.cpp, producing this output: console output.

💡 Q & A

What is SI? It's the International System of Units which is made up of 7 base units that define the 22 derived units.

What is CODATA? It's the Committee On Data of the International Science Council (ISC). It publishes fundamental physical constants on a four-year cycle. Latest update was CODATA 2022 which is equal to: NIST SP 961 (May 2024).

What are use-cases for libSI? Applied math such as simulations, simulators, astronomy/physics calculations, games, etc.

What are numbers like 1.2e23? It's the scientific notation in C/C++ for 1.2 x 10²³, the letter 'e' or 'E' represents the 'times 10 to the power of' part.

How to import or export numbers? Import double x = 42; with length dist = x * 1_m; and export with double y = dist / 1_m;.

How to import or export strings? Import with from_string("42m", dist) and export with std::string str = to_string(dist);

Where are the list of references? References are always listed at the end of each source code file.

🤝 Contributing

  • Contributions, suggestions, and improvements are welcome!
  • Open an Issue if you encounter bugs or have feature ideas.
  • Create a Pull Request if you'd like to improve something.

📜 License & Copyright

This open source project is licensed under the CC0-1.0 license. All trademarks are the property of their respective owners.

About

C++ units library to apply math with physical units correct, convenient, and with high precision.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages