sin(pi)

本文深入分析了在MATLAB环境下,计算sin(pi)为何得出非零结果的原因,解释了数值计算中的误差来源,并讨论了如何正确理解计算机输出的数学函数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Thread Subject:
sin(pi) does not equal zero

Subject: sin(pi) does not equal zero

From: Steve Waikam SIU

Date: 11 Dec, 1998 20:12:23

Message: 1 of 6

Hi,
I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
zero. Is it a bug? Is there any methods to correct it?

sin(pi)

ans =

  1.2246e-016

Steve

Subject: sin(pi) does not equal zero

From: 112358etc@my-dejanews.com

Date: 11 Dec, 1998 18:27:23

Message: 2 of 6

In article <36710C27.8DA4BC75@ee.cityu.edu.hk>,
  Steve Waikam SIU <wksiu@ee.cityu.edu.hk> wrote:
> Hi,
> I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
> zero. Is it a bug? Is there any methods to correct it?
>
> sin(pi) = 1.2246e-016


Steve:
    I don't want to disappoint you, but "pi" is not really pi,
"sin" is not really the sine function, and indeed, NO matlab
trigonometric, logarithmic or exponential function is the true
algebraic/calculus function you learned about in school. The
reality is that Matlab uses only a finite number of decimal
places (roughly 16), and that the algorithms that it uses
to ESTIMATE these functions do only that: estimate them.
Incidentally, the same situation occurs with scientific
calculators, and has since they first hit the market in
the '70's, and for those of us old enough to remember,
there was only limited accuracy in all of those books full
of tables (all based on estimates anyway). The important
point is to be able to interpret the output intelligently,
and to be able to understand when the output really should
be zero. So far, that is one edge that humans retain over
computers. For the moment. Finally, if you want to know
more about this issue of what the computer is really doing,
look at a book on numerical analysis, or better yet, take
a course somewhere. It's a fascinating subject!

