469,353 Members | 2,162 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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 6612
Mark McIntyre <ma**********@spamcop.netwrites:
[...]
>>It happens that the number he's printing, because of the way he
computed it, contains very little valid information, but that's not
the fault of printf or of the format he chose.

In fact it is ALSO the fault of the print format he used since %g
prints significant figures if the %f form is excessively long. Had he
printed it with %f, he'd either have got the answer he expected (eg
0.000000) or had to massively increase the digits to see the delta.
Right, and if he'd used "%f", he wouldn't *know* that he got a
non-zero result (and he'd probably come here and ask us why it
compares unequal to 0.0).

--
Keith Thompson (The_Other_Keith) 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 13 '07 #51
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 13 Jul 2007 18:06:14 +1200, in comp.lang.c , Adam
<ad**@sendnospam.comwrote:
>>So why do sin(M_PI/2.0) and sin(M_PI_2) both give exactly 1?

If I were implementing a sin() function, I'd check for special
cases...
How would you detect special cases? If you have it return 0.0 for
values "sufficiently" close to pi, then you lose the ability to
compute accurate sines for values that really are close to pi.

--
Keith Thompson (The_Other_Keith) 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 13 '07 #52
On Fri, 13 Jul 2007 11:23:39 -0700, user923005 wrote:
On Jul 12, 11:06 pm, Adam <a...@sendnospam.comwrote:
>Joe Wright wrote:
I suggest the information is completely valid, if approximate.
sin(M_PI) here gives..
%.16e 1.2246467991473532e-16
%.16g 1.224646799147353e-16
%.16f 0.0000000000000001
..which is real close to zero. In fact the result is actually the error
in the calculation. Off by 1.2246467991473532e-16 for example.

So why do sin(M_PI/2.0) and sin(M_PI_2) both give exactly 1?
%.30e 1.000000000000000000000000000000e+00
%.30g 1
%.30f 1.000000000000000000000000000000

Probably compiler magic. It's the *wrong* answer. Think about it --
neither M_PI nor M_PI_2 is equal to pi or pi/2 (for obvious reasons --
it's literally impossible for them to contain the right answer).
It isn't. It is well possible that sin(M_PI_2) 1 - DBL_EPSILON/4
and sin(M_PI_2) < 1 + DBL_EPSILON/2, for reasons stated by Keith.

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #53
user923005 <dc*****@connx.comwrites:
On Jul 12, 11:06 pm, Adam <a...@sendnospam.comwrote:
[...]
>So why do sin(M_PI/2.0) and sin(M_PI_2) both give exactly 1?
%.30e 1.000000000000000000000000000000e+00
%.30g 1
%.30f 1.000000000000000000000000000000

Probably compiler magic. It's the *wrong* answer. Think about it --
neither M_PI nor M_PI_2 is equal to pi or pi/2 (for obvious reasons --
it's literally impossible for them to contain the right answer).
No compiler magic is needed. M_PI/2.0 is only an approximation of
pi/2, and sin(M_PI/2.0) is only an approximation of sin(pi/2), but the
error turns out to be small enough that the result is very likely to
be exactly 1.0. See my response elsethread.

--
Keith Thompson (The_Other_Keith) 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 13 '07 #54
On Sat, 14 Jul 2007 06:44:47 +1200, Adam wrote:
Mark McIntyre wrote:
>If I were implementing a sin() function, I'd check for special
cases...
How about this:

y = sin(x)
answer = (fabs(y)<1e-15) ? 0 : y ;
I'd expect sin(1e-25) to be approximately (possibly exactly)[1]
to 1e-25.

[1] sin(1e-25) = 1e-25 - 1e-75 / 6 + 1e-125 / 120...
Very likely to be 1e-25 * (1 - DBL_EPSILON).

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #55

"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:46****************@news.xs4all.nl...
"Malcolm McLean" <re*******@btinternet.comwrote:
>>
"Dale Henderson" <ni****@hotpop.comwrote in message
news:87************@hotpop.com...
>>>>>"JT" == Jens Thoms Toerring <jt@toerring.dewrites:

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. :)
More subtly you can use base i (sqrt -1), and allow imaginary digits.

No, you can't. And you can't use pi, either.

# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)

Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.

Of course, in mathematics, matters are different. But mathematics deals
with infinite precision, while C deals with the real world, where
everything exists only in quanta.
There have been serious proposals to build base phi - the golden ratio OR
1.618... - processors. These have certain advantages because corrupt bits in
integers can be detected. So far no one has implemented a Fibonnaci or base
Phi machine in hardware, to my knowledge.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 14 '07 #56
On Fri, 13 Jul 2007 12:17:31 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
[...]
>>>It happens that the number he's printing, because of the way he
computed it, contains very little valid information, but that's not
the fault of printf or of the format he chose.

In fact it is ALSO the fault of the print format he used since %g
prints significant figures if the %f form is excessively long. Had he
printed it with %f, he'd either have got the answer he expected (eg
0.000000) or had to massively increase the digits to see the delta.

