You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.6 KiB

  1. /*
  2. A test program to which explores the ability to round trip a nanometer
  3. internal unit in the form of a 32 bit int, out to ASCII floating point
  4. millimeters and back in without variation. It tests all 4 billion values
  5. that an int can hold, and converts to ASCII and back and verifies integrity
  6. of the round tripped value.
  7. Author: Dick Hollenbeck
  8. */
  9. #include <limits.h>
  10. #include <stdio.h>
  11. #include <cmath>
  12. #include <string>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. static inline int KiROUND( double v )
  17. {
  18. return int( v < 0 ? v - 0.5 : v + 0.5 );
  19. }
  20. typedef int BIU;
  21. #define BIU_PER_MM 1e6
  22. //double scale = BIU_PER_MM;
  23. //double scale = UM_PER_BIU;
  24. double scale = 1.0/BIU_PER_MM;
  25. std::string biuFmt( BIU aValue )
  26. {
  27. double engUnits = aValue * scale;
  28. char temp[48];
  29. int len;
  30. if( engUnits != 0.0 && fabsl( engUnits ) <= 0.0001 )
  31. {
  32. len = snprintf( temp, sizeof( temp ), "%.10f", engUnits );
  33. while( --len > 0 && temp[len] == '0' )
  34. temp[len] = '\0';
  35. ++len;
  36. }
  37. else
  38. {
  39. len = snprintf( temp, sizeof( temp ), "%.10g", engUnits );
  40. }
  41. return std::string( temp, len );;
  42. }
  43. int parseBIU( const char* s )
  44. {
  45. double d = strtod( s, NULL );
  46. return KiROUND( double( d * BIU_PER_MM ) );
  47. // return int( d * BIU_PER_MM );
  48. }
  49. int main( int argc, char** argv )
  50. {
  51. unsigned mismatches = 0;
  52. if( argc > 1 )
  53. {
  54. // take a value on the command line and round trip it back to ASCII.
  55. int i = parseBIU( argv[1] );
  56. printf( "%s: i:%d\n", __func__, i );
  57. std::string s = biuFmt( i );
  58. printf( "%s: s:%s\n", __func__, s.c_str() );
  59. exit(0);
  60. }
  61. // printf( "sizeof(long double): %zd\n", sizeof( long double ) );
  62. // Emperically prove that we can round trip all 4 billion 32 bit integers representative
  63. // of nanometers out to textual floating point millimeters, and back without error using
  64. // the above two functions.
  65. // for( int i = INT_MIN; int64_t( i ) <= int64_t( INT_MAX ); ++i )
  66. for( int64_t j = INT_MIN; j <= int64_t( INT_MAX ); ++j )
  67. {
  68. int i = int( j );
  69. std::string s = biuFmt( int( i ) );
  70. int r = parseBIU( s.c_str() );
  71. if( r != i )
  72. {
  73. printf( "i:%d biuFmt:%s r:%d\n", i, s.c_str(), r );
  74. ++mismatches;
  75. }
  76. if( !( i & 0xFFFFFF ) )
  77. {
  78. printf( " %08x", i );
  79. fflush( stdout );
  80. }
  81. }
  82. printf( "mismatches:%u\n", mismatches );
  83. return 0;
  84. }