Jeff Stuart, Ph.D
Department of Mathematics
University of Southern Mississippi
jeff.stuart_REMOVE_THIS@usm.edu (Isn't spam grand?)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Subject: sin(pi) does not equal zero

From: Matt Jones

Date: 11 Dec, 1998 18:27:54

Message: 3 of 6

In article <36710C27.8DA4BC75@ee.cityu.edu.hk> Steve Waikam SIU,
wksiu@ee.cityu.edu.hk writes:
>Hi,
>I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
>zero. Is it a bug? Is there any methods to correct it?
>
>sin(pi)
>
>ans =
>
> 1.2246e-016
>

Hmm, that's very strange. I'm using Matlab 5.2.1 on the Mac, and here's
what I get:

»format short
»sin(pi)

ans =

     0

»format long
»sin(pi)

ans =

     0

»format long e
»sin(pi)

ans =

     0


Matt Jones

Subject: sin(pi) does not equal zero

From: rayn@mathworks.com (raymond s. norris)

Date: 11 Dec, 1998 14:06:29

Message: 4 of 6

Hi Steve-

Given what Jeff says in his post, that doesn't prevent compilers from
doing the right thing. In some instances, the result can depend heavily
on the vendor's compiler interpretation of sin(pi) (for example.)

If you are looking for zeroness, perhaps a better approach would be the
following:

       >> value=sin(pi);
       >> if abs(value-0)<eps, value=0, end
       value =
            0

type
>> help eps

for more info on eps.

hth,
-raymond


In article <74ro68$22h$1@nnrp1.dejanews.com>, 112358etc@my-dejanews.com
says...
> In article <36710C27.8DA4BC75@ee.cityu.edu.hk>,
> Steve Waikam SIU <wksiu@ee.cityu.edu.hk> wrote:
> > Hi,
> > I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
> > zero. Is it a bug? Is there any methods to correct it?
> >
> > sin(pi) = 1.2246e-016
>
>
> Steve:
> I don't want to disappoint you, but "pi" is not really pi,
> "sin" is not really the sine function, and indeed, NO matlab
> trigonometric, logarithmic or exponential function is the true
> algebraic/calculus function you learned about in school. The
> reality is that Matlab uses only a finite number of decimal
> places (roughly 16), and that the algorithms that it uses
> to ESTIMATE these functions do only that: estimate them.
> Incidentally, the same situation occurs with scientific
> calculators, and has since they first hit the market in
> the '70's, and for those of us old enough to remember,
> there was only limited accuracy in all of those books full
> of tables (all based on estimates anyway). The important
> point is to be able to interpret the output intelligently,
> and to be able to understand when the output really should
> be zero. So far, that is one edge that humans retain over
> computers. For the moment. Finally, if you want to know
> more about this issue of what the computer is really doing,
> look at a book on numerical analysis, or better yet, take
> a course somewhere. It's a fascinating subject!
>
> Jeff Stuart, Ph.D
> Department of Mathematics
> University of Southern Mississippi
> jeff.stuart_REMOVE_THIS@usm.edu (Isn't spam grand?)

Subject: sin(pi) does not equal zero

From: moler@mathworks.com (Cleve Moler)

Date: 11 Dec, 1998 14:20:35

Message: 5 of 6

Steve Waikam SIU <wksiu@ee.cityu.edu.hk> wrote:

> I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
> zero. Is it a bug? Is there any methods to correct it?
>
> sin(pi)
>
> ans =
>
> 1.2246e-016
>
> Steve


Hi.

I can't easily use Greek letters in newsgroup followups, so let me
instead use Pi to denote the classic mathematical quantity which is
the ratio of the circumference of a circle to its diameter and the
half-period of the sin function. MATLAB's floating point variable pi
is only an approximation to Pi. The graph of sin(x) is very nearly
linear near x = Pi, so

   sin(pi) - sin(Pi) = -(pi - Pi) + a term of size (pi - Pi)^3.

By definition, sin(Pi) is zero, so

   sin(pi) ~= Pi - pi

In other words, the value of sin(pi) should be very close to the
(negative of) the error in pi as an approximation to Pi.

We can ask the symbolic toolbox to compare our floating point pi with
whatever it uses to compute Pi.

   >> double(sym('pi') - sym(pi,'f'))

   ans =
       1.224646799147353e-016

So, Steve found that MATLAB actually computes the right answer for sin(pi).

Also note that, when applied to floating point numbers, cos(x) is never 0
and tan(x) is a bounded function.

Of course, we could arrange that sin(pi) comes out to be zero. In fact,
this happens on some machines because the underlying elementary math
function library believes that pi is equal to Pi. But then sin(10*pi)
and sin(1000000*pi) should also be zero, but they aren't.

   -- Cleve Moler
   moler@mathworks.com

Subject: sin(pi) does not equal zero

From: spamthis@my-dejanews.com

Date: 11 Dec, 1998 19:28:17

Message: 6 of 6

In article <36710C27.8DA4BC75@ee.cityu.edu.hk>,
  Steve Waikam SIU <wksiu@ee.cityu.edu.hk> wrote:
> Hi,
> I'm using MATLAB 5.1 for Windows, I found that sin(pi) doesn't equal
> zero. Is it a bug? Is there any methods to correct it?
>
> sin(pi)
>
> ans =
>
> 1.2246e-016
>
> Steve
>
>

Yes it is a bug.. I like to call it the "finite precision bug", or sometimes,
the "anal retention bug". I suspect it is the same bug that leads to:


1-0.1*exp(log(10))= -2.220446049250313e-16


To fix it, you just need to buy a computer with enough bits of precision...
enough that you lose interest and wander off before the
still-slightly-incorrect answer is returned. Until you make this wise
purchase, you can convince yourself that nothing insidious lurks within by
looking at:

k=0:1000;
plot(sin(k*pi))

This reassures me (at least) that no grave errors were made in the sin()
function.
The errors remain small (as does the mean of these errors). For much larger
k, the mean drifts slightly, but if you need better precision, you might wish
to consider using a .mex file and doing it yourself.

Helpful as ever,
Claude

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rss Feed for this Thread

Contact us

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值