Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
cos(pi/2) or sin(-pi) problem

Subject: cos(pi/2) or sin(-pi) problem

From: muhaha

Date: 17 Jan, 2009 13:37:55

Message: 1 of 17

hi guys..
i wanna ask you somthing..
i m using MATLAB R2008a and i have a problem with it..


matlab returns " cos(pi/2) = 6.1232e-017" and also " sin(-pi) = -1.2246e-016 ".. and this make me crazy.. both must be zero..

do you know how to fix this without using a "for" or "if" loop.. i mean to fix from the MATLAB program..
thanx..

Subject: cos(pi/2) or sin(-pi) problem

From: Matt

Date: 17 Jan, 2009 14:34:02

Message: 2 of 17

muhaha <muhalkan@yahoo.com> wrote in message <15244285.1232199505643.JavaMail.jakarta@nitrogen.mathforum.org>...
> hi guys..
> i wanna ask you somthing..
> i m using MATLAB R2008a and i have a problem with it..
>
>
> matlab returns " cos(pi/2) = 6.1232e-017" and also " sin(-pi) = -1.2246e-016 ".. and this make me crazy.. both must be zero..
>

I'm not sure why it makes you crazy. You must know that machine math is often not exact. This is especially true when it involves numbers like pi, which have to be approximated using a finite number of digits and when it involves functions like cos() and sin() which can only be approximated by arithmetic operations.

Nevertheless, you might consider the following if you need to exact zeros.

>> cosd(90)

ans =

     0

>> sind(-180)

ans =

     0

Subject: cos(pi/2) or sin(-pi) problem

From: Peter Perkins

Date: 18 Jan, 2009 18:40:20

Message: 3 of 17

Matt wrote:
> muhaha <muhalkan@yahoo.com> wrote in message <15244285.1232199505643.JavaMail.jakarta@nitrogen.mathforum.org>...
>> matlab returns " cos(pi/2) = 6.1232e-017" and also " sin(-pi) = -1.2246e-016 ".. and this make me crazy.. both must be zero..
>
> I'm not sure why it makes you crazy. You must know that machine math is often not exact. This is especially true when it involves numbers like pi, which have to be approximated using a finite number of digits and when it involves functions like cos() and sin() which can only be approximated by arithmetic operations.

That's right, but just to be clear: There is absolutely NOTHING wrong here. The irrational constant that mathematicians denote by the greek letter pi cannot be represented exactly in floating point, and the sin/cos above are the correct values for the floating point numbers that one gets when one types pi/2 and -pi at the MATLAB command line.

> Nevertheless, you might consider the following if you need to exact zeros.
>
>>> cosd(90)
>
> ans =
>
> 0
>
>>> sind(-180)
>
> ans =
>
> 0

Correct. 90 and -180 have an exact floating point representation.

Subject: cos(pi/2) or sin(-pi) problem

From: Roger Stafford

Date: 18 Jan, 2009 20:57:01

Message: 4 of 17

Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote in message <gkvt2k$4ab$1@fred.mathworks.com>...
>
> That's right, but just to be clear: There is absolutely NOTHING wrong here. The irrational constant that mathematicians denote by the greek letter pi cannot be represented exactly in floating point, and the sin/cos above are the correct values for the floating point numbers that one gets when one types pi/2 and -pi at the MATLAB command line.
> ......

  Strictly speaking that isn't quite true, Peter. It is impossible for 'cos' and 'sin' to return the exactly correct values for angles which are only rational approximations to pi/2 and pi, respectively, because the exact answers would themselves be irrational. For radian measure, except for x = 0, I believe it is true that there is no case where x and cosine(x), or x and sine(x), are simultaneously rational numbers.

Roger Stafford

Subject: cos(pi/2) or sin(-pi) problem

From: Matt Fig

Date: 18 Jan, 2009 20:59:02

Message: 5 of 17

If you must work with radians you might consider using a tolerance.

tol = eps; % for example.

