Ejemplo n.º 1
0
/*
 *  compute X = (a * X + c) MOD m       where c = a
 */
void	m_apm_get_random(M_APM mrnd)
{

	if (M_firsttime2)         /* use the system time as the initial seed value */
	{
		M_firsttime2 = FALSE;

		M_rnd_aa = m_apm_init();
		M_rnd_XX = m_apm_init();
		M_rnd_mm = m_apm_init();
		M_rtmp0  = m_apm_init();
		M_rtmp1  = m_apm_init();

		/* set the multiplier M_rnd_aa and M_rnd_mm */

		m_apm_set_string(M_rnd_aa, "716805947629621");
		m_apm_set_string(M_rnd_mm, "1.0E15");

		M_get_rnd_seed(M_rnd_XX);
	}

	m_apm_multiply(M_rtmp0, M_rnd_XX, M_rnd_aa);
	m_apm_add(M_rtmp1, M_rtmp0, M_rnd_aa);
	m_apm_integer_div_rem(M_rtmp0, M_rnd_XX, M_rtmp1, M_rnd_mm);
	m_apm_copy(mrnd, M_rnd_XX);
	mrnd->m_apm_exponent -= 15;
}
Ejemplo n.º 2
0
M_APM apm_get(struct DeltaVariable *v)
{
	M_APM r = m_apm_init();
	
	if(v->type == DELTA_TYPE_NUMBER)
		m_apm_set_double(r, delta_cast_number(v));
	else {
		int release;
		struct DeltaVariable *arg = delta_cast_string(v, &release);
		
		if(release)
			m_apm_set_string(r, arg->value.ptr);
		else
			m_apm_set_string(r, delta_copy_string(arg->value.ptr));
	}
	
	return r;
}
Ejemplo n.º 3
0
void	m_apm_set_random_seed(char *ss)
{
	M_APM   btmp;

	if (M_firsttime2)
	{
		btmp = M_get_stack_var();
		m_apm_get_random(btmp);
		M_restore_stack(1);
	}

	m_apm_set_string(M_rnd_XX, ss);
}
Ejemplo n.º 4
0
void	M_get_rnd_seed(M_APM mm)
{
	double		 timer0;
	int		 millisec;
	char             *cvi_time, *cvi_date, buf1[64], buf2[32];
	M_APM		 atmp;

	atmp = M_get_stack_var();

	cvi_date = DateStr();
	cvi_time = TimeStr();
	timer0   = Timer();

	/*
	 *  note that Timer() is not syncronized to TimeStr(),
	 *  but we don't care here since we are just looking
	 *  for a random source of digits.
	 */

	millisec = (int)(0.01 + 1000.0 * (timer0 - floor(timer0)));

	sprintf(buf1, "%d", millisec);

	buf2[0]  = cvi_time[6];	/* time format: "HH:MM:SS" */
	buf2[1]  = cvi_time[7];
	buf2[2]  = cvi_time[3];
	buf2[3]  = cvi_time[4];
	buf2[4]  = cvi_time[0];
	buf2[5]  = cvi_time[1];

	buf2[6]  = cvi_date[3];	/* date format: "MM-DD-YYYY" */
	buf2[7]  = cvi_date[4];
	buf2[8]  = cvi_date[0];
	buf2[9]  = cvi_date[1];
	buf2[10] = cvi_date[8];
	buf2[11] = cvi_date[9];
	buf2[12] = cvi_date[7];

	buf2[13] = '4';
	buf2[14] = '7';
	buf2[15] = '\0';

	strcat(buf1, buf2);

	m_apm_set_string(atmp, buf1);
	atmp->m_apm_exponent = 15;
	m_apm_integer_divide(mm, atmp, MM_One);

	M_restore_stack(1);
}
Ejemplo n.º 5
0
/*
 *  for DOS / Win 9x/NT systems : use 'ftime'
 */
