home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / msdos / programm / 11484 < prev    next >
Encoding:
Text File  |  1992-12-17  |  2.4 KB  |  73 lines

  1. Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!wet!mcs
  2. From: mcs@wet.UUCP (Gil Nardo)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: Math error in BC++?  Help!
  5. Message-ID: <4874@wet.UUCP>
  6. Date: 17 Dec 92 19:08:19 GMT
  7. References: <1992Dec15.211516.1664@schunix.uucp>
  8. Distribution: usa
  9. Organization: Wetware Diversions, San Francisco
  10. Lines: 61
  11.  
  12. sonix@schunix.uucp (Duane Morin) writes:
  13. > This is driving us NUTS!! Will someone please take a look at the following 
  14. > code and see if they can figure out why it does what it does?  Thank you.
  15. > The following code segment shows a snippet of a program that needs
  16. > to calculate
  17. > the log base 2 of 128 (which happens to be 7).  The problem is that the 
  18. > code works fine on a 386DX/40, with 387 installed, and does NOT work on 
  19. > a 486DX/33!  Help??
  20.  
  21. I included a shortened version of your program.  I did not completely
  22. solved it but I do have some clues as to what's causing the problem.
  23. After compiling with the assembly output option, I found that at
  24. least two methods can be used to make it return the answer of 7.
  25.  
  26. One method is to pop the answer off of the co-processor stack and
  27. pop it back on using the fstp and fld instructions prior to the
  28. call to N_FTOL (float to long, near call version). This method
  29. is equivalent to the two step processes of as shown in the code.
  30.  
  31. The other method is to substitute N_FTOL with the use of fistp
  32. (store and pop stack as integer) and followed by the use of the
  33. lower part of the of the quad word as the result.
  34.  
  35. I would guess at this point that the function N_FTOL inadvertantly
  36. modifies either the rounding or the precision bits of the
  37. co-processor control word. I will leave it to you and others to
  38. figure out why this happens on the 486 chip and not
  39. on the 386/387 chips, mainly because I need to get back to work.
  40.  
  41. ----------
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <math.h>
  45. #include <float.h>
  46.  
  47. // The following constants are normally defined within the program:
  48.  
  49. #define LOG2 (log(2))
  50. #define LOGN(y)        (log ((double)y) / LOG2 )
  51. main()
  52. {
  53.      double l;
  54.     int i;
  55.  
  56.     l = LOGN(128);
  57.     i = (int)l;
  58.     printf("two step = %d\n", i); // this comes up with 7
  59.  
  60.     i = (int)(LOGN(128));
  61.     printf("one step = %d\n", i); // this comes up with 6?
  62.  
  63.     return(0);
  64. }
  65.  
  66. -- 
  67. Gil Nardo                  | mcs@wet.com
  68. Migrant Computing Services | (415)664-1032
  69. 1032 Irving Street, #435   |-----------------
  70. San Francisco, 94122       | Supernova = *!
  71.