Pi in VBA?

P

Prof Wonmug

What's the best way to get the most accurate value for pi in a VBA
function?

Excel 2007 has a pi() worksheet function, but there doesn't appear to
be a corresponding VBA function. The best I could come up with is

C = 2 * Application.WorksheetFunction.Pi() * R

That works, but it's a litte messy.
 
D

dk

What's the best way to get the most accurate value for pi in a VBA
function?

Excel 2007 has a pi() worksheet function, but there doesn't appear to
be a corresponding VBA function. The best I could come up with is

  C = 2 * Application.WorksheetFunction.Pi() * R

That works, but it's a litte messy.

WHY DONT YOU JUST HARD CODE IT ?
 
R

Rick Rothstein

You could just assign the value to a constant directly...

Const PI = 3.14159265358979

Or you can let VB calculate it for you...

PI = 4 * ATN(1)
 
A

AltaEgo

Set a variable and use the variable

PI = Application.WorksheetFunction.Pi()

C = 2*PI*R
 
J

JoeU2004

Rick Rothstein said:
You could just assign the value to a constant directly...
Const PI = 3.14159265358979

Or you can let VB calculate it for you...
PI = 4 * ATN(1)

Your constant for PI does not equal VB 4*Atn(1) or Excel PI().

However, if you enter the constant as 3.141592653589793, that does result in
the same binary value as 4*Atn(1) and PI(), even though VB will not display
the last 3.
 
J

Jim Thomlinson

I like that one... I would never have thought to use the ArcTangent but it
makes sense. I guess that is why they pay you the big money. That being said
I would be inclined to just use the constant and avoid the overhead of a
function. Why calculate a constant?
 
P

Prof Wonmug

Your constant for PI does not equal VB 4*Atn(1) or Excel PI().

However, if you enter the constant as 3.141592653589793, that does result in
the same binary value as 4*Atn(1) and PI(), even though VB will not display
the last 3.

This is why I don't want to hard code a trancendental constant.
 
P

Prof Wonmug

I like that one... I would never have thought to use the ArcTangent but it
makes sense. I guess that is why they pay you the big money. That being said
I would be inclined to just use the constant and avoid the overhead of a
function.

You mean the 10 ns overhead (or whatever it is)?

If the function call is in a tight loop that is called billions of
times and if the function call (to define the constant) cannot be
moved outside the loop, then maybe.
Why calculate a constant?

Accuracy, portability, compatibility?
 
R

Rick Rothstein

Why calculate a constant?
Accuracy, portability, compatibility?

For the most accuracy, declare PI like this...

Dim PI As Variant
PI = CDec("3.1415926535897932384626433833")

Then, if PI is not placed inside a VB math function call, your calculations
should maintain an accuracy of 28 significant figures (VB math function
calls can only return a Double at most, so if you placed PI inside the math
function call, like Sin(PI/6) for example, then the Sin function will return
a Double; but if you did PI*Sin(0.123) for example, then the calculation
would return a number with 28 significant digits
(0.3854422854886583808804090009 to be exact). So, from your original
question, this...

C = 2 * PI * R

would assign to C a value accurate to 28 significant digits.
 
J

JoeU2004

Prof Wonmug said:
I did. It was my second choice. Using the worksheet
function is a little more obvious, that's all.

..... And more likely to be accurate insofar as matching the Excel value. I
agree.

__You__ were the one who was asking for a VB-only solution, or so it seemed.


----- previous message -----
 
J

Jim Thomlinson

Why calculate a constant?

In a couple of seconds I can google Pi and get more decimals than my
computer can effectively handle so accuracy is not an issue. As for
portability and compatability, what is more portable or compatable than a
constant. No functions. No problems. While I agree that the overhead is
minimal how much wasted overhead is acceptable? If you want readability then
nothing will be more clear than a constant.

Just my 2 cents and I have probably overcharged...
 
J

JoeU2004

Prof Wonmug said:
Accuracy, portability, compatibility?

It should be noted that Jim is referring to the expression 4*Atn(1). That
does not ensure accuracy or compatilibity.

Atn is a transcendental function, which is typically estimated using a
polynomial algorithm. Moreover, VBA sometimes uses different algorithms
than Excel for similar functions. I was pleasantly surprised to learn that
4*Atn(1) has exactly the same binary result as Excel's PI function. There
was certainly no guarantee that would be the case.

On the other hand, Wonmug had used WorksheetFunction.PI(). I do agree that
that is better than a constant for ensuring compatibility with the Excel PI
function with the same accuracy.

Entering a constant with decimal fractions that is not exactly the sum of up
to 53 consecutive powers of 2 might not be portable. I don't know if the
IEEE specifies a standard conversion algorithm. But it is clear that Excel
and VBA treat numbers with more than 15 significant digits differently.

Even within 15 significant digits, I have seen constants where the Excel
conversion could be improved by adding 1 or 2 to the least significant bit.
So I can imagine that different implementations of Excel and VBA could do
the conversion differently. (But I don't know if that would violate a
conversion standard, if any.)
 
J

JoeU2004

PS....
On the other hand, Wonmug had used WorksheetFunction.PI().
[....]
Entering a constant with decimal fractions that is not
exactly the sum of up to 53 consecutive powers of 2 might
not be portable.

You can get the best of both worlds by assigning WorksheetFunction.PI to a
module-level variable only the first time. For example:

Private pi as double

Function doit()
If pi = 0 Then pi = WorksheetFunction.PI
....
end Function

I cannot say with impunity that that is any better than simply calling
WorksheetFunction.PI the first time in each function. But I suspect it is.

I also cannot say how using a module-level variable compares with using a
function-level Const identifier. But I suspect they are both loaded from
memory.


----- original message -----
 
D

Dana DeLouis

Sin function will return a Double; but if you did PI*Sin(0.123) for
example, then the calculation would return
a number with 28 significant digits
(0.3854422854886583808804090009 to be exact).

As a side note, Trig Functions like Sin are not supported.
Hence, the solution is only accurate to 15 digits, despite the number of
digits displayed.

HTH :>)
Dana DeLouis
= = = =
 
D

Dana DeLouis

Hi. Just to be a little different...
Not really recommended, but a poor-man's version of the Pi symbol can
sometimes be done via the Paragraph symbol. You have to have a little
imagination thou to see it as Pi :>)

Sub Demo()
Dim c
Dim ¶ 'Alt + 0182

¶ = [Pi()]

c = 2 * ¶ * 5
End Sub

Dana DeLouis
= = = =
 
A

AltaEgo

Thank you.

Wrapping in [ ] instead of Application.WorksheetFunction is new
information for me. Hopefully, the memory banks will retain this gem.

--
Steve

Dana DeLouis said:
Hi. Just to be a little different...
Not really recommended, but a poor-man's version of the Pi symbol can
sometimes be done via the Paragraph symbol. You have to have a little
imagination thou to see it as Pi :>)

Sub Demo()
Dim c
Dim ¶ 'Alt + 0182

¶ = [Pi()]

c = 2 * ¶ * 5
End Sub

Dana DeLouis
= = = =

Prof said:
This is why I don't want to hard code a trancendental constant.
 
P

Prof Wonmug

Hi. Just to be a little different...
Not really recommended, but a poor-man's version of the Pi symbol can
sometimes be done via the Paragraph symbol. You have to have a little
imagination thou to see it as Pi :>)

Sub Demo()
Dim c
Dim ¶ 'Alt + 0182

¶ = [Pi()]

c = 2 * ¶ * 5
End Sub

You've got *two* surprises in that code snippet. I doubt I'll use the
paragraph symbol (you could have won a reasonably large bet with me in
a bar with that one), but the [pi()] notation in intriguing.

The best I can discover is that it is another way of invoking the
Evaluate method, which I also don't fully understand.

Are you the keeper of VBA esoterica?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top