Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Bug in C compiler?
@ 1996-09-19  4:00 Theo Vosse
  1996-09-19  4:00 ` Ron Natalie
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Theo Vosse @ 1996-09-19  4:00 UTC (permalink / raw)



Hi,

I wrote this piece of program:

static double FillBinomTable(double tab[], int b)
{
    double factor = 1.0 / pow(2.0, b);
    int n = b, m = 1;
    int i = 0;

    while (n != -1)
    {
        tab[i++] = factor;
        factor *= (double) n-- / (double) m++;
    }
}

which fills a table with binomial probabilities. The problem is that
when I use DEC's c compiler (with -g), 'factor' becomes 0.707106...,
which is sqrt(2), whereas when I use gcc, it becomes 1/(2**b). The
last one is correct.

Is this some kind of optimizer bug?

	Theo Vosse
	----------
	Unit for Experimental Psychology
	University of Leiden
	The Netherlands
-- 
	Theo Vosse
	----------
	Unit for Experimental Psychology
	University of Leiden
	The Netherlands



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bug in C compiler?
  1996-09-19  4:00 Bug in C compiler? Theo Vosse
@ 1996-09-19  4:00 ` Ron Natalie
  1996-09-19  4:00   ` Ron Natalie
  1996-09-19  4:00 ` Stephen Hoff Hoffman
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Ron Natalie @ 1996-09-19  4:00 UTC (permalink / raw)



Theo Vosse wrote:
> 
> Hi,
> 
> I wrote this piece of program:
> 
> static double FillBinomTable(double tab[], int b)
> {
>     double factor = 1.0 / pow(2.0, b);
> 
> which fills a table with binomial probabilities. The problem is that
> when I use DEC's c compiler (with -g), 'factor' becomes 0.707106...,
> which is sqrt(2), whereas when I use gcc, it becomes 1/(2**b). The
> last one is correct.
> 

Actually, .707... is 1/sqrt(2) or most likely 1/ pow(2.0, 0);

Any chance that you don't have "pow" properly declared.  It looks
like "b" might be passed to power as an "int" (it wants double).
You are using the ANSI compiler flags?

A real quick test would be to cast b to double....

-Ron



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bug in C compiler?
  1996-09-19  4:00 Bug in C compiler? Theo Vosse
  1996-09-19  4:00 ` Ron Natalie
@ 1996-09-19  4:00 ` Stephen Hoff Hoffman
  1996-09-20  4:00 ` Phil Ottewell
  1996-09-20  4:00 ` Theo Vosse
  3 siblings, 0 replies; 6+ messages in thread
From: Stephen Hoff Hoffman @ 1996-09-19  4:00 UTC (permalink / raw)




In article <4buf2zhlw.fsf@ruls41.fsw.LeidenUniv.nl>, vosse@ruls41.fsw.LeidenUniv.nl (Theo Vosse) writes:

:I wrote this piece of program:
:
:static double FillBinomTable(double tab[], int b)
:{
..
:}
:
:which fills a table with binomial probabilities. The problem is that
:when I use DEC's c compiler (with -g), 'factor' becomes 0.707106...,
:which is sqrt(2), whereas when I use gcc, it becomes 1/(2**b). The
:last one is correct.
:
:Is this some kind of optimizer bug?

  Please e-mail me with the operating system and version, platform, and
  the complete series of DCL or shell commands used to compile and build
  the image here, and a *short* standalone copy of the C source to me,
  and I'll pass it all along to the appropriate engineering folks. 

  (From the compiler commands, I'll be able to tell which C compiler
  is involved, but -- if applicable -- the C compiler version would
  also be useful.)

  When e-mailing, note I have munged the headers.

  -------------------------- pure personal opinion ---------------------------
   Stephen 'Hoff' Hoffman   OpenVMS Engineering   hoffman@xdelta.enet.dec.com
   <Headers deliberately munged to avoid automated junkmail -- the spam-bots.>
    OpenVMS Frequently Asked Questions (FAQ): http://www.openvms.digital.com




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bug in C compiler?
  1996-09-19  4:00 ` Ron Natalie
@ 1996-09-19  4:00   ` Ron Natalie
  0 siblings, 0 replies; 6+ messages in thread
From: Ron Natalie @ 1996-09-19  4:00 UTC (permalink / raw)



Ron Natalie wrote:
> 
> Theo Vosse wrote:
> 
> Actually, .707... is 1/sqrt(2) or most likely 1/ pow(2.0, 0);

I meant pow(2.0, 0.5)...



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bug in C compiler?
  1996-09-19  4:00 Bug in C compiler? Theo Vosse
  1996-09-19  4:00 ` Ron Natalie
  1996-09-19  4:00 ` Stephen Hoff Hoffman
@ 1996-09-20  4:00 ` Phil Ottewell
  1996-09-20  4:00 ` Theo Vosse
  3 siblings, 0 replies; 6+ messages in thread
From: Phil Ottewell @ 1996-09-20  4:00 UTC (permalink / raw)



-----BEGIN PGP SIGNED MESSAGE-----

In article <4buf2zhlw.fsf@ruls41.fsw.LeidenUniv.nl>,
 vosse@ruls41.fsw.LeidenUniv.nl (Theo Vosse) writes:
[snip]
>     double factor = 1.0 / pow(2.0, b);
> 
> which fills a table with binomial probabilities. The problem is that
> when I use DEC's c compiler (with -g), 'factor' becomes 0.707106...,
> which is sqrt(2), whereas when I use gcc, it becomes 1/(2**b). The
> last one is correct.

  I saw a bug very similar to this on DEC C for VMS. It was actually my fault,
because I had omitted to include math.h, and as I recall the code was
(correctly because I hadn't declared it) interpreting the floating point 'bit
pattern', returned by the function, as an _integer_ . It was then doing a
calculation similar to yours, so it converted the 'integer' to double and gave
the unexpected result.

  Once I had tracked down the cause, I wondered why I hadn't received and
'implicit function type' whinge from the compiler. It took me several hours
to track the problem down to a buglet in the types.h standard header. Near
the top was a "#pragma __nostandard" but there was no corresponding
"#pragma __standard" at the bottom to turn the warnings back on. So, anything
that included types.h (and this tended to be indirectly, via X11 headers for
example) had the warnings switched off ! This was fixed by DEC C 5.3 I believe.

  Short version: Have you included math.h ? :-)

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3i
Charset: noconv

iQCVAwUBMkHu0gFrKHWv1IPxAQEKSAQAxR7HpHhIaYzcp9rOed4n7KbWJfTcZJQD
/rggaWqcOCH7Bxg2SCr6fHcP8ErsblNQuAxf+8na7LxqGqPaZFuMBe3MRdt03vee
AkTz3VTq7OK897WGqDsG6xd7G3nT234NgXHG3IlZ+Qg/XTyCs39mRwJsM5IXSQXT
HevJrf6w8Dc=
=wB+0
-----END PGP SIGNATURE-----
-- 
Phil Ottewell - PGP Key ID: 0xAFD483F1 -  WWW: http://www.pottsoft.demon.co.uk/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bug in C compiler?
  1996-09-19  4:00 Bug in C compiler? Theo Vosse
                   ` (2 preceding siblings ...)
  1996-09-20  4:00 ` Phil Ottewell
@ 1996-09-20  4:00 ` Theo Vosse
  3 siblings, 0 replies; 6+ messages in thread
From: Theo Vosse @ 1996-09-20  4:00 UTC (permalink / raw)



After a letter from dsr@lns598.lns.cornell.edu (Daniel S. Riley), I
decided to use -std1 to compile the program, and indeed, it suddenly
worked.

Inspection of <math.h> (which I *had* included) reveiled that
functions are declared without their prototype, unless the
preprocessor macro _NO_PROTO is *undefined*.

What reason anybody can have to not included prototypes is beyond me,
but now we are apparently forced to use -std all the time. However,
since the compiler in default mode does recognize prototypes, there
seems to be no reason not to include them.

Perhaps the next releases can leave _NO_PROTO undefined???

	Theo Vosse
	----------
	Unit for Experimental Psychology
	University of Leiden
	The Netherlands
-- 
	Theo Vosse
	----------
	Unit for Experimental Psychology
	University of Leiden
	The Netherlands



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1996-09-20  4:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-09-19  4:00 Bug in C compiler? Theo Vosse
1996-09-19  4:00 ` Ron Natalie
1996-09-19  4:00   ` Ron Natalie
1996-09-19  4:00 ` Stephen Hoff Hoffman
1996-09-20  4:00 ` Phil Ottewell
1996-09-20  4:00 ` Theo Vosse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox