Problem with cos()

I'm trying to do: cos(120)

When I use my phone's calculator and my scientific calculator for school, both of which are set to degrees, I get -0.5. This is correct.

When I do cos(120) in Arduino, it returns 0.81 radians. This is also correct. But when I try converting 0.81 to degrees using 0.81 * 180 / PI I get 46.43.

So why is 46.43 not -0.5?

Because you are doing cosine of 120 radians. Try converting degrees to radians first and see what you get.

Thanks. That worked. It's odd how arduino acos takes a degree and returns a radian,
where arduino cos takes a radian and returns a degree. I expected them to be the same.

Nonsense. From math.h:

/**
    The acos() function computes the principal value of the arc cosine of
    \a __x.  The returned value is in the range [0, pi] radians. A domain
    error occurs for arguments not in the range [-1, +1].
 */
extern double acos(double __x) __ATTR_CONST__;

You have a misunderstanding of trig functions.
Trig functions take a measurement in angles that are portions of the unit circle and return a ratio:
sine returns a ratio of y over h,
cosine returns a ratio of x over h,
and tangent returns a ratio of y over x
The inverse trig functions take the ratios and return angles.

What's nonsense?

extern double acos(double __x) __ATTR_CONST__;

The double__X is in degrees, and the comment says:

// The returned value is in the range [0, pi] radians

Also, in reply to what I said:

"It's odd how arduino acos..."

I now understand that this isn't odd.

What's odd is that it's different from my scientific calculator and my phone's calculator.

My phone's calculator and my scientific calculator I use for school:
cos takes degrees, and returns degrees.
acos takes degrees, and returns degrees.

Arduino:
cos takes radians, and returns degrees.
acos takes degrees, and returns radians.

Side note: It seems the confusion between replies is caused by how the Arduino forum shows replies. when someone posts a reply to someone else's reply, it's displayed under the other persons reply as expected, but it is also considered a reply to the original question / topic. So the reply is shown twice, which is very confusing. Forums become unorganized and confusing.

Hi,

If your Scientific calculator is a true Scientific, you should be able to work in degrees, radians or Grads.

Tom... :smiley: :+1: :coffee: :australia:

No.

Your reply is not helpful. You claim something without providing anything to support your claim. I just triple checked it, and my comment was correct

The argument to sin(), cos() and tan() is an angle. Angles can be specified in degrees or radians. The c/c++ functions ONLY accept radians. All three functions return a unitless number.

The argument to asin(), acos() and atan() is a unitless number. All three functions return an angle in degrees or radians. The c/c++ functions ONLY return a result in radians.

Hi,

Tom... :smiley: :+1: :coffee: :australia:

A scientific calculator can be set to both degrees and radians, therefore, cos can take radians or degrees.
Arduino cos can only take radians. It returns degrees.

Hi,
It calculates the cosine of the angle (radians) the answer is a number, not degrees or radians.

Tom... :smiley: :coffee: :coffee: :australia:

@nebula2: If many people say you are wrong maybe you are wrong.

In a right triangle, the cosine (cos) of either of the acute angles is equal to the quotient of the adjacent side divided by tbe Hypotenuse. It is a unitless number in the range [-1,+1]. It is not an angle so it is not expressed in degrees or in radians. Say for example, that the sides of the triangle are measured in feet. If the adjacent side is 10 feet long and the Hypotenuse is 20 feet long, the cosine is 10feet/20feet = 0.5. No units.

You can calculate the cosine of x as
cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10! …. This is an infinite series that must be truncated at some point to make it practical.

This method (known as a Taylor Series for cosine) only works when x is expressed in radians. Hence, if you want to compute cos(120°) you have to put x=120 * PI / 180 ≈ 2.094395. This is the reason why x is always expressed in radians when using trig functions in computer languages. To make it more comfortable, calculators and spreadsheets allow angles expressed in degrees, but the very first thing they do is to convert them to radians.

Then, when you put z = cos(120) in a computer language such as c++, you are calculating the cosine of an angle that measures 120 radians, which is indeed 0.81. Just 0.81. Not 0.81 degrees or 0.81 radians. The 0.81 is a “unitless” quantity as pointed above, not an angle.

You can use a calculator in radian mode to do this calculation. Or, convert 120 radians to degrees (6875.493542) and get its cosine using degrees mode in your calculator.

If you do try the Taylor Series for cosine to compute cos, be aware that there will be a truncating error which is less than the absolute value of the last term included in the calculation. For a large angle such as 120 radians it is best to first calculate an equivalent angle in the range (-pi,+pi) to speed up convergence and minimize rounding errors. You do this by adding or subtracting multiples of 2pi as needed, given that for any integer n, cos(x) = cos(x + 2pi*n)

So, 120 is “converted” to 120 - 38*pi = 0.619479. This is the value of x to be used when calculating cos using the above formula.

Last, but not least, everybody in this forum is here to help.

Again, no. You stuck on this detail.

tl;dr lifted from the previous post:

Sines and cosines are unitless numbers in the range [-1,+1]. They are not angles, they are not expressed in degrees or in radians.

a7

That should be:

The return value of acos() is also in a limited range.
For radians it is typically 0 to Pi. In degrees it is typically 0 to 180.

  1. It is clear that OP has a misconception of trig functions and will not accept any explanations from the multitude of engineers and mathematicians who have offered it.
  2. It's clear that OP has had their original question answered.

As such this thread no longer has a point and I've requested it to be closed.