NAME
Astro::Constants - Perl library to provide physical constants for use in Physics and Astronomy based on values from 2018 CODATA.
VERSION
version 0.14051
SYNOPSIS
use strict;
use Astro::Constants qw( :all );
# to calculate the gravitational force of the Sun on the Earth
# in Newtons, use GMm/r^2
my $force_sun_earth = GRAVITATIONAL * MASS_SOLAR * MASS_EARTH / ASTRONOMICAL_UNIT**2;
DESCRIPTION
This module provides physical and mathematical constants for use in Astronomy and Astrophysics.
The values are stored in Physical_Constants.xml in the data directory and are mostly based on the 2018 CODATA values from NIST.
NOTE: Other popular languages are still using 2014 CODATA values for their constants and may produce different results in comparison. On the roadmap is a set of modules to allow you to specify the year or data set for the values of constants, defaulting to the most recent.
The :all
tag imports all the constants in their long name forms (i.e. GRAVITATIONAL). Useful subsets can be imported with these tags: :fundamental
:conversion
:mathematics
:cosmology
:planetary
:electromagnetic
or :nuclear
. Alternate names such as LIGHT_SPEED instead of SPEED_LIGHT or HBAR instead of H_BAR are imported with :alternates
. I'd like to move away from their use, but they have been in the module for years.
Long name constants are constructed with the constant pragma and are not interpolated in double quotish situations because they are really inlined functions. Short name constants were constructed with the age-old idiom of fiddling with the symbol table using typeglobs, e.g. *PI = \3.14159
, and can be slower than the long name constants.
Why use this module
You are tired of typing in all those numbers and having to make sure that they are all correct. How many significant figures is enough or too much? Where's the definitive source, Wikipedia? And which mass does "$m1" refer to, solar or lunar?
The constant values in this module are protected against accidental re-assignment in your code. The test suite protects them against accidental finger trouble in my code. Other people are using this module, so more eyeballs are looking for errors and we all benefit. The constant names are a little longer than you might like, but you gain in the long run from readable, sharable code that is clear in meaning. Your programming errors are a little easier to find when you can see that the units don't match. Isn't it reassuring that you can verify how a number is produced and which meeting of which standards body is responsible for its value?
Trusting someone else's code does carry some risk, which you should consider, but have you also considered the risk of doing it yourself with no one else to check your work? And, are you going to check for the latest values from NIST every 4 years?
And plus, it's FASTER
Benchmarking has shown that the imported constants can be more than 3 times faster than using variables or other constant modules because of the way the compiler optimizes your code. So, if you've got a lot of calculating to do, this is the module to do it with.
CONSTANTS
SPEED_LIGHT
299792458
speed of light in a vacuum
This constant is also available using the alternate name LIGHT_SPEED
(imported using the :alternate tag for backwards compatibility)
BOLTZMANN
1.380649e-23
Boltzmann's constant
GRAVITATIONAL
6.67430e-11
universal gravitational constant
ELECTRON_VOLT
1.602176634e-19
electron volt
PLANCK
6.62607015e-34
Planck constant
H_BAR
1.0545718176763e-34
the reduced Planck constant, Planck's constant (exact) /2pi
This constant is also available using the alternate name HBAR
(imported using the :alternate tag for backwards compatibility)
CHARGE_ELEMENTARY
1.602176634e-19
electron charge (defined positive)
This constant is also available using the alternate name ELECTRON_CHARGE
(imported using the :alternate tag for backwards compatibility)
STEFAN_BOLTZMANN
5.670374419e-8
Stefan-Boltzmann constant
DENSITY_RADIATION
7.565723e-16
radiation density constant, 4 * sigma / c
This constant is also available using the alternate name A_RAD
(imported using the :alternate tag for backwards compatibility)
WIEN
2.897771955e-3
Wien wavelength displacement law constant
ALPHA
7.2973525693e-3
fine structure constant
IMPEDANCE_VACUUM
376.730313461
characteristic impedance of vacuum
This constant is also available using the alternate name VACUUM_IMPEDANCE
(imported using the :alternate tag for backwards compatibility)
PERMITIV_FREE_SPACE
8.8541878128e-12
permittivity of free space, epsilon_0, the electric constant
This constant is also available using the alternate name PERMITIVITY_0
(imported using the :alternate tag for backwards compatibility)
PERMEABL_FREE_SPACE
1.25663706212e-6
permeability of free space, mu_0, the magnetic constant
This constant is also available using these alternate names (imported using the :alternate tag): PERMEABILITY_0, CONSTANT_MAGNETIC
PI
3.14159265358979324
trig constant pi
FOUR_PI
12.5663706143592
trig constant pi times 4 (shorthand for some calculations)
This constant is also available using the alternate name FOURPI
(imported using the :alternate tag for backwards compatibility)
STERADIAN
3282.80635001174
a measure of solid angle in square degrees and a SI derived unit
EXP
2.71828182846
base of natural logarithm
ATOMIC_MASS_UNIT
1.66053906660e-27
unified atomic mass unit, 1 u
PARSEC
3.08567758149e16
parsec
ASTRONOMICAL_UNIT
149597870700
astronomical unit
LIGHT_YEAR
9460730472580800
the distance that light travels in vacuum in one Julian year
ANGSTROM
1e-10
Angstrom
JANSKY
1e-26
Jansky, a unit of flux density
AVOGADRO
6.02214076e23
Avogadro's number
YEAR
31557600
defined as exactly 365.25 days of 86400 SI seconds
This constant is also available using the alternate name YEAR_JULIAN
(imported using the :alternate tag for backwards compatibility)
YEAR_TROPICAL
31556925.1
the period of time for the ecliptic longitude of the Sun to increase 360 degrees, approximated by the Gregorian calendar
YEAR_SIDEREAL
31558149.8
the period of revolution of the Earth around the Sun in a fixed reference frame
YEAR_ANOMALISTIC
31558432.6
the period between successive passages of the Earth through perihelion
YEAR_ECLIPSE
29947974.3
the period between successive passages of the Sun (as seen from the geocenter) through the same lunar node
MASS_SOLAR
1.9884e30
solar mass
This constant is also available using the alternate name SOLAR_MASS
(imported using the :alternate tag for backwards compatibility)
LUMINOSITY_SOLAR
3.828e26
solar luminosity
This constant is also available using the alternate name SOLAR_LUMINOSITY
(imported using the :alternate tag for backwards compatibility)
DENSITY_CRITICAL_RHOC
1.87834e-26
Critical Density parameter expressed in terms of
ρ_c / h² = 3 × (100 km s⁻¹ Mpc⁻¹)² / 8πG
Multiply by the square of the dimensionless Hubble parameter, h, in your calculations to get the actual value
This constant is also available using the alternate name RHO_C
(imported using the :alternate tag for backwards compatibility)
HUBBLE_TIME
3.0853056e17
Hubble time *h, the inverse of Hubble's constant valued at 100 km/s/Mpc (DEPRECATED - see ChangeLog)
TEMPERATURE_CMB
2.72548
cosmic microwave background temperature in Kelvin
This constant is also available using the alternate name CMB_TEMPERATURE
(imported using the :alternate tag for backwards compatibility)
MAGNITUDE_SOLAR_V
-26.74
visual brightness of the Sun
This constant is also available using the alternate name SOLAR_V_MAG
(imported using the :alternate tag for backwards compatibility)
MAGNITUDE_SOLAR_V_ABSOLUTE
4.83
solar absolute V magnitude
This constant is also available using the alternate name SOLAR_V_ABS_MAG
(imported using the :alternate tag for backwards compatibility)
RADIUS_SOLAR
6.96e8
solar radius
This constant is also available using the alternate name SOLAR_RADIUS
(imported using the :alternate tag for backwards compatibility)
MASS_EARTH
5.9722e24
mass of Earth
This constant is also available using the alternate name EARTH_MASS
(imported using the :alternate tag for backwards compatibility)
RADIUS_EARTH
6.3781366e6
radius of Earth
This constant is also available using the alternate name EARTH_RADIUS
(imported using the :alternate tag for backwards compatibility)
TEMPERATURE_SOLAR_SURFACE
5772
surface temperature of sun (photosphere)
This constant is also available using the alternate name SOLAR_TEMPERATURE
(imported using the :alternate tag for backwards compatibility)
DENSITY_SOLAR
1408
mean solar density
This constant is also available using the alternate name SOLAR_DENSITY
(imported using the :alternate tag for backwards compatibility)
DENSITY_EARTH
5515
mean Earth density
This constant is also available using the alternate name EARTH_DENSITY
(imported using the :alternate tag for backwards compatibility)
GRAVITY_SOLAR
274.78
solar surface gravity
This constant is also available using the alternate name SOLAR_GRAVITY
(imported using the :alternate tag for backwards compatibility)
GRAVITY_EARTH
9.80665
Earth surface gravity
This constant is also available using the alternate name EARTH_GRAVITY
(imported using the :alternate tag for backwards compatibility)
RADIUS_LUNAR
1.7381e6
lunar radius
This constant is also available using the alternate name LUNAR_RADIUS
(imported using the :alternate tag for backwards compatibility)
MASS_LUNAR
7.346e22
lunar mass
This constant is also available using the alternate name LUNAR_MASS
(imported using the :alternate tag for backwards compatibility)
AXIS_SM_LUNAR
3.84402e8
lunar orbital semi-major axis
This constant is also available using the alternate name LUNAR_SM_AXIS
(imported using the :alternate tag for backwards compatibility)
ECCENTRICITY_LUNAR
0.0549
lunar orbital eccentricity
This constant is also available using the alternate name LUNAR_ECCENTRICITY
(imported using the :alternate tag for backwards compatibility)
THOMSON_CROSS_SECTION
6.6524587321e-29
Thomson cross-section
This constant is also available using the alternate name THOMSON_XSECTION
(imported using the :alternate tag for backwards compatibility)
MASS_ELECTRON
9.1093837015e-31
mass of electron
This constant is also available using the alternate name ELECTRON_MASS
(imported using the :alternate tag for backwards compatibility)
MASS_PROTON
1.67262192369e-27
mass of proton
This constant is also available using the alternate name PROTON_MASS
(imported using the :alternate tag for backwards compatibility)
MASS_NEUTRON
1.67492749804e-27
neutron mass
This constant is also available using the alternate name NEUTRON_MASS
(imported using the :alternate tag for backwards compatibility)
MASS_HYDROGEN
1.6738e-27
mass of Hydrogen atom -- This value is from the IUPAC and is a little smaller than MASS_PROTON + MASS_ELECTRON, but within the uncertainty given here. The current value is 1.008u +/- 0.0002 derived from a range of terrestrial materials. If this is for precision work, you had best understand what you're using. See https://iupac.org/what-we-do/periodic-table-of-elements/
This constant is also available using the alternate name HYDROGEN_MASS
(imported using the :alternate tag for backwards compatibility)
MASS_ALPHA
6.6446573357e-27
mass of alpha particle
RADIUS_ELECTRON
2.8179403262e-15
classical electron radius
This constant is also available using the alternate name ELECTRON_RADIUS
(imported using the :alternate tag for backwards compatibility)
RADIUS_BOHR
5.29177210903e-11
Bohr radius
This constant is also available using the alternate name BOHR_RADIUS
(imported using the :alternate tag for backwards compatibility)
RADIUS_JUPITER
69911000
Volumetric mean radius of Jupiter
MASS_JUPITER
1.89819e27
mass of Jupiter
EXPORT
Nothing is exported by default, so the module doesn't clobber any of your variables. Select from the following tags:
:all
(everything except :deprecated):fundamental
:conversion
:mathematics
:cosmology
:planetary
:electromagnetic
:nuclear
:alternates
:deprecated
FUNCTIONS
pretty
This is a helper function that rounds a value or list of values to 5 significant figures.
precision
Give this method the string of the constant and it returns the precision or uncertainty listed.
$rel_precision = precision('GRAVITATIONAL');
$abs_precision = precision('MASS_EARTH');
At the moment you need to know whether the uncertainty is relative or absolute. Looking to fix this in future versions.
Deprecated functions
I've gotten rid of list_constants
and describe_constants
because they are now in the documentation. Use perldoc Astro::Constants
for that information.
SEE ALSO
Test::Deep::NumberTolerant for testing values within objects
Reference Documents:
REPOSITORY
* github
ISSUES
Feel free to file bugs or suggestions in the Issues section of the Github repository.
Using strict
is a must with this code. Any constants you forgot to import will evaluate to 0 and silently introduce errors in your code. Caveat Programmer.
If you are using this module, drop me a line using any available means at your disposal, including *gasp* email (address in the Author section), to let me know how you're using it. What new features would you like to see?
Current best method to contact me is via a Github Issue.
Extending the data set
If you want to add in your own constants or override the factory defaults, run make, edit the PhysicalConstants.xml file and then run dzil build
again. If you have a pre-existing PhysicalConstants.xml file, drop it in place before running dzil build
.
Availability
the original astroconst sites have disappeared
ROADMAP
I have moved to a noun_adjective format for long names. LIGHT_SPEED and SOLAR_MASS become SPEED_LIGHT and MASS_SOLAR. This principle should make the code easier to read with the most important information coming at the beginning of the name. See also Astro::Constants::Roadmap
ASTROCONST
(Gleaned from the Astroconst home page - astroconst.org )
Astroconst is a set of header files in various languages (currently C, Fortran, Perl, Java, IDL and Gnuplot) that provide a variety of useful astrophysical constants without constantly needing to look them up.
The generation of the header files from one data file is automated, so you can add new constants to the data file and generate new header files in all the appropriate languages without needing to fiddle with each header file individually.
This package was created and is maintained by Jeremy Bailin. It's license states that it is completely free, both as in speech and as in beer.
DISCLAIMER
No warranty expressed or implied. This is free software. If you want someone to assume the risk of an incorrect value, you better be paying them.
(What would you want me to test in order for you to depend on this module?)
from Jeremy Bailin's astroconst header files
The Astroconst values have been gleaned from a variety of sources, and have quite different precisions depending both on the known precision of the value in question, and in some cases on the precision of the source I found it from. These values are not guaranteed to be correct. Astroconst is not certified for any use whatsoever. If your rocket crashes because the precision of the lunar orbital eccentricity isn't high enough, that's too bad.
ACKNOWLEDGMENTS
Jeremy Balin, for writing the astroconst package and helping test and develop this module.
Doug Burke, for giving me the idea to write this module in the first place, tidying up Makefile.PL, testing and improving the documentation.
AUTHOR
Boyd Duffee <duffee@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2024 by Boyd Duffee.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.