float_string_cvt.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2013 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
  5. //
  6. // Generic routines for converting floating point values to and from decimal strings.
  7. // Note that these use "naive" algorithms which result in rounding error - so they
  8. // do not round trip to and from the string representation (but should only be out
  9. // in the last bit).
  10. //
  11. #ifndef BOOST_MP_FLOAT_STRING_CVT_HPP
  12. #define BOOST_MP_FLOAT_STRING_CVT_HPP
  13. #include <cctype>
  14. namespace boost{ namespace multiprecision{ namespace detail{
  15. inline void round_string_up_at(std::string& s, int pos, int& expon)
  16. {
  17. //
  18. // Rounds up a string representation of a number at pos:
  19. //
  20. if(pos < 0)
  21. {
  22. s.insert(0, 1, '1');
  23. s.erase(s.size() - 1);
  24. ++expon;
  25. }
  26. else if(s[pos] == '9')
  27. {
  28. s[pos] = '0';
  29. round_string_up_at(s, pos - 1, expon);
  30. }
  31. else
  32. {
  33. if((pos == 0) && (s[pos] == '0') && (s.size() == 1))
  34. ++expon;
  35. ++s[pos];
  36. }
  37. }
  38. template <class Backend>
  39. std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base::fmtflags f)
  40. {
  41. using default_ops::eval_log10;
  42. using default_ops::eval_floor;
  43. using default_ops::eval_pow;
  44. using default_ops::eval_convert_to;
  45. using default_ops::eval_multiply;
  46. using default_ops::eval_divide;
  47. using default_ops::eval_subtract;
  48. using default_ops::eval_fpclassify;
  49. typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
  50. typedef typename Backend::exponent_type exponent_type;
  51. std::string result;
  52. bool iszero = false;
  53. bool isneg = false;
  54. exponent_type expon = 0;
  55. std::streamsize org_digits = digits;
  56. BOOST_ASSERT(digits > 0);
  57. int fpt = eval_fpclassify(b);
  58. if(fpt == FP_ZERO)
  59. {
  60. result = "0";
  61. iszero = true;
  62. }
  63. else if(fpt == FP_INFINITE)
  64. {
  65. if(b.compare(ui_type(0)) < 0)
  66. return "-inf";
  67. else
  68. return ((f & std::ios_base::showpos) == std::ios_base::showpos) ? "+inf" : "inf";
  69. }
  70. else if(fpt == FP_NAN)
  71. {
  72. return "nan";
  73. }
  74. else
  75. {
  76. //
  77. // Start by figuring out the exponent:
  78. //
  79. isneg = b.compare(ui_type(0)) < 0;
  80. if(isneg)
  81. b.negate();
  82. Backend t;
  83. Backend ten;
  84. ten = ui_type(10);
  85. eval_log10(t, b);
  86. eval_floor(t, t);
  87. eval_convert_to(&expon, t);
  88. if(-expon > std::numeric_limits<number<Backend> >::max_exponent10 - 3)
  89. {
  90. int e = -expon / 2;
  91. Backend t2;
  92. eval_pow(t2, ten, e);
  93. eval_multiply(t, t2, b);
  94. eval_multiply(t, t2);
  95. if(expon & 1)
  96. eval_multiply(t, ten);
  97. }
  98. else
  99. {
  100. eval_pow(t, ten, -expon);
  101. eval_multiply(t, b);
  102. }
  103. //
  104. // Make sure we're between [1,10) and adjust if not:
  105. //
  106. if(t.compare(ui_type(1)) < 0)
  107. {
  108. eval_multiply(t, ui_type(10));
  109. --expon;
  110. }
  111. else if(t.compare(ui_type(10)) >= 0)
  112. {
  113. eval_divide(t, ui_type(10));
  114. ++expon;
  115. }
  116. Backend digit;
  117. ui_type cdigit;
  118. //
  119. // Adjust the number of digits required based on formatting options:
  120. //
  121. if(((f & std::ios_base::fixed) == std::ios_base::fixed) && (expon != -1))
  122. digits += expon + 1;
  123. if((f & std::ios_base::scientific) == std::ios_base::scientific)
  124. ++digits;
  125. //
  126. // Extract the digits one at a time:
  127. //
  128. for(unsigned i = 0; i < digits; ++i)
  129. {
  130. eval_floor(digit, t);
  131. eval_convert_to(&cdigit, digit);
  132. result += static_cast<char>('0' + cdigit);
  133. eval_subtract(t, digit);
  134. eval_multiply(t, ten);
  135. }
  136. //
  137. // Possibly round result:
  138. //
  139. if(digits >= 0)
  140. {
  141. eval_floor(digit, t);
  142. eval_convert_to(&cdigit, digit);
  143. eval_subtract(t, digit);
  144. if((cdigit == 5) && (t.compare(ui_type(0)) == 0))
  145. {
  146. // Bankers rounding:
  147. if((*result.rbegin() - '0') & 1)
  148. {
  149. round_string_up_at(result, result.size() - 1, expon);
  150. }
  151. }
  152. else if(cdigit >= 5)
  153. {
  154. round_string_up_at(result, result.size() - 1, expon);
  155. }
  156. }
  157. }
  158. while((result.size() > digits) && result.size())
  159. {
  160. // We may get here as a result of rounding...
  161. if(result.size() > 1)
  162. result.erase(result.size() - 1);
  163. else
  164. {
  165. if(expon > 0)
  166. --expon; // so we put less padding in the result.
  167. else
  168. ++expon;
  169. ++digits;
  170. }
  171. }
  172. BOOST_ASSERT(org_digits >= 0);
  173. if(isneg)
  174. result.insert(0, 1, '-');
  175. format_float_string(result, expon, org_digits, f, iszero);
  176. return result;
  177. }
  178. template <class Backend>
  179. void convert_from_string(Backend& b, const char* p)
  180. {
  181. using default_ops::eval_multiply;
  182. using default_ops::eval_add;
  183. using default_ops::eval_pow;
  184. using default_ops::eval_divide;
  185. typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type;
  186. b = ui_type(0);
  187. if(!p || (*p == 0))
  188. return;
  189. bool is_neg = false;
  190. bool is_neg_expon = false;
  191. static const ui_type ten = ui_type(10);
  192. typename Backend::exponent_type expon = 0;
  193. int digits_seen = 0;
  194. typedef std::numeric_limits<number<Backend, et_off> > limits;
  195. static const int max_digits = limits::is_specialized ? limits::max_digits10 + 1 : INT_MAX;
  196. if(*p == '+') ++p;
  197. else if(*p == '-')
  198. {
  199. is_neg = true;
  200. ++p;
  201. }
  202. if((std::strcmp(p, "nan") == 0) || (std::strcmp(p, "NaN") == 0) || (std::strcmp(p, "NAN") == 0))
  203. {
  204. eval_divide(b, ui_type(0));
  205. if(is_neg)
  206. b.negate();
  207. return;
  208. }
  209. if((std::strcmp(p, "inf") == 0) || (std::strcmp(p, "Inf") == 0) || (std::strcmp(p, "INF") == 0))
  210. {
  211. b = ui_type(1);
  212. eval_divide(b, ui_type(0));
  213. if(is_neg)
  214. b.negate();
  215. return;
  216. }
  217. //
  218. // Grab all the leading digits before the decimal point:
  219. //
  220. while(std::isdigit(*p))
  221. {
  222. eval_multiply(b, ten);
  223. eval_add(b, ui_type(*p - '0'));
  224. ++p;
  225. ++digits_seen;
  226. }
  227. if(*p == '.')
  228. {
  229. //
  230. // Grab everything after the point, stop when we've seen
  231. // enough digits, even if there are actually more available:
  232. //
  233. ++p;
  234. while(std::isdigit(*p))
  235. {
  236. eval_multiply(b, ten);
  237. eval_add(b, ui_type(*p - '0'));
  238. ++p;
  239. --expon;
  240. if(++digits_seen > max_digits)
  241. break;
  242. }
  243. while(std::isdigit(*p))
  244. ++p;
  245. }
  246. //
  247. // Parse the exponent:
  248. //
  249. if((*p == 'e') || (*p == 'E'))
  250. {
  251. ++p;
  252. if(*p == '+') ++p;
  253. else if(*p == '-')
  254. {
  255. is_neg_expon = true;
  256. ++p;
  257. }
  258. typename Backend::exponent_type e2 = 0;
  259. while(std::isdigit(*p))
  260. {
  261. e2 *= 10;
  262. e2 += (*p - '0');
  263. ++p;
  264. }
  265. if(is_neg_expon)
  266. e2 = -e2;
  267. expon += e2;
  268. }
  269. if(expon)
  270. {
  271. // Scale by 10^expon, note that 10^expon can be
  272. // outside the range of our number type, even though the
  273. // result is within range, if that looks likely, then split
  274. // the calculation in two:
  275. Backend t;
  276. t = ten;
  277. if(expon > limits::min_exponent10 + 2)
  278. {
  279. eval_pow(t, t, expon);
  280. eval_multiply(b, t);
  281. }
  282. else
  283. {
  284. eval_pow(t, t, expon + digits_seen + 1);
  285. eval_multiply(b, t);
  286. t = ten;
  287. eval_pow(t, t, -digits_seen - 1);
  288. eval_multiply(b, t);
  289. }
  290. }
  291. if(is_neg)
  292. b.negate();
  293. if(*p)
  294. {
  295. // Unexpected input in string:
  296. BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected characters in string being interpreted as a float128."));
  297. }
  298. }
  299. }}} // namespaces
  300. #endif