num = [sin(-pi) cos(pi/2)];
num(abs(num)<eps)=0;




xxmm'bhga dZ3h\Z^^RxxnxF[m^ixhbi9^nagx`Z[xf\Zhohfa>ZehrZeZ

Subject: cos(pi/2) or sin(-pi) problem

From: Peter Perkins

Date: 19 Jan, 2009 16:13:04

Message: 6 of 17

Roger Stafford wrote:

> Strictly speaking that isn't quite true, Peter. It is impossible for 'cos' and 'sin' to return the exactly correct values for angles which are only rational approximations to pi/2 and pi, respectively, because the exact answers would themselves be irrational.

Sure. I mean "correct" in the sense that those values are the correct floating point values for the sin and cos of the floating point approximations to pi/2 and -pi.

Subject: cos(pi/2) or sin(-pi) problem

From: Ravi Teja Nidumolu

Date: 22 Oct, 2010 18:33:06

Message: 7 of 17

instead you can try this sin(sym(-pi)) or cos(syms(pi/2))...
the reason for cos(pi/2) = 6.1232 × 10^−17 is matlab approximates pi to 15 places after decimal point and sym(pi/2) is symbolic representation of pi/2 which gives you the exact answer

Subject: cos(pi/2) or sin(-pi) problem

From: Ravi Teja Nidumolu

Date: 22 Oct, 2010 18:34:08

Message: 8 of 17

instead you can try this sin(sym(-pi)) or cos(syms(pi/2))...
the reason for cos(pi/2) = 6.1232 × 10^−17 is matlab approximates pi to 15 places after decimal point and sym(pi/2) is symbolic representation of pi/2 which gives you the exact answer

Subject: cos(pi/2) or sin(-pi) problem

From: Jan Simon

Date: 22 Oct, 2010 19:11:04

Message: 9 of 17

Dear Matt,

> xxmm'bhga dZ3h\Z^^RxxnxF[m^ixhbi9^nagx`Z[xf\Zhohfa>ZehrZeZ

An astonished armadillo has entered your keybord. Offer it some cashew nuts, then it will not eat up the Z.

Jan

Subject: cos(pi/2) or sin(-pi) problem

From: Matt Fig

Date: 22 Oct, 2010 19:48:03

Message: 10 of 17

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i9snk8$dmh$1@fred.mathworks.com>...
> Dear Matt,
>
> > xxmm'bhga dZ3h\Z^^RxxnxF[m^ixhbi9^nagx`Z[xf\Zhohfa>ZehrZeZ
>
> An astonished armadillo has entered your keybord. Offer it some cashew nuts, then it will not eat up the Z.
>
> Jan

Actually somebody solved this crypto-puzzle long ago. Look how old this thread is! I don't know why it was revived...

Subject: cos(pi/2) or sin(-pi) problem

From: Jan Simon

Date: 22 Oct, 2010 21:33:03

Message: 11 of 17

Dear Matt,

and my armadillo theories are not really new also.

Jan

Subject: cos(pi/2) or sin(-pi) problem

From: Noa

Date: 24 Nov, 2010 13:53:03

Message: 12 of 17

I also noted. I do not have the symbolic toolbox. Is it the only possible solution?

Subject: cos(pi/2) or sin(-pi) problem

From: Sung-Eun Jo

Date: 20 Sep, 2014 15:35:08

Message: 13 of 17

"Noa" wrote in message <icj5bv$ona$1@fred.mathworks.com>...
> I also noted. I do not have the symbolic toolbox. Is it the only possible solution?

You can try the following mapping functions:
http://www.mathworks.co.kr/matlabcentral/fileexchange/47894-trigonometric-function-errors-around-pi-or-pi-2--sin-pi--or-cos-pi-2-

There are functions: sin_rad. cos_radl, exp_rad. and expm_rad.
Those function assumes 'pi' is the real 'pi'. Thus, they return zero where cos(x)=0 or sin(x)=0.
They all call cosh and sinh by converting radians to degrees.

