Exemplo n.º 1
0
static int Btostring(lua_State *L)		/** tostring(x,[n,exp]) */
{
 char *s;
 M_APM a=Bget(L,1);
 int n=luaL_optint(L,2,DIGITS);
 if (lua_toboolean(L,3))
 {
  int m=(n<0) ? m_apm_significant_digits(a) : n;
  s=malloc(m+16);
  if (s!=NULL) m_apm_to_string(s,n,a);
 }
 else
  s=m_apm_to_fixpt_stringexp(n,a,'.',0,0);
 lua_pushstring(L,s);
 if (s!=NULL) free(s);
 return 1;
}
Exemplo n.º 2
0
void	m_apm_to_string_mt(char *s, int places, M_APM mtmp)
{
	m_apm_enter();
	m_apm_to_string(s,places,mtmp);
	m_apm_leave();
}
Exemplo n.º 3
0
void	m_apm_reciprocal(M_APM rr, int places, M_APM aa)
{
M_APM   last_x, guess, tmpN, tmp1, tmp2;
char    sbuf[32];
int	ii, bflag, dplaces, nexp, tolerance;

if (aa->m_apm_sign == 0)
  {
   M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_reciprocal\', Input = 0");

   M_set_to_zero(rr);
   return;
  }

last_x = M_get_stack_var();
guess  = M_get_stack_var();
tmpN   = M_get_stack_var();
tmp1   = M_get_stack_var();
tmp2   = M_get_stack_var();

m_apm_absolute_value(tmpN, aa);

/* 
    normalize the input number (make the exponent 0) so
    the 'guess' below will not over/under flow on large
    magnitude exponents.
*/

nexp = aa->m_apm_exponent;
tmpN->m_apm_exponent -= nexp;

m_apm_to_string(sbuf, 15, tmpN);
m_apm_set_double(guess, (1.0 / atof(sbuf)));

tolerance = places + 4;
dplaces   = places + 16;
bflag     = FALSE;

m_apm_negate(last_x, MM_Ten);

/*   Use the following iteration to calculate the reciprocal :


         X     =  X  *  [ 2 - N * X ]
          n+1
*/

ii = 0;

while (TRUE)
  {
   m_apm_multiply(tmp1, tmpN, guess);
   m_apm_subtract(tmp2, MM_Two, tmp1);
   m_apm_multiply(tmp1, tmp2, guess);

   if (bflag)
     break;

   m_apm_round(guess, dplaces, tmp1);

   /* force at least 2 iterations so 'last_x' has valid data */

   if (ii != 0)
     {
      m_apm_subtract(tmp2, guess, last_x);

      if (tmp2->m_apm_sign == 0)
        break;

      /* 
       *   if we are within a factor of 4 on the error term,
       *   we will be accurate enough after the *next* iteration
       *   is complete.
       */

      if ((-4 * tmp2->m_apm_exponent) > tolerance)
        bflag = TRUE;
     }

   m_apm_copy(last_x, guess);
   ii++;
  }

m_apm_round(rr, places, tmp1);
rr->m_apm_exponent -= nexp;
rr->m_apm_sign = aa->m_apm_sign;
M_restore_stack(5);
}