Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Unit.hpp File Reference

A physical unit as a value: its SI dimension and its magnitude in SI base units. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  shamunits::Dimension
 Exponents of the seven SI base dimensions. More...
struct  shamunits::Unit< Tscal >
 A unit: its dimension and its magnitude expressed in SI base units, in precision Tscal. More...

Namespaces

namespace  shamunits
 namespace containing the units library

Functions

constexpr Dimension shamunits::operator* (Dimension a, Dimension b) noexcept
 Dimension of a product: exponents add.
constexpr Dimension shamunits::operator/ (Dimension a, Dimension b) noexcept
 Dimension of a quotient: exponents subtract.
constexpr Dimension shamunits::dim_pow (Dimension d, int n) noexcept
 Dimension raised to an integer power: exponents scale.
template<class Tdst, class Tsrc>
constexpr Unit< Tdst > shamunits::unit_cast (Unit< Tsrc > u) noexcept
 Explicit precision conversion, e.g. unit_cast<float>(units::metre).
template<class T>
constexpr Unit< T > shamunits::operator* (Unit< T > a, Unit< T > b) noexcept
 Product of two units: exponents add, factors multiply.
template<class T>
constexpr Unit< T > shamunits::operator/ (Unit< T > a, Unit< T > b) noexcept
 Quotient of two units: exponents subtract, factors divide.
template<class T>
constexpr Unit< T > shamunits::operator* (T s, Unit< T > u) noexcept
 Scale a unit, e.g. 149597870700. * metre.
template<class T>
constexpr Unit< T > shamunits::operator* (Unit< T > u, T s) noexcept
 Scale a unit, e.g. metre * 1e3.
template<class T>
constexpr Unit< T > shamunits::operator/ (Unit< T > u, T s) noexcept
 Divide a unit by a scalar, e.g. joule / 1e7.
template<class T>
constexpr Unit< T > shamunits::operator/ (T s, Unit< T > u) noexcept
 Scalar over a unit, e.g. 1. / second: the dimension is inverted.
template<class T>
constexpr Unit< T > shamunits::upow (Unit< T > u, int n) noexcept
 Integer power of a unit.
template<class T = double>
constexpr Unit< T > shamunits::base_unit (Dimension d) noexcept
 A base unit of the given dimension with factor 1, e.g. base_unit({.metre = 1}).
template<class T>
constexpr Unit< T > shamunits::scale (T s) noexcept
 A dimensionless scale factor, e.g. a prefix: scale(1e6) (precision of the argument).

Detailed Description

A physical unit as a value: its SI dimension and its magnitude in SI base units.

Author
Timothée David–Cléris (tim.s.nosp@m.hamr.nosp@m.ock@p.nosp@m.roto.nosp@m.n.me)

Units, prefixes and physical constants are all the same thing: a dimension plus a factor. Everything here is constexpr and STL-free so it can be used in device code.

Unit<Tscal> is a structural type for any floating-point Tscal: it can be passed by value as a template argument (template<Unit u>, or template<Unit<double> u>), which is what lets UnitSystem::get<u>() keep every exponent a constant expression. Only a raw template<double> parameter is unsupported on clang < 18, so do not add one.

The algebra never converts precisions implicitly, not even for a scalar operand: a scalar must have exactly the unit's type (149597870700. * Unit<float>{} does not compile, write 149597870700.f), and units of different precisions are converted with unit_cast<T>() first. Spell the precision when brace-initialising (Unit<float>{.dim = ..., .si_factor = ...}): class template argument deduction from an aggregate initializer needs clang >= 17.

Definition in file Unit.hpp.