Time constants module - standard-library fodder?
Chuck Swiger
cswiger at mac.com
Fri May 30 20:05:55 EDT 2003
Ben Finney wrote:
[ ... ]
> Using the numeric literal '604800' instead of 'SECS_PER_WEEK' is as bad
> as using '3.14159265' instead of 'math.pi', for all the same reasons:
> it's prone to hard-to-find typing errors, it's not obvious to anyone
> unfamiliar with the value of the constant, it gives no context as to why
> this number is being used, etc.
[ ... ]
The fact that the number of seconds in a day is 86400, from 60*60*24 or from
HOURS_PER_DAY * SECS_PER_HOUR is reasonably straightforward, and I think there
is some use in making even the obvious explicit. :-) What would interest me
more, however, is a representation not just of dimensionless numbers, but a
representation that included the units themselves.
If I keep a 60 Watt light bulb on 24-hours a day, how much energy am I using per
day? If I pay 12 cents per kilowatt-hour, how much does that lightbulb cost me
to run each month? There is semantic validation of correctness available from
asking a question and getting not just a number, but a number in the units you
asked for.
For example, would you answer "how much does it cost" in dollars or in pennies?
Just a number won't tell you what the units were!
--
Maybe a non-trivial example would be more helpful. Here's a chunk of C code
from a program called accrete, which simulates the formation of planets.
/*--------------------------------------------------------------------------*/
/* This function implements the escape velocity calculation. Note that */
/* it appears that Fogg's eq.15 is incorrect. */
/* The mass is in units of solar mass, the radius in kilometers, and the */
/* velocity returned is in cm/sec. */
/*--------------------------------------------------------------------------*/
double escape_vel(mass, radius)
double mass, radius;
{
double mass_in_grams, radius_in_cm;
mass_in_grams = mass * SOLAR_MASS_IN_GRAMS;
radius_in_cm = radius * CM_PER_KM;
return(sqrt(2.0 * GRAV_CONSTANT * mass_in_grams / radius_in_cm));
}
/*--------------------------------------------------------------------------*/
/* This is Fogg's eq.16. The molecular weight (usually assumed to be N2) */
/* is used as the basis of the Root Mean Square velocity of the molecule */
/* or atom. The velocity returned is in cm/sec. */
/*--------------------------------------------------------------------------*/
double rms_vel(molecular_weight, orbital_radius)
double molecular_weight, orbital_radius;
{
double exospheric_temp;
exospheric_temp = EARTH_EXOSPHERE_TEMP / pow(orbital_radius, 2.0);
return(sqrt((3.0 * MOLAR_GAS_CONST * exospheric_temp) / molecular_weight) *
CM_PER_METER);
}
/*--------------------------------------------------------------------------*/
/* This function returns the smallest molecular weight retained by the */
/* body, which is useful for determining the atmosphere composition. */
/* Orbital radius is in A.U.(ie: in units of the earth's orbital radius), */
/* mass is in units of solar masses, and equatorial radius is in units of */
/* kilometers. */
/*--------------------------------------------------------------------------*/
double molecule_limit(orbital_radius, mass, equatorial_radius)
double orbital_radius, mass, equatorial_radius;
{
double numerator, denominator1, denominator2, escape_velocity, temp;
escape_velocity = escape_vel(mass,equatorial_radius);
return((3.0 * pow(GAS_RETENTION_THRESHOLD * CM_PER_METER, 2.0) * MOLAR_GAS_
CONST * EARTH_EXOSPHERE_TEMP) / pow(escape_velocity, 2.0));
}
...using the following constants:
#define ECCENTRICITY_COEFF (0.077) /* Dole's was 0.077 */
#define PROTOPLANET_MASS (1.0E-15) /* Units of solar masses */
#define SOLAR_MASS_IN_GRAMS (1.989E33) /* Units of grams */
#define EARTH_MASS_IN_GRAMS (5.977E27) /* Units of grams */
#define EARTH_RADIUS (6.378E6) /* Units of cm */
#define EARTH_RADIUS_IN_KM (6378.0) /* Units of km */
#define EARTH_ACCELERATION (981.0) /* Units of cm/sec2 */
#define EARTH_AXIAL_TILT (23.4) /* Units of degrees */
#define EARTH_EXOSPHERE_TEMP (1273.0) /* Units of degrees Kelvin */
#define EARTH_MASSES_PER_SOLAR_MASS (332775.64)
#define EARTH_EFFECTIVE_TEMP (255.0) /* Units of degrees Kelvin */
#define EARTH_ALBEDO (0.39)
#define CLOUD_COVERAGE_FACTOR (1.839E-8) /* Km2/kg */
#define EARTH_WATER_MASS_PER_AREA (3.83E15)/* grams per square km */
#define EARTH_SURF_PRES_IN_MILLIBARS (1000.0)
#define EARTH_CONVECTION_FACTOR (0.43) /* from Hart, eq.20 */
#define FREEZING_POINT_OF_WATER (273.0) /* Units of degrees Kelvin */
#define DAYS_IN_A_YEAR (365.256) /* Earth days per Earth year*/
/* gas_retention_threshold = 6.0;*/ /* ratio of esc vel to RMS vel*/
#define GAS_RETENTION_THRESHOLD (5.0) /* ratio of esc vel to RMS vel*/
#define GAS_GIANT_ALBEDO (0.5) /* albedo of a gas giant */
#define CLOUD_ALBEDO (0.52)
#define AIRLESS_ROCKY_ALBEDO (0.07)
#define ROCKY_ALBEDO (0.15)
#define WATER_ALBEDO (0.04)
#define AIRLESS_ICE_ALBEDO (0.5)
#define ICE_ALBEDO (0.7)
#define SECONDS_PER_HOUR (3600.0)
#define CM_PER_AU (1.495978707E13)/* number of cm in an AU */
#define CM_PER_KM (1.0E5) /* number of cm in a km */
#define CM_PER_METER (100.0)
#define MILLIBARS_PER_BAR (1000.0)
#define KELVIN_CELCIUS_DIFFERENCE (273.0)
#define GRAV_CONSTANT (6.672E-8) /* units of dyne cm2/gram2 */
#define GREENHOUSE_EFFECT_CONST (0.93) /* affects inner radius.. */
#define MOLAR_GAS_CONST (8314.41) /* units: g*m2/(sec2*K*mol) */
/* Now for a few molecular weights (used for RMS velocity calcs): */
/* This table is from Dole's book "Habitable Planets for Man", p. 38 */
#define ATOMIC_HYDROGEN (1.0) /* H */
#define MOLECULAR_HYDROGEN (2.0) /* H2 */
#define HELIUM (4.0) /* He */
#define ATOMIC_NITROGEN (14.0) /* N */
#define ATOMIC_OXYGEN (16.0) /* O */
#define METHANE (16.0) /* CH4 */
#define AMMONIA (17.0) /* NH3 */
#define WATER_VAPOR (18.0) /* H2O */
#define NEON (20.2) /* Ne */
#define MOLECULAR_NITROGEN (28.0) /* N2 */
#define CARBON_MONOXIDE (28.0) /* CO */
#define NITRIC_OXIDE (30.0) /* NO */
#define MOLECULAR_OXYGEN (32.0) /* O2 */
#define HYDROGEN_SULPHIDE (34.1) /* H2S */
#define ARGON (39.9) /* Ar */
#define CARBON_DIOXIDE (44.0) /* CO2 */
#define NITROUS_OXIDE (44.0) /* N2O */
#define NITROGEN_DIOXIDE (46.0) /* NO2 */
#define OZONE (48.0) /* O3 */
#define SULPHUR_DIOXIDE (64.1) /* SO2 */
#define SULPHUR_TRIOXIDE (80.1) /* SO3 */
#define KRYPTON (83.8) /* Kr */
#define XENON (131.3) /* Xe */
--
-Chuck
More information about the Python-list
mailing list