473,587 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sin (M_PI)

Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?

Thanks,
Adam
Jul 12 '07 #1
70 7322
On 12 Jul, 11:14, Adam <a...@sendnospa m.comwrote:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why,
The usual issues with floating point accuracy, I'd think.
and what I can do about it?
See if your target implementation has an alternative sin()
implementation (sinl(), which takes and returns long double, may be
available for example) and a suitable definition of PI.

Jul 12 '07 #2
On 12 Jul, 11:26, mark_blue...@po box.com wrote:
On 12 Jul, 11:14, Adam <a...@sendnospa m.comwrote:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why,

The usual issues with floating point accuracy, I'd think.
and what I can do about it?

See if your target implementation has an alternative sin()
implementation (sinl(), which takes and returns long double, may be
available for example) and a suitable definition of PI.
I must give up replying to myself...

sinl() is part of the standard and should be available. Finding a good
definition of PI may be more difficult, but a GNU libc header gives:-

# define M_PIl 3.1415926535897 932384626433832 795029L /*
pi */

Jul 12 '07 #3
Adam wrote:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Firstly M_PI is not defined in Standard C, though most implementations
define it as an extension. You can also compute it's value at runtime
or define it yourself.

Secondly make sure you include stdio.h and math.h for printf and sin.
And try using the %f format specifier.

Jul 12 '07 #4
santosh wrote:
Adam wrote:
>Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?

Firstly M_PI is not defined in Standard C, though most implementations
define it as an extension. You can also compute it's value at runtime
or define it yourself.

Secondly make sure you include stdio.h and math.h for printf and sin.
And try using the %f format specifier.
Using %f works, but isn't a suitable solution, as my code is actually
printf("%g", sin(x)), where x could be pi, or something else, and %f
messes up the formatting for the other possibilities for x.
Jul 12 '07 #5
Adam wrote:
santosh wrote:
Adam wrote:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Firstly M_PI is not defined in Standard C, though most implementations
define it as an extension. You can also compute it's value at runtime
or define it yourself.

Secondly make sure you include stdio.h and math.h for printf and sin.
And try using the %f format specifier.
Using %f works, but isn't a suitable solution, as my code is actually
printf("%g", sin(x)), where x could be pi, or something else, and %f
messes up the formatting for the other possibilities for x.
One possibility is to compare x with M_PI and if they're near enough,
print zero.
Something like:

if(abs(M_PI - x) <= delta) puts("0000e0");

Jul 12 '07 #6
Adam <ad**@sendnospa m.comwrote:
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Pi is an irrational number, i.e. you can't write it down
exactly without giving an infinite number of digits (what-
ever number system you use). Computers can't deal with
irrational numbers very well (i.e. at all) since in a
computer you can only store a finite number of digits.
Thus what you use for pi is never the true value of pi
but an approximation. So you can't expect more that an
approximation of the value of sin of pi when you use an
approximation of pi to start with.

Whenever you use floating point numbers you must consider
that the computer only stores a limited number of (binary)
digits. In many cases the number of digits that can be
stored is less than the true number of digits of the
floating point number - rather obvious for irrational
numbers like pi, but even a simple looking number like 0.1
becomes a number with infinitely many digits when expressed
in binary (like 1/3 has an infinite number of digits when
written in decimal). Thus in many (if not most) cases you
do calculations with approximations only. And that's not
the end of it. E.g. function like sin are computed using
(clever) approximate algorithms (which tend to be a limi-
ted number of summands of an infinite sum). So you do ap-
proximate calculations of functions on approximate values.
And the result is, of course, that you end up with more or
less good approximate results. And when you think of it,
1e-16 is a rather good approximation for a 0 when you con-
sider the range [-1:1] of the sin function.

Thus if you do floating point calculations you have to be
aware of the approximate nature of such computations. And
you have to be aware that small errors due to the approxi-
mations can sum up considerably - if you aren't careful
you can end up with completely bogus results.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jul 12 '07 #7
santosh wrote:
Adam wrote:
santosh wrote:
Adam wrote:
>Hi,
>I am using the following code:
>printf("%g", sin(M_PI))
>and getting 1.22461e-16 instead of zero.
>Does anyone have any idea why, and what I can do about it?
>
Firstly M_PI is not defined in Standard C, though most implementations
define it as an extension. You can also compute it's value at runtime
or define it yourself.
>
Secondly make sure you include stdio.h and math.h for printf and sin.
And try using the %f format specifier.
>
Using %f works, but isn't a suitable solution, as my code is actually
printf("%g", sin(x)), where x could be pi, or something else, and %f
messes up the formatting for the other possibilities for x.

One possibility is to compare x with M_PI and if they're near enough,
print zero.
Something like:

if(abs(M_PI - x) <= delta) puts("0000e0");
Sorry about that. Use fabs instead of abs:

if(fabs(M_PI - x) <= delta) printf("%g\n", 0.0);

Jul 12 '07 #8
Adam wrote:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Ponder these questions:

1: Is M_PI exactly equal to pi, or is it equal to pi+x
for some small x?

2: What is the value of sin(pi+x)?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 12 '07 #9
Adam said:
Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Presumably M_PI represents a value close to pi. It cannot represent pi
exactly, because pi is irrational. Given the nature of a sine wave, you
can expect sin(reasonable_ approximation_t o_pi) to be a reasonable
approximation to 0, and indeed that is what you are getting.

If you want to round the value, decide at which decimal place or
significant figure you wish to round it, and whether you wish to round
it up, down, or nearest. Then write code to do the rounding for you.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
27547
by: JustSomeGuy | last post by:
In MS Visual C++ 6.0 Where is M_PI defined? Where is round defined?
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.