1. /* mathtp.h by K.Tsuru */
  2. /********************************
  3. Mathematical template functions
  4. include non ANSI functions.
  5. *********************************/
  6. #ifndef MATH_TEMPLATE_H
  7. #define MATH_TEMPLATE_H
  8. #include <iostream> // added since ver 2.21
  9. #include <algorithm> // for min() and max() since ver 2.19
  10. using namespace std; // ver. 2.19
  11. /**************************************************
  12. It returns 10^n by long, defined in "ipow10.cpp"
  13. If n < 0, it returns 0.
  14. It does not check that 10^n < LONG_MAX.
  15. ***************************************************/
  16. long ipow10(int n);
  17. /**************************************************
  18. 10^n double version defined in "dpow10.cpp"
  19. 'n' can be negative.
  20. [notice] "pow(10.0, 2)" returns 99.999999.... , then
  21. int k = pow(10.0,2); you get k=99.
  22. ***************************************************/
  23. double dpow10(const int n);
  24. // difference |a-b|
  25. template <class Type> inline Type diff(Type a, Type b){
  26. return (a > b) ? (a - b) : (b - a);
  27. }
  28. // sign of a, when a == 0 return zero.
  29. template <class Type> inline int Sgn(Type a){
  30. if(a != 0) return ( a > 0 ) ? 1 : -1;
  31. return 0;
  32. }
  33. // It returns "true" if x and y have same sign.
  34. template <class Type> inline bool sameSign(Type x, Type y){ // ver 2.17
  35. return (x > 0) == (y > 0);
  36. }
  37. // return the figures of n. n must have integer type.
  38. template <class Type> int iFigures(Type n){
  39. int f = 0;
  40. while(n){
  41. n /= (Type)10;
  42. f++;
  43. }
  44. return f;
  45. }
  46. // "n" is 2^n ? since ver.2.18
  47. template <class Type> inline bool isPow2(Type n){
  48. return (n > 0) && ( (n & (n - 1)) == 0 );
  49. }
  50. // return ceil(u/v)
  51. template <class Type> inline Type ceil(Type u, Type v){
  52. Type n = u/v;
  53. if(u % v) n++;
  54. return n;
  55. }
  56. /*******************
  57. usable for SLong, SDouble class
  58. usage : cin >> a; ( s = cin )
  59. ********************/
  60. template <class Type> inline istream& operator>>(istream& s, Type& a) // added since version 2.21
  61. {
  62. string buf;
  63. std::getline(s, buf);
  64. a = buf.data();
  65. return s;
  66. }
  67. #endif // MATH_TEMPLATE_H

mathtp.h : last modifiled at 2017/02/16 21:48:49(2,059 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).