1

What I want to do is to calculate square root of a number N and than we have to store this square root with P correct decimal values.For example

  Lets,N=2 and P=100

  //I want to store square root till P correct decimal values 
  like storing 
  1.41421356237309504880...till 100 values

EDIT - Initially I asked question for specifically c++ but as told by vsoftco that it is almost impossible for c++ to do this without boost.So I have tagged python too and not removed c++ tag in hope for correct answer in C++

chota bheem
  • 371
  • 3
  • 8
  • 20
  • You should also realize that Boost (or other libraries) are written in C++. So there is no other "correct" answer, you won't be able to get multiprecision types by default since the language does not support them (same is true about Python). You only have two options: write your own multiprecision library (highly nontrivial), or use one from other parties. – vsoftco May 09 '15 at 20:17

2 Answers2

1

C++ uses floating-point types (such as float, double etc) to store floating-point values and perform floating point arithmetic. The size of these types (which directly influences their precision) is implementation-defined (usually you won't get more than 128 bit of precision). Also, precision is a relative term: when your numbers grow, you have less and less precision.

So, to answer your question: it is impossible to store a number with arbitrary precision using standard C++ types. You need to use a multi-precision library for that, e.g. Boost.Multiprecision.

Example code using Boost.Multiprecison:

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>

int main()
{
   using namespace boost::multiprecision;
   typedef number<cpp_dec_float<100> > cpp_dec_float_100; // 100 decimal places

   cpp_dec_float_100 N = 2;
   cpp_dec_float_100 result = sqrt(N); // calls boost::multiprecision::sqrt
   std::cout << result.str() << std::endl; // displays the result as a string
}

If you use Python, you can make use of decimal module:

from decimal import *

getcontext().prec = 100
result = Decimal.sqrt(Decimal(2))
print("Decimal.sqrt(2): {0}".format(result))
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Okay,can you suggest me any other language for this purpose than because boost is not allowed :( – chota bheem May 09 '15 at 19:57
  • @chotabheem I take it back, I think Python has arbitrary precision integers, not floats. You need to use the `decimal` library for that, see [here](http://stackoverflow.com/questions/11522933/python-floating-point-arbitrary-precision-available). In general programming languages do not implement arbitrary precision numbers by default, since they are quite slow, and the precision doesn't really make a difference in most cases. – vsoftco May 09 '15 at 20:01
  • I will be obliged if you can edit your answer for python as boost is not allowed – chota bheem May 09 '15 at 20:03
  • @chotabheem you should tag your question with python also. Right now it looks as C++ only. – vsoftco May 09 '15 at 20:04
0

Well, there is a proposed extension to c++ that defines a decimal type family described in http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2849.pdf. There is some support for it in modern gcc version, however it's still not full, even in 4.9.2

Maybe it will be feasible for you.

luk32
  • 15,812
  • 38
  • 62