473,915 Members | 5,967 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 7442
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
27563
by: JustSomeGuy | last post by:
In MS Visual C++ 6.0 Where is M_PI defined? Where is round defined?
0
11354
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11066
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3368
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.