float_string_cvt.hpp 8.4 KB

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