VBA - Can't get COS to give me the right value.

swayp11

Board Regular
Joined
Apr 27, 2009
Messages
107
Office Version
  1. 365
Platform
  1. Windows
For some reason, I can't get COS to evaluate correctly for certain values.

Code:
Dim Pi As Double
Pi = 3.14159265358979




Debug.Print (Cos(0))
Debug.Print (Cos(Pi / 2))
Debug.Print (Cos(Pi))
Debug.Print (Cos(3 * Pi / 2))
Debug.Print (Cos(2 * Pi))

The results I get are:
Code:
 1 
 1.61554255216634E-15 
-1 
-4.62458305157398E-15 
 1

But it should be 1, 0, -1, 0 ,1. Why is not calculating correctly when it should evaluate to 0?

Thanks!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The values that should be zero are very close to it, and the error is probably a result of the Taylor series (polynomial) approximation used to calculate it. You can use round(cos(angle), 15).
 
Upvote 0

Forum statistics

Threads
1,214,800
Messages
6,121,641
Members
449,044
Latest member
hherna01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top