Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.

CalculateSqrt shows how to calculate the square root of a value.
Technical facts
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppCalculateSqrt/CppCalculateSqrt.pro
./CppCalculateSqrt/main.cpp
#include <cmath>
#include <iomanip>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/units/unit.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/io.hpp>
#pragma GCC diagnostic pop
template <class T> T CalculateSqrt(const T& x);
///Simple approximation algorithm to calculate the square root,
///instead of using std::sqrt
template <>
double CalculateSqrt(const double& x)
{
assert(x >= 0.0);
if (x == 0.0) return 0.0;
const int precision = 6;
double y = 0.5*x;
for (int i=0; i!=precision; ++i)
{
y+=(x/y);
y*=0.5;
}
return y;
}
template <>
boost::units::quantity<boost::units::si::length> CalculateSqrt(
const boost::units::quantity<boost::units::si::length>& x)
{
return CalculateSqrt(x.value()) * boost::units::si::meter;
}
int main()
{
std::cout << std::setprecision(35) << std::fixed;
for (int i=0; i!=10; ++i)
{
const double x = static_cast<double>(i);
std::cout << std::sqrt(x) << '\t' << CalculateSqrt(x) << '\n';
}
std::cout << '\n';
for (int i=0; i!=10; ++i)
{
const boost::units::quantity<boost::units::si::length> x
= static_cast<double>(i) * boost::units::si::meter;
std::cout << std::sqrt(x.value()) << '\t' << CalculateSqrt(x) << '\n';
}
}
/* Screen output:
0.00000000000000000000000000000000000 0.00000000000000000000000000000000000
1.00000000000000000000000000000000000 1.00000000000000000000000000000000000
1.41421356237309514547462185873882845 1.41421356237309514547462185873882845
1.73205080756887719317660412343684584 1.73205080756887719317660412343684584
2.00000000000000000000000000000000000 2.00000000000000000000000000000000000
2.23606797749978980505147774238139391 2.23606797749978980505147774238139391
2.44948974278317788133563226438127458 2.44948974278317788133563226438127458
2.64575131106459071617109657381661236 2.64575131106459071617109657381661236
2.82842712474619029094924371747765690 2.82842712474619029094924371747765690
3.00000000000000000000000000000000000 3.00000000000000000000000000000000000
0.00000000000000000000000000000000000 0.00000000000000000000000000000000000 m
1.00000000000000000000000000000000000 1.00000000000000000000000000000000000 m
1.41421356237309514547462185873882845 1.41421356237309514547462185873882845 m
1.73205080756887719317660412343684584 1.73205080756887719317660412343684584 m
2.00000000000000000000000000000000000 2.00000000000000000000000000000000000 m
2.23606797749978980505147774238139391 2.23606797749978980505147774238139391 m
2.44948974278317788133563226438127458 2.44948974278317788133563226438127458 m
2.64575131106459071617109657381661236 2.64575131106459071617109657381661236 m
2.82842712474619029094924371747765690 2.82842712474619029094924371747765690 m
3.00000000000000000000000000000000000 3.00000000000000000000000000000000000 m
Press <RETURN> to close this window...
*/
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml