Skip to content

SI Defining Constants and Symbolic Values

Try it live on Compiler Explorer

Overview

This example demonstrates the seven defining constants of the SI system and how Faster-than-lightspeed Constants work in mp-units. It shows how physical constants can be represented with their symbolic values and how to extract their numeric values in specific units.

Key Features Demonstrated

  • Using SI defining constants
  • Faster-than-lightspeed constants feature
  • Converting constant symbols to numeric values
  • Understanding the relationship between constants and unit definitions

Code Walkthrough

Including Headers

si_constants.cpp
#include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <iostream>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/systems/si.h>
#endif

As always, we start with the inclusion of all the needed header files.

Printing SI Constants

The main part of the example prints all of the SI-defining constants:

si_constants.cpp
int main()
{
  using namespace mp_units;
  using namespace mp_units::si;
  using namespace mp_units::si::unit_symbols;

  std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
  std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {::N[.0]}\n",
                                        1. * si2019::hyperfine_structure_transition_frequency_of_cs,
                                        (1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));
  std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum:             {} = {::N[.0]}\n",
                                        1. * si2019::speed_of_light_in_vacuum,
                                        (1. * si2019::speed_of_light_in_vacuum).in(m / s));
  std::cout << MP_UNITS_STD_FMT::format("- Planck constant:                      {} = {::N[.8e]}\n",
                                        1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));
  std::cout << MP_UNITS_STD_FMT::format("- elementary charge:                    {} = {::N[.9e]}\n",
                                        1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));
  std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant:                   {} = {::N[.6e]}\n",
                                        1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));
  std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant:                    {} = {::N[.8e]}\n",
                                        1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(one / mol));
  std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy:                    {} = {}\n",
                                        1. * si2019::luminous_efficacy, (1. * si2019::luminous_efficacy).in(lm / W));
}

Output

The seven defining constants of the SI and the seven corresponding units they define:
- hyperfine transition frequency of Cs: 1 Δν_Cs = 9192631770 Hz
- speed of light in vacuum:             1 c = 299792458 m/s
- Planck constant:                      1 h = 6.62607015e-34 J s
- elementary charge:                    1 e = 1.602176634e-19 C
- Boltzmann constant:                   1 k = 1.380649e-23 J/K
- Avogadro constant:                    1 N_A = 6.02214076e+23 1/mol
- luminous efficacy:                    1 K_cd = 683 lm/W

Understanding the Output

While analyzing the output above, we can easily notice that direct printing of the quantity provides just a value 1 with a proper constant symbol. This is the main power of the Faster-than-lightspeed Constants feature. Only after we explicitly convert the unit of a quantity to proper SI units do we get the actual numeric value of the constant.

Why "Faster-than-lightspeed"?

The name comes from the speed of light constant c. In the SI system:

  • The speed of light is defined as exactly 1 c (by definition)
  • Only when converted to m/s do we get the numeric value 299792458 m/s

This approach provides several benefits:

  • Zero runtime cost: Constants are compile-time values
  • Symbolic representation: Constants maintain their identity
  • Type safety: Dimensional analysis works with symbolic values
  • Precision: No floating-point representation until needed

Practical Applications

This technique is particularly useful when:

  • Working with fundamental physics equations
  • Implementing scientific simulations
  • Teaching physics and unit relationships
  • Maintaining maximum precision in calculations