vb.net 如何在 VB 中计算 pi (π)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/790365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I calculate pi (π) in VB?
提问by Jin Yong
Does anyone know how can I calculate pi (π) in VB?
有谁知道如何在 VB 中计算 pi (π)?
回答by mqp
System.Math.Pi
回答by Paul Wicks
Assuming you actually want to compute pi instead of just using the built in constants, there are a bunch of ways that you can do it. Here are a few links that could be useful:
假设您实际上想要计算 pi 而不是仅使用内置常量,那么您可以通过多种方法来计算。以下是一些可能有用的链接:
回答by ggf31416
If you mean VB6, it doesn't have a pi constant. You can use:
如果您的意思是 VB6,则它没有 pi 常数。您可以使用:
Dim pi as Double
pi = 4 * Atn(1)
回答by duffymo
If the OP is asking about algorithms as a learning experience, good for him/her.
如果 OP 将算法作为学习经验询问,那对他/她有好处。
If the OP wanted help finding the built-in value, s/he has it now.
如果 OP 需要帮助找到内置值,他/她现在就有了。
But if the goal is a good value of higher precision than the built-in value with a minimum of effort, here's pi to one million digits:
但是,如果目标是以最少的努力获得比内置值更高的精度的好值,那么这里是 pi 到一百万位:
http://www.eveandersson.com/pi/digits/1000000
http://www.eveandersson.com/pi/digits/1000000
That should be enough.
那应该就够了。
I hope the OP isn't asking how to recalculate the value of Pi each and every time it's used. That would be madness.
我希望 OP 不会在每次使用时询问如何重新计算 Pi 的值。那太疯狂了。
回答by Henrik Paul
Meh, so efficient, accurate and most of all boring approximations... Try thisinstead! Pseudocode ensues:
嗯,如此高效,准确,最重要的是无聊的近似......试试这个!伪代码如下:
- initialize insideand totalas 0
- repeat an insane amount of times:
- assign both xand yrandom values between (and including) 0 and +1.
- assign distanceas the square root of (x2+ y2)
- if distance≤ 1, add 1 to inside
- add 1 to total
- assign pias inside/ total* 4
- 初始化inside和total为 0
- 重复疯狂的次数:
- 在(包括)0 和+1 之间分配x和y随机值。
- 将距离指定为 ( x 2+ y 2) 的平方根
- 如果距离≤ 1,则在里面加 1
- 总数加 1
- 将pi指定为内部/总计* 4
回答by TheTXI
If you don't want to use the built in values in the .net math library...
如果您不想使用 .net 数学库中的内置值...
22 / 7
22 / 7