home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!wet!mcs
- From: mcs@wet.UUCP (Gil Nardo)
- Newsgroups: comp.os.msdos.programmer
- Subject: Re: Math error in BC++? Help!
- Message-ID: <4873@wet.UUCP>
- Date: 17 Dec 92 19:04:52 GMT
- References: <1992Dec15.211516.1664@schunix.uucp>
- Distribution: na
- Organization: Wetware Diversions, San Francisco
- Lines: 61
-
- sonix@schunix.uucp (Duane Morin) writes:
- > This is driving us NUTS!! Will someone please take a look at the following
- > code and see if they can figure out why it does what it does? Thank you.
- >
- > The following code segment shows a snippet of a program that needs
- > to calculate
- > the log base 2 of 128 (which happens to be 7). The problem is that the
- > code works fine on a 386DX/40, with 387 installed, and does NOT work on
- > a 486DX/33! Help??
- >
-
- I included a shortened version of your program. I did not completely
- solved it but I do have some clues as to what's causing the problem.
- After compiling with the assembly output option, I found that at
- least two methods can be used to make it return the answer of 7.
-
- One method is to pop the answer off of the co-processor stack and
- pop it back on using the fstp and fld instructions prior to the
- call to N_FTOL (float to long, near call version). This method
- is equivalent to the two step processes of as shown in the code.
-
- The other method is to substitute N_FTOL with the use of fistp
- (store and pop stack as integer) and followed by the use of the
- lower part of the of the quad word as the result.
-
- I would guess at this point that the function N_FTOL inadvertantly
- modifies either the rounding or the precision bits of the
- co-processor control word. I will leave it to you and others to
- figure out why this happens on the 486 chip and not
- on the 386/387 chips, mainly because I need to get back to work.
-
- ----------
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <float.h>
-
- // The following constants are normally defined within the program:
-
- #define LOG2 (log(2))
- #define LOGN(y) (log ((double)y) / LOG2 )
- main()
- {
- double l;
- int i;
-
- l = LOGN(128);
- i = (int)l;
- printf("two step = %d\n", i); // this comes up with 7
-
- i = (int)(LOGN(128));
- printf("one step = %d\n", i); // this comes up with 6?
-
- return(0);
- }
-
- --
- Gil Nardo | mcs@wet.com
- Migrant Computing Services | (415)664-1032
- 1032 Irving Street, #435 |-----------------
- San Francisco, 94122 | Supernova = *!
-