Right, and if he'd used "%f", he wouldn't *know* that he got a
non-zero result (and he'd probably come here and ask us why it
compares unequal to 0.0).
I was pointing out that had he matched his printf to the precision of
hte type he was using, there would have been no "error" visible in the
output of printf.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 15 '07 #57
On Fri, 13 Jul 2007 12:20:51 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>On Fri, 13 Jul 2007 18:06:14 +1200, in comp.lang.c , Adam
<ad**@sendnospam.comwrote:
>>>So why do sin(M_PI/2.0) and sin(M_PI_2) both give exactly 1?

If I were implementing a sin() function, I'd check for special
cases...

How would you detect special cases? If you have it return 0.0 for
values "sufficiently" close to pi, then you lose the ability to
compute accurate sines for values that really are close to pi.
Sure - its a matter of defining which special cases to trap, and how
closely you want to trap them. This is an algorithm question, not a C
language one.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 15 '07 #58
"Malcolm McLean" <re*******@btinternet.comwrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
"Malcolm McLean" <re*******@btinternet.comwrote:
"Dale Henderson" <ni****@hotpop.comwrote in message
This isn't so. You can use base pi where pi becomes 10 of course 4 is
a problem. :)

More subtly you can use base i (sqrt -1), and allow imaginary digits.
No, you can't. And you can't use pi, either.

# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)

Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.

Of course, in mathematics, matters are different. But mathematics deals
with infinite precision, while C deals with the real world, where
everything exists only in quanta.

There have been serious proposals to build base phi - the golden ratio OR
1.618... - processors. These have certain advantages because corrupt bits in
integers can be detected. So far no one has implemented a Fibonnaci or base
Phi machine in hardware, to my knowledge.
I'd like to see them do it. It sounds like one of those ideas that work
like magic in practice, but hit all kinds of snags when you build them
for real.

Richard
Jul 16 '07 #59
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Malcolm McLean" <re*******@btinternet.comwrote:
[...]
>There have been serious proposals to build base phi - the golden
ratio OR 1.618... - processors. These have certain advantages
because corrupt bits in integers can be detected. So far no one has
implemented a Fibonnaci or base Phi machine in hardware, to my
knowledge.

I'd like to see them do it. It sounds like one of those ideas that work
like magic in practice, but hit all kinds of snags when you build them
for real.
s/in practice/in theory/

(unless you're making some really subtle point).

--
Keith Thompson (The_Other_Keith) 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 16 '07 #60
On Jul 13, 10:01 pm, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
"Richard Bos" <r...@hoekstra-uitgeverij.nlwrote in message

news:46****************@news.xs4all.nl...
"Malcolm McLean" <regniz...@btinternet.comwrote:
"Dale Henderson" <nil...@hotpop.comwrote in message
news:87************@hotpop.com...
"JT" == Jens Thoms Toerring <j...@toerring.dewrites:
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. :)
More subtly you can use base i (sqrt -1), and allow imaginary digits.
No, you can't. And you can't use pi, either.
# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)
Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.
Of course, in mathematics, matters are different. But mathematics deals
with infinite precision, while C deals with the real world, where
everything exists only in quanta.

