{"id":17977644,"url":"https://github.com/martinmoene/physunits-ct-cpp11","last_synced_at":"2025-08-31T04:06:39.000Z","repository":{"id":10484471,"uuid":"12663713","full_name":"martinmoene/PhysUnits-CT-Cpp11","owner":"martinmoene","description":"A small C++11, C++14 header-only library for compile-time dimensional analysis and unit/quantity manipulation and conversion","archived":false,"fork":false,"pushed_at":"2023-02-09T11:52:17.000Z","size":148,"stargazers_count":81,"open_issues_count":3,"forks_count":16,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-07-03T07:54:35.032Z","etag":null,"topics":["compile-time","cpp11","cpp14","dimensional-analysis","header-only","no-dependencies","single-file","unit-conversion","units-of-measure"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martinmoene.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE_1_0.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2013-09-07T11:13:08.000Z","updated_at":"2025-01-04T18:05:03.000Z","dependencies_parsed_at":"2023-01-11T20:14:38.931Z","dependency_job_id":"60d52b96-2a79-401e-bb06-e278e5f441a0","html_url":"https://github.com/martinmoene/PhysUnits-CT-Cpp11","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/martinmoene/PhysUnits-CT-Cpp11","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2FPhysUnits-CT-Cpp11","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2FPhysUnits-CT-Cpp11/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2FPhysUnits-CT-Cpp11/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2FPhysUnits-CT-Cpp11/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martinmoene","download_url":"https://codeload.github.com/martinmoene/PhysUnits-CT-Cpp11/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2FPhysUnits-CT-Cpp11/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272936420,"owners_count":25018160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compile-time","cpp11","cpp14","dimensional-analysis","header-only","no-dependencies","single-file","unit-conversion","units-of-measure"],"created_at":"2024-10-29T17:28:54.867Z","updated_at":"2025-08-31T04:06:38.969Z","avatar_url":"https://github.com/martinmoene.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"PhysUnits C++11 (compile-time)\n==============================\n\n[![Language](https://img.shields.io/badge/language-C++-blue.svg)](https://isocpp.org/) [![Standard](https://img.shields.io/badge/c%2B%2B-11-blue.svg)](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) [![License](https://img.shields.io/badge/license-BSL-blue.svg)](https://opensource.org/licenses/BSL-1.0) [![Build Status master](https://img.shields.io/travis/martinmoene/PhysUnits-CT-Cpp11/master.svg?label=master)](https://travis-ci.org/martinmoene/PhysUnits-CT-Cpp11/branches) [![Version](https://badge.fury.io/gh/martinmoene%2FPhysUnits-CT-Cpp11.svg)](https://github.com/martinmoene/PhysUnits-CT-Cpp11/releases) [![download](https://img.shields.io/badge/latest%20version%20%20-download-blue.svg)](https://github.com/martinmoene/PhysUnits-CT-Cpp11/archive/master.zip) [![hunter](https://img.shields.io/badge/hunter-PhysUnits-blue.svg)](https://docs.hunter.sh/en/latest/packages/pkg/PhysUnits.html)\n\nA small C++11 header-only library for compile-time dimensional analysis and unit/quantity manipulation and conversion.\n\nThis library is based on the quantity compile-time library by Michael S. Kenniston[1] and expanded and adapted for C++11 by Martin Moene.\n\n**Contents**\n\n- [Hello quantity](#hello-quantity)\n- [Other libraries](#other-libraries)\n- [Dependencies](#dependencies)\n- [Limitations](#limitations)\n- [Error handling](#error-handling)\n- [Definition of terms](#definition-of-terms)\n- [Dimensions, units and literals](#dimensions-units-and-literals)\n- [Include files](#include-files)\n- [Configuration](#configuration)  \n- [Types and declarations](#types-and-declarations)\n- [Operations and expressions](#operations-and-expressions)\n- [Convenience functions](#convenience-functions)\n- [Output variations](#output-variations)\n- [Reported to work with](#reported-to-work-with)\n- [Performance](#performance)\n- [Ideas for improvement](#ideas-for-improvement)\n- [References](#references)\n\nHello quantity\n--------------\n\n```C++\n#include \"phys/units/quantity.hpp\"\n\nusing namespace phys::units;\nusing namespace phys::units::literals;\n\nint main()\n{\n    quantity\u003cspeed_d\u003e speed = 45_km / hour;\n}\n```\n\nOther libraries\n---------------\n\n- Search GitHub for [unit dimension language:C++](https://github.com/search?utf8=%E2%9C%93\u0026q=unit+dimension+language%3AC%2B%2B\u0026type=Repositories).\n- [PhysUnits-CT](https://github.com/martinmoene/PhysUnits-CT) - C++98 companion of this library.\n- [PhysUnits-RT](https://github.com/martinmoene/PhysUnits-RT) - C++98 Run-time companion of this library.\n- [Au](https://github.com/aurora-opensource/au) - A C++14-compatible physical units library with no dependencies and a single-file delivery option. Emphasis on safety, accessibility, performance, and developer experience. Aurora Open Source.\n- [Boost.Units](http://www.boost.org/doc/libs/1_51_0/libs/units/) - Zero-overhead dimensional analysis and unit/quantity manipulation and conversion in C++.\n- [SI](https://github.com/bernedom/SI) - A header only C++ library that provides type safety and user defined literal. Dominik Berner.\n- [PQS](https://github.com/kwikius/pqs) - A C++ library for modelling physical quantities. kwikius.\n- [quan](https://github.com/kwikius/quan-trunk) - C++ library for physical quantities and much more. kwikius.\n- [Quantities](http://www.echem.uni-tuebingen.de/index.shtml/Quantities/quantities.html) - C++ objects with dimensionality and units attached in a way similar to the built-in types float or double. Universität Tübingen.\n- [unlib](https://github.com/gitsbi/unlib) - A light-weight, header-only, dependency-free, C++14 library for ISO units. sbi.\n- [units](https://github.com/mpusz/units) - A Physical Units Library for C++ providing compile-time dimensional analysis and unit/quantity manipulation. Mateusz Pusz.\n- [units](https://github.com/nholthaus/units) - A compile-time, header-only, dimensional analysis and unit conversion library built on C++14 with no dependencies. Nic Holthaus.\n- [units](https://github.com/tonypilz/units) - A lightweight compile-time, header-only, dimensional analysis and unit conversion library built on C++11 with no dependencies. Tony Pilz.\n- [units](https://github.com/legento/units) - C++ compile time dimensional analysis. Oliver Esser.  \n- [Units](https://github.com/VincentDucharme/Units) - C++ Library for managing values with units. Vincent Ducharme.\n- [DimensionalAnalysis\n](https://github.com/njoy/DimensionalAnalysis) - A compact C++ header-only library providing compile-time dimensional analysis and unit awareness. Austin McCartney.\n- [units_literals\n](https://github.com/acecil/units_literals) - User defined literals for Boost.Units.\n- [unitscpp](http://code.google.com/p/unitscpp/) - A lightweight C++ library for physical calculation with units.\n- Python packages [Numericalunits](http://pypi.python.org/pypi/numericalunits), [Pint](http://pypi.python.org/pypi/Pint) and [Units](http://pypi.python.org/pypi/units), mentioned in [4].\n\nDependencies\n--------------\n\nThis library has no dependencies other than the standard C++ library.\n\nLimitations\n-----------\n\nThis library only supports the use of the \u003cem\u003eSI unit system\u003c/em\u003e.\n\nThis library only supports \u003cem\u003eintegral powers\u003c/em\u003e of the dimensions.\n\nError handling\n--------------\n\nError handling with respect to mixing incompatible dimensions occurs at compile-time.\n\nDefinition of terms\n-------------------\n\nAdapted from Boost.Units:\n\n- \u003cb\u003eBase dimension\u003c/b\u003e: A base dimension is loosely defined as a measurable entity of interest; in conventional dimensional analysis, base dimensions include length ([L]), mass ([M]), time ([T]), etc.. Base dimensions are essentially a tag type and provide no dimensional analysis functionality themselves.\n- \u003cb\u003eDimension\u003c/b\u003e: A collection of zero or more base dimensions, each potentially raised to a different rational power. For example, length = [L]^1, area = [L]^2, velocity = [L]^1/[T]^1, and energy = [M]^1 [L]^2/[T]^2 are all dimensions.\n- \u003cb\u003eBase unit\u003c/b\u003e: A base unit represents a specific measure of a dimension. For example, while length is an abstract measure of distance, the meter is a concrete base unit of distance. Conversions are defined using base units. Much like base dimensions, base units are a tag type used solely to define units and do not support dimensional analysis algebra.\n- \u003cb\u003eUnit\u003c/b\u003e: A set of base units raised to rational exponents, e.g. m^1, kg^1, m^1/s^2.\n- \u003cb\u003eSystem\u003c/b\u003e: A unit system is a collection of base units representing all the measurable entities of interest for a specific problem. For example, the SI unit system defines seven base units : length ([L]) in meters, mass ([M]) in kilograms, time ([T]) in seconds, current ([I]) in amperes, temperature ([theta]) in kelvin, amount ([N]) in moles, and luminous intensity ([J]) in candelas. All measurable entities within the SI system can be represented as products of various integer or rational powers of these seven base units.\n- \u003cb\u003eQuantity\u003c/b\u003e: A quantity represents a concrete amount of a unit. Thus, while the meter is the base unit of length in the SI system, 5.5 meters is a quantity of length in that system.\n\nDimensions, units and literals\n------------------------------\n\nThe seven fundamental SI [2] *dimensions* are length, mass, time interval, electric current, thermodynamic temperature, quantity of substance and luminous intensity. The base *units* are meter (m), kilogram (kg), second (s), ampere (A), mole (mol) and candela (cd).\n\nThe library defines dimensions such as `length_d` and `mass_d` and it defines quantities that represent their units as `meter` and `kilogram`. The library also defines [user-defined literal suffixes](https://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals) with prefixes ranging from yocto (1e-24L) through yotta (1e+24). Thus you can write quantity literals such as `1_ns` and ` 42.195_km`.\n\nTo use literals of non-base units, include the file `io.hpp` or `io_symbols.hpp`, or one or more of the following files named quantity_io_ *unit* .hpp where *unit* is becquerel, celsius, coulomb, farad, gray, henry, hertz, joule, lumen, lux, newton, ohm, pascal, radian, siemens, sievert, speed, steradian, tesla, volt, watt, weber.\n\nInclude files\n-------------\n\n- io.hpp - include all io-related include files.\n- io_output.hpp - provide basic stream output in base dimensions.\n- io_output_eng.hpp - provide stream output in [engineering notation](http://en.wikipedia.org/wiki/Engineering_notation), using [metric prefixes](http://en.wikipedia.org/wiki/Metric_prefix).\n- io_symbols.hpp - include all files quantity_io_ *unit* .hpp\n- other_units.hpp - units that are *not* approved for use with SI.\n- physical_constants.hpp - Planck constant, speed of light etc.\n- quantity.hpp - quantity, SI dimensions and units, base unit literals.\n- quantity_io_ *unit* .hpp - name, symbol and literals for *unit*.\n\nConfiguration\n-------------\n\n-D\u003cb\u003ePHYS_UNITS_REP_TYPE\u003c/b\u003e=double  \nDefine this to the \u003cem\u003erepresentation or value type\u003c/em\u003e for the magnitude of quantity. Default is `double`.\nYou can change the type for all uses within a translation unit by defining `PHYS_UNITS_REP_TYPE` before inclusion of header quantity.hpp.\n\n-D\u003cb\u003ePHYS_UNITS_COLLAPSE_TO_REP\u003c/b\u003e=1  \nThe library can collapse dimensionless results to the representation type or continue with type `quantity\u003cdimensionless_d\u003e`. Define `PHYS_UNITS_COLLAPSE_TO_REP` to 0 to allow dimensionless quantities. Default is 1.\n\nTypes and declarations\n----------------------\n\n```C++\n#include \"phys/units/quantity.hpp\"\n\nusing namespace phys::units;\n\nquantity\u003cmass_d\u003e q_rep;           // magnitude has type Rep (PHYS_UNITS_REP_TYPE)\nquantity\u003cmass_d, float\u003e q_float;  // magnitude has type float\n```\n\nThe default \u003cem\u003erepresentation or value type\u003c/em\u003e `Rep` for the magnitude of quantity is `double`. You can change the type for all uses within a translation unit by defining `PHYS_UNITS_REP_TYPE` before inclusion of header quantity.hpp.\n\nOperations and expressions\n--------------------------\n\n- `N` is an integer constant\n- `num` is an int, long, float, double, etc.\n- `quantity1` and `quantity2` have different dimensions\n- quantities with different magnitude types can be mixed\n\n|Operation     |Operand Type(s)            |Result Type |\n|--------------|---------------------------|------------|\n|Construction  |`quantity()`               |`quantity`  |\n|              |`quantity( quantity )`     |`quantity`  |\n|Assignment    |`quantity = quantity`      |`quantity \u0026`|\n|Addition \u0026    |`quantity += quantity`     |`quantity \u0026`|\n| Subtraction  |`quantity -= quantity`     |`quantity \u0026`|\n|              |`+quantity`                |`quantity`  |\n|              |`-quantity`                |`quantity`  |\n|              |`quantity + quantity`      |`quantity`  |\n|              |`quantity - quantity`      |`quantity`  |\n|Multiplication|`quantity *= num`          |`quantity \u0026`|\n|              |`quantity * num`           |`quantity`  |\n|              |`num * quantity`           |`quantity`  |\n|              |`quantity1 * quantity1`    |`quantity2` |\n|              |`quantity1 * quantity2`    |`num` or `quantity3` |\n|Division      |`quantity /= num`          |`quantity \u0026`|\n|              |`quantity / num`           |`quantity`  |\n|              |`num / quantity1`          |`quantity2` |\n|              |`quantity / quantity`      |`num`       |\n|              |`quantity1 / quantity2`    |`quantity3` |\n|Powers        |`nth_power\u003cN\u003e( quantity1 )`|`num` if N=0, `quantity1` if  N=1, `quantity2` otherwise|\n|              |`square( quantity1 )`      |`quantity2` |\n|              |`cube( quantity1 )`        |`quantity2` |\n|Roots         |`nth_root\u003cN\u003e( quantity1 )` |`quantity2`, iff dimensions of quantity1 are all even multiples of N|\n|              |`sqrt( quantity1 )`        |`quantity2`, iff dimensions of quantity1 are all even multiples of 2|\n|Conversion    |`quantity1.to( quantity2 )`|`num` or `quantity3` (quantity1/quantity2)|\n|Zero          |`quantity.zero()`          |`quantity` with magnitude 0|\n\nConvenience functions\n---------------------\n\nThe following convenience functions are provided.\n\nIn namespace `phys::units`:\n\n- `DX dimension( quantity\u003cDX, ...\u003e const \u0026 q )` - the quantity's dimension.\n- `X magnitude( quantity\u003c..., X\u003e const \u0026 q )` - the quantity's magnitude.\n- `std::string to_magnitude( quantity\u003c...\u003e const \u0026 q )` - the quantity's magnitude represented as string.\n- `std::string to_unit_name( quantity\u003c...\u003e const \u0026 q )` - the quantity's unit name, e.g. 'hertz'.\n- `std::string to_unit_symbol( quantity\u003c...\u003e const \u0026 q )` - the quantity's unit symbol, e.g. 'Hz'.\n- `std::string to_string( long double const value )` - the value of a long double represented as string.\n\nIn namespace `phys::units::io`:\n\n- `std::string to_string( quantity\u003c...\u003e const \u0026 q )` - the quantity represented as string in scientific notation.\n- `std::ostream \u0026 operator\u003c\u003c( std::ostream \u0026 os, quantity\u003c...\u003e const \u0026 q )` - output the quantity to a stream in scientific notation.\n\nIn namespace `phys::units::io::eng`:\n\n- `std::string to_string( quantity\u003c...\u003e const \u0026 q )` - the quantity represented as string in engineering notation.\n- `std::ostream \u0026 operator\u003c\u003c( std::ostream \u0026 os, quantity\u003c...\u003e const \u0026 q )` - output the quantity to a stream in engineering notation.\n\nOutput variations\n-----------------\n\nThe following example shows the quantity type in the computation of work from force and distance and the printing of the result on standard output.\n\n```C++\n#include \u003ciostream\u003e\n\n#include \"phys/units/io.hpp\"\n#include \"phys/units/quantity.hpp\"\n\nusing namespace phys::units;\nusing namespace phys::units::io;\nusing namespace phys::units::literals;\n\nquantity\u003cenergy_d\u003e\nwork( const quantity\u003cforce_d\u003e \u0026 F, const quantity\u003clength_d\u003e \u0026 dx )\n{\n    return F * dx; // Defines the relation: work = force * distance.\n}\n\nint main()\n{\n    // Test calculation of work.\n    quantity\u003cforce_d\u003e       F { 2_N           };  // Define a quantity of force.\n    quantity\u003clength_d\u003e      dx{ 2_m           };  // and a distance,\n    quantity\u003cenergy_d\u003e      E { work( F, dx ) };  // and calculate the work done.\n\n    std::cout \u003c\u003c \"F  = \" \u003c\u003c F  \u003c\u003c std::endl\n              \u003c\u003c \"dx = \" \u003c\u003c dx \u003c\u003c std::endl\n              \u003c\u003c \"E  = \" \u003c\u003c E  \u003c\u003c std::endl;\n}\n```\n\nThe output produced is:\n\n```Text\nF  = 2 N\ndx = 2 m\nE  = 4 J\n```\n\nThe following example demonstrates printing in default floating point notation and in [engineering notation](http://en.wikipedia.org/wiki/Engineering_notation), using [metric prefixes](http://en.wikipedia.org/wiki/Metric_prefix).\n\n```C++\n#include \u003ciostream\u003e\n\n#include \"phys/units/io.hpp\"\n#include \"phys/units/quantity.hpp\"\n\nusing namespace phys::units;\nusing namespace phys::units::literals;\n\nint main()\n{\n    quantity\u003celectric_resistance_d\u003e R{ 4.7_kV / ampere };\n\n    {\n        using namespace phys::units::io;\n        std::cout \u003c\u003c \"R = \" \u003c\u003c R \u003c\u003c std::endl;\n    }\n    {\n        using namespace phys::units::io::eng;\n        std::cout \u003c\u003c \"R = \" \u003c\u003c R \u003c\u003c std::endl;\n    }\n}\n```\n\nThe output produced is:\n\n```Text\nR = 4700 Ohm\nR = 4.70 kOhm\n```\n\nSee namespaces `io` and `io::eng` for further information.\n\nInstead of unit names such as J, you can also obtain the unit expressed in base dimensions.\n\n```C++\n#include \u003ciostream\u003e\n\n#include \"phys/units/quantity.hpp\"\n#include \"phys/units/quantity_io.hpp\"\n\nusing namespace phys::units;\nusing namespace phys::units::io;\n\nint main()\n{\n    std::cout \u003c\u003c \"J = \" \u003c\u003c joule \u003c\u003c std::endl;\n}\n```\n\nThe output produced is:\n\n```Text\nJ = m+2 kg s-2\n```\n\nTo get the presentation in base dimensions, you should not include `quantity_io_joule`, `io_symbols.hpp` or `io.hpp`.\n\nReported to work with\n---------------------\n\n- GCC 4.8.1\n- Clang 3.2\n\nPerformance\n-------------\n\n```Text\nRelative running time (lower is better)\n\nCompiler        Option : double : quantity\n-----------------------+--------+-------------\nGCC 4.8.1         -O0  :  1     :  7\nGCC 4.8.1         -O2  :  1     :  1\nClang 3.2         -O0  :  .     :  .\nClang 3.2         -O2  :  .     :  .\n\nMeasured on a AMD Athlon 64 X2 Dual Core Processor 5600+, 64kB L1 Data, 64kB L1 Instruction, 512kB L2, 3.2 GB RAM\n```\n\nIdeas for improvement\n-----------------------\n\nAllow to specify a conversion offset between two units, e.g. to make conversion between 'C and K possible (see Boost.Units).\n\nReferences\n----------\n\n[1] Michael Kenniston. [The Quantity Library](http://home.xnet.com/~msk/quantity/quantity.html). ([Rationale](http://home.xnet.com/%7Emsk/quantity/quantity.html), Quantity [folder](http://www.xnet.com/%7Emsk/quantity)). 16 July 2001, rev 0.4.\n\n[2] Ambler Thompson and Barry N. Taylor. [Guide for the Use of the International System of Units (SI)](http://physics.nist.gov/cuu/pdf/sp811.pdf). NIST Special Publication 811 2008 Edition.\n\n[3] David B. Newell and Eite Tiesinga, Editors. [The International System of Units (SI)](https://doi.org/10.6028/NIST.SP.330-2019). NIST Special Publication 330 2019 Edition.\n\n[4] Gaston Hillar. [Quantities and Units in Python](http://www.drdobbs.com/jvm/quantities-and-units-in-python/240161101). Dr. Dobb's. 10 September 2013.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fphysunits-ct-cpp11","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartinmoene%2Fphysunits-ct-cpp11","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fphysunits-ct-cpp11/lists"}