ulps_plot.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // (C) Copyright Nick Thompson 2020.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_ULP_PLOT_HPP
  6. #define BOOST_MATH_TOOLS_ULP_PLOT_HPP
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <cassert>
  11. #include <vector>
  12. #include <utility>
  13. #include <fstream>
  14. #include <string>
  15. #include <list>
  16. #include <random>
  17. #include <limits>
  18. #include <stdexcept>
  19. #include <boost/math/tools/is_standalone.hpp>
  20. #include <boost/math/tools/condition_numbers.hpp>
  21. #ifndef BOOST_MATH_STANDALONE
  22. #include <boost/random/uniform_real_distribution.hpp>
  23. #endif
  24. // Design of this function comes from:
  25. // https://blogs.mathworks.com/cleve/2017/01/23/ulps-plots-reveal-math-function-accurary/
  26. // The envelope is the maximum of 1/2 and half the condition number of function evaluation.
  27. namespace boost::math::tools {
  28. namespace detail {
  29. template<class F1, class F2, class CoarseReal, class PreciseReal>
  30. void write_gridlines(std::ostream& fs, int horizontal_lines, int vertical_lines,
  31. F1 x_scale, F2 y_scale, CoarseReal min_x, CoarseReal max_x, PreciseReal min_y, PreciseReal max_y,
  32. int graph_width, int graph_height, int margin_left, std::string const & font_color)
  33. {
  34. // Make a grid:
  35. for (int i = 1; i <= horizontal_lines; ++i) {
  36. PreciseReal y_cord_dataspace = min_y + ((max_y - min_y)*i)/horizontal_lines;
  37. auto y = y_scale(y_cord_dataspace);
  38. fs << "<line x1='0' y1='" << y << "' x2='" << graph_width
  39. << "' y2='" << y
  40. << "' stroke='gray' stroke-width='1' opacity='0.5' stroke-dasharray='4' />\n";
  41. fs << "<text x='" << -margin_left/4 + 5 << "' y='" << y - 3
  42. << "' font-family='times' font-size='10' fill='" << font_color << "' transform='rotate(-90 "
  43. << -margin_left/4 + 8 << " " << y + 5 << ")'>"
  44. << std::setprecision(4) << y_cord_dataspace << "</text>\n";
  45. }
  46. for (int i = 1; i <= vertical_lines; ++i) {
  47. CoarseReal x_cord_dataspace = min_x + ((max_x - min_x)*i)/vertical_lines;
  48. CoarseReal x = x_scale(x_cord_dataspace);
  49. fs << "<line x1='" << x << "' y1='0' x2='" << x
  50. << "' y2='" << graph_height
  51. << "' stroke='gray' stroke-width='1' opacity='0.5' stroke-dasharray='4' />\n";
  52. fs << "<text x='" << x - 10 << "' y='" << graph_height + 10
  53. << "' font-family='times' font-size='10' fill='" << font_color << "'>"
  54. << std::setprecision(4) << x_cord_dataspace << "</text>\n";
  55. }
  56. }
  57. }
  58. template<class F, typename PreciseReal, typename CoarseReal>
  59. class ulps_plot {
  60. public:
  61. ulps_plot(F hi_acc_impl, CoarseReal a, CoarseReal b,
  62. size_t samples = 1000, bool perturb_abscissas = false, int random_seed = -1);
  63. ulps_plot& clip(PreciseReal clip);
  64. ulps_plot& width(int width);
  65. ulps_plot& envelope_color(std::string const & color);
  66. ulps_plot& title(std::string const & title);
  67. ulps_plot& background_color(std::string const & background_color);
  68. ulps_plot& font_color(std::string const & font_color);
  69. ulps_plot& crop_color(std::string const & color);
  70. ulps_plot& nan_color(std::string const & color);
  71. ulps_plot& ulp_envelope(bool write_ulp);
  72. template<class G>
  73. ulps_plot& add_fn(G g, std::string const & color = "steelblue");
  74. ulps_plot& horizontal_lines(int horizontal_lines);
  75. ulps_plot& vertical_lines(int vertical_lines);
  76. void write(std::string const & filename) const;
  77. friend std::ostream& operator<<(std::ostream& fs, ulps_plot const & plot)
  78. {
  79. using std::abs;
  80. using std::floor;
  81. using std::isnan;
  82. if (plot.ulp_list_.size() == 0)
  83. {
  84. throw std::domain_error("No functions added for comparison.");
  85. }
  86. if (plot.width_ <= 1)
  87. {
  88. throw std::domain_error("Width = " + std::to_string(plot.width_) + ", which is too small.");
  89. }
  90. PreciseReal worst_ulp_distance = 0;
  91. PreciseReal min_y = (std::numeric_limits<PreciseReal>::max)();
  92. PreciseReal max_y = std::numeric_limits<PreciseReal>::lowest();
  93. for (auto const & ulp_vec : plot.ulp_list_)
  94. {
  95. for (auto const & ulp : ulp_vec)
  96. {
  97. if (static_cast<PreciseReal>(abs(ulp)) > worst_ulp_distance)
  98. {
  99. worst_ulp_distance = static_cast<PreciseReal>(abs(ulp));
  100. }
  101. if (static_cast<PreciseReal>(ulp) < min_y)
  102. {
  103. min_y = static_cast<PreciseReal>(ulp);
  104. }
  105. if (static_cast<PreciseReal>(ulp) > max_y)
  106. {
  107. max_y = static_cast<PreciseReal>(ulp);
  108. }
  109. }
  110. }
  111. // half-ulp accuracy is the best that can be expected; sometimes we can get less, but barely less.
  112. // then the axes don't show up; painful!
  113. if (max_y < 0.5) {
  114. max_y = 0.5;
  115. }
  116. if (min_y > -0.5) {
  117. min_y = -0.5;
  118. }
  119. if (plot.clip_ > 0)
  120. {
  121. if (max_y > plot.clip_)
  122. {
  123. max_y = plot.clip_;
  124. }
  125. if (min_y < -plot.clip_)
  126. {
  127. min_y = -plot.clip_;
  128. }
  129. }
  130. int height = static_cast<int>(floor(static_cast<double>(plot.width_)/1.61803));
  131. int margin_top = 40;
  132. int margin_left = 25;
  133. if (plot.title_.size() == 0)
  134. {
  135. margin_top = 10;
  136. margin_left = 15;
  137. }
  138. int margin_bottom = 20;
  139. int margin_right = 20;
  140. int graph_height = height - margin_bottom - margin_top;
  141. int graph_width = plot.width_ - margin_left - margin_right;
  142. // Maps [a,b] to [0, graph_width]
  143. auto x_scale = [&](CoarseReal x)->CoarseReal
  144. {
  145. return ((x-plot.a_)/(plot.b_ - plot.a_))*static_cast<CoarseReal>(graph_width);
  146. };
  147. auto y_scale = [&](PreciseReal y)->PreciseReal
  148. {
  149. return ((max_y - y)/(max_y - min_y) )*static_cast<PreciseReal>(graph_height);
  150. };
  151. fs << "<?xml version=\"1.0\" encoding='UTF-8' ?>\n"
  152. << "<svg xmlns='http://www.w3.org/2000/svg' width='"
  153. << plot.width_ << "' height='"
  154. << height << "'>\n"
  155. << "<style>\nsvg { background-color:" << plot.background_color_ << "; }\n"
  156. << "</style>\n";
  157. if (plot.title_.size() > 0)
  158. {
  159. fs << "<text x='" << floor(plot.width_/2)
  160. << "' y='" << floor(margin_top/2)
  161. << "' font-family='Palatino' font-size='25' fill='"
  162. << plot.font_color_ << "' alignment-baseline='middle' text-anchor='middle'>"
  163. << plot.title_
  164. << "</text>\n";
  165. }
  166. // Construct SVG group to simplify the calculations slightly:
  167. fs << "<g transform='translate(" << margin_left << ", " << margin_top << ")'>\n";
  168. // y-axis:
  169. fs << "<line x1='0' y1='0' x2='0' y2='" << graph_height
  170. << "' stroke='gray' stroke-width='1'/>\n";
  171. PreciseReal x_axis_loc = y_scale(static_cast<PreciseReal>(0));
  172. fs << "<line x1='0' y1='" << x_axis_loc
  173. << "' x2='" << graph_width << "' y2='" << x_axis_loc
  174. << "' stroke='gray' stroke-width='1'/>\n";
  175. if (worst_ulp_distance > 3)
  176. {
  177. detail::write_gridlines(fs, plot.horizontal_lines_, plot.vertical_lines_, x_scale, y_scale, plot.a_, plot.b_,
  178. min_y, max_y, graph_width, graph_height, margin_left, plot.font_color_);
  179. }
  180. else
  181. {
  182. std::vector<double> ys{-3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0};
  183. for (double & i : ys)
  184. {
  185. if (min_y <= i && i <= max_y)
  186. {
  187. PreciseReal y_cord_dataspace = i;
  188. PreciseReal y = y_scale(y_cord_dataspace);
  189. fs << "<line x1='0' y1='" << y << "' x2='" << graph_width
  190. << "' y2='" << y
  191. << "' stroke='gray' stroke-width='1' opacity='0.5' stroke-dasharray='4' />\n";
  192. fs << "<text x='" << -margin_left/2 << "' y='" << y - 3
  193. << "' font-family='times' font-size='10' fill='" << plot.font_color_ << "' transform='rotate(-90 "
  194. << -margin_left/2 + 7 << " " << y << ")'>"
  195. << std::setprecision(4) << y_cord_dataspace << "</text>\n";
  196. }
  197. }
  198. for (int i = 1; i <= plot.vertical_lines_; ++i)
  199. {
  200. CoarseReal x_cord_dataspace = plot.a_ + ((plot.b_ - plot.a_)*i)/plot.vertical_lines_;
  201. CoarseReal x = x_scale(x_cord_dataspace);
  202. fs << "<line x1='" << x << "' y1='0' x2='" << x
  203. << "' y2='" << graph_height
  204. << "' stroke='gray' stroke-width='1' opacity='0.5' stroke-dasharray='4' />\n";
  205. fs << "<text x='" << x - 10 << "' y='" << graph_height + 10
  206. << "' font-family='times' font-size='10' fill='" << plot.font_color_ << "'>"
  207. << std::setprecision(4) << x_cord_dataspace << "</text>\n";
  208. }
  209. }
  210. int color_idx = 0;
  211. for (auto const & ulp : plot.ulp_list_)
  212. {
  213. std::string color = plot.colors_[color_idx++];
  214. for (size_t j = 0; j < ulp.size(); ++j)
  215. {
  216. if (isnan(ulp[j]))
  217. {
  218. if(plot.nan_color_ == "")
  219. continue;
  220. CoarseReal x = x_scale(plot.coarse_abscissas_[j]);
  221. PreciseReal y = y_scale(static_cast<PreciseReal>(plot.clip_));
  222. fs << "<circle cx='" << x << "' cy='" << y << "' r='1' fill='" << plot.nan_color_ << "'/>\n";
  223. y = y_scale(static_cast<PreciseReal>(-plot.clip_));
  224. fs << "<circle cx='" << x << "' cy='" << y << "' r='1' fill='" << plot.nan_color_ << "'/>\n";
  225. }
  226. if (plot.clip_ > 0 && static_cast<PreciseReal>(abs(ulp[j])) > plot.clip_)
  227. {
  228. if (plot.crop_color_ == "")
  229. continue;
  230. CoarseReal x = x_scale(plot.coarse_abscissas_[j]);
  231. PreciseReal y = y_scale(static_cast<PreciseReal>(ulp[j] < 0 ? -plot.clip_ : plot.clip_));
  232. fs << "<circle cx='" << x << "' cy='" << y << "' r='1' fill='" << plot.crop_color_ << "'/>\n";
  233. }
  234. else
  235. {
  236. CoarseReal x = x_scale(plot.coarse_abscissas_[j]);
  237. PreciseReal y = y_scale(static_cast<PreciseReal>(ulp[j]));
  238. fs << "<circle cx='" << x << "' cy='" << y << "' r='1' fill='" << color << "'/>\n";
  239. }
  240. }
  241. }
  242. if (plot.ulp_envelope_)
  243. {
  244. std::string close_path = "' stroke='" + plot.envelope_color_ + "' stroke-width='1' fill='none'></path>\n";
  245. size_t jstart = 0;
  246. while (plot.cond_[jstart] > max_y)
  247. {
  248. ++jstart;
  249. if (jstart >= plot.cond_.size())
  250. {
  251. goto done;
  252. }
  253. }
  254. size_t jmin = jstart;
  255. new_top_path:
  256. if (jmin >= plot.cond_.size())
  257. {
  258. goto start_bottom_paths;
  259. }
  260. fs << "<path d='M" << x_scale(plot.coarse_abscissas_[jmin]) << " " << y_scale(plot.cond_[jmin]);
  261. for (size_t j = jmin + 1; j < plot.coarse_abscissas_.size(); ++j)
  262. {
  263. bool bad = isnan(plot.cond_[j]) || (plot.cond_[j] > max_y);
  264. if (bad)
  265. {
  266. ++j;
  267. while ( (j < plot.coarse_abscissas_.size() - 2) && bad)
  268. {
  269. bad = isnan(plot.cond_[j]) || (plot.cond_[j] > max_y);
  270. ++j;
  271. }
  272. jmin = j;
  273. fs << close_path;
  274. goto new_top_path;
  275. }
  276. CoarseReal t = x_scale(plot.coarse_abscissas_[j]);
  277. PreciseReal y = y_scale(plot.cond_[j]);
  278. fs << " L" << t << " " << y;
  279. }
  280. fs << close_path;
  281. start_bottom_paths:
  282. jmin = jstart;
  283. new_bottom_path:
  284. if (jmin >= plot.cond_.size())
  285. {
  286. goto done;
  287. }
  288. fs << "<path d='M" << x_scale(plot.coarse_abscissas_[jmin]) << " " << y_scale(-plot.cond_[jmin]);
  289. for (size_t j = jmin + 1; j < plot.coarse_abscissas_.size(); ++j)
  290. {
  291. bool bad = isnan(plot.cond_[j]) || (-plot.cond_[j] < min_y);
  292. if (bad)
  293. {
  294. ++j;
  295. while ( (j < plot.coarse_abscissas_.size() - 2) && bad)
  296. {
  297. bad = isnan(plot.cond_[j]) || (-plot.cond_[j] < min_y);
  298. ++j;
  299. }
  300. jmin = j;
  301. fs << close_path;
  302. goto new_bottom_path;
  303. }
  304. CoarseReal t = x_scale(plot.coarse_abscissas_[j]);
  305. PreciseReal y = y_scale(-plot.cond_[j]);
  306. fs << " L" << t << " " << y;
  307. }
  308. fs << close_path;
  309. }
  310. done:
  311. fs << "</g>\n"
  312. << "</svg>\n";
  313. return fs;
  314. }
  315. private:
  316. std::vector<PreciseReal> precise_abscissas_;
  317. std::vector<CoarseReal> coarse_abscissas_;
  318. std::vector<PreciseReal> precise_ordinates_;
  319. std::vector<PreciseReal> cond_;
  320. std::list<std::vector<CoarseReal>> ulp_list_;
  321. std::vector<std::string> colors_;
  322. CoarseReal a_;
  323. CoarseReal b_;
  324. PreciseReal clip_;
  325. int width_;
  326. std::string envelope_color_;
  327. bool ulp_envelope_;
  328. int horizontal_lines_;
  329. int vertical_lines_;
  330. std::string title_;
  331. std::string background_color_;
  332. std::string font_color_;
  333. std::string crop_color_;
  334. std::string nan_color_;
  335. };
  336. template<class F, typename PreciseReal, typename CoarseReal>
  337. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::envelope_color(std::string const & color)
  338. {
  339. envelope_color_ = color;
  340. return *this;
  341. }
  342. template<class F, typename PreciseReal, typename CoarseReal>
  343. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::clip(PreciseReal clip)
  344. {
  345. clip_ = clip;
  346. return *this;
  347. }
  348. template<class F, typename PreciseReal, typename CoarseReal>
  349. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::width(int width)
  350. {
  351. width_ = width;
  352. return *this;
  353. }
  354. template<class F, typename PreciseReal, typename CoarseReal>
  355. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::horizontal_lines(int horizontal_lines)
  356. {
  357. horizontal_lines_ = horizontal_lines;
  358. return *this;
  359. }
  360. template<class F, typename PreciseReal, typename CoarseReal>
  361. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::vertical_lines(int vertical_lines)
  362. {
  363. vertical_lines_ = vertical_lines;
  364. return *this;
  365. }
  366. template<class F, typename PreciseReal, typename CoarseReal>
  367. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::title(std::string const & title)
  368. {
  369. title_ = title;
  370. return *this;
  371. }
  372. template<class F, typename PreciseReal, typename CoarseReal>
  373. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::background_color(std::string const & background_color)
  374. {
  375. background_color_ = background_color;
  376. return *this;
  377. }
  378. template<class F, typename PreciseReal, typename CoarseReal>
  379. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::font_color(std::string const & font_color)
  380. {
  381. font_color_ = font_color;
  382. return *this;
  383. }
  384. template<class F, typename PreciseReal, typename CoarseReal>
  385. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::crop_color(std::string const & color)
  386. {
  387. crop_color_ = color;
  388. return *this;
  389. }
  390. template<class F, typename PreciseReal, typename CoarseReal>
  391. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::nan_color(std::string const & color)
  392. {
  393. nan_color_ = color;
  394. return *this;
  395. }
  396. template<class F, typename PreciseReal, typename CoarseReal>
  397. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::ulp_envelope(bool write_ulp_envelope)
  398. {
  399. ulp_envelope_ = write_ulp_envelope;
  400. return *this;
  401. }
  402. namespace detail{
  403. bool ends_with(std::string const& filename, std::string const& suffix)
  404. {
  405. if(filename.size() < suffix.size())
  406. {
  407. return false;
  408. }
  409. return std::equal(std::begin(suffix), std::end(suffix), std::end(filename) - suffix.size());
  410. }
  411. }
  412. template<class F, typename PreciseReal, typename CoarseReal>
  413. void ulps_plot<F, PreciseReal, CoarseReal>::write(std::string const & filename) const
  414. {
  415. if(!boost::math::tools::detail::ends_with(filename, ".svg"))
  416. {
  417. throw std::logic_error("Only svg files are supported at this time.");
  418. }
  419. std::ofstream fs(filename);
  420. fs << *this;
  421. fs.close();
  422. }
  423. template<class F, typename PreciseReal, typename CoarseReal>
  424. ulps_plot<F, PreciseReal, CoarseReal>::ulps_plot(F hi_acc_impl, CoarseReal a, CoarseReal b,
  425. size_t samples, bool perturb_abscissas, int random_seed) : crop_color_("red")
  426. {
  427. // Use digits10 for this comparison in case the two types have differeing radixes:
  428. static_assert(std::numeric_limits<PreciseReal>::digits10 >= std::numeric_limits<CoarseReal>::digits10, "PreciseReal must have higher precision that CoarseReal");
  429. if (samples < 10)
  430. {
  431. throw std::domain_error("Must have at least 10 samples, samples = " + std::to_string(samples));
  432. }
  433. if (b <= a)
  434. {
  435. throw std::domain_error("On interval [a,b], b > a is required.");
  436. }
  437. a_ = a;
  438. b_ = b;
  439. std::mt19937_64 gen;
  440. if (random_seed == -1)
  441. {
  442. std::random_device rd;
  443. gen.seed(rd());
  444. }
  445. // Boost's uniform_real_distribution can generate quad and multiprecision random numbers; std's cannot:
  446. #ifndef BOOST_MATH_STANDALONE
  447. boost::random::uniform_real_distribution<PreciseReal> dis(static_cast<PreciseReal>(a), static_cast<PreciseReal>(b));
  448. #else
  449. // Use std::random in standalone mode if it is a type that the standard library can support (float, double, or long double)
  450. static_assert(std::numeric_limits<PreciseReal>::digits10 <= std::numeric_limits<long double>::digits10, "Standalone mode does not support types with precision that exceeds long double");
  451. std::uniform_real_distribution<PreciseReal> dis(static_cast<PreciseReal>(a), static_cast<PreciseReal>(b));
  452. #endif
  453. precise_abscissas_.resize(samples);
  454. coarse_abscissas_.resize(samples);
  455. if (perturb_abscissas)
  456. {
  457. for(size_t i = 0; i < samples; ++i)
  458. {
  459. precise_abscissas_[i] = dis(gen);
  460. }
  461. std::sort(precise_abscissas_.begin(), precise_abscissas_.end());
  462. for (size_t i = 0; i < samples; ++i)
  463. {
  464. coarse_abscissas_[i] = static_cast<CoarseReal>(precise_abscissas_[i]);
  465. }
  466. }
  467. else
  468. {
  469. for(size_t i = 0; i < samples; ++i)
  470. {
  471. coarse_abscissas_[i] = static_cast<CoarseReal>(dis(gen));
  472. }
  473. std::sort(coarse_abscissas_.begin(), coarse_abscissas_.end());
  474. for (size_t i = 0; i < samples; ++i)
  475. {
  476. precise_abscissas_[i] = static_cast<PreciseReal>(coarse_abscissas_[i]);
  477. }
  478. }
  479. precise_ordinates_.resize(samples);
  480. for (size_t i = 0; i < samples; ++i)
  481. {
  482. precise_ordinates_[i] = hi_acc_impl(precise_abscissas_[i]);
  483. }
  484. cond_.resize(samples, std::numeric_limits<PreciseReal>::quiet_NaN());
  485. for (size_t i = 0 ; i < samples; ++i)
  486. {
  487. PreciseReal y = precise_ordinates_[i];
  488. if (y != 0)
  489. {
  490. // Maybe cond_ is badly names; should it be half_cond_?
  491. cond_[i] = boost::math::tools::evaluation_condition_number(hi_acc_impl, precise_abscissas_[i])/2;
  492. // Half-ULP accuracy is the correctly rounded result, so make sure the envelop doesn't go below this:
  493. if (cond_[i] < 0.5)
  494. {
  495. cond_[i] = 0.5;
  496. }
  497. }
  498. // else leave it as nan.
  499. }
  500. clip_ = -1;
  501. width_ = 1100;
  502. envelope_color_ = "chartreuse";
  503. ulp_envelope_ = true;
  504. horizontal_lines_ = 8;
  505. vertical_lines_ = 10;
  506. title_ = "";
  507. background_color_ = "black";
  508. font_color_ = "white";
  509. }
  510. template<class F, typename PreciseReal, typename CoarseReal>
  511. template<class G>
  512. ulps_plot<F, PreciseReal, CoarseReal>& ulps_plot<F, PreciseReal, CoarseReal>::add_fn(G g, std::string const & color)
  513. {
  514. using std::abs;
  515. size_t samples = precise_abscissas_.size();
  516. std::vector<CoarseReal> ulps(samples);
  517. for (size_t i = 0; i < samples; ++i)
  518. {
  519. PreciseReal y_hi_acc = precise_ordinates_[i];
  520. PreciseReal y_lo_acc = static_cast<PreciseReal>(g(coarse_abscissas_[i]));
  521. PreciseReal absy = abs(y_hi_acc);
  522. PreciseReal dist = static_cast<PreciseReal>(nextafter(static_cast<CoarseReal>(absy), (std::numeric_limits<CoarseReal>::max)()) - static_cast<CoarseReal>(absy));
  523. ulps[i] = static_cast<CoarseReal>((y_lo_acc - y_hi_acc)/dist);
  524. }
  525. ulp_list_.emplace_back(ulps);
  526. colors_.emplace_back(color);
  527. return *this;
  528. }
  529. } // namespace boost::math::tools
  530. #endif