📄 astronomicalalgorithms.c
字号:
/**************************************************************************
AstronomicalAlgorithms.c
A portable ANSI C implementation of some of the algorithms published in
Astronomical Algorithms
by Jean Meeus
2nd edition (December 1998)
Willmann-Bell
ISBN: 0943396638
by Christophe DAVID (christophe.david@christophedavid.org)
You may use parts of this source code as long as
- you mention clearly that its latest version can be obtained
free of charge at
http://www.christophedavid.org/
AND
- you send me a free copy of whatever you make using this code.
Comments and suggestions welcome.
**************************************************************************/
/*!
This main source file implements the book examples. The \#ifdef PAGE_nnn_TEST
directives in AstronomicalAlgorithms.h are used to include/exclude the related
code.
@file
@brief AstronomicalAlgorithms main source file
@author Christophe DAVID \n
christophe.david@christophedavid.org \n
http://www.christophedavid.org
@since 01/07/1999
@version 1.0
@date 05/04/2001
@bug no known bug
@todo implement the remaining algorithms of the book...
@note
The screen output of this program is quite long.
It is easier to re-direct it to a file and then read this file with an editor
- AstronomicalAlgorithms > aa.txt
- edit aa.txt
@if logger
@image html http://www.mot.be/cgi-bin/logger.cgi?AstronomicalAlgorithms.c
@endif
*/
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>
/****************************************************************************/
#include "AstronomicalAlgorithms.h"
#include "InterpolationDifferences.c"
#include "IsLeapYear.c"
#include "JulianDay.c"
#include "DateFromJulianDay.c"
#include "DayOfWeek.c"
#include "DayOfYear.c"
#include "DateFromYearDay.c"
#include "EasterSunday.c"
#include "RhoSinPhiTwo.c"
#include "RhoCosPhiTwo.c"
#include "MoonPhasesJDE.c"
#include "SolarCoordinates.c"
/****************************************************************************/
int main(int iArgumentsCount, char *aszArguments[], char *aszEnvironment[])
{
char sz1[LO_DEFAULT_WORKSTRING_LENGTH] = {(char) 0};
int iReturnValue = 0;
struct tm *ptmTimeNow = NULL;
time_t ttSecondsNow = (time_t) 0;
if (iArgumentsCount < 1)
{
iReturnValue = 1;
}
else if (aszArguments == NULL)
{
iReturnValue = 2;
}
else if (aszEnvironment == NULL)
{
iReturnValue = 3;
}
else
{
(void) fprintf(stdout,
"\n%s%s%s\n%s\n%s\n",
__FILE__,
", by Christophe DAVID, compiled ",
__DATE__,
"christophe.david@christophedavid.org",
"http://www.christophedavid.org/"
);
ttSecondsNow = time(NULL);
ptmTimeNow = localtime(&ttSecondsNow);
if (strftime(sz1, (size_t) (sizeof(sz1) - (size_t) 1),
"\nCurrent local time : %A %B %d, %Y - %H:%M:%S\n\n",
ptmTimeNow) == (size_t) 0)
{
*sz1 = (char) 0;
}
(void) fprintf(stdout, "%s", sz1);
/**********************************************************************/
#ifdef PAGE_007_TEST
(void) fprintf(stdout, "\npage 007 : sine(36000030) = %f\n",
sin(DEGREES2RADIAN(36000030)));
(void) fprintf(stdout, "page 007 : 23d26m49s = %f degrees\n",
DEGMINSEC2DECIMAL(+, 23, 26, 49));
#endif
/**********************************************************************/
#ifdef PAGE_008_TEST
(void) fprintf(stdout,
"\npage 008 : 9h14m55.8s = %f degrees, tan(alpha) = %f\n",
RIGHTASC2DECIMAL(9,14,55.8),
tan(DEGREES2RADIAN(RIGHTASC2DECIMAL(9,14,55.8)))
);
#endif
/**********************************************************************/
#ifdef PAGE_009_TEST
(void) fprintf(stdout, "\npage 009 : -13d47m22s = %f degrees\n",
DEGMINSEC2DECIMAL(-, 13,47,22));
#endif
/**********************************************************************/
#ifdef PAGE_013_TEST
/* swap trick given in the book */
if (1) /*lint !e506 !e774 */
{
double doX = do_pi;
double doY = do_e;
(void) fprintf(stdout,
"\npage 013 : before swap : X = %.20f, Y = %.20f\n",
doX, doY);
doX = doX + doY;
doY = doX - doY;
doX = doX - doY;
(void) fprintf(stdout,
"page 013 : after swap : Y = %.20f, X = %.20f\n",
doY, doX);
}
/* this is pure intellectual curiosity here, but could be useful to swap
large arrays (strings, matrices, etc.) */
if (1) /*lint !e506 !e774 */
{
size_t st1 = (size_t) 0;
unsigned char *puchX = NULL;
unsigned char *puchY = NULL;
double doX = do_pi;
double doY = do_e;
(void) fprintf(stdout,
"\npage 013 : before swap : X = %.20f, Y = %.20f\n",
doX, doY);
puchX = (unsigned char *) &doX;
puchY = (unsigned char *) &doY;
for (st1 = (size_t) 0 ; st1 < sizeof(doX) ; st1++)
{
*puchX = *puchX ^ *puchY;
*puchY = *puchY ^ *puchX;
*puchX = *puchX ^ *puchY;
puchX++;
puchY++;
}
(void) fprintf(stdout,
"page 013 : after swap : Y = %.20f, X = %.20f\n",
doY, doX);
}
if (1) /*lint !e506 !e774 */
{
size_t st1 = (size_t) 0;
unsigned char *puchX = NULL;
unsigned char *puchY = NULL;
char szX[] = "0123456789abcd";
char szY[] = "ABCDEFGHIJKLMN";
(void) fprintf(stdout,
"\npage 013 : before swap : X = %s, Y = %s\n",
szX, szY);
puchX = (unsigned char *) szX;
puchY = (unsigned char *) szY;
for (st1 = (size_t) 0 ; st1 < sizeof(szX) ; st1++)
{
*puchX = *puchX ^ *puchY;
*puchY = *puchY ^ *puchX;
*puchX = *puchX ^ *puchY;
puchX++;
puchY++;
}
(void) fprintf(stdout,
"page 013 : after swap : Y = %s, X = %s\n", szY, szX);
}
#endif
/**********************************************************************/
#ifdef PAGE_017_TEST
if (1) /*lint !e506 !e774 */
{
float flX = (float) 1;
long loJ = (long) 0;
(void) fprintf(stdout, "%s", "\n");
while (1) /*lint !e506 !e716 */
{
flX = flX * (float) 2;
if (flX + (float) 1 == flX) /*lint !e777 */
{
break;
}
loJ += (long) 1;
}
(void) fprintf(stdout,
"page 017 : float : J = %ld, J * 0.30103 = %f\n",
loJ, (double) ((float)((float) loJ * (float) 0.30103)));
}
if (1) /*lint !e506 !e774 */
{
double doX = (double) 1;
long loJ = (long) 0;
while (1) /*lint !e506 !e716 */
{
doX = doX * (double) 2;
if (doX + (double) 1 == doX) /*lint !e777 */
{
break;
}
loJ += (long) 1;
}
(void) fprintf(stdout,
"page 017 : double : J = %ld, J * 0.30103 = %f\n",
loJ, (double)((double) loJ * (double) 0.30103));
}
#endif
/**********************************************************************/
#ifdef PAGE_018_TEST
(void) fprintf(stdout,
"\npage 018 : 4 * atan(1) = %21.20f\n"
" PI = 3.14159265358979323846\n\n"
,
(double) ((double) 4 * atan((double) 1)));
if (1) /*lint !e506 !e774 */
{
double doX = (double) ((double) 1 / (double) 3);
long loJ = (long) 0;
for (loJ = (long) 1 ; loJ <= 30 ; loJ++)
{
doX = ((((double) 9 * doX) + (double) 1) * doX) - (double) 1;
(void) fprintf(stdout,
"page 018 : %2ld %21.20f\n",
loJ, doX);
}
}
if (1) /*lint !e506 !e774 */
{
double doX = (double) 1.0000001;
long loJ = (long) 0;
for (loJ = (long) 1 ; loJ <= 27 ; loJ++)
{
doX *= doX;
}
(void) fprintf(stdout,
"\npage 018 : 27 iterations of 1.0000001 ^ 2 give %.4f\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -