Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have posted a few questions lately about PI, and C# math, and i almost there, but i need to ask one last question.

I figured out how to calculate PI, only to realize this is not what i needed for you can only go so far with that. Then I found out you can calculate PI to a digit of your choice with the BBP Formula.

My problem is the BBP is in 16 base and i need this number in 10 base.
My current code looks like this:

C#
double pi = 0;
for (int k = 0; k <=25; k++)
{
    pi = Math.Pow(16.0,-k) *
    ((4.0/(8*k+1.0))-
    (2.0/(8*k+4.0))-
    (1.0/(8*k+5.0))-
    (1.0/(8*k+6.0)));
    Console.WriteLine(pi.ToString());
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);



And the out put of this code is below:

5.06722085385879E-06
1.8789290093772E-07
7.76775121517736E-09
3.44793293050862E-10
1.6091877155537E-11
7.79570295400102E-13
3.88711525990975E-14
1.98322539359813E-15
1.03097121697889E-16
5.44347406057178E-18
2.91211179438419E-19
1.57549800977008E-20
8.60692632700397E-22
4.74204674455623E-23
2.63228669401317E-24
1.47090939027733E-25
8.2683300282764E-27
4.67271101635284E-28
2.65348590144992E-29
1.51345479607531E-30
8.66682856034781E-32
4.98130681533109E-33
2.87270191974158E-34
Press any key to continue...


So now here is the place i am stuck at.
How in the world do i get those long decimal numbers to turn into "1415926535897932384626433".

Thanks once again!
Posted

Those are not base 16 numbers, they are base 10 (decimal) numbers displayed in scientific notation. Use the String.Format() method to display in the format that you require.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-May-11 16:07pm    
Correct, a 5. It looks like OP does not understand how numbers are represented in principle.
--SA
Hi again texcodec, i used this and I get correct values, no need to change the base.

3,13333333333333
0,00808913308913309
0,000164923924115101
5,06722085385879E-06
1,8789290093772E-07
7,76775121517736E-09
3,44793293050862E-10
1,6091877155537E-11
7,79570295400102E-13
3,88711525990975E-14
Press any key to continue...


or better pi += to get the agregate value of every calculation.

3,13333333333333
3,14142246642247
3,14158739034658
3,14159245756744
3,14159264546034
3,14159265322809
3,14159265357288
3,14159265358897
3,14159265358975
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
Math.PI ->3,14159265358979
Press any key to continue...


I see 2 thing about this method:

1-. k is no equal to nth digit of pi, I mean with k=1 you get 3 correct decimal of pi, with k=2 -> 4, with k=3 -> 6, k=4 -> 7, so if you ask for a number of digit this is not directly the number of for to execute.
2-.With k>9 the double value is not enough to show such precission, so you'll get as better as Math.PI.

The class I linked you in the other post give pi as a string, and I thing that in every bucle you get a new digit, appending it to existing pi string, so the number of bucle (k) gives you the nth digit.

So seeing your code you got the formula working correctly, congratulations.


Regards
 
Share this answer
 
v2
Comments
thexcodec 1-May-11 12:14pm    
I thought the algorithm was suppose to to find a specific digit.
I was really hoping to do this my self, but i guess ill have to loop at that class you sent me and see if i understand it.
You can convert base 16 number to base 10 using Convert.ToInt32 method. It has an overload where you can give the input as a string and base as a number. Like this:

int number = Convert.ToInt32("base 16 number", 16);


Update: Misunderstood the question. This time probably got it. :)

Following works fine for me.

double.Parse("exponential number as string here", System.Globalization.NumberStyles.Any);


If it still doesn't for you, then you may have to use string representation of the numbers for display.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-May-11 16:11pm    
What happened to you? It won't even execute, will throw exception. Correct answer was by Richard.
It's "1". Probably you mean something else; so I'll re-vote if you have to say something meaningful here. Alternatively, remove it.
Sorry,
--SA
dan!sh 2-May-11 0:24am    
OP asked how to convert hexadecimal to decimal, if not I misunderstood the question. The above code will work. Here is the MSDN reference: here[^] and here[^].
dan!sh 2-May-11 0:26am    
I now understand that OP is confused between exponential notation and hexadecimal numbers. Updated the reply.
Sergey Alexandrovich Kryukov 2-May-11 1:22am    
Oh, is it just pseudo-code (in "")? You should explain that in your answer...
--SA
Sergey Alexandrovich Kryukov 2-May-11 1:25am    
Correct answer is: nothing needs to be "converted".
As to OP's confusion... hard to say... it looks like 1) there is not full understanding of the fact that the notion hex or decimal have nothing to do with numeric types, only related to string representation, 2) failure to understand decimal exponential notation.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900