File: root_finding_algorithms.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (905 lines) | stat: -rw-r--r-- 33,745 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// Copyright Paul A. Bristow 2015

// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

// Comparison of finding roots using TOMS748, Newton-Raphson, Schroder & Halley algorithms.

// Note that this file contains Quickbook mark-up as well as code
// and comments, don't change any of the special comment mark-ups!

// root_finding_algorithms.cpp

#include <boost/cstdlib.hpp>
#include <boost/config.hpp>
#include <boost/array.hpp>
#include "table_type.hpp"
// Copy of i:\modular-boost\libs\math\test\table_type.hpp
// #include "handle_test_result.hpp"
// Copy of i:\modular - boost\libs\math\test\handle_test_result.hpp

#include <boost/math/tools/roots.hpp>
//using boost::math::policies::policy;
//using boost::math::tools::newton_raphson_iterate;
//using boost::math::tools::halley_iterate; //
//using boost::math::tools::eps_tolerance; // Binary functor for specified number of bits.
//using boost::math::tools::bracket_and_solve_root;
//using boost::math::tools::toms748_solve;
//using boost::math::tools::schroder_iterate;

#include <boost/math/special_functions/next.hpp> // For float_distance.
#include <tuple> // for tuple and make_tuple.
#include <boost/math/special_functions/cbrt.hpp> // For boost::math::cbrt.

#include <boost/multiprecision/cpp_bin_float.hpp> // is binary.
//#include <boost/multiprecision/cpp_dec_float.hpp> // is decimal.
using boost::multiprecision::cpp_bin_float_100;
using boost::multiprecision::cpp_bin_float_50;

#include <boost/timer/timer.hpp>
#include <boost/system/error_code.hpp>
#include <boost/multiprecision/cpp_bin_float/io.hpp>
#include <boost/preprocessor/stringize.hpp>

// STL
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <limits>
#include <fstream> // std::ofstream
#include <cmath>
#include <typeinfo> // for type name using typid(thingy).name();
#include <type_traits>

#ifndef BOOST_ROOT
# define BOOST_ROOT i:/modular-boost/
#endif
// Need to find this 

#ifdef __FILE__
std::string sourcefilename = __FILE__;
#endif

std::string chop_last(std::string s)
{
   std::string::size_type pos = s.find_last_of("\\/");
   if(pos != std::string::npos)
      s.erase(pos);
   else if(s.empty())
      abort();
   else
      s.erase();
   return s;
}

std::string make_root()
{
   std::string result;
   if(sourcefilename.find_first_of(":") != std::string::npos)
   {
      result = chop_last(sourcefilename); // lose filename part
      result = chop_last(result);   // lose /example/
      result = chop_last(result);   // lose /math/
      result = chop_last(result);   // lose /libs/
   }
   else
   {
      result = chop_last(sourcefilename); // lose filename part
      if(result.empty())
         result = ".";
      result += "/../../..";
   }
   return result;
}

std::string short_file_name(std::string s)
{
   std::string::size_type pos = s.find_last_of("\\/");
   if(pos != std::string::npos)
      s.erase(0, pos + 1);
   return s;
}

std::string boost_root = make_root();

#ifdef _MSC_VER
  std::string filename = boost_root.append("/libs/math/doc/roots/root_comparison_tables_msvc.qbk");
#else // assume GCC
  std::string filename = boost_root.append("/libs/math/doc/roots/root_comparison_tables_gcc.qbk");
#endif

std::ofstream fout (filename.c_str(), std::ios_base::out);

//std::array<std::string, 6> float_type_names =
//{
//  "float", "double", "long double", "cpp_bin_128", "cpp_dec_50", "cpp_dec_100"
//};

std::vector<std::string> algo_names =
{
  "cbrt", "TOMS748", "Newton", "Halley", "Schr'''&#xf6;'''der"
};

std::vector<int> max_digits10s;
std::vector<std::string> typenames; // Full computer generated type name.
std::vector<std::string> names; // short name.

uintmax_t iters; // Global as iterations is not returned by rooting function.

const int count = 1000000; // Number of iterations to average.
 
