void Ball::contactWith(CCNode* target) { if( target->getTag() == NODE_TAG_BLOCK ){ Block* block = (Block*)target; block->hit(); } else if( target->getTag() == NODE_TAG_SLIDER ){ Slider* slider = (Slider*)target; b2Vec2 v0( mpPhysicsSprite->getPositionX(), mpPhysicsSprite->getPositionY() ); b2Vec2 v1( slider->getPositionX(), slider->getPositionY() ); // 力学を適用する float angular = mpPhysicsSprite->getB2Body()->GetAngularVelocity() + 0.85f; b2Vec2 power1( v0 - v1 ); b2Vec2 power2( mpPhysicsSprite->getB2Body()->GetLinearVelocity() ); const float velocity = std::max( MIN_BALL_VELOCITY, std::min( MAX_BALL_VELOCITY, power2.Length() * ADDITIONAL_BALL_POWER ) ); power1.y = fabsf(power1.y); power2.y = fabsf(power2.y); power1.Normalize(); power2.Normalize(); b2Vec2 force( power1 + power2 ); force.Normalize(); force *= velocity; CCLOG("POWER %.1f", force.Length()); mpPhysicsSprite->getB2Body()->SetLinearVelocity( force ); mpPhysicsSprite->getB2Body()->SetAngularVelocity( angular ); } }
int main (void) { int x, n; printf("Please enter an integer: "); scanf("%d", &x); printf("How many times would you like to multiply it? : "); scanf("%d", &n); printf("Old way result of power of x is %d.\n", power1(x, n)); printf("New way result of power of x is %d.\n", power2(x, n)); return 0; }
int main() { unsigned int n = 0; int i; /* printf("Enter Number: "); scanf("%d", &n); */ for (i=1; i<=100; i++) printf("%10d %10uld %10uld %10uld %10d \n", i, power1(2, i), power2(2, i), power3(2, i), is_prime(i)); //printf("%d is %sa prime number\n", i, is_prime(i)?"":"not "); return 0; }
int main() { double x = 2; for ( int n = 0; n <= MAX; ++n ) { power1_Mult_Cnt = 0; power2_Mult_Cnt = 0; std::cout << "power1(" << x << "," << n << ") = " << power1( x, n ); std::cout << "\t# of multiplies = " << power1_Mult_Cnt << std::endl; std::cout << "power2(" << x << "," << n << ") = " << power2( x, n ); std::cout << "\t# of multiplies = " << power2_Mult_Cnt << std::endl; std::cout << std::endl; } } // end main()
unsigned long strtoint(char *argv) { int i=0,j; unsigned long num=0; char *a=argv, *tmp; tmp=a; while (*tmp){ tmp++; i++; } tmp--; for(j=0;j<i;j++) { num+=(*tmp-0x30)*power1(10,j); tmp--; } //printf("%d \n",num); return num; }
double power1( double x, int n ) { /* Fill in your code for power1() here. */ /* Keep track of the number of multiplications with power1_Mult_Cnt */ if(n == 0) // x^0 = 1 { return 1; } else if(n == 1) // x^1 = x { return x; } else // If x != 0 or 1, recurse { power1_Mult_Cnt++; return x * power1(x, n-1); } } // end power1()
int main() { long long t,i; long long m,n; scanf("%lld",&t); for(i=0;i<t;i++) { scanf("%lld %lld",&n,&m); if(n%2==0) { power1(n,m); } else { power2(n,m); } } return 0; }
double SECTION __ieee754_pow(double x, double y) { double z,a,aa,error, t,a1,a2,y1,y2; #if 0 double gor=1.0; #endif mynumber u,v; int k; int4 qx,qy; v.x=y; u.x=x; if (v.i[LOW_HALF] == 0) { /* of y */ qx = u.i[HIGH_HALF]&0x7fffffff; /* Checking if x is not too small to compute */ if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x; if (y == 1.0) return x; if (y == 2.0) return x*x; if (y == -1.0) return 1.0/x; if (y == 0) return 1.0; } /* else */ if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */ (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) && /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */ (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */ double retval; SET_RESTORE_ROUND (FE_TONEAREST); /* Avoid internal underflow for tiny y. The exact value of y does not matter if |y| <= 2**-64. */ if (ABS (y) < 0x1p-64) y = y < 0 ? -0x1p-64 : 0x1p-64; z = log1(x,&aa,&error); /* x^y =e^(y log (X)) */ t = y*134217729.0; y1 = t - (t-y); y2 = y - y1; t = z*134217729.0; a1 = t - (t-z); a2 = (z - a1)+aa; a = y1*a1; aa = y2*a1 + y*a2; a1 = a+aa; a2 = (a-a1)+aa; error = error*ABS(y); t = __exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */ retval = (t>0)?t:power1(x,y); return retval; } if (x == 0) { if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0) || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000) return y; if (ABS(y) > 1.0e20) return (y>0)?0:1.0/0.0; k = checkint(y); if (k == -1) return y < 0 ? 1.0/x : x; else return y < 0 ? 1.0/0.0 : 0.0; /* return 0 */ } qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */ qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */ if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) return NaNQ.x; if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0)) return x == 1.0 ? 1.0 : NaNQ.x; /* if x<0 */ if (u.i[HIGH_HALF] < 0) { k = checkint(y); if (k==0) { if (qy == 0x7ff00000) { if (x == -1.0) return 1.0; else if (x > -1.0) return v.i[HIGH_HALF] < 0 ? INF.x : 0.0; else return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x; } else if (qx == 0x7ff00000) return y < 0 ? 0.0 : INF.x; return NaNQ.x; /* y not integer and x<0 */ } else if (qx == 0x7ff00000) { if (k < 0) return y < 0 ? nZERO.x : nINF.x; else return y < 0 ? 0.0 : INF.x; } return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */ } /* x>0 */ if (qx == 0x7ff00000) /* x= 2^-0x3ff */ {if (y == 0) return NaNQ.x; return (y>0)?x:0; } if (qy > 0x45f00000 && qy < 0x7ff00000) { if (x == 1.0) return 1.0; if (y>0) return (x>1.0)?huge*huge:tiny*tiny; if (y<0) return (x<1.0)?huge*huge:tiny*tiny; } if (x == 1.0) return 1.0; if (y>0) return (x>1.0)?INF.x:0; if (y<0) return (x<1.0)?INF.x:0; return 0; /* unreachable, to make the compiler happy */ }
/* An ultimate power routine. Given two IEEE double machine numbers y, x it computes the correctly rounded (to nearest) value of X^y. */ double SECTION __ieee754_pow (double x, double y) { double z, a, aa, error, t, a1, a2, y1, y2; mynumber u, v; int k; int4 qx, qy; v.x = y; u.x = x; if (v.i[LOW_HALF] == 0) { /* of y */ qx = u.i[HIGH_HALF] & 0x7fffffff; /* Is x a NaN? */ if ((((qx == 0x7ff00000) && (u.i[LOW_HALF] != 0)) || (qx > 0x7ff00000)) && (y != 0 || issignaling (x))) return x + x; if (y == 1.0) return x; if (y == 2.0) return x * x; if (y == -1.0) return 1.0 / x; if (y == 0) return 1.0; } /* else */ if (((u.i[HIGH_HALF] > 0 && u.i[HIGH_HALF] < 0x7ff00000) || /* x>0 and not x->0 */ (u.i[HIGH_HALF] == 0 && u.i[LOW_HALF] != 0)) && /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */ (v.i[HIGH_HALF] & 0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */ double retval; { SET_RESTORE_ROUND (FE_TONEAREST); /* Avoid internal underflow for tiny y. The exact value of y does not matter if |y| <= 2**-64. */ if (fabs (y) < 0x1p-64) y = y < 0 ? -0x1p-64 : 0x1p-64; z = log1 (x, &aa, &error); /* x^y =e^(y log (X)) */ t = y * CN; y1 = t - (t - y); y2 = y - y1; t = z * CN; a1 = t - (t - z); a2 = (z - a1) + aa; a = y1 * a1; aa = y2 * a1 + y * a2; a1 = a + aa; a2 = (a - a1) + aa; error = error * fabs (y); t = __exp1 (a1, a2, 1.9e16 * error); /* return -10 or 0 if wasn't computed exactly */ retval = (t > 0) ? t : power1 (x, y); } if (isinf (retval)) retval = huge * huge; else if (retval == 0) retval = tiny * tiny; else math_check_force_underflow_nonneg (retval); return retval; } if (x == 0) { if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0) || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000) /* NaN */ return y + y; if (fabs (y) > 1.0e20) return (y > 0) ? 0 : 1.0 / 0.0; k = checkint (y); if (k == -1) return y < 0 ? 1.0 / x : x; else return y < 0 ? 1.0 / 0.0 : 0.0; /* return 0 */ } qx = u.i[HIGH_HALF] & 0x7fffffff; /* no sign */ qy = v.i[HIGH_HALF] & 0x7fffffff; /* no sign */ if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) /* NaN */ return x + y; if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0)) /* NaN */ return x == 1.0 && !issignaling (y) ? 1.0 : y + y; /* if x<0 */ if (u.i[HIGH_HALF] < 0) { k = checkint (y); if (k == 0) { if (qy == 0x7ff00000) { if (x == -1.0) return 1.0; else if (x > -1.0) return v.i[HIGH_HALF] < 0 ? INF.x : 0.0; else return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x; } else if (qx == 0x7ff00000) return y < 0 ? 0.0 : INF.x; return (x - x) / (x - x); /* y not integer and x<0 */ } else if (qx == 0x7ff00000) { if (k < 0) return y < 0 ? nZERO.x : nINF.x; else return y < 0 ? 0.0 : INF.x; } /* if y even or odd */ if (k == 1) return __ieee754_pow (-x, y); else { double retval; { SET_RESTORE_ROUND (FE_TONEAREST); retval = -__ieee754_pow (-x, y); } if (isinf (retval)) retval = -huge * huge; else if (retval == 0) retval = -tiny * tiny; return retval; } } /* x>0 */ if (qx == 0x7ff00000) /* x= 2^-0x3ff */ return y > 0 ? x : 0; if (qy > 0x45f00000 && qy < 0x7ff00000) { if (x == 1.0) return 1.0; if (y > 0) return (x > 1.0) ? huge * huge : tiny * tiny; if (y < 0) return (x < 1.0) ? huge * huge : tiny * tiny; } if (x == 1.0) return 1.0; if (y > 0) return (x > 1.0) ? INF.x : 0; if (y < 0) return (x < 1.0) ? INF.x : 0; return 0; /* unreachable, to make the compiler happy */ }
double upow(double x, double y) { double z,a,aa,error, t,a1,a2,y1,y2,gor=1.0; mynumber u,v; int k; int4 qx,qy; v.x=y; u.x=x; if (y==0.0) return 1.0; if (x==1.0) return 1.0; if (v.i[LOW_HALF] == 0) { /* of y */ qx = u.i[HIGH_HALF]&0x7fffffff; /* Checking if x is not too small to compute */ if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x; if (y == 1.0) return x; if (y == 2.0) return x*x; if ((y == -1.0) && (x!=0)) return 1.0/x; } /* else */ if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */ (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) && /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */ (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */ z = logg1(x,&aa,&error); /* x^y =e^(y log (X)) */ t = y*134217729.0; y1 = t - (t-y); y2 = y - y1; t = z*134217729.0; a1 = t - (t-z); a2 = (z - a1)+aa; a = y1*a1; aa = y2*a1 + y*a2; a1 = a+aa; a2 = (a-a1)+aa; error = error*ABS(y); t = exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */ return (t>0)?t:power1(x,y); } if (x == 0.0) { /*if (ABS(y) > 1.0e20) return (y>0)?0:NaNQ.x;*/ k = checkint(y); if (y < 0.0 ) return ((k==-1) && (u.i[HIGH_HALF]==0x80000000))? -INF.x:INF.x; /* y>0 */ else return (k==-1)?x:0; /* return signed 0 */ } qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */ qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */ if (qx > 0x7ff00000 || (qx == 0x7ff00000 && u.i[LOW_HALF] != 0)) return NaNQ.x; if (qy > 0x7ff00000 || (qy == 0x7ff00000 && v.i[LOW_HALF] != 0)) return NaNQ.x; /* if x<0 */ if (u.i[HIGH_HALF] < 0) { if ((x==-1.0) && (qy == 0x7ff00000)) return 1.0; k = checkint(y); if (k==0) return NaNQ.x; /* y not integer and x<0 */ return (k==1)?upow(-x,y):-upow(-x,y); /* if y even or odd */ } /* x>0 */ if (qx == 0x7ff00000) return (y>0)?x:0; if (y>0) return (x>1.0)?INF.x:0; if (y<0) return (x<1.0)?INF.x:0; return 0; /* unreachable, to make the compiler happy */ }
int is_prime(unsigned int n) { if (n == 2) return 1; else return (power1(2, n-1) %n == 1); }
int power1 (int x, int n) { if (n == 0) return 1; else return x * power1(x, n-1); }