There have been serious proposals to build base phi - the golden ratio OR
1.618... - processors. These have certain advantages because corrupt bits in
integers can be detected. So far no one has implemented a Fibonnaci or base
Phi machine in hardware, to my knowledge.
I have heard of base e and base 3 suggestions but never phi before.
Apparently there is some mathematical reason that base e offers the
most dense compaction of information. Since 3 is close to 2.71828...
base 3 is nearly ideal and can be represented by +1,0,-1 voltage
states. On the other hand, since there is no existing hardware for
base 3 computations, it seems to remain an academic curiosity.

Do you have any citation for the golden ratio base? I would be
curious to read it.

Jul 16 '07 #61
user923005 <dc*****@connx.comwrites:
[...]
I have heard of base e and base 3 suggestions but never phi before.
Apparently there is some mathematical reason that base e offers the
most dense compaction of information. Since 3 is close to 2.71828...
base 3 is nearly ideal and can be represented by +1,0,-1 voltage
states. On the other hand, since there is no existing hardware for
base 3 computations, it seems to remain an academic curiosity.
[...]

That's not quite true, though they are mostly an academic curiosity.

See <http://en.wikipedia.org/wiki/Ternary_computer>. (Yes, it's a
Wikipedia article, but one of the citations is Knuth.)

--
Keith Thompson (The_Other_Keith) 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 16 '07 #62

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>"Malcolm McLean" <re*******@btinternet.comwrote:
[...]
>>There have been serious proposals to build base phi - the golden
ratio OR 1.618... - processors. These have certain advantages
because corrupt bits in integers can be detected. So far no one has
implemented a Fibonnaci or base Phi machine in hardware, to my
knowledge.

I'd like to see them do it. It sounds like one of those ideas that work
like magic in practice, but hit all kinds of snags when you build them
for real.

s/in practice/in theory/