void	M_get_rnd_seed(M_APM mm)
{
	int              millisec;
	time_t 		 timestamp;
	unsigned long    ul;
	char             ss[32], buf1[48], buf2[32];
	struct timeb     timebuffer;
	M_APM		 atmp;

	atmp = M_get_stack_var();

	ftime(&timebuffer);

	millisec  = (int)timebuffer.millitm;
	timestamp = timebuffer.time;
	ul        = (unsigned long)(timestamp / 7);
	ul       += timestamp + 537;
	strcpy(ss, ctime(&timestamp));       /* convert to string and copy to ss */

	sprintf(buf1, "%d", (millisec / 10));
	sprintf(buf2, "%lu", ul);

	ss[0] = ss[18];
	ss[1] = ss[17];
	ss[2] = ss[15];
	ss[3] = ss[14];
	ss[4] = ss[12];
	ss[5] = ss[11];
	ss[6] = ss[9];
	ss[7] = ss[23];
	ss[8] = ss[20];
	ss[9] = '\0';

	M_reverse_string(buf2);
	strcat(buf1, buf2);
	strcat(buf1, ss);

	m_apm_set_string(atmp, buf1);
	atmp->m_apm_exponent = 15;
	m_apm_integer_divide(mm, atmp, MM_One);

	M_restore_stack(1);
}
Ejemplo n.º 6
0
/*
 *  for unix systems : use 'gettimeofday'
 */
