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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

"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 12

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 12

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

Tags for this Thread

No tags are associated with 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