Hi,
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e16 instead of zero.
Does anyone have any idea why, and what I can do about it?
Thanks,
Adam
Jul 12 '07
70 7266
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
nonzero 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"
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"
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.2246467991473532e16
%.16g 1.224646799147353e16
%.16f 0.0000000000000001
..which is real close to zero. In fact the result is actually the error
in the calculation. Off by 1.2246467991473532e16 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 (?)
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"
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)<1e15) ? 0 : y ;
I'd expect sin(1e25) to be approximately (possibly exactly)[1]
to 1e25.
[1] sin(1e25) = 1e25  1e75 / 6 + 1e125 / 120...
Very likely to be 1e25 * (1  DBL_EPSILON).

Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity."  R. J. Hanlon (?)
"Richard Bos" <rl*@hoekstrauitgeverij.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) hexadecimalbased 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
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 nonzero 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
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
"Malcolm McLean" <re*******@btinternet.comwrote:
"Richard Bos" <rl*@hoekstrauitgeverij.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) hexadecimalbased 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 rl*@hoekstrauitgeverij.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"
On Jul 13, 10:01 pm, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
"Richard Bos" <r...@hoekstrauitgeverij.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) hexadecimalbased 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.
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"
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
rl*@hoekstrauitgeverij.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 oneoff 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
Keith Thompson <ks***@mib.orgwrote: rl*@hoekstrauitgeverij.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
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
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
In article <46****************@news.xs4all.nl rl*@hoekstrauitgeverij.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) hexadecimalbased FP. But bases pi and i are not integral.
There has been one machine that used ternary floatingpoint. A host of
machines with hexadecimal floatingpoints (think IBM and lookalikes).
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/
In article <JL********@cwi.nl"Dik T. Winter" <Di********@cwi.nlwrites:
In article <46****************@news.xs4all.nlrl*@hoekstrauitgeverij.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) hexadecimalbased FP. But bases pi and i are not integral.
There has been one machine that used ternary floatingpoint. A host of
machines with hexadecimal floatingpoints (think IBM and lookalikes).
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/
"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^(N2) + Phi^(N1)
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^(N1) + Phi(N2). 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
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <JL********@cwi.nl"Dik T. Winter" <Di********@cwi.nlwrites:
In article <46****************@news.xs4all.nlrl*@hoekstrauitgeverij.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) hexadecimalbased FP. But bases pi and i are not integral.
>
There has been one machine that used ternary floatingpoint. A host of
machines with hexadecimal floatingpoints (think IBM and lookalikes).
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
In article <46***************@news.xs4all.nl rl*@hoekstrauitgeverij.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*@hoekstrauitgeverij.nl (Richard Bos) writes:
....
There has been one machine that used ternary floatingpoint. A host of
machines with hexadecimal floatingpoints (think IBM and lookalikes).
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 email 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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JustSomeGuy 
last post by:
In MS Visual C++ 6.0
Where is M_PI defined?
Where is round defined?

by: Faith0G 
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

by: ryjfgjl 
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...

by: Charles Arthur 
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone

by: aa123db 
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...

by: ryjfgjl 
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and timeconsuming...

by: ryjfgjl 
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

by: emmanuelkatto 
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel

by: BarryA 
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

by: Hystou 
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
 