delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/03/02:18:44

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Can't find an error! Suspect GCC bug!!!
Date: Fri, 01 Aug 1997 18:21:15 -0400
Organization: Cornell University http://www.cornell.edu
Lines: 57
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <33E2615B.21A2@cornell.edu>
References: <33E21BEC DOT 2964E06C AT execulink DOT com>
Reply-To: asu1 AT cornell DOT edu
NNTP-Posting-Host: cu-dialup-0068.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Jeff Weeks wrote:
> 
> Hi everybody,
> 
> I've been writting a switcheable fixed/floating point math library, 
> and I've come into a very wierd problem.  The fixed point math works 
> fine, but the floating point is rather sparatic.  Worst of all, I 
> can't find an error, so I think perhaps GCC is at fault!

wow, we even have an exclamation point. gcc must really be buggy!

> 
> Anyway, the lib is C++, so I have a "decimal" class, and I define my
> numbers like this:
> 
> decimal num1(10.5);
> decimal num2(50.75);
> 
> And that working fine... untill I added sin/cos functions.  These
> sin/cos functions use look-up tables and therefore need an init 
> function (which I defined as static):
> 
> decimal::init();
> 
> However, if after calling init(), num1 somehow equals 1.  Somehow, the
> init function changed the value of my num1 variable.  Here's what init
> looks like:
> 
> float floating_cosine[360];
> float floating_sine[360];
> 
> inline void Floating::init(void) {
>   int i;
>   // initialise the sine and cosine tables (360 degrees).
>   for(i = 0; i <= 360; i++) {
>     floating_cosine[i] = (float)(cos((double)i*3.1415926/180.0));
>     floating_sine[i]   = (float)(sin((double)i*3.1415926/180.0));
>   }
> }

floating_cosine (yuck) is an array with 360 elements. your loop writes
to the 361st element. basic mistake.

also, it is not good style to come up with your own PI. the header
math.h defines explicit constants for that purpose. look there.

-- 
   Sinan
 
*******************************************************************
 A. Sinan Unur                                WWWWWW
                                              |--O+O
 mailto:sinan DOT unur AT cornell DOT edu                C   ^ 
 http://www.people.cornell.edu/pages/asu1/     \  ~/ 

Unsolicited e-mail is _not_ welcome, and will be billed for.
*******************************************************************

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019