void	M_get_rnd_seed(M_APM mm)
{
	unsigned long    sec3;
	long             usec3;
	char             buf1[32], buf2[32];
	M_APM		 atmp;

	atmp = M_get_stack_var();
	M_get_microsec(&sec3, &usec3);

	sprintf(buf1, "%ld", usec3);
	sprintf(buf2, "%lu", sec3);
	M_reverse_string(buf2);
	strcat(buf1, buf2);

	m_apm_set_string(atmp, buf1);
	atmp->m_apm_exponent = 15;
	m_apm_integer_divide(mm, atmp, MM_One);

	M_restore_stack(1);
}
Ejemplo n.º 7
0
static M_APM Bget(lua_State *L, int i)
{
 LL=L;
 switch (lua_type(L,i))
 {
  case LUA_TNUMBER:
  {
   M_APM x=Bnew(L);
   m_apm_set_double(x,lua_tonumber(L,i));
   lua_replace(L,i);
   return x;
  }
  case LUA_TSTRING:
  {
   M_APM x=Bnew(L);
   m_apm_set_string(x,(char*)lua_tostring(L,i));
   lua_replace(L,i);
   return x;
  }
  default:
   return *((void**)luaL_checkudata(L,i,MYTYPE));
 }
 return NULL;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
char	 version_info[80];
int      ct;
				/* declare the M_APM variables ... */
M_APM    aa_mapm;
M_APM    bb_mapm;
M_APM    cc_mapm;
M_APM    dd_mapm;

if (argc < 2)
  {
   m_apm_lib_short_version(version_info);

   fprintf(stdout,
      "Usage: primenum number\t\t\t[Version 1.3, MAPM Version %s]\n",
      	      version_info);
   fprintf(stdout,
      "       find the first 10 prime numbers starting with \'number\'\n");

   exit(4);
  }
				/* now initialize the M_APM variables ... */
aa_mapm = m_apm_init();
bb_mapm = m_apm_init();
cc_mapm = m_apm_init();
dd_mapm = m_apm_init();

init_working_mapm();

m_apm_set_string(dd_mapm, argv[1]);

/*
 *  if input < 3, set start point = 3
 */

if (m_apm_compare(dd_mapm, MM_Three) == -1)
  {
   m_apm_copy(dd_mapm, MM_Three);
  }

/*
 *  make sure we start with an odd integer
 */

m_apm_integer_divide(aa_mapm, dd_mapm, MM_Two);
m_apm_multiply(bb_mapm, MM_Two, aa_mapm);
m_apm_add(aa_mapm, MM_One, bb_mapm);

ct = 0;

while (TRUE)
  {
   if (is_number_prime(aa_mapm))
     {
      m_apm_to_integer_string(buffer, aa_mapm);
      fprintf(stdout,"%s\n",buffer);

      if (++ct == 10)
        break;
     }

   m_apm_add(cc_mapm, MM_Two, aa_mapm);
   m_apm_copy(aa_mapm, cc_mapm);
  }

free_working_mapm();

m_apm_free(aa_mapm);
m_apm_free(bb_mapm);
m_apm_free(cc_mapm);
m_apm_free(dd_mapm);

m_apm_free_all_mem();

exit(0);
}
Ejemplo n.º 9
0
void	m_apm_exp(M_APM r, int places, M_APM x)
{
M_APM   tmp7, tmp8, tmp9;
int	dplaces, nn, ii;

if (MM_firsttime1)
  {
   MM_firsttime1 = FALSE;

   MM_exp_log2R = m_apm_init();
   MM_exp_512R  = m_apm_init();

   m_apm_set_string(MM_exp_log2R, "1.44269504089");   /* ~ 1 / log(2) */
   m_apm_set_string(MM_exp_512R,  "1.953125E-3");     /*   1 / 512    */
  }

tmp7 = M_get_stack_var();
tmp8 = M_get_stack_var();
tmp9 = M_get_stack_var();

if (x->m_apm_sign == 0)		/* if input == 0, return '1' */
  {
   m_apm_copy(r, MM_One);
   M_restore_stack(3);
   return;
  }

if (x->m_apm_exponent <= -3)  /* already small enough so call _raw directly */
  {
   M_raw_exp(tmp9, (places + 6), x);
   m_apm_round(r, places, tmp9);
   M_restore_stack(3);
   return;
  }

/*
    From David H. Bailey's MPFUN Fortran package :

    exp (t) =  (1 + r + r^2 / 2! + r^3 / 3! + r^4 / 4! ...) ^ q * 2 ^ n

    where q = 256, r = t' / q, t' = t - n Log(2) and where n is chosen so
    that -0.5 Log(2) < t' <= 0.5 Log(2).  Reducing t mod Log(2) and
    dividing by 256 insures that -0.001 < r <= 0.001, which accelerates
    convergence in the above series.

    I use q = 512 and also limit how small 'r' can become. The 'r' used
    here is limited in magnitude from 1.95E-4 < |r| < 1.35E-3. Forcing
    'r' into a narrow range keeps the algorithm 'well behaved'.

    ( the range is [0.1 / 512] to [log(2) / 512] )
*/

if (M_exp_compute_nn(&nn, tmp7, x) != 0)
  {
   M_apm_log_error_msg(M_APM_RETURN, 
      "\'m_apm_exp\', Input too large, Overflow");

   M_set_to_zero(r);
   M_restore_stack(3);
   return;
  }

dplaces = places + 8;

/* check to make sure our log(2) is accurate enough */

M_check_log_places(dplaces);

m_apm_multiply(tmp8, tmp7, MM_lc_log2);
m_apm_subtract(tmp7, x, tmp8);

/*
 *     guarantee that |tmp7| is between 0.1 and 0.9999999....
 *     (in practice, the upper limit only reaches log(2), 0.693... )
 */

while (TRUE)
  {
   if (tmp7->m_apm_sign != 0)
     {
      if (tmp7->m_apm_exponent == 0)
        break;
     }
     
   if (tmp7->m_apm_sign >= 0)
     {
      nn++;
      m_apm_subtract(tmp8, tmp7, MM_lc_log2);
      m_apm_copy(tmp7, tmp8);
     }
   else
     {
      nn--;
      m_apm_add(tmp8, tmp7, MM_lc_log2);
      m_apm_copy(tmp7, tmp8);
     }
  }

m_apm_multiply(tmp9, tmp7, MM_exp_512R);

/* perform the series expansion ... */

M_raw_exp(tmp8, dplaces, tmp9);

/*
 *   raise result to the 512 power
 *
 *   note : x ^ 512  =  (((x ^ 2) ^ 2) ^ 2) ... 9 times
 */

ii = 9;

while (TRUE)
  {
   m_apm_multiply(tmp9, tmp8, tmp8);
   m_apm_round(tmp8, dplaces, tmp9);

   if (--ii == 0)
     break;
  }

/* now compute 2 ^ N */

m_apm_integer_pow(tmp7, dplaces, MM_Two, nn);

m_apm_multiply(tmp9, tmp7, tmp8);
m_apm_round(r, places, tmp9);

M_restore_stack(3);                    /* restore the 3 locals we used here */
}
Ejemplo n.º 10
0
void	m_apm_set_string_mt(M_APM ctmp, char *s_in)
{
	m_apm_enter();
	m_apm_set_string(ctmp,s_in);
	m_apm_leave();
}
Ejemplo n.º 11
0
void	M_init_trig_globals()
{
	MM_lc_PI_digits      = VALID_DECIMAL_PLACES;
	MM_lc_log_digits     = VALID_DECIMAL_PLACES;
	MM_cpp_min_precision = 30;

	MM_Zero          = m_apm_init();
	MM_One           = m_apm_init();
	MM_Two           = m_apm_init();
	MM_Three         = m_apm_init();
	MM_Four          = m_apm_init();
	MM_Five          = m_apm_init();
	MM_Ten           = m_apm_init();
	MM_0_5           = m_apm_init();
	MM_LOG_2_BASE_E  = m_apm_init();
	MM_LOG_3_BASE_E  = m_apm_init();
	MM_E             = m_apm_init();
	MM_PI            = m_apm_init();
	MM_HALF_PI       = m_apm_init();
	MM_2_PI          = m_apm_init();
	MM_lc_PI         = m_apm_init();
	MM_lc_HALF_PI    = m_apm_init();
	MM_lc_2_PI       = m_apm_init();
	MM_lc_log2       = m_apm_init();
	MM_lc_log10      = m_apm_init();
	MM_lc_log10R     = m_apm_init();
	MM_0_85          = m_apm_init();
	MM_5x_125R       = m_apm_init();
	MM_5x_64R        = m_apm_init();
	MM_5x_256R       = m_apm_init();
	MM_5x_Eight      = m_apm_init();
	MM_5x_Sixteen    = m_apm_init();
	MM_5x_Twenty     = m_apm_init();
	MM_LOG_E_BASE_10 = m_apm_init();
	MM_LOG_10_BASE_E = m_apm_init();

	m_apm_set_string(MM_One, "1");
	m_apm_set_string(MM_Two, "2");
	m_apm_set_string(MM_Three, "3");
	m_apm_set_string(MM_Four, "4");
	m_apm_set_string(MM_Five, "5");
	m_apm_set_string(MM_Ten, "10");
	m_apm_set_string(MM_0_5, "0.5");
	m_apm_set_string(MM_0_85, "0.85");

	m_apm_set_string(MM_5x_125R, "8.0E-3");
	m_apm_set_string(MM_5x_64R, "1.5625E-2");
	m_apm_set_string(MM_5x_256R, "3.90625E-3");
	m_apm_set_string(MM_5x_Eight, "8");
	m_apm_set_string(MM_5x_Sixteen, "16");
	m_apm_set_string(MM_5x_Twenty, "20");

	m_apm_set_string(MM_LOG_2_BASE_E, MM_cnst_log_2);
	m_apm_set_string(MM_LOG_3_BASE_E, MM_cnst_log_3);
	m_apm_set_string(MM_LOG_10_BASE_E, MM_cnst_log_10);
	m_apm_set_string(MM_LOG_E_BASE_10, MM_cnst_1_log_10);

	m_apm_set_string(MM_lc_log2, MM_cnst_log_2);
	m_apm_set_string(MM_lc_log10, MM_cnst_log_10);
	m_apm_set_string(MM_lc_log10R, MM_cnst_1_log_10);

	m_apm_set_string(MM_E, MM_cnst_E);
	m_apm_set_string(MM_PI, MM_cnst_PI);
	m_apm_multiply(MM_HALF_PI, MM_PI, MM_0_5);
	m_apm_multiply(MM_2_PI, MM_PI, MM_Two);

	m_apm_copy(MM_lc_PI, MM_PI);
	m_apm_copy(MM_lc_HALF_PI, MM_HALF_PI);
	m_apm_copy(MM_lc_2_PI, MM_2_PI);
}