sqrt.hpp 1.49 KB
Newer Older
1
// Software License for MTL
2
//
3
4
5
6
7
// Copyright (c) 2007 The Trustees of Indiana University.
//               2008 Dresden University of Technology and the Trustees of Indiana University.
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
8
//
9
// This file is part of the Matrix Template Library
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
// See also license.mtl.txt in the distribution.

#ifndef META_MATH_SQRT_INCLUDE
#define META_MATH_SQRT_INCLUDE

#include <boost/numeric/meta_math/abs.hpp>

namespace meta_math {

template <long int root, long int x>
struct sqrt_check
{
    static bool const value = root * root <= x && (root+1) * (root+1) > x;
};


namespace impl {

    template <long int guess, long int x, bool Converged>
    struct sqrt_impl
    {
	typedef long int type;
	typedef sqrt_impl   self;
	static long int const quotient = x / guess,
	                      new_value = (quotient + guess) / 2;
	static bool const converging = abs<guess - quotient>::value < 2;
	static long int const value = sqrt_impl<new_value, x, converging>::value;
    };

    // If the condition becomes true the guessed root will be the returned value
    template <long int guess, long int x>
42
    struct sqrt_impl<guess, x, true>
43
44
45
46
47
48
49
    {
	static long int const value = guess;
    };

}

template <long int x>
50
struct sqrt
51
52
53
{
  typedef long int type;
  static long int const value = impl::sqrt_impl<1, x, false>::value;
54
};
55
56
57
58
59


} // namespace meta_math

#endif // META_MATH_SQRT_INCLUDE