-- jo.

Subject: cos(pi/2) or sin(-pi) problem

From: John D'Errico

Date: 20 Sep, 2014 16:20:06

Message: 14 of 17

"Sung-Eun Jo" <sungeun.jo+matlab@gmail.com> wrote in message <lvk6rc$oj3$1@newscl01ah.mathworks.com>...
> "Noa" wrote in message <icj5bv$ona$1@fred.mathworks.com>...
> > I also noted. I do not have the symbolic toolbox. Is it the only possible solution?
>
> You can try the following mapping functions:
> http://www.mathworks.co.kr/matlabcentral/fileexchange/47894-trigonometric-function-errors-around-pi-or-pi-2--sin-pi--or-cos-pi-2-
>
> There are functions: sin_rad. cos_radl, exp_rad. and expm_rad.
> Those function assumes 'pi' is the real 'pi'. Thus, they return zero where cos(x)=0 or sin(x)=0.
> They all call cosh and sinh by converting radians to degrees.
>
> -- jo.

Um, jo....

First of all, why in god's name would you answer a trivial
question that has not been looked at for multiple years?
LOOK AT THE TIME STAMP!

Second, there are no functions needed in MATLAB called
sin_rad, etc., since MATLAB already has sind, cosd, etc..
These functions work in terms of degrees, not radians
as the default sin and cos tools do.

Third, exp_rad? expm_rad? ARE YOU kidding? Why would
you care about a matrix exponential in terms of radians?

Lastly, you are confusing things, if you think that a
conversion of radians to degrees is involved with sinh
and cosh. these latter functions are the hyperbolic sine
and hyperbolic cosine, functions which are only somewhat
related to the basic trigonometric sine and cosine.

John

Subject: cos(pi/2) or sin(-pi) problem

From: Sung-Eun Jo

Date: 22 Sep, 2014 12:45:06

Message: 15 of 17

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <lvk9fm$vd$1@newscl01ah.mathworks.com>...
> "Sung-Eun Jo" <sungeun.jo+matlab@gmail.com> wrote in message <lvk6rc$oj3$1@newscl01ah.mathworks.com>...
> > "Noa" wrote in message <icj5bv$ona$1@fred.mathworks.com>...
> > > I also noted. I do not have the symbolic toolbox. Is it the only possible solution?
> >
> > You can try the following mapping functions:
> > http://www.mathworks.co.kr/matlabcentral/fileexchange/47894-trigonometric-function-errors-around-pi-or-pi-2--sin-pi--or-cos-pi-2-
> >
> > There are functions: sin_rad. cos_radl, exp_rad. and expm_rad.
> > Those function assumes 'pi' is the real 'pi'. Thus, they return zero where cos(x)=0 or sin(x)=0.
> > They all call cosh and sinh by converting radians to degrees.
> >
> > -- jo.
>
> Um, jo....
>
> First of all, why in god's name would you answer a trivial
> question that has not been looked at for multiple years?
> LOOK AT THE TIME STAMP!
>
> Second, there are no functions needed in MATLAB called
> sin_rad, etc., since MATLAB already has sind, cosd, etc..
> These functions work in terms of degrees, not radians
> as the default sin and cos tools do.
>
> Third, exp_rad? expm_rad? ARE YOU kidding? Why would
> you care about a matrix exponential in terms of radians?
>
> Lastly, you are confusing things, if you think that a
> conversion of radians to degrees is involved with sinh
> and cosh. these latter functions are the hyperbolic sine
> and hyperbolic cosine, functions which are only somewhat
> related to the basic trigonometric sine and cosine.
>
> John

sinh and cosh in my previous reply were typo. I tried to say sind and cosd.

My silly mapping functions are only for who want to have the equations:

sin_rad(pi) = 0
cos_rad(pi/2) = 0
exp_rad(i*pi) = -1
exp_rad(i*pi) = i

That is all.

