Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:15

0001 #!/usr/bin/env python3
0002 #
0003 # Compute physical constants
0004 
0005 from decimal import Decimal
0006 
0007 
0008 # circle constant beyond double precision floating point
0009 # from https://oeis.org/A000796
0010 pi_digits = (
0011     3,
0012     1,
0013     4,
0014     1,
0015     5,
0016     9,
0017     2,
0018     6,
0019     5,
0020     3,
0021     5,
0022     8,
0023     9,
0024     7,
0025     9,
0026     3,
0027     2,
0028     3,
0029     8,
0030     4,
0031     6,
0032     2,
0033     6,
0034     4,
0035     3,
0036     3,
0037     8,
0038     3,
0039     2,
0040     7,
0041     9,
0042     5,
0043     0,
0044     2,
0045     8,
0046     8,
0047     4,
0048     1,
0049     9,
0050     7,
0051     1,
0052     6,
0053     9,
0054     3,
0055     9,
0056     9,
0057     3,
0058     7,
0059     5,
0060     1,
0061     0,
0062     5,
0063     8,
0064     2,
0065     0,
0066     9,
0067     7,
0068     4,
0069     9,
0070     4,
0071     4,
0072     5,
0073     9,
0074     2,
0075     3,
0076     0,
0077     7,
0078     8,
0079     1,
0080     6,
0081     4,
0082     0,
0083     6,
0084     2,
0085     8,
0086     6,
0087     2,
0088     0,
0089     8,
0090     9,
0091     9,
0092     8,
0093     6,
0094     2,
0095     8,
0096     0,
0097     3,
0098     4,
0099     8,
0100     2,
0101     5,
0102     3,
0103     4,
0104     2,
0105     1,
0106     1,
0107     7,
0108     0,
0109     6,
0110     7,
0111     9,
0112     8,
0113     2,
0114     1,
0115     4,
0116 )
0117 pi = Decimal((0, pi_digits, 1 - len(pi_digits)))
0118 
0119 # values are taken from Review of Particle Physics 2020
0120 # see https://pdg.lbl.gov/2020/reviews/rpp2020-rev-phys-constants.pdf
0121 
0122 # vacuum speed-of-light, exact
0123 c_m_s = Decimal((0, (2, 9, 9, 7, 9, 2, 4, 5, 8), 0))
0124 # electron charge, exact
0125 e_C = Decimal((0, (1, 6, 0, 2, 1, 7, 6, 6, 3, 4), -28))
0126 # Planck constant, exact
0127 h_Js = Decimal((0, (6, 6, 2, 6, 0, 7, 0, 1, 5), -42))
0128 h_eVs = h_Js / e_C
0129 h_MeVs = h_eVs / Decimal((0, (1,), 6))
0130 h_GeVs = h_eVs / Decimal((0, (1,), 9))
0131 # reduced Planck constant
0132 hbar_Js = h_Js / (2 * pi)
0133 hbar_eVs = h_eVs / (2 * pi)
0134 hbar_MeVs = h_MeVs / (2 * pi)
0135 hbar_GeVs = h_GeVs / (2 * pi)
0136 
0137 # unit conversions
0138 degree_radian = pi / Decimal((0, (1, 8, 0), 0))
0139 J_eV = 1 / e_C
0140 J_GeV = J_eV / Decimal((0, (1,), 9))
0141 
0142 full_constants = [
0143     ("pi", pi, ""),
0144     ("speed of light in vacuum", c_m_s, "m/s"),
0145     ("electron charge", e_C, "C"),
0146     ("Planck constant", h_Js, "J*s"),
0147     ("Planck constant", h_eVs, "eV*s"),
0148     ("Planck constant", h_MeVs, "MeV*s"),
0149     ("Planck constant", h_GeVs, "GeV*s"),
0150     ("reduced Planck constant", hbar_Js, "J*s"),
0151     ("reduced Planck constant", hbar_eVs, "eV*s"),
0152     ("reduced Planck constant", hbar_MeVs, "MeV*s"),
0153     ("reduced Planck constant", hbar_GeVs, "GeV*s"),
0154     ("degree", degree_radian, "radian"),
0155     ("Joule", J_eV, "eV"),
0156     ("Joule", J_GeV, "GeV"),
0157 ]
0158 float_constants = [(n, float(v), u) for n, v, u in full_constants]
0159 
0160 
0161 def print_constants(constants):
0162     # find the largest name first for consistent formatting
0163     max_len_name = max((len(n) for n, *_ in constants))
0164     line_format = f"{{:>{max_len_name}}}:  {{}} {{}}"
0165     for name, value, unit in constants:
0166         print(line_format.format(name, value, unit))
0167 
0168 
0169 if __name__:
0170     print("=== high precision values:")
0171     print_constants(full_constants)
0172     print("=== double precision values:")
0173     print_constants(float_constants)