 |
| Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 27th, 2010, 07:06 PM
|
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Generating Pi to more than 14 decimal places
Hello everyone,
I recently purchased the book Beginning Visual Basic 2010 so I came here, as the book suggests, to ask for help with some problems.
The first problem is one I encountered when I wrote a console program for calculating π, or Pi. The problem is that it will only allow me to display the first 16 digits, or 14 decimal places of the calculation. Despite its simplicity, the algorithm I implemented is capable of calculating π, precisely, to many more decimal places and I would like my program to do so. Is this possible with Visual Basic.Net? If so could you please amend my code and/or explain accordingly.
Below is the code for my Pi Generator:
Code:
Module Module1
Sub Main()
Start:
Dim f As Decimal = 0
Dim j As Double = 1
Dim n As Integer = 0
Console.ForegroundColor = ConsoleColor.Green
Console.CursorVisible = True
Console.Write("Iterations: ")
n = Console.ReadLine()
'Estimate Pi
For i = 0 To n
If i Mod 2 = 0 Then
f += 1 / (j * Math.Pow(3, i))
Else
f -= 1 / (j * Math.Pow(3, i))
End If
j += 2
Next
Dim Pi As String = 2 * Math.Sqrt(3) * f
'Determine the accuracy of the estimation
Dim PiReference As String = ""
Dim Accuracy As Integer = 0
PiReference = Math.PI
For i = 2 To Len(Pi) - 1 Step 1
If Pi.Substring(i, 1) = PiReference.Substring(i, 1) Then
Accuracy += 1
Else
Exit For
End If
Next
'Display Results
Console.WriteLine(Pi)
Console.WriteLine("Accurate to {0} DP {1}", Accuracy, vbNewLine)
Console.Beep()
GoTo Start
End Sub
End Module
I would also like to know how to create a list of variables, of the same data type, but to an unspecified amount as a standard array requires you to specify the size first.
I ask because want to write a program which allows the user to input as many numbers as they wish and then returns the mean, median and modal averages of said numbers.
Thank you for help! 
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
|
|

October 27th, 2010, 07:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Nothing to do with VB, per se. What you have run into is a limitation of the *HARDWARE* in the machine. And that, in turn, is in compliance with the IEEE specification for double precision floating point calculations, so even using different hardware would not change things. (Unless you could find some really old computer that used a different floating point standard...but if you found such a beast it would likely be *LESS* accurate.)
The best you can do is here:
http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx
For a complete list of available types:
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
****************
Quote:
|
a standard array requires you to specify the size first.
|
Depends on they syntax you use.
Try this:
Code:
Dim n As Integer = 37 * 14
Dim foo As Integer() = New Integer(n)
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:26 PM
|
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Sorry but I don't see how my hardware can be a limiting factor. Read this article: http://www.codeproject.com/KB/recipes/CRHpi.aspx...
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
|
|

October 27th, 2010, 07:27 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
I should point out that all the standard functions, such as Math.pow(), are written for Double. You would have to avoid their use if you wanted better accuracy. I believe that the only operators the are defined for Decimal are the 4 primitives: + - * /
Yeah, see here:
http://msdn.microsoft.com/en-US/libr...=VS.80%29.aspx
Even the ^ operator will only work with Double.
And notice here:
http://msdn.microsoft.com/en-US/libr...=VS.80%29.aspx
Even + - * / will only give a Decimal result if *BOTH* the operands are Decimal. So watch that carefully.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:31 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Quote:
Originally Posted by SamC
|
Try reading it yourself. More carefully. To wit:
Quote:
Multi-length arithmetic
A 32-bit integer only gives us about 9 significant digits. To get one million decimal places, I've done a simple implementation of the multi-length arithmetic operations that I need, using a big array of ints. The multi-length numbers are wrapped up inside the class CRHMultiLengthInteger. I've made full use of the operator notation in C++ to make the code look like we're just working with ordinary numbers.
|
If you can figure out how to convert his C++ code to VB, then indeed you can use the same technique.
HINT: Some of the code can't be directly converted but will require ugly work-arounds in VB.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:32 PM
|
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
OK, now I'm utterly perplexed... are these limitations unique to the .NET Framework?
Because people are calculating π to thousands of decimal places using unmodified home PCs, how are they doing this?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
|
|

October 27th, 2010, 07:32 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
And I suspect that his definition of "simple implementation" would make your head hurt. It makes my scalp itch, and I used to write this kind of stuff in assembly language, no less.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:34 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Quote:
Originally Posted by SamC
OK, now I'm utterly perplexed... are these limitations unique to the .NET Framework?
|
No. I said that. Double precision floating point numbers and arithmetic are an *INTERNATIONAL STANDARD*.
Quote:
|
Because people are calculating π to thousands of decimal places using unmodified home PCs, how are they doing this?
|
Read his C++ code and understand it and then you will know. Basically, they do it by *NOT* using the built-in operations of the hardware and of the standard languages.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:35 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
http://en.wikipedia.org/wiki/IEEE_754-1985
Sorry...I pointed you to the 2008 standard first, but no currently available PCs implement it yet. And likely won't for some time.
Last edited by Old Pedant; October 27th, 2010 at 07:38 PM..
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
SamC (October 30th, 2010)
|
|

October 27th, 2010, 07:37 PM
|
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
So it's possible even with Visual Basic.Net, just very complicated?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
|
|
 |