numerictools.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #include "core/basics/numerictools.h"
  7. // STL includes
  8. #include <errno.h>
  9. #include <sstream>
  10. #include <iomanip>
  11. #include <clocale>
  12. // NICE-core includes
  13. #include <core/basics/Log.h>
  14. #include <core/basics/Timer.h>
  15. #include <core/basics/Exception.h>
  16. #include <core/basics/Streamable.h>
  17. typedef unsigned int uint;
  18. namespace NICE {
  19. // check if endptr is empty string (except for whitespaces)
  20. static bool isEndPtrOk(char* endptr) {
  21. std::stringstream s(endptr);
  22. // skip whitespaces
  23. ws(s);
  24. return s.eof();
  25. }
  26. // double stringToDouble(const char* s) {
  27. // errno = 0;
  28. // char* endptr;
  29. // const double v = strtod(s, &endptr);
  30. // if (errno != 0 || !isEndPtrOk(endptr)) {
  31. // fthrow(Exception, "Format error in floating point input");
  32. // }
  33. // return v;
  34. // }
  35. double stringToDouble(const char* s) {
  36. char* endptr;
  37. setlocale(LC_NUMERIC, US_ENGLISH_LOCALE);
  38. errno = 0;
  39. const double v = strtod(s, &endptr);
  40. if (errno != 0 || !isEndPtrOk(endptr)) {
  41. std::string st(s);
  42. if (st == "nan") {
  43. return doubleNaN();
  44. } else if (st == "inf") {
  45. return std::numeric_limits<double>::infinity();
  46. } else if (st == "-inf") {
  47. return -std::numeric_limits<double>::infinity();
  48. }
  49. fthrow(Exception, "Format error in floating point input: "
  50. << s << " (" << errno << ")");
  51. }
  52. return v;
  53. }
  54. long stringToInt(const char* s) {
  55. errno = 0;
  56. char* endptr;
  57. const long v = strtol(s, &endptr, 10);
  58. if (errno != 0 || !isEndPtrOk(endptr)) {
  59. fthrow(Exception, "Format error in integer input");
  60. }
  61. return v;
  62. }
  63. void initRand( bool fixedSeed, unsigned int seed ) {
  64. // const double now = Timer::getNow();
  65. // const unsigned int seed = (unsigned int)(now * 1000.0);
  66. // Log::debug() << "now, seed: " << now << ", " << seed << std::endl;
  67. // srand(seed);
  68. if ( fixedSeed )
  69. srand ( (uint)seed );
  70. else
  71. srand((uint)Timer::getMicroseconds());
  72. }
  73. std::string intToString(const int i) {
  74. std::stringstream s;
  75. s << i;
  76. return s.str();
  77. }
  78. std::string intToString(const int i, const uint & length) {
  79. std::stringstream s;
  80. s << std::setw( length ) << std::setfill('0') << i;
  81. return s.str();
  82. }
  83. std::string doubleToString(const double d, const unsigned int digits,
  84. const bool fixedPoint) {
  85. std::stringstream s;
  86. s.imbue(std::locale(US_ENGLISH_LOCALE));
  87. s.precision(digits);
  88. if (fixedPoint) {
  89. s << std::fixed;
  90. }
  91. s << d;
  92. return s.str();
  93. }
  94. } // namespace