Skip to content

fleschutz/libSI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

800 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMake on multiple platforms

Type-safe Units Library in C++

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

🧱 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

🔎 Example Code

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

💡 Q & A

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

🤝 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

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages