Skip to content

si_constants

The next example presents all the seven defining constants of the SI system. We can observe how Faster-than-lightspeed Constants work in practice.

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

template<class T>
  requires mp_units::is_scalar<T>
inline constexpr bool mp_units::is_vector<T> = true;

As always, we start with the inclusion of all the needed header files. After that, for the simplicity of this example, we hack the character of quantities to be able to express vector quantities with simple scalar types.

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));
}

The main part of the example prints all of the SI-defining constants. While analyzing the output of this program (provided below), we can easily notice that a 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 we get an actual numeric value of the constant.

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