Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,574 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,883 people online right now. Registration is fast and FREE... Join Now!




Square Root of 2 to 100 decimal places

 
Reply to this topicStart new topic

Square Root of 2 to 100 decimal places

ajaymatrix
17 May, 2008 - 04:55 AM
Post #1

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
The program is finding the Square Root of 2 to 100 decimal places.

This is the code i wrote following Newton's Method. But 1.4142135623730951 is the value I am getting.

CODE
#include<iostream>
#include<iomanip>
using namespace std;
int main(){

long double x=1;
for (i=0;i<30;i++) {  //30 iterations
x=(x+2/x)/2;
}
           cout<<setprecision(100)<<(x);      
     system("pause");
       return 0;
}


How to improve the accuracy to 100 digits.

User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Square Root Of 2 To 100 Decimal Places
17 May, 2008 - 07:40 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
Accuracy is not the problem here. It's the data type that is. Even a double (which is usually 64-bits in length), will never give you 100 decimal places (without even considering the storage method.... 10^100 > 2^64, 10^100 > 2^128, and an 80-bit BCD number is capable of 20 decimal digits). Floating point numbers are usually stored in form of exponent and mantissa, but the type cannot store many digits of precision (the mantissa).

QUOTE(ajaymatrix @ 17 May, 2008 - 05:55 AM) *

The program is finding the Square Root of 2 to 100 decimal places.

This is the code i wrote following Newton's Method. But 1.4142135623730951 is the value I am getting.

CODE
#include<iostream>
#include<iomanip>
using namespace std;
int main(){

long double x=1;
for (i=0;i<30;i++) {  //30 iterations
x=(x+2/x)/2;
}
           cout<<setprecision(100)<<(x);      
     system("pause");
       return 0;
}


How to improve the accuracy to 100 digits.


This post has been edited by perfectly.insane: 17 May, 2008 - 07:42 AM
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Square Root Of 2 To 100 Decimal Places
17 May, 2008 - 03:46 PM
Post #3

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
then how can I extract digit by digit?
Any algorithm for that..
User is offlineProfile CardPM
+Quote Post

fluxion
RE: Square Root Of 2 To 100 Decimal Places
18 May, 2008 - 04:22 AM
Post #4

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 7


My Contributions
QUOTE(ajaymatrix @ 17 May, 2008 - 04:46 PM) *

then how can I extract digit by digit?
Any algorithm for that..


most straightforward way to do stuff like this is to use an arbitrary-precision math library

you cant simply extract digit-by-digit, in general, since the extra information is never stored to begin with. sometimes there are clever ways around thing, but no general method i know of
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Square Root Of 2 To 100 Decimal Places
18 May, 2008 - 04:51 AM
Post #5

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
There are methods..
http://www.rossi.com/sqr2.htm
The first 1000 digits are calculated over there.
QUOTE
This took about 15 mins. to compute on my 486-33. It was calulated by applying Newton's Method to the curve y=x*x, and solving of x when y=2.

Even some source code is given on that site..

I tried using Newton's Method, but got defeated since the datatype doesn't support that many decimal places..
User is offlineProfile CardPM
+Quote Post

fluxion
RE: Square Root Of 2 To 100 Decimal Places
18 May, 2008 - 11:40 AM
Post #6

New D.I.C Head
*

Joined: 19 Apr, 2008
Posts: 7


My Contributions
QUOTE(ajaymatrix @ 18 May, 2008 - 05:51 AM) *

There are methods..
http://www.rossi.com/sqr2.htm
The first 1000 digits are calculated over there.
QUOTE
This took about 15 mins. to compute on my 486-33. It was calulated by applying Newton's Method to the curve y=x*x, and solving of x when y=2.

Even some source code is given on that site..

I tried using Newton's Method, but got defeated since the datatype doesn't support that many decimal places..


he used an arbitrary precision math library

but sometimes you dont have to, sometimes there's way to build precision as you go. but there no general way to do this, it's highly dependent on the algorithm you're using
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month