CalculateSqrt shows how to calculate the square root of a value.
Operating system(s) or programming environment(s)
- Lubuntu 15.04 (vivid)
- Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
- STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppCalculateSqrt/CppCalculateSqrt.pro
include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG += console # CONFIG -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { # DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { # QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/Boost.pri) #Or use the code below # win32 { # INCLUDEPATH += \ # ../../Libraries/boost_1_54_0 # } SOURCES += 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... */