// call every tick (0.1s) void compute_speed(uint sim_time) { if(sim_time % SPEED_AVERAGER_STEP == 0) { speed_.x = (pos_.x - old_pos_.x) * INV_DELTA_TIME * INV_SPEED_AVERAGER_STEP * SPEED_RESIZE_FACTOR; speed_.y = (pos_.y - old_pos_.y) * INV_DELTA_TIME * INV_SPEED_AVERAGER_STEP * SPEED_RESIZE_FACTOR; sp_x_term = f_abs(speed_.x); sp_y_term = f_abs(speed_.y); old_pos_.x = pos_.x; old_pos_.y = pos_.y; } }
inline static VALUE f_divide(VALUE self, VALUE other, VALUE (*func)(VALUE, VALUE), ID id) { if (k_complex_p(other)) { int flo; get_dat2(self, other); flo = (k_float_p(adat->real) || k_float_p(adat->imag) || k_float_p(bdat->real) || k_float_p(bdat->imag)); if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) { VALUE r, n; r = (*func)(bdat->imag, bdat->real); n = f_mul(bdat->real, f_add(ONE, f_mul(r, r))); if (flo) return f_complex_new2(CLASS_OF(self), (*func)(self, n), (*func)(f_negate(f_mul(self, r)), n)); return f_complex_new2(CLASS_OF(self), (*func)(f_add(adat->real, f_mul(adat->imag, r)), n), (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n)); } else { VALUE r, n; r = (*func)(bdat->real, bdat->imag); n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r))); if (flo) return f_complex_new2(CLASS_OF(self), (*func)(f_mul(self, r), n), (*func)(f_negate(self), n)); return f_complex_new2(CLASS_OF(self), (*func)(f_add(f_mul(adat->real, r), adat->imag), n), (*func)(f_sub(f_mul(adat->imag, r), adat->real), n)); } } if (k_numeric_p(other) && f_real_p(other)) { get_dat1(self); return f_complex_new2(CLASS_OF(self), (*func)(dat->real, other), (*func)(dat->imag, other)); } return rb_num_coerce_bin(self, other, id); }
inline static VALUE f_lcm(VALUE x, VALUE y) { if (f_zero_p(x) || f_zero_p(y)) return ZERO; return f_abs(f_mul(f_div(x, f_gcd(x, y)), y)); }
void switching(float e1, float e2){ /* dP = |e * Bp| */ /* P = P + dP*t */ /* PP = sign(dP)*P */ float dp[2]; dp[0] = e1 * Bp[0][0] + e2 * Bp[1][0]; dp[1] = e1 * Bp[0][1] + e2 * Bp[1][1]; P[0] = P[0] + f_abs(dp[0]) * st; P[1] = P[1] + f_abs(dp[1]) * st; /* limit of value */ if(P[0] >= 5) P[0] = 5.0f; if(P[1] >= 5) P[1] = 5.0f; ys[0] = f_sign(dp[0])*P[0]; ys[1] = f_sign(dp[1])*P[1]; }
/* * call-seq: * cmp.abs -> real * cmp.magnitude -> real * * Returns the absolute part of its polar form. * * Complex(-1).abs #=> 1 * Complex(3.0, -4.0).abs #=> 5.0 */ static VALUE nucomp_abs(VALUE self) { get_dat1(self); if (f_zero_p(dat->real)) { VALUE a = f_abs(dat->imag); if (k_float_p(dat->real) && !k_float_p(dat->imag)) a = f_to_f(a); return a; } if (f_zero_p(dat->imag)) { VALUE a = f_abs(dat->real); if (!k_float_p(dat->real) && k_float_p(dat->imag)) a = f_to_f(a); return a; } return m_hypot(dat->real, dat->imag); }
/* f_tanh *********************************************************************/ inline float f_tanh(float x) { double xa = f_abs(x); double x2 = xa * xa; double x3 = xa * x2; double x4 = x2 * x2; double x7 = x3 * x4; double res = (1.0 - 1.0 / (1.0 + xa + x2 + 0.58576695 * x3 + 0.55442112 * x4 + 0.057481508 * x7)); return (x > 0 ? res : -res); }
void mfm_(){ int index = 0; for(index = 0; index < N_MFM; index++) { // 0: ball far from population center, 1: ball close to population center vector2d distance = v_sub(pos_, center[index]); float dist_x = f_abs(distance.x); float dist_y = f_abs(distance.y); float Iext = (2.0 - dist_x) / 2.0; float dv = ( -F_x[index] + IF( Iext ) ) * TAU_M; F_x[index] += dv; Iext = (2.0 - dist_y) / 2.0; dv = ( -F_y[index] + IF( Iext ) ) * TAU_M; F_y[index] += dv; } }
bool TestExtMath::test_abs() { VS(f_abs(-4.2), 4.2); VS(f_abs(5), 5); VS(f_abs(-5), 5); VS(f_abs("-4.2"), 4.2); VS(f_abs("5"), 5); VS(f_abs("-5"), 5); return Count(true); }
static VALUE f_format(VALUE self, VALUE (*func)(VALUE)) { VALUE s, impos; get_dat1(self); impos = f_tpositive_p(dat->imag); s = (*func)(dat->real); rb_str_cat2(s, !impos ? "-" : "+"); rb_str_concat(s, (*func)(f_abs(dat->imag))); if (!rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1])) rb_str_cat2(s, "*"); rb_str_cat2(s, "i"); return s; }
static VALUE m_sqrt(VALUE x) { if (f_real_p(x)) { if (f_positive_p(x)) return m_sqrt_bang(x); return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x))); } else { get_dat1(x); if (f_negative_p(dat->imag)) return f_conj(m_sqrt(f_conj(x))); else { VALUE a = f_abs(x); return f_complex_new2(rb_cComplex, m_sqrt_bang(f_div(f_add(a, dat->real), TWO)), m_sqrt_bang(f_div(f_sub(a, dat->real), TWO))); } } }
/* * call-seq: * cmp ** numeric -> complex * * Performs exponentiation. * * Complex('i') ** 2 #=> (-1+0i) * Complex(-8) ** Rational(1, 3) #=> (1.0000000000000002+1.7320508075688772i) */ static VALUE nucomp_expt(VALUE self, VALUE other) { if (k_numeric_p(other) && k_exact_zero_p(other)) return f_complex_new_bang1(CLASS_OF(self), ONE); if (k_rational_p(other) && f_one_p(f_denominator(other))) other = f_numerator(other); /* c14n */ if (k_complex_p(other)) { get_dat1(other); if (k_exact_zero_p(dat->imag)) other = dat->real; /* c14n */ } if (k_complex_p(other)) { VALUE r, theta, nr, ntheta; get_dat1(other); r = f_abs(self); theta = f_arg(self); nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)), f_mul(dat->imag, theta))); ntheta = f_add(f_mul(theta, dat->real), f_mul(dat->imag, m_log_bang(r))); return f_complex_polar(CLASS_OF(self), nr, ntheta); } if (k_fixnum_p(other)) { if (f_gt_p(other, ZERO)) { VALUE x, z; long n; x = self; z = x; n = FIX2LONG(other) - 1; while (n) { long q, r; while (1) { get_dat1(x); q = n / 2; r = n % 2; if (r) break; x = nucomp_s_new_internal(CLASS_OF(self), f_sub(f_mul(dat->real, dat->real), f_mul(dat->imag, dat->imag)), f_mul(f_mul(TWO, dat->real), dat->imag)); n = q; } z = f_mul(z, x); n--; } return z; } return f_expt(f_reciprocal(self), f_negate(other)); } if (k_numeric_p(other) && f_real_p(other)) { VALUE r, theta; if (k_bignum_p(other)) rb_warn("in a**b, b may be too big"); r = f_abs(self); theta = f_arg(self); return f_complex_polar(CLASS_OF(self), f_expt(r, other), f_mul(theta, other)); } return rb_num_coerce_bin(self, other, id_expt); }
/* * call-seq: * num.polar -> array * * Returns an array; [num.abs, num.arg]. */ static VALUE numeric_polar(VALUE self) { return rb_assoc_new(f_abs(self), f_arg(self)); }
float r_pos_y() { return 1.0 - f_abs(pos_.y); }
// reward float r_pos_x() { return 1.0 - f_abs(pos_.x); }
void __UpdateEnemy(Game* MyGame) { int i=0; static int db = 0; Projectile* iter = MyGame->player->proj_list->eleje->kovetkezo; db++; for(i=0;i<10;i++) { if(db>20) { MyGame->enemy[i]->coord.y += MyGame->enemy[i]->speed; // statikus változó segítségével szabályozzuk a block sebességt } if(MyGame->enemy[i]->coord.y > SCREEN_H) { FreeEnemy(MyGame->enemy[i]); NewEnemy(MyGame,i); //ha egy ellenség eléri a képernyõ alját vesztünk egy életet MyGame->player->life_left--; //és új ellenfél kerül a listára } for(iter = MyGame->player->proj_list->eleje->kovetkezo; iter != MyGame->player->proj_list->vege; iter = iter->kovetkezo) { if(iter->shot == 1) { if(f_abs(iter->coord.x - MyGame->enemy[i]->coord.x) < 70 && //az ütközés vizsgálása (f_abs((iter->coord.y) - MyGame->enemy[i]->coord.y)) < 0.1f && MyGame->enemy[i]->dead != 1) { if( (f_abs(iter->coord.x - MyGame->enemy[i]->coord.x)) < 0.1f && //a találat vizsgálása (f_abs((iter->coord.y) - MyGame->enemy[i]->coord.y)) < 0.1f && (MyGame->enemy[i]->type == iter->type) ) { MyGame->enemy_down++; if(MyGame->enemy_down == 11) // a háttér lebomálásának mûvelete { MyGame->enemy_down = 0; MyGame->next_bgforeground_index++; if(MyGame->next_bgforeground_index == MyGame->MaxBackGround) { MyGame->next_bgforeground_index = MyGame->next_bgbackground_index; // ha vége van a hátterek számának ciklikusan visszatér } // az elejére MyGame->next_bgbackground_index++; if(MyGame->next_bgbackground_index == MyGame->MaxBackGround) { MyGame->next_bgbackground_index = 0; } } if(MyGame->enemy_down == 5 && MyGame->next_bgforeground_index >= 1) { NewSurprise(MyGame->surprise,MyGame->SurprisePath[MyGame->next_bgforeground_index - 1]); } MyGame->enemy[i]->dead = 1; //találat esetén meghal az enemy GameBGLoad(MyGame,MyGame->enemy_down); } if(iter != MyGame->player->proj_list->vege) iter = FreeListedProjectile(iter); //ütközés esetén a projectile elvész NewListedProjectile(MyGame); } } } if(MyGame->enemy[i]->ready_to_free == 1) { FreeEnemy(MyGame->enemy[i]); //dead animation végén felszabadítjuk a resource-t NewEnemy(MyGame,i); } } if(db>20) { db = 0; } }