struct root_info
{ // for a floating-point type, float, double ...
  std::size_t max_digits10; // for type.
  std::string full_typename; // for type from type_id.name().
  std::string short_typename; // for type "float", "double", "cpp_bin_float_50" ....

  std::size_t bin_digits;  // binary in floating-point type numeric_limits<T>::digits;  
  int get_digits; // fraction of maximum possible accuracy required.
  // = digits * digits_accuracy
  // Vector of values for each algorithm, std::cbrt, boost::math::cbrt, TOMS748, Newton, Halley.
  //std::vector< std::int_least64_t> times;  converted to int.
  std::vector<int> times;
  //std::int_least64_t min_time = std::numeric_limits<std::int_least64_t>::max(); // Used to normalize times (as int).
  std::vector<double> normed_times;
  std::int_least64_t min_time = (std::numeric_limits<std::int_least64_t>::max)(); // Used to normalize times.
  std::vector<uintmax_t> iterations;
  std::vector<long int> distances;
  std::vector<cpp_bin_float_100> full_results;
}; // struct root_info

std::vector<root_info> root_infos;  // One element for each type used.

int type_no = -1; // float = 0, double = 1, ... indexing root_infos.

inline std::string build_test_name(const char* type_name, const char* test_name)
{
  std::string result(BOOST_COMPILER);
  result += "|";
  result += BOOST_STDLIB;
  result += "|";
  result += BOOST_PLATFORM;
  result += "|";
  result += type_name;
  result += "|";
  result += test_name;
#if defined(_DEBUG ) || !defined(NDEBUG)
  result += "|";
  result += " debug";
#else
  result += "|";
  result += " release";
#endif
  result += "|";
    return result;
}

// No derivatives - using TOMS748 internally.
template <class T>
struct cbrt_functor_noderiv
{ //  cube root of x using only function - no derivatives.
  cbrt_functor_noderiv(T const& to_find_root_of) : a(to_find_root_of)
  { // Constructor just stores value a to find root of.
  }
  T operator()(T const& x)
  {
    T fx = x*x*x - a; // Difference (estimate x^3 - a).
    return fx;
  }
private:
  T a; // to be 'cube_rooted'.
}; // template <class T> struct cbrt_functor_noderiv

template <class T>
T cbrt_noderiv(T x)
{ // return cube root of x using bracket_and_solve (using NO derivatives).
  using namespace std;  // Help ADL of std functions.
  using namespace boost::math::tools; // For bracket_and_solve_root.

  // Maybe guess should be double, or use enable_if to avoid warning about conversion double to float here?
  T guess;
  if (std::is_fundamental<T>::value)
  { 
    int exponent;
    frexp(x, &exponent); // Get exponent of z (ignore mantissa).
    guess = ldexp((T)1., exponent / 3); // Rough guess is to divide the exponent by three.
  }
  else
  { // (boost::is_class<T>)
    double dx = static_cast<double>(x);
    guess = boost::math::cbrt<T>(dx); // Get guess using double.
  }
  
  T factor = 2; // How big steps to take when searching.

  const std::uintmax_t maxit = 50; // Limit to maximum iterations.
  std::uintmax_t it = maxit; // Initially our chosen max iterations, but updated with actual.
  bool is_rising = true; // So if result if guess^3 is too low, then try increasing guess.
  // Some fraction of digits is used to control how accurate to try to make the result.
  int get_digits = static_cast<int>(std::numeric_limits<T>::digits - 2);

  eps_tolerance<T> tol(get_digits); // Set the tolerance.
  std::pair<T, T> r =
    bracket_and_solve_root(cbrt_functor_noderiv<T>(x), guess, factor, is_rising, tol, it);
  iters = it;
  T result = r.first + (r.second - r.first) / 2;  // Midway between brackets.
  return result;
} // template <class T> T cbrt_noderiv(T x)


// Using 1st derivative only Newton-Raphson

template <class T>
struct cbrt_functor_deriv
{ // Functor also returning 1st derivative.
  cbrt_functor_deriv(T const& to_find_root_of) : a(to_find_root_of)
  { // Constructor stores value a to find root of,
    // for example: calling cbrt_functor_deriv<T>(x) to use to get cube root of x.
  }
  std::pair<T, T> operator()(T const& x)
  { // Return both f(x) and f'(x).
    T fx = x*x*x - a; // Difference (estimate x^3 - value).
    T dx = 3 * x*x; // 1st derivative = 3x^2.
    return std::make_pair(fx, dx); // 'return' both fx and dx.
  }
private:
  T a; // to be 'cube_rooted'.
};

