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

  1. Path: sparky!uunet!spool.mu.edu!caen!nic.umass.edu!m2c!crackers!jjmhome!schunix!sonix
  2. From: sonix@schunix.uucp (Duane Morin)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Math error in BC++?  Help!
  5. Message-ID: <1992Dec15.211516.1664@schunix.uucp>
  6. Date: 15 Dec 92 21:15:16 GMT
  7. Distribution: na
  8. Organization: SCHUNIX Public Access Unix for Worcester County, Massachusetts, USA
  9. Lines: 59
  10.  
  11. This is driving us NUTS!! Will someone please take a look at the following 
  12. code and see if they can figure out why it does what it does?  Thank you.
  13.  
  14. The following code segment shows a snippet of a program that needs to calculate
  15. the log base 2 of 128 (which happens to be 7).  The problem is that the 
  16. code works fine on a 386DX/40, with 387 installed, and does NOT work on 
  17. a 486DX/33!  Help??
  18.  
  19. ----------------------------
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23.  
  24. // The following constants are normally defined within the program:
  25.  
  26. #define LOG2 (log(2))
  27. #define LOGN(y)        (log ((double)y) / LOG2 )
  28.  
  29. // This next one is our error:
  30. #define ILOGN(y) (int) (log ((double)y) / LOG2 )
  31.  
  32. main()
  33.   {
  34.      double l;
  35.     int i;
  36.     printf("The log of 128 is %lf\n", log(128));
  37.     printf("The log of 2   is %lf\n", log(2));
  38.     printf("The log2 of 128 is %lf\n", log(128)/log(2));
  39.     printf("\n");
  40.     printf("Log2 as defined by a constant is %lf\n", LOG2);
  41.     printf("just for info, double(y) = %lf\n", (double)128);
  42.     printf("Log(base 2) 128 as defined is %lf\n", LOGN(128));
  43. // Up to here, everything is fine.  The constants for Log(2) and Log(128)
  44. // are calculated correctly, and dividing them yields 7.000, as expected.
  45. // Even the LOGN #defined formula gets it right.  So how come this next
  46. // line, which is identical to LOGN except for an (int) cast, gets it 
  47. // wrong, but only on a 486?
  48.  
  49.     printf("Log(base 2) 128 as defined is %d\n", ILOGN(128));  // 6???
  50.  
  51. // Just for the record, if we do this in two steps, putting LOGN in a variable
  52. // and then casting that variable into an integer, that works fine.
  53.  
  54.     l = LOGN(128);
  55.     printf("l as a double = %lf\n", l);
  56.     i = (int)l;
  57.     printf("l as an int = %d\n", i);
  58.     exit(1);
  59. }
  60.  
  61. *PLEASE* tell me that this is an honest error and not some stupid mistake 
  62. on our part.  I'm leaning against the possibility that it's our fault, since
  63. the exact same code works on one machine and not another.
  64.  
  65. Help?  Anyone?
  66.  
  67. Duane Morin
  68.  Walker Sonix, Inc
  69.  Worcester, MA 01604
  70.