473,909 Members | 5,624 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
70 7439
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?
As the other responders have pointed out, this is to be expected. If you
had opened up your search engine, you surely would have found some close
references.
As you haven't said anything about your goals for modifying this
behavior, the suggestion about sinl(M_PIl) is as good as any, keeping in
mind that default Windows behavior is for long double to be simply an
alias for double.

Think about the steps you are taking here.
M_PI (not defined in std C) is likely a truncated decimal approximation
to the irrational value of pi.
The argument to sin() is at best the nearest binary value to the decimal
value. It could be as much as DBL_EPSILON larger or smaller than the
ideal value. A little application of high school math should help you
figure out how much difference this makes to the ideal value of sin().

If you are writing the code for a compiler to use in constant
propagation, say for a target processor like x87 which has some built-in
extra precision constants, you could take the position that you should
pick the closest of those built-in constants, when it satisfies the
demands of the C standard for closeness to the value in question. This
would enable the compiler to evaluate sin(M_PI) as 0. at compile time,
but then you would have a discrepancy against the behavior of values
generated at run time. Unless you want to write a sin() function which
searches for a match to certain multiples of the built-in pi before
evaluation. Where do you draw the line? pi/4,..., INT_MAX/4*PI ?

I understand there has been academic work on the precision attainable by
math functions including sin(). When people are building careers on this
subject, can you seriously ask "does anyone have any idea?"
Jul 12 '07 #11
"Jens Thoms Toerring" wrote:
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)
One nitpick. I think pi is transcendental rather than irrational.

http://en.wikipedia.org/wiki/Lindema...strass_theorem
Jul 12 '07 #12
Tim Prince 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?

As the other responders have pointed out, this is to be expected. If you
had opened up your search engine, you surely would have found some close
references.
As you haven't said anything about your goals for modifying this
behavior, the suggestion about sinl(M_PIl) is as good as any, keeping in
mind that default Windows behavior is for long double to be simply an
alias for double.
This is wrong. The lcc-win32 compiler features full precision (80 bits
floating point) for long double. The long double equals double
feature is just a feature of one compiler system under windows:
Microsoft MSVC. Other compilers under windows offer more precision.

jacob
Jul 12 '07 #13
"osmium" <r1********@com cast.netwrote:
"Jens Thoms Toerring" wrote:
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)

One nitpick. I think pi is transcendental rather than irrational.

http://en.wikipedia.org/wiki/Lindema...strass_theorem
Transcendental, and _therefore_ irrational. The transcendental numbers
are a strict subset of the rational ones.

Richard
Jul 12 '07 #14
"Richard Bos" writes:
"osmium" <r1********@com cast.netwrote:
>"Jens Thoms Toerring" wrote:
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)

One nitpick. I think pi is transcendental rather than irrational.

http://en.wikipedia.org/wiki/Lindema...strass_theorem

Transcendental, and _therefore_ irrational. The transcendental numbers
are a strict subset of the rational ones.
I already said it was a nitpick. But the subject of the thread *is*
precision, so one should use the most precise word. "Apple" is more precise
than "fruit".
Jul 12 '07 #15
osmium wrote:
"Jens Thoms Toerring" wrote:
>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)

One nitpick. I think pi is transcendental rather than irrational.
(fx:counter-nitpick)

All the transcendentals are irrational. "Pi isn't just irrational;
it's transcendental" is better than "... rather than ...".

It's certainly a subject one can get one's teeth into.

--
RIP Donald Michie 11 November 1923 - 7 July 2007

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jul 12 '07 #16
osmium wrote:
I already said it was a nitpick. But the subject of the thread *is*
precision, so one should use the most precise word.
That doesn't follow. (Covariance and contravariance come to mind.
Possibly only metaphorically. )

--
RIP Donald Michie 11 November 1923 - 7 July 2007

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jul 12 '07 #17
Richard Bos wrote:
Transcendental, and _therefore_ irrational. The transcendental numbers
are a strict subset of the rational ones.
s/rat/irrat/?

--
RIP Donald Michie 11 November 1923 - 7 July 2007

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jul 12 '07 #18
>>>>"JT" == Jens Thoms Toerring <jt@toerring.de writes:

JTPi is an irrational number, i.e. you can't write it down
JTexactly without giving an infinite number of digits (what-
JTever number system you use).

This isn't so. You can use base pi where pi becomes 10 of course 4 is
a problem. :)

See http://mathworld.wolfram.com/Base.html and
http://www.dwheeler.com/essays/bases.html

Jul 12 '07 #19
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"osmium" <r1********@com cast.netwrote:
>"Jens Thoms Toerring" wrote:
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)

One nitpick. I think pi is transcendental rather than irrational.

http://en.wikipedia.org/wiki/Lindema...strass_theorem

Transcendental, and _therefore_ irrational. The transcendental numbers
are a strict subset of the rational ones.
(s/rational/irrational/)

And the relevant point here is that pi cannot be represented exactly
as a floating-point number. That's merely because it's not an integer
multiple of a power of 2.0. The same issue can arise even for
rational numbers. For example, mathematically, sqrt(1.0/9.0) =
1.0/3.0, but neither 1.0/9.0 nor 1.0/3.0 can be represented exactly in
most floating-point systems.

(Quibble: C doesn't require binary floating-point. A trinary
floating-point system could represent 1.0/3.0 and 1.0/9.0 exactly --
but not 0.5. Conceivably a base-pi system could represent pi exactly,
but not 1.0.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 12 '07 #20

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
10037
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9879
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11348
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
10921
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
11052
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,...
1
8099
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
7249
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3359
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.