(unless you're making some really subtle point).
What he means is that the principle is sound, the engineering maybe a bit
more difficult. For instance you would have to design all the logic from
scratch, which might mean a big one-off start up cost. When you add that
venture capitalists want a high return on something so risky - they don't
understand the computer science, but they know it is radically new - it
might simply not be feasible as a commercial proposition.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 16 '07 #63
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Malcolm McLean" <re*******@btinternet.comwrote:
[...]
There have been serious proposals to build base phi - the golden
ratio OR 1.618... - processors. These have certain advantages
because corrupt bits in integers can be detected. So far no one has
implemented a Fibonnaci or base Phi machine in hardware, to my
knowledge.
I'd like to see them do it. It sounds like one of those ideas that work
like magic in practice, but hit all kinds of snags when you build them
for real.

s/in practice/in theory/

(unless you're making some really subtle point).
No, that's what I meant. A rather extended typo, that...

Richard
Jul 17 '07 #64
Keith Thompson <ks***@mib.orgwrote:
user923005 <dc*****@connx.comwrites:
[...]
I have heard of base e and base 3 suggestions but never phi before.
Apparently there is some mathematical reason that base e offers the
most dense compaction of information. Since 3 is close to 2.71828...
base 3 is nearly ideal and can be represented by +1,0,-1 voltage
states. On the other hand, since there is no existing hardware for
base 3 computations, it seems to remain an academic curiosity.

That's not quite true, though they are mostly an academic curiosity.

See <http://en.wikipedia.org/wiki/Ternary_computer>. (Yes, it's a
Wikipedia article, but one of the citations is Knuth.)
One Knuth citation does not turn anything into the truth, as is
definitively proven by Shaks. Merch.Ven. I,3:94.

Richard
Jul 17 '07 #65
Richard Bos said:

<snip>
One Knuth citation does not turn anything into the truth, as is
definitively proven by Shaks. Merch.Ven. I,3:94.
The man is, notwithstanding, sufficient. [op cit]

--
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 17 '07 #66
In article <46****************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
....
More subtly you can use base i (sqrt -1), and allow imaginary digits.

No, you can't. And you can't use pi, either.

# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)

Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.
There has been one machine that used ternary floating-point. A host of
machines with hexadecimal floating-points (think IBM and look-alikes).
Also quarternary did occur.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 17 '07 #67
In article <JL********@cwi.nl"Dik T. Winter" <Di********@cwi.nlwrites:
In article <46****************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
...
More subtly you can use base i (sqrt -1), and allow imaginary digits.
>
No, you can't. And you can't use pi, either.
>
# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)
>
Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.

There has been one machine that used ternary floating-point. A host of
machines with hexadecimal floating-points (think IBM and look-alikes).
Also quarternary did occur.
And I did forget base 8, decimal; and Illiac was base 16384 (IIRC).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 17 '07 #68

"user923005" <dc*****@connx.comwrote in message
Do you have any citation for the golden ratio base? I would be
curious to read it.
Phi is 1.62... when you square it you obtain Phi+1. The other number which
has this property, phi, is 0.62... or Phi -1, and is also 1/Phi.

So Phi^0 = 1s
Phi^1 = 1.62s
Phi^2 = 1 + 1.62s
Phi^3 = 1 + 2 * 1.62s
...
Phi^N = Phi^(N-2) + Phi^(N-1)

What it mean is that if we ever have two consecutive 1s, we can turn them
into zeroes and set the following digit.

So you can detect bit corruption.

Also, neatly, you can represent integers. 1 is easy - 1. 2 is 10.01. Why?
because 0.11 = 1, remembering our rule that Phi^N = Phi^(N-1) + Phi(N-2). So
2 is 1.11, but we don't like consecutive set digits, so it becomes 10.01.

Every integer can be represented with the same logic.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Jul 17 '07 #69
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <JL********@cwi.nl"Dik T. Winter" <Di********@cwi.nlwrites:
In article <46****************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
...
More subtly you can use base i (sqrt -1), and allow imaginary digits.
>
No, you can't. And you can't use pi, either.
>
# 5.2.4.2.2 Characteristics of floating types <float.h>
...
# b base or radix of exponent representation (an integer 1)
>
Note: integer. Ternary floating point arithmetic is allowed in C, as is
(probably more commonly; IIRC at least one implementation actually used
it) hexadecimal-based FP. But bases pi and i are not integral.
>
There has been one machine that used ternary floating-point. A host of
machines with hexadecimal floating-points (think IBM and look-alikes).
Also quarternary did occur.

And I did forget base 8, decimal; and Illiac was base 16384 (IIRC).
Ok, ternary I can accept. It's silly, but OK. Hex, quat, octal, fine.
Decimal, I can see the reason. But 16384? 2**14? Come on, they must have
been taking the piss with that one...

Richard
Jul 18 '07 #70
In article <46***************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <JL********@cwi.nl"Dik T. Winter" <Di********@cwi.nlwrites:
In article <46****************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
....
There has been one machine that used ternary floating-point. A host of
machines with hexadecimal floating-points (think IBM and look-alikes).
Also quarternary did occur.
And I did forget base 8, decimal; and Illiac was base 16384 (IIRC).

Ok, ternary I can accept. It's silly, but OK. Hex, quat, octal, fine.
Decimal, I can see the reason. But 16384? 2**14? Come on, they must have
been taking the piss with that one...
Yes, my bad memory, but it is from an e-mail I received some 13 years ago.
It was Maniac and the base was 65536 (it had 68 bit words, presumably 64
for the mantissa and 4 for the exponent).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 19 '07 #71

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.