ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Using COS in VBA, incorrect value returned (https://www.excelbanter.com/excel-worksheet-functions/158833-using-cos-vba-incorrect-value-returned.html)

Gnrnr

Using COS in VBA, incorrect value returned
 
I did a bit of a search on this but when using VBA and having the
following equation the Cos function returns a value of 1 rather than
the correct -1. What am i doing wrong?

temp=cos(pi)

regards

Steve


Dave Peterson

Using COS in VBA, incorrect value returned
 
Add
Option Explicit
at the top of your module and you'll see the problem.

Dim Pi As Double
Pi = 3.141592654
MsgBox Cos(Pi)



Gnrnr wrote:

I did a bit of a search on this but when using VBA and having the
following equation the Cos function returns a value of 1 rather than
the correct -1. What am i doing wrong?

temp=cos(pi)

regards

Steve


--

Dave Peterson

Dave Peterson

Using COS in VBA, incorrect value returned
 
ps. You could use:

msgbox cos(application.pi)

==
or
Pi = 3.14159265358979
If you need that many decimal places.

Dave Peterson wrote:

Add
Option Explicit
at the top of your module and you'll see the problem.

Dim Pi As Double
Pi = 3.141592654
MsgBox Cos(Pi)

Gnrnr wrote:

I did a bit of a search on this but when using VBA and having the
following equation the Cos function returns a value of 1 rather than
the correct -1. What am i doing wrong?

temp=cos(pi)

regards

Steve


--

Dave Peterson


--

Dave Peterson

Gnrnr

Using COS in VBA, incorrect value returned
 
On Sep 19, 12:22 pm, Dave Peterson wrote:
Add
Option Explicit
at the top of your module and you'll see the problem.

Dim Pi As Double
Pi = 3.141592654
MsgBox Cos(Pi)

Dave Peterson


Thanks Dave.

I'll give that a go. I thought that pi was a defined constant within
VBA though. I'm guessing it is something to do with the computer
having to think for me to create a constant on the fly so to speak?

Regards
Steve


Dana DeLouis

Using COS in VBA, incorrect value returned
 
Just for fun... This is not ideal, but a fun attempt at using a Pi symbol in
vba.
You have to hold Alt key, then 0182. Plus, you have to use a little
imagination on the Pi symbol.

Sub Fun()
Dim ¶ As Double ' Alt+0182
¶ = [Pi()]
MsgBox Cos(¶)
End Sub

--
Dana DeLouis



"Gnrnr" wrote in message
ups.com...
On Sep 19, 12:22 pm, Dave Peterson wrote:
Add
Option Explicit
at the top of your module and you'll see the problem.

Dim Pi As Double
Pi = 3.141592654
MsgBox Cos(Pi)

Dave Peterson


Thanks Dave.

I'll give that a go. I thought that pi was a defined constant within
VBA though. I'm guessing it is something to do with the computer
having to think for me to create a constant on the fly so to speak?

Regards
Steve


Dave Peterson

Using COS in VBA, incorrect value returned
 
And just to clarify:

¶ = [Pi()]
is equivalent to:
¶ = application.evaluate("Pi()")

which could be replaced by:
¶ = application.pi

Sometimes using [] can be confusing.

Dana DeLouis wrote:

Just for fun... This is not ideal, but a fun attempt at using a Pi symbol in
vba.
You have to hold Alt key, then 0182. Plus, you have to use a little
imagination on the Pi symbol.

Sub Fun()
Dim ¶ As Double ' Alt+0182
¶ = [Pi()]
MsgBox Cos(¶)
End Sub

--
Dana DeLouis

"Gnrnr" wrote in message
ups.com...
On Sep 19, 12:22 pm, Dave Peterson wrote:
Add
Option Explicit
at the top of your module and you'll see the problem.

Dim Pi As Double
Pi = 3.141592654
MsgBox Cos(Pi)

Dave Peterson


Thanks Dave.

I'll give that a go. I thought that pi was a defined constant within
VBA though. I'm guessing it is something to do with the computer
having to think for me to create a constant on the fly so to speak?

Regards
Steve


--

Dave Peterson


All times are GMT +1. The time now is 10:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com