template <class T>
T cbrt_deriv(T x)
{ // return cube root of x using 1st derivative and Newton_Raphson.
  using namespace boost::math::tools;
  int exponent;
  T guess;
  if(std::is_fundamental<T>::value)
  {
     frexp(x, &exponent); // Get exponent of z (ignore mantissa).
     guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
  }
  else
     guess = boost::math::cbrt(static_cast<double>(x));
  T min = guess / 2; // Minimum possible value is half our guess.
  T max = 2 * guess; // Maximum possible value is twice our guess.
  int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.6);
  const std::uintmax_t maxit = 20;
  std::uintmax_t it = maxit;
  T result = newton_raphson_iterate(cbrt_functor_deriv<T>(x), guess, min, max, get_digits, it);
  iters = it;
  return result;
}

// Using 1st and 2nd derivatives with Halley algorithm.

template <class T>
struct cbrt_functor_2deriv
{ // Functor returning both 1st and 2nd derivatives.
  cbrt_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
  { // Constructor stores value a to find root of, for example:
    // calling cbrt_functor_2deriv<T>(x) to get cube root of x,
  }
  std::tuple<T, T, T> operator()(T const& x)
  { // Return both f(x) and f'(x) and f''(x).
    T fx = x*x*x - a; // Difference (estimate x^3 - value).
    T dx = 3 * x*x; // 1st derivative = 3x^2.
    T d2x = 6 * x; // 2nd derivative = 6x.
    return std::make_tuple(fx, dx, d2x); // 'return' fx, dx and d2x.
  }
private:
  T a; // to be 'cube_rooted'.
};

template <class T>
T cbrt_2deriv(T x)
{ // return cube root of x using 1st and 2nd derivatives and Halley.
  //using namespace std;  // Help ADL of std functions.
  using namespace boost::math::tools;
  int exponent;
  T guess;
  if(std::is_fundamental<T>::value)
  {
     frexp(x, &exponent); // Get exponent of z (ignore mantissa).
     guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
  }
  else
     guess = boost::math::cbrt(static_cast<double>(x));
  T min = guess / 2; // Minimum possible value is half our guess.
  T max = 2 * guess; // Maximum possible value is twice our guess.
  // digits used to control how accurate to try to make the result.
  int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
  std::uintmax_t maxit = 20;
  std::uintmax_t it = maxit;
  T result = halley_iterate(cbrt_functor_2deriv<T>(x), guess, min, max, get_digits, it);
  iters = it;
  return result;
}

// Using 1st and 2nd derivatives using Schroder algorithm.

template <class T>
T cbrt_2deriv_s(T x)
{ // return cube root of x using 1st and 2nd derivatives and Schroder algorithm.
  //using namespace std;  // Help ADL of std functions.
  using namespace boost::math::tools;
  int exponent;
  T guess;
  if(std::is_fundamental<T>::value)
  {
     frexp(x, &exponent); // Get exponent of z (ignore mantissa).
     guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
  }
  else
     guess = boost::math::cbrt(static_cast<double>(x));
  T min = guess / 2; // Minimum possible value is half our guess.
  T max = 2 * guess; // Maximum possible value is twice our guess.
  // digits used to control how accurate to try to make the result.
  int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
  const std::uintmax_t maxit = 20;
  std::uintmax_t it = maxit;
  T result = schroder_iterate(cbrt_functor_2deriv<T>(x), guess, min, max, get_digits, it);
  iters = it;
  return result;
} // template <class T> T cbrt_2deriv_s(T x)



