
/*  C14.C -- Computes the true date given a radiometric age.	*
 *											*
 * 	The formula used is:							*
 *											*
 *                                   -3.52(4.53 - t)              *
 *        R = t - 8.26 ln[ 1 - 0.99 e                ]            *
 *											*
 *	Where 	R = radio_age in millenia				*
 *		t = true age in millenia					*
 *											*
 *                                                                */


#include <stdio.h>
#include <math.h>

#define		HORIZON		4.35

main(argc, argv)
int	argc;
char	*argv[];
{

	int		calendar_date,
			argv_num = 1;  	/*  Scans command line	*/

	unsigned	i,		/* Loop counter			*/
			start,	/* Start value for i		*/
			end;		/* End value for i		*/

	long float	c1,		/* Coefficient 1			*/
			c2,
			c3,
			c4,
			c5,
			cnvrt_to_millenia_factor,
			t_end,
			true_age=1,
			t=1;

	unsigned	r,		/* Assumed radio date		*/
			radio_age;	/* Passed from command line	*/


	if(argc < 2) {
		fprintf(stderr, "\nC14: format c14 age\n");
		exit(0);
	}

	printf("\n\n");

   while(--argc) {

	radio_age = (unsigned) atoi(argv[argv_num++]);
	

	if ( radio_age >= 42000 && radio_age <= 46000) {
	  printf("\nAn age of %u years is from the flood year (2345-4 B.C.).\n", 
										radio_age);
		continue;
	}

	if ( radio_age > 46000 ) {
		printf ("\nAn age of %u years is from before the flood ", 
								   		radio_age);
		printf ("(2345 B.C.).\n");
		continue;
	}

	c1=(long float) 8.26;		/*  Assign coefficients		*/
	c2=(long float) 1.0;
	c3=(long float) 0.99;
	c4=(long float) 3.52;
	c5=(long float) 4.35;
	r=65000;

	i=start=0;

	cnvrt_to_millenia_factor = (long float) 10.0;
	t_end=HORIZON;

	while( i<=HORIZON*1000 ) {
		for( i=start, r=0; r<= radio_age && t<=t_end; i++) {
			true_age=t;
			t=((long float) i)/cnvrt_to_millenia_factor;
			r=(unsigned) 1000*(t-c1*log(c2-c3*exp(-c4*(c5-t))));
		}

		i--;
		i--;
		t=true_age;
		t_end*=10;
		start=i*10;
		cnvrt_to_millenia_factor *= (long float) 10.0;
	}

	true_age *= (long float) 1000.0;

	printf("\nThe true age for %u years is %u", 
		((unsigned) radio_age), 
		(unsigned) (true_age) );

	printf(" which is about ");
	calendar_date = 2000 - (int) (true_age+.5);

	if( calendar_date > 0 ) 
		printf("A.D. %d.\n", ++calendar_date );
	else
		printf("%d B.C.\n", ++calendar_date*(-1) );

   }

}

