Units library to apply math with physical units correct, convenient and with high precision. This lib provides:
- 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).
- Datatypes such as SI::length or SI::time in SI/datatypes.h
- Units such as SI::meters or SI::seconds in SI/units.h
- Literals such as 100_m or 60_s in SI/literals.h
- Constants such as SI::constant::speed_of_light in SI/constants.h
- Formulas such as SI::formula::wavelength() in SI/formulas.h
- Conversions in SI/from_string.h, SI/to_string.h, and in SI/to_equivalent.h
- Tests at compile-time in SI/unit_tests.h and continuously by GitHub Actions
- Datasets such as chemical elements in 📂datasets
#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
Please find 42 more examples in: examples.cpp and the corresponding console output.
(click to expand)
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.- 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.
This open source project is licensed under the CC0-1.0 license. All trademarks are the property of their respective owners.