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; }
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; }
void m_apm_reciprocal(M_APM rr, int places, M_APM aa) { M_APM last_x, guess, tmpN, tmp1, tmp2; int ii, bflag, dplaces, nexp, tolerance; if (aa->m_apm_sign == 0) { M_apm_log_error_msg(M_APM_RETURN, "Warning! ... \'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_set_double(guess, (1.0 / m_apm_get_double(tmpN))); 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); }
void m_apm_set_double_mt(M_APM atmp,double dd) { m_apm_enter(); m_apm_set_double(atmp,dd); m_apm_leave(); }