1 #ifndef _MATHDEFS_H_ 2 #define _MATHDEFS_H_ 3 4 #include "../../lib/compat.h" 5 #include <cmath> 6 #include <cstdlib> 7 #include <climits> 8 #include <string> 9 #include <sstream> 10 11 // useful constants: 12 #ifndef M_PI 13 # define M_PI 3.14159265358979323846 14 #endif 15 #ifndef M_PI_2 16 # define M_PI_2 1.57079632679489661923 17 #endif 18 #ifndef M_SQRT2 19 # define M_SQRT2 1.41421356237309504880 20 #endif 21 22 #define RAD_PER_HOUR 0.26179938779914943654 /* == (pi / 12) */ 23 #define RAD_PER_DEGREE 0.017453292519943295769 /* == (pi / 180) */ 24 #define HOURS * RAD_PER_HOUR 25 #define DEGREES * RAD_PER_DEGREE 26 27 // Not all of the below are actually used, but maybe I will eventually 28 // support other units in StarPlot 29 30 #define C_KM_PER_SEC 299792.458 // exact (by definition of the meter) 31 #define SEC_PER_YEAR 31556925.187 // Mean tropical year 32 #define SEC_PER_JYEAR 31557600. // Julian year (exact) 33 #define KM_PER_AU 149597870.69 // +/- 0.03 km 34 #define AU_PER_PARSEC 206264.806247 // 360*60*60 / 2*pi to be exact 35 #define KM_PER_MILE 1.609344 // exact (1 inch is defined as 25.4 mm) 36 37 #define KM_PER_LY (C_KM_PER_SEC * SEC_PER_JYEAR) // IAU recommends using 38 #define AU_PER_LY (KM_PER_LY / KM_PER_AU) // Julian year in def. 39 #define MILE_PER_LY (KM_PER_LY / KM_PER_MILE) // of light-year 40 #define KM_PER_PARSEC (AU_PER_PARSEC * KM_PER_AU) 41 #define MILE_PER_PARSEC (KM_PER_PARSEC / KM_PER_MILE) 42 #define LY_PER_PARSEC (KM_PER_PARSEC / KM_PER_LY) 43 44 /* source for the following two constants: _Intro to Modern Astrophysics_, 45 * Carroll and Ostlie, 1996, page A-1 */ 46 #define SUN_DIAMETER (1391980.0 / KM_PER_LY) 47 #define SUN_TEMP 5770.0 /* Kelvins */ 48 #define SUN_BOLO_MAG +4.70 49 50 /* XXX Euler angles for Celestial -> Galactic transform go here */ 51 // for now, the transform is hardcoded in with values in vector3.h 52 // (not sure how they correspond to Euler angles) 53 54 #define ERROR_VAL -1.0e9 55 56 // useful macros 57 58 #define ROUND(x) (static_cast<int>(std::floor((x) + 0.5))) 59 #define FLOOR(x) (static_cast<int>(std::floor(x))) 60 #define FRAC(x) ((x) - FLOOR(x)) 61 62 // useful functions: 63 64 namespace starmath { 65 // for rounding to a given number of significant figures 66 // examples: sigdigits(375.25, 1) == 400.0 67 // sigdigits(375.25, 4) == 375.3 68 double sigdigits(double value, int numdigits); 69 70 // for rounding to a given number of decimal places 71 // examples: roundoff(375.25, 1) == 375.3 72 // roundoff(375.25, -2) == 400.0 roundoff(double value,int numdigits)73 inline double roundoff(double value, int numdigits) 74 { 75 return std::pow(10.0, -numdigits) * 76 std::floor(std::pow(10.0, numdigits) * value + 0.5); 77 } 78 79 // safe versions of atoi, atol, atof atof(const std::string & numstring)80 inline double atof(const std::string &numstring) 81 { return std::strtod(numstring.c_str(), 0); } 82 atol(const std::string & numstring)83 inline long int atol(const std::string &numstring) 84 { return std::strtol(numstring.c_str(), 0, 10); } 85 atoi(const std::string & numstring)86 inline int atoi(const std::string &numstring) 87 { 88 long int temp = starmath::atol(numstring); 89 return (temp < INT_MIN) ? INT_MIN : 90 (temp > INT_MAX) ? INT_MAX : static_cast<int>(temp); 91 } 92 93 // conversion of apparent and absolute magnitudes and distance get_absmag(double apparentmag,double distance)94 inline double get_absmag(double apparentmag, double distance /* in LY */) 95 { return apparentmag + 5 - 5 * std::log10(distance / LY_PER_PARSEC); } 96 get_appmag(double absolutemag,double distance)97 inline double get_appmag(double absolutemag, double distance /* in LY */) 98 { return absolutemag - 5 + 5 * std::log10(distance / LY_PER_PARSEC); } 99 get_distance(double apparentmag,double absolutemag)100 inline double /* LY */ get_distance(double apparentmag, double absolutemag) 101 { return 10*LY_PER_PARSEC*std::pow(10.0, 0.2*(apparentmag-absolutemag)); } 102 } 103 104 #endif 105