jo

Subject: cos(pi/2) or sin(-pi) problem

From: Steven Lord

Date: 22 Sep, 2014 14:49:12

Message: 16 of 17


"Sung-Eun Jo" <sungeun.jo+matlab@gmail.com> wrote in message
news:lvp5ki$hb8$1@newscl01ah.mathworks.com...

*snip*

> sinh and cosh in my previous reply were typo. I tried to say sind and
> cosd.
>
> My silly mapping functions are only for who want to have the equations:
>
> sin_rad(pi) = 0
> cos_rad(pi/2) = 0
> exp_rad(i*pi) = -1
> exp_rad(i*pi) = I

So what does sin_rad(100*pi) return? How about sin_rad(1000*pi)?
sin_rad(12345*pi)?

http://www.mathworks.com/company/newsletters/articles/the-tetragamma-function-and-numerical-craftsmanship.html

"Near , sin(x) is very nearly linear, with a slope of -1. So sin(pi) should
be about 1.2246e-16. The exact value reveals information about the value of
 used internally by the math library for argument reduction. Many people
expect sin(pi) to be zero, but then we have to worry about sin(k*pi) for
increasingly large integer k."

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: cos(pi/2) or sin(-pi) problem

From: Sung-Eun Jo

Date: 23 Sep, 2014 18:51:05

Message: 17 of 17

"Steven Lord" <Steven_Lord@mathworks.com> wrote in message <lvpcun$baj$1@newscl01ah.mathworks.com>...
>
> "Sung-Eun Jo" <sungeun.jo+matlab@gmail.com> wrote in message
> news:lvp5ki$hb8$1@newscl01ah.mathworks.com...
>
> *snip*
>
> > sinh and cosh in my previous reply were typo. I tried to say sind and
> > cosd.
> >
> > My silly mapping functions are only for who want to have the equations:
> >
> > sin_rad(pi) = 0
> > cos_rad(pi/2) = 0
> > exp_rad(i*pi) = -1
> > exp_rad(i*pi) = I
>
> So what does sin_rad(100*pi) return? How about sin_rad(1000*pi)?
> sin_rad(12345*pi)?
>
> http://www.mathworks.com/company/newsletters/articles/the-tetragamma-function-and-numerical-craftsmanship.html
>
> "Near , sin(x) is very nearly linear, with a slope of -1. So sin(pi) should
> be about 1.2246e-16. The exact value reveals information about the value of
> used internally by the math library for argument reduction. Many people
> expect sin(pi) to be zero, but then we have to worry about sin(k*pi) for
> increasingly large integer k."
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thanks to Steve's comment. I checked large integer k.
I used the following code:
tt=linspace(-N,N,4*N+1)*pi;
sum(sin_rad(tt)==0) returns the number of zeros.
sum(cos_rad(tt)==0) is also used.
As a result, sin_rad and cos_rad works fine with N<6. Otherwise, it happen to miss some zeros. For example, N=5000, we have
 sum(sin_rad(tt)==0) = 8731
 sum(cos_rad(tt)==0) = 8731
Respectively, they are expected to be 10001 and 10000.
Thus sin_rad function may be used within 5*pi radians.

Well, in order to cover large angle case, I tried the following function:
function A = sin_eps (X)
% Compute the sine for each element of X in radians.
% It returns zero where output is less than epsilon.
%
A = sin(X);
idx_less_eps = abs(A)<eps(X);
A(idx_less_eps) = 0;
return

It is a little slow, but it works well with even large angle.

Considering speed, we can use sin_rad() when we know radians are less than 5*pi.
Otherwise sin_eps() is used to have the exact zeros at k*pi points.

Similarly, I can define cos_eps(), exp_eps(), and expm_eps().
Those names has better meaning than those of *_rad().

Again, these functions can be used to have the equations sin(k*pi)=0 and cos((k+0.5)*pi)=0 without using symbolic toolbox. They are simple to execute.

-- jo

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us