Advertisement
Guest User

C++ Limits, NaN and overflow

a guest
Sep 3rd, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <limits>
  6. #include <cfloat>
  7. #include <climits>
  8. #include <typeinfo>
  9.  
  10. int main(int argc, char* argv[]) {
  11.   printf("Different numeric types and it's output:\n");
  12.   printf("int(%%d): %d\n", 42);
  13.   printf("long long int(%%lld): %lld\n", 42LL);
  14.   printf("unsigned int(%%u): %u\n", 42u);
  15.    
  16.   printf("float(%%f): %f\n", 3.14F);
  17.   printf("double(%%lf): %lf\n", 3.14);
  18.   printf("long double(%%Lf): %Lf\n", 3.14L);
  19.  
  20.   printf("double(%%lE): %lE //Scientific format\n", 3.14);
  21.   printf("\n\n");
  22.  
  23.   printf("sizeof() & typeid():\n");
  24.   printf("sizeof(int) = %u\n", sizeof(int));
  25.   int i;
  26.   printf("sizeof(i) = %u\n", sizeof(i));
  27.   printf("typeid(i).name() = %s\n", typeid(i).name());
  28.  
  29.   printf("typeid(10u + 5).name() = %s\n", typeid(10u + 5).name());
  30.  
  31.   printf("\n\n");
  32.  
  33.   printf("cmath demo:\n");
  34.   printf("sqrt(%lf) = %lf\n", 3.0, sqrt(3.0));
  35.   printf("pi = %lf\n", M_PI);
  36.   printf("\n\n");
  37.  
  38.  
  39.   printf("Infinity, NaN and overflow:\n");
  40.   printf("+INF = %lf\n", std::numeric_limits<double>::infinity());
  41.   printf("NaN = %lf\n", std::numeric_limits<double>::quiet_NaN());
  42.   printf("sqrt(-1) = %lf //It's also NaN\n", sqrt(-1.0));
  43.   printf("_isnan(sqrt(-1)) = %d\n", _isnan(sqrt(-1.0)));
  44.  
  45.  
  46.   printf("max(double) = %lf\n", std::numeric_limits<double>::max());
  47.   printf("max(double) = %lf\n", DBL_MAX);
  48.   printf("And in scientific format max(double) = %lE\n", DBL_MAX);
  49.  
  50.   printf("\n");
  51.   printf("max(int) = %d\n", INT_MAX);
  52.   printf("max(int) = %d\n", std::numeric_limits<int>::max());
  53.  
  54.   printf("\n\n");
  55.  
  56.   printf("Integral overflow demo:\n");
  57.   printf("max(int) + 1 = %d\n", std::numeric_limits<int>::max() + 1);
  58.   printf("\n");
  59.  
  60.   printf("Floating point overflow demo:\n");
  61.   printf("max(double) * 2.0 = %lf\n", std::numeric_limits<double>::max() * 2.0);
  62.   printf("\n");
  63.  
  64.   printf("Lack of precision in f.p. operations:\n");
  65.   printf("max(double) + 1 = %lf\n", std::numeric_limits<double>::max() + 1);
  66.  
  67.   system("pause");
  68.   return 0;
  69. }
  70.  
  71. /*Output
  72. Different numeric types and it's output:
  73. int(%d): 42
  74. long long int(%lld): 42
  75. unsigned int(%u): 42
  76. float(%f): 3.140000
  77. double(%lf): 3.140000
  78. long double(%Lf): 3.140000
  79. double(%lE): 3.140000E+000 //Scientific format
  80.  
  81.  
  82. sizeof() & typeid():
  83. sizeof(int) = 4
  84. sizeof(i) = 4
  85. typeid(i).name() = int
  86. typeid(10u + 5).name() = unsigned int
  87.  
  88.  
  89. cmath demo:
  90. sqrt(3.000000) = 1.732051
  91. pi = 3.141593
  92.  
  93.  
  94. Infinity, NaN and overflow:
  95. +INF = 1.#INF00
  96. NaN = 1.#QNAN0
  97. sqrt(-1) = -1.#IND00 //It's also NaN
  98. _isnan(sqrt(-1)) = 1
  99. max(double) = 179769313486231570000000000000000000000000000000000000000000000000
  100. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  101. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  102. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  103. 000.000000
  104. max(double) = 179769313486231570000000000000000000000000000000000000000000000000
  105. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  106. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  107. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  108. 000.000000
  109. And in scientific format max(double) = 1.797693E+308
  110.  
  111. max(int) = 2147483647
  112. max(int) = 2147483647
  113.  
  114.  
  115. Integral overflow demo:
  116. max(int) + 1 = -2147483648
  117.  
  118. Floating point overflow demo:
  119. max(double) * 2.0 = 1.#INF00
  120.  
  121. Lack of precision in f.p. operations:
  122. max(double) + 1 = 17976931348623157000000000000000000000000000000000000000000000
  123. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  124. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  125. 00000000000000000000000000000000000000000000000000000000000000000000000000000000
  126. 0000000.000000
  127. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement