00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef META_MATH_SQRT_INCLUDE
00013 #define META_MATH_SQRT_INCLUDE
00014
00015 #include <boost/numeric/meta_math/abs.hpp>
00016
00017 namespace meta_math {
00018
00019 template <long int root, long int x>
00020 struct sqrt_check
00021 {
00022 static bool const value = root * root <= x && (root+1) * (root+1) > x;
00023 };
00024
00025
00026 namespace impl {
00027
00028 template <long int guess, long int x, bool Converged>
00029 struct sqrt_impl
00030 {
00031 typedef long int type;
00032 typedef sqrt_impl self;
00033 static long int const quotient = x / guess,
00034 new_value = (quotient + guess) / 2;
00035 static bool const converging = abs<guess - quotient>::value < 2;
00036 static long int const value = sqrt_impl<new_value, x, converging>::value;
00037 };
00038
00039
00040 template <long int guess, long int x>
00041 struct sqrt_impl<guess, x, true>
00042 {
00043 static long int const value = guess;
00044 };
00045
00046 }
00047
00048 template <long int x>
00049 struct sqrt
00050 {
00051 typedef long int type;
00052 static long int const value = impl::sqrt_impl<1, x, false>::value;
00053 };
00054
00055
00056 }
00057
00058 #endif // META_MATH_SQRT_INCLUDE