template <typename T>
int test_root(cpp_bin_float_100 big_value, cpp_bin_float_100 answer, const char* type_name)
{
  //T value = 28.; // integer (exactly representable as floating-point)
  // whose cube root is *not* exactly representable.
  // Wolfram Alpha command N[28 ^ (1 / 3), 100] computes cube root to 100 decimal digits.
  // 3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895
  
  std::size_t max_digits = 2 + std::numeric_limits<T>::digits * 3010 / 10000;
  // For new versions use max_digits10
  // std::cout.precision(std::numeric_limits<T>::max_digits10);
  std::cout.precision(max_digits);
  std::cout << std::showpoint << std::endl; // Trailing zeros too.

  root_infos.push_back(root_info());
  type_no++;  // Another type.

  root_infos[type_no].max_digits10 = max_digits;
  root_infos[type_no].full_typename = typeid(T).name(); // Full typename.
  root_infos[type_no].short_typename = type_name; // Short typename.

  root_infos[type_no].bin_digits = std::numeric_limits<T>::digits;

  root_infos[type_no].get_digits = std::numeric_limits<T>::digits;

  T to_root = static_cast<T>(big_value);
  T result; // root
  T ans = static_cast<T>(answer);
  int algo = 0; // Count of algorithms used.
 
  using boost::timer::nanosecond_type;
  using boost::timer::cpu_times;
  using boost::timer::cpu_timer;

  cpu_times now; // Holds wall, user and system times.
  T sum = 0;

  // std::cbrt is much the fastest, but not useful for this comparison because it only handles fundamental types.
  // Using enable_if allows us to avoid a compile fail with multiprecision types, but still distorts the results too much.

  //{
  //  algorithm_names.push_back("std::cbrt"); 
  //  cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
  //  ti.start();
  //  for (long i = 0; i < count; ++i)
  //  {
  //    stdcbrt(big_value);
  //  }
  //  now = ti.elapsed();
  //  int time = static_cast<int>(now.user / count);
  //  root_infos[type_no].times.push_back(time); // CPU time taken per root.
  //  if (time < root_infos[type_no].min_time)
  //  {
  //    root_infos[type_no].min_time = time;
  //  }
  //  ti.stop();
  //  long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
  //  root_infos[type_no].distances.push_back(distance);
  //  root_infos[type_no].iterations.push_back(0); // Not known.
  //  root_infos[type_no].full_results.push_back(result);
  //  algo++;
  //}
  //{
  //  //algorithm_names.push_back("boost::math::cbrt"); // .
  //  cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
  //  ti.start();
  //  for (long i = 0; i < count; ++i)
  //  {
  //    result = boost::math::cbrt(to_root); // 
  //  }
  //  now = ti.elapsed();
  //  int time = static_cast<int>(now.user / count);
  //  root_infos[type_no].times.push_back(time); // CPU time taken.
  //  ti.stop();
  //  if (time < root_infos[type_no].min_time)
  //  {
  //    root_infos[type_no].min_time = time;
  //  }
  //  long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
  //  root_infos[type_no].distances.push_back(distance);
  //  root_infos[type_no].iterations.push_back(0); // Iterations not knowable.
  //  root_infos[type_no].full_results.push_back(result);
  //}



  {
    //algorithm_names.push_back("boost::math::cbrt"); // .
    result = 0;
    cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
    ti.start();
    for (long i = 0; i < count; ++i)
    {
      result = boost::math::cbrt(to_root); // 
      sum += result;
    }
    now = ti.elapsed();

    long time = static_cast<long>(now.user/1000); // convert nanoseconds to microseconds (assuming this is resolution).
    root_infos[type_no].times.push_back(time); // CPU time taken.
    ti.stop();
    if (time < root_infos[type_no].min_time)
    {
      root_infos[type_no].min_time = time;
    }
    long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
    root_infos[type_no].distances.push_back(distance);
    root_infos[type_no].iterations.push_back(0); // Iterations not knowable.
    root_infos[type_no].full_results.push_back(result);
  }
  {
    //algorithm_names.push_back("TOMS748"); // 
    cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
    ti.start();
    for (long i = 0; i < count; ++i)
    {
      result = cbrt_noderiv<T>(to_root); // 
      sum += result;
    }
    now = ti.elapsed();
//    int time = static_cast<int>(now.user / count);
    long time = static_cast<long>(now.user/1000);
    root_infos[type_no].times.push_back(time); // CPU time taken.
    if (time < root_infos[type_no].min_time)
    {
      root_infos[type_no].min_time = time;
    }
    ti.stop();
    long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
    root_infos[type_no].distances.push_back(distance);
    root_infos[type_no].iterations.push_back(iters); // 
    root_infos[type_no].full_results.push_back(result);
  }
  {
   // algorithm_names.push_back("Newton"); // algorithm
    cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
    ti.start();
    for (long i = 0; i < count; ++i)
    {
      result = cbrt_deriv(to_root); // 
      sum += result;
    }
    now = ti.elapsed();
//    int time = static_cast<int>(now.user / count);
    long time = static_cast<long>(now.user/1000);
    root_infos[type_no].times.push_back(time); // CPU time taken.
    if (time < root_infos[type_no].min_time)
    {
      root_infos[type_no].min_time = time;
    }

    ti.stop();
    long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
    root_infos[type_no].distances.push_back(distance);
    root_infos[type_no].iterations.push_back(iters); //
    root_infos[type_no].full_results.push_back(result);
  }
  {
  //algorithm_names.push_back("Halley"); // algorithm
    cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
    ti.start();
    for (long i = 0; i < count; ++i)
    {
      result = cbrt_2deriv(to_root); // 
      sum += result;
    }
    now = ti.elapsed(); 
//    int time = static_cast<int>(now.user / count);
    long time = static_cast<long>(now.user/1000);
    root_infos[type_no].times.push_back(time); // CPU time taken.
    ti.stop();
    if (time < root_infos[type_no].min_time)
    {
      root_infos[type_no].min_time = time;
    }
    long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
    root_infos[type_no].distances.push_back(distance);
    root_infos[type_no].iterations.push_back(iters); // 
    root_infos[type_no].full_results.push_back(result);
  }

  {
   // algorithm_names.push_back("Shroeder"); // algorithm
    cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
    ti.start();
    for (long i = 0; i < count; ++i)
    {
      result = cbrt_2deriv_s(to_root); // 
      sum += result;
    }
    now = ti.elapsed();
//    int time = static_cast<int>(now.user / count);
    long time = static_cast<long>(now.user/1000);
    root_infos[type_no].times.push_back(time); // CPU time taken.
    if (time < root_infos[type_no].min_time)
    {
      root_infos[type_no].min_time = time;
    }
    ti.stop();
    long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
    root_infos[type_no].distances.push_back(distance);
    root_infos[type_no].iterations.push_back(iters); // 
    root_infos[type_no].full_results.push_back(result);
  }
  for (size_t i = 0; i != root_infos[type_no].times.size(); i++)
  { // Normalize times.
    double normed_time = static_cast<double>(root_infos[type_no].times[i]);
    normed_time /= root_infos[type_no].min_time;
    root_infos[type_no].normed_times.push_back(normed_time);
  }
  algo++;
  std::cout << "Accumulated sum was " << sum << std::endl;
  return algo;  // Count of how many algorithms used.
} // test_root

void table_root_info(cpp_bin_float_100 full_value, cpp_bin_float_100 full_answer)
{
   // Fill the elements. 
  test_root<float>(full_value, full_answer, "float");
  test_root<double>(full_value, full_answer, "double");
  test_root<long double>(full_value, full_answer, "long double");
  test_root<cpp_bin_float_50>(full_value, full_answer, "cpp_bin_float_50");
  //test_root<cpp_bin_float_100>(full_value, full_answer, "cpp_bin_float_100");

  std::cout << root_infos.size() << " floating-point types tested:" << std::endl;
#ifndef NDEBUG
  std::cout << "Compiled in debug mode." << std::endl;
#else
  std::cout << "Compiled in optimise mode." << std::endl;
#endif


  for (size_t tp = 0; tp != root_infos.size(); tp++)
  { // For all types:

    std::cout << std::endl;

    std::cout << "Floating-point type = " << root_infos[tp].short_typename << std::endl;
    std::cout << "Floating-point type = " << root_infos[tp].full_typename << std::endl;
    std::cout << "Max_digits10 = " << root_infos[tp].max_digits10 << std::endl;
    std::cout << "Binary digits = " << root_infos[tp].bin_digits << std::endl;
    std::cout << "Accuracy digits = " << root_infos[tp].get_digits - 2 << ", " << static_cast<int>(root_infos[tp].get_digits * 0.6) << ", " << static_cast<int>(root_infos[tp].get_digits * 0.4) << std::endl;
    std::cout << "min_time = " << root_infos[tp].min_time << std::endl;

    std::cout << std::setprecision(root_infos[tp].max_digits10 ) << "Roots = ";
    std::copy(root_infos[tp].full_results.begin(), root_infos[tp].full_results.end(), std::ostream_iterator<cpp_bin_float_100>(std::cout, " "));
    std::cout << std::endl;

    // Header row.
    std::cout << "Algorithm         " << "Iterations  " << "Times  " << "Norm_times  " << "Distance" << std::endl;

    // Row for all algorithms.
    for (unsigned algo = 0; algo != algo_names.size(); algo++)
    { 
      std::cout
        << std::left << std::setw(20) << algo_names[algo] << "  "
        << std::setw(8) << std::setprecision(2) << root_infos[tp].iterations[algo] << "  "
        << std::setw(8) << std::setprecision(5) << root_infos[tp].times[algo] << " "
        << std::setw(8) << std::setprecision(3) << root_infos[tp].normed_times[algo] << " "
        << std::setw(8) << std::setprecision(2) << root_infos[tp].distances[algo]
        << std::endl;
    } // for algo
  } // for tp

  // Print info as Quickbook table.
#if 0
  fout << "[table:cbrt_5  Info for float, double, long double and cpp_bin_float_50\n"
    << "[[type name] [max_digits10] [binary digits] [required digits]]\n";// header.

  for (size_t tp = 0; tp != root_infos.size(); tp++)
  { // For all types:
    fout << "["
     <<  "[" << root_infos[tp].short_typename << "]" 
      << "[" << root_infos[tp].max_digits10 << "]"  // max_digits10
      << "["  << root_infos[tp].bin_digits << "]"// < "Binary digits 
      << "["  << root_infos[tp].get_digits << "]]\n"; // Accuracy digits.
  } // tp
  fout << "] [/table cbrt_5] \n" << std::endl;
#endif
  // Prepare Quickbook table of floating-point types.
  fout << "[table:cbrt_4 Cube root(28) for float, double, long double and cpp_bin_float_50\n"
    << "[[][float][][][] [][double][][][] [][long d][][][] [][cpp50][][]]\n"
    << "[[Algorithm]"; 
  for (size_t tp = 0; tp != root_infos.size(); tp++)
  { // For all types:
    fout << "[Its]" << "[Times]" << "[Norm]" << "[Dis]" << "[ ]";
  }
  fout << "]" << std::endl;

  // Row for all algorithms.
  for (size_t algo = 0; algo != algo_names.size(); algo++)
  {
    fout << "[[" << std::left << std::setw(9) << algo_names[algo] << "]";
    for (size_t tp = 0; tp != root_infos.size(); tp++)
    { // For all types:

       fout
          << "[" << std::right << std::showpoint
          << std::setw(3) << std::setprecision(2) << root_infos[tp].iterations[algo] << "]["
          << std::setw(5) << std::setprecision(5) << root_infos[tp].times[algo] << "][";
       if(fabs(root_infos[tp].normed_times[algo]) <= 1.05)
          fout << "[role blue " << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo] << "]";
       else if(fabs(root_infos[tp].normed_times[algo]) > 4)
          fout << "[role red " << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo] << "]";
       else
          fout << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo];
       fout
        << "]["
        << std::setw(3) << std::setprecision(2) << root_infos[tp].distances[algo] << "][ ]";
    } // tp
     fout <<"]" << std::endl;
  } // for algo
  fout << "] [/end of table cbrt_4]\n";
} // void table_root_info

int main()
{
  using namespace boost::multiprecision;
  using namespace boost::math;
 
  try
  {
    std::cout << "Tests run with " << BOOST_COMPILER << ", "
      << BOOST_STDLIB << ", " << BOOST_PLATFORM << ", ";

    if (fout.is_open())
    {
      std::cout << "\nOutput to " << filename << std::endl;
    }
    else
    { // Failed to open.
      std::cout << " Open file " << filename << " for output failed!" << std::endl;
      std::cout << "error" << errno << std::endl;
      return boost::exit_failure;
    }

    fout <<
      "[/""\n"
      "Copyright 2015 Paul A. Bristow.""\n"
      "Copyright 2015 John Maddock.""\n"
      "Distributed under the Boost Software License, Version 1.0.""\n"
      "(See accompanying file LICENSE_1_0.txt or copy at""\n"
      "http://www.boost.org/LICENSE_1_0.txt).""\n"
      "]""\n"
      << std::endl;
    std::string debug_or_optimize;
#ifdef _DEBUG
#if (_DEBUG == 0)
    debug_or_optimize = "Compiled in debug mode.";
#else
    debug_or_optimize = "Compiled in optimise mode.";
#endif
#endif

    // Print out the program/compiler/stdlib/platform names as a Quickbook comment:
    fout << "\n[h5 Program " << short_file_name(sourcefilename) << ", "
      << BOOST_COMPILER << ", "
      << BOOST_STDLIB << ", "
      << BOOST_PLATFORM << (sizeof(void*) == 8 ? ", x64" : ", x86")
      << debug_or_optimize << "[br]"
      << count << " evaluations of each of " << algo_names.size() << " root_finding algorithms."
      << "]"
      << std::endl;
    
    std::cout << count << " evaluations of root_finding." << std::endl;

    BOOST_MATH_CONTROL_FP;

    cpp_bin_float_100 full_value("28");

    cpp_bin_float_100 full_answer ("3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895");

    std::copy(max_digits10s.begin(), max_digits10s.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    table_root_info(full_value, full_answer);


    return boost::exit_success;
  }
  catch (std::exception const& ex)
  {
    std::cout << "exception thrown: " << ex.what() << std::endl;
    return boost::exit_failure;
  }
} // int main()

/*
debug

1>  float, maxdigits10 = 9
1>  6 algorithms used.
1>  Digits required = 24.0000000
1>  find root of 28.0000000, expected answer = 3.03658897
1>  Times 156 312 18750 4375 3437 3906
1>  Iterations: 0 0 8 6 4 5
1>  Distance: 0 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891

release

1>  float, maxdigits10 = 9
1>  6 algorithms used.
1>  Digits required = 24.0000000
1>  find root of 28.0000000, expected answer = 3.03658897
1>  Times 0 312 6875 937 937 937
1>  Iterations: 0 0 8 6 4 5
1>  Distance: 0 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891


1>
1>  5 algorithms used:
1>  10 algorithms used:
1>  boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
1>  2 types compared.
1>  Precision of full type = 102 decimal digits
1>  Find root of 28.000000000000000,
1>  Expected answer = 3.0365889718756625
1>  typeid(T).name()float, maxdigits10 = 9
1>  find root of 28.0000000, expected answer = 3.03658897
1>
1>  Iterations: 0 8 6 4 5
1>  Times 468 8437 4375 3593 4062
1>  Min Time 468
1>  Normalized Times 1.00 18.0 9.35 7.68 8.68
1>  Distance: 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
1>  ==================================================================
1>  typeid(T).name()double, maxdigits10 = 17
1>  find root of 28.000000000000000, expected answer = 3.0365889718756625
1>
1>  Iterations: 0 11 7 5 6
1>  Times 312 15000 4531 3906 4375
1>  Min Time 312
1>  Normalized Times 1.00 48.1 14.5 12.5 14.0
1>  Distance: 1 2 0 0 0
1>  Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
1>  ==================================================================


Release

1>  5 algorithms used:
1>  10 algorithms used:
1>  boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
1>  2 types compared.
1>  Precision of full type = 102 decimal digits
1>  Find root of 28.000000000000000,
1>  Expected answer = 3.0365889718756625
1>  typeid(T).name()float, maxdigits10 = 9
1>  find root of 28.0000000, expected answer = 3.03658897
1>
1>  Iterations: 0 8 6 4 5
1>  Times 312 781 937 937 937
1>  Min Time 312
1>  Normalized Times 1.00 2.50 3.00 3.00 3.00
1>  Distance: 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
1>  ==================================================================
1>  typeid(T).name()double, maxdigits10 = 17
1>  find root of 28.000000000000000, expected answer = 3.0365889718756625
1>
1>  Iterations: 0 11 7 5 6
1>  Times 312 1093 937 937 937
1>  Min Time 312
1>  Normalized Times 1.00 3.50 3.00 3.00 3.00
1>  Distance: 1 2 0 0 0
1>  Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
1>  ==================================================================



1>  5 algorithms used:
1>  15 algorithms used:
1>  boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
1>  3 types compared.
1>  Precision of full type = 102 decimal digits
1>  Find root of 28.00000000000000000000000000000000000000000000000000,
1>  Expected answer = 3.036588971875662519420809578505669635581453977248111
1>  typeid(T).name()float, maxdigits10 = 9
1>  find root of 28.0000000, expected answer = 3.03658897
1>
1>  Iterations: 0 8 6 4 5
1>  Times 156 781 937 1093 937
1>  Min Time 156
1>  Normalized Times 1.00 5.01 6.01 7.01 6.01
1>  Distance: 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
1>  ==================================================================
1>  typeid(T).name()double, maxdigits10 = 17
1>  find root of 28.000000000000000, expected answer = 3.0365889718756625
1>
1>  Iterations: 0 11 7 5 6
1>  Times 312 1093 937 937 937
1>  Min Time 312
1>  Normalized Times 1.00 3.50 3.00 3.00 3.00
1>  Distance: 1 2 0 0 0
1>  Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
1>  ==================================================================
1>  typeid(T).name()class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50,10,void,int,0,0>,0>, maxdigits10 = 52
1>  find root of 28.00000000000000000000000000000000000000000000000000, expected answer = 3.036588971875662519420809578505669635581453977248111
1>
1>  Iterations: 0 13 9 6 7
1>  Times 8750 177343 30312 52968 58125
1>  Min Time 8750
1>  Normalized Times 1.00 20.3 3.46 6.05 6.64
1>  Distance: 0 0 -1 0 0
1>  Roots: 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248117 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106
1>  ==================================================================

Reduce accuracy required to 0.5

1>  5 algorithms used:
1>  15 algorithms used:
1>  boost::math::cbrt TOMS748 Newton Halley Shroeder
1>  3 floating_point types compared.
1>  Precision of full type = 102 decimal digits
1>  Find root of 28.00000000000000000000000000000000000000000000000000,
1>  Expected answer = 3.036588971875662519420809578505669635581453977248111
1>  typeid(T).name() = float, maxdigits10 = 9
1>  Digits accuracy fraction required = 0.500000000
1>  find root of 28.0000000, expected answer = 3.03658897
1>
1>  Iterations: 0 8 5 3 4
1>  Times 156 5937 1406 1250 1250
1>  Min Time 156
1>  Normalized Times 1.0 38. 9.0 8.0 8.0
1>  Distance: 0 -1 0 0 0
1>  Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
1>  ==================================================================
1>  typeid(T).name() = double, maxdigits10 = 17
1>  Digits accuracy fraction required = 0.50000000000000000
1>  find root of 28.000000000000000, expected answer = 3.0365889718756625
1>
1>  Iterations: 0 8 6 4 5
1>  Times 156 6250 1406 1406 1250
1>  Min Time 156
1>  Normalized Times 1.0 40. 9.0 9.0 8.0
1>  Distance: 1 3695766 0 0 0
1>  Roots: 3.0365889718756622 3.0365889702344129 3.0365889718756627 3.0365889718756627 3.0365889718756627
1>  ==================================================================
1>  typeid(T).name() = class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50,10,void,int,0,0>,0>, maxdigits10 = 52
1>  Digits accuracy fraction required = 0.5000000000000000000000000000000000000000000000000000
1>  find root of 28.00000000000000000000000000000000000000000000000000, expected answer = 3.036588971875662519420809578505669635581453977248111
1>
1>  Iterations: 0 11 8 5 6
1>  Times 11562 239843 34843 47500 47812
1>  Min Time 11562
1>  Normalized Times 1.0 21. 3.0 4.1 4.1
1>  Distance: 0 0 -1 0 0
1>  Roots: 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248117 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106
1>  ==================================================================



*/