The COS()
function in SQLite calculates the cosine of a given angle. The angle must be specified in radians. The result is the cosine of the angle, which is a real number between -1 and 1.
Syntax
COS(X)
Where X
is the angle in radians for which you want to calculate the cosine. The angle can be any real number (positive, negative, or zero).
The result will be a real number representing the cosine of the angle. The cosine of an angle is always between -1 and 1.
Example 1
Here’s a quick example to demonstrate how it works:
SELECT COS(0.37);
Output:
0.932327345606034
Example 2
Suppose we have a table angles
with a column radians
that contains various angle values in radians. We want to calculate the cosine of each angle.
CREATE TABLE angles (radians REAL);
INSERT INTO angles (radians) VALUES (0), (PI()/2), (PI()), (-PI()/2), (PI()/4);
Now, we can use the COS()
function to calculate the cosine of each angle:
SELECT radians, COS(radians) AS cosine_value
FROM angles;
Output:
radians cosine_value
----------------- --------------------
0.0 1.0
1.5707963267949 6.12323399573677e-17
3.14159265358979 -1.0
-1.5707963267949 6.12323399573677e-17
0.785398163397448 0.707106781186548
Explanation:
radians = 0.0
:- The cosine of 0 radians is exactly
1.0
, as expected.
- The cosine of 0 radians is exactly
radians = π/2
(1.5707963267949):- The cosine of
π/2
radians (90°) should ideally be0
. However, due to floating-point precision limitations, I got a very small number close to0
, specifically6.12323399573677e-17
. This is essentially0
but expressed in scientific notation.
- The cosine of
radians = π
(3.14159265358979):- The cosine of
π
radians (180°) is exactly-1.0
, as expected.
- The cosine of
radians = -π/2
(-1.5707963267949):- The cosine of
-π/2
radians (-90°) should ideally be0
. Similar to the previous case, it’s shown as a very small value close to0
in scientific notation:6.12323399573677e-17
.
- The cosine of
radians = π/4
(0.785398163397448):- The cosine of
π/4
radians (45°) is approximately0.707106781186548
, which is the expected value.
- The cosine of
Why is 6.12323399573677e-17
appearing instead of 0
?
This is due to the way computers represent floating-point numbers. Although cos(π/2)
and cos(-π/2)
should be exactly 0
, floating-point arithmetic may introduce very small errors. These errors occur because the value is so close to 0
that it gets represented in scientific notation, but it’s practically equivalent to 0
.
How to Handle This?
If you prefer a cleaner output (i.e., displaying 0
instead of a very small number), you can use the ROUND()
function to round the result to a certain number of decimal places.
For example:
SELECT radians, ROUND(COS(radians), 6) AS cosine_value
FROM angles;
Output:
radians cosine_value
----------------- ------------
0.0 1.0
1.5707963267949 0.0
3.14159265358979 -1.0
-1.5707963267949 0.0
0.785398163397448 0.707107
This rounds the cosine value to 6 decimal places, and we see 0.0
instead of the scientific notation.
Radians vs Degrees
The input angle for the COS()
function must be in radians. If you have an angle in degrees, you need to convert it to radians first using the formula:
radians = degrees * (PI() / 180)
For example, to find the cosine of 45 degrees, you would first convert it to radians:
SELECT COS(45 * PI() / 180) AS cosine_value;
The COS()
function is commonly used in trigonometry, geometry, physics, and computer graphics to calculate the cosine of angles, which is important for various calculations like vector projections, rotations, and waveforms.