BIG_INTS_REAL


SUBMITTED BY: davindran21

DATE: Oct. 12, 2016, 1:58 a.m.

FORMAT: Text only

SIZE: 4.1 kB

HITS: 3889

  1. # include <cstdlib>
  2. # include <iomanip>
  3. # include <iostream>
  4. # include <ctime>
  5. # include <float.h>
  6. # include <limits.h>
  7. using namespace std;
  8. int main ( );
  9. void test01 ( );
  10. void timestamp ( );
  11. //****************************************************************************80
  12. int main ( )
  13. //****************************************************************************80
  14. //
  15. // Purpose:
  16. //
  17. // MAIN is the main program for BIG_INTS_REAL.
  18. //
  19. // Licensing:
  20. //
  21. // This code is distributed under the GNU LGPL license.
  22. //
  23. // Modified:
  24. //
  25. // 28 September 2014
  26. //
  27. // Author:
  28. //
  29. // John Burkardt
  30. //
  31. {
  32. timestamp ( );
  33. cout << "\n";
  34. cout << "BIG_INTS_REAL:\n";
  35. cout << " C++ version\n";
  36. cout << " Examine the transfer of integer values into and out of real variables.\n";
  37. test01 ( );
  38. //
  39. // Terminate.
  40. //
  41. cout << "\n";
  42. cout << "BIG_INTS_REAL:\n";
  43. cout << " Normal end of execution.\n";
  44. cout << "\n";
  45. timestamp ( );
  46. return 0;
  47. }
  48. //****************************************************************************80
  49. void test01 ( )
  50. //****************************************************************************80
  51. //
  52. // Purpose:
  53. //
  54. // TEST01 stores huge integers as reals.
  55. //
  56. // Licensing:
  57. //
  58. // This code is distributed under the GNU LGPL license.
  59. //
  60. // Modified:
  61. //
  62. // 28 September 2014
  63. //
  64. // Author:
  65. //
  66. // John Burkardt
  67. //
  68. {
  69. int i4;
  70. int i4r4i4;
  71. int i4r8i4;
  72. long long int i8;
  73. long long int i8r4i8;
  74. long long int i8r8i8;
  75. float r4;
  76. float r4i4;
  77. float r4i8;
  78. double r8;
  79. double r8i4;
  80. double r8i8;
  81. cout << "\n";
  82. cout << "TEST01\n";
  83. cout << " Compute the largest possible integers.\n";
  84. cout << " Try to store them as real values.\n";
  85. cout << " Then copy them back.\n";
  86. cout << "\n";
  87. cout << " 'Huge' integers and huge reals:\n";
  88. cout << "\n";
  89. i4 = INT_MAX;
  90. i8 = LLONG_MAX;
  91. r4 = FLT_MAX;
  92. r8 = DBL_MAX;
  93. cout << " i4 = INT_MAX = " << i4 << "\n";
  94. cout << " i8 = LLONG_MAX = " << i8 << "\n";
  95. cout << " r4 = FLT_MAX = " << r4 << "\n";
  96. cout << " r8 = DBL_MAX = " << r8 << "\n";
  97. cout << "\n";
  98. cout << " Convert huge integers to real values:\n";
  99. cout << "\n";
  100. r4i4 = ( float ) ( i4 );
  101. r4i8 = ( float ) ( i8 );
  102. r8i4 = ( double ) ( i4 );
  103. r8i8 = ( double ) ( i8 );
  104. cout << " r4i4 = ( float ) ( i4 ) = " << r4i4 << "\n";
  105. cout << " r4i8 = ( float ) ( i8 ) = " << r4i8 << "\n";
  106. cout << " r8i4 = ( double ) ( i4 ) = " << r8i4 << "\n";
  107. cout << " r8i8 = ( double ) ( i8 ) = " << r8i8 << "\n";
  108. cout << "\n";
  109. cout << " Convert real values of integers back to integers:\n";
  110. cout << "\n";
  111. i4r4i4 = ( int ) ( r4i4 );
  112. i4r8i4 = ( int ) ( r8i4 );
  113. i8r4i8 = ( long long int ) ( r4i8 );
  114. i8r8i8 = ( long long int ) ( r8i8 );
  115. cout << " i4r4i4 = ( int ) ( r4i4 ) = " << i4r4i4 << "\n";
  116. cout << " i4r8i4 = ( int ) ( r8i4 ) = " << i4r8i4 << "\n";
  117. cout << " i8r4i8 = ( long long int ) ( r4i8 ) = " << i8r4i8 << "\n";
  118. cout << " i8r8i8 = ( long long int ) ( r8i8 ) = " << i8r8i8 << "\n";
  119. return;
  120. }
  121. //****************************************************************************80
  122. void timestamp ( )
  123. //****************************************************************************80
  124. //
  125. // Purpose:
  126. //
  127. // TIMESTAMP prints the current YMDHMS date as a time stamp.
  128. //
  129. // Example:
  130. //
  131. // 31 May 2001 09:45:54 AM
  132. //
  133. // Licensing:
  134. //
  135. // This code is distributed under the GNU LGPL license.
  136. //
  137. // Modified:
  138. //
  139. // 08 July 2009
  140. //
  141. // Author:
  142. //
  143. // John Burkardt
  144. //
  145. // Parameters:
  146. //
  147. // None
  148. //
  149. {
  150. # define TIME_SIZE 40
  151. static char time_buffer[TIME_SIZE];
  152. const struct std::tm *tm_ptr;
  153. size_t len;
  154. std::time_t now;
  155. now = std::time ( NULL );
  156. tm_ptr = std::localtime ( &now );
  157. len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
  158. std::cout << time_buffer << "\n";
  159. return;
  160. # undef TIME_SIZE
  161. }

comments powered by Disqus