🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #6471

Math error in BC++? Help!

#6471
From: Duane Morin
Date: Tue, 15 Dec 1992 21:15
59 lines
2456 bytes
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??

----------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// The following constants are normally defined within the program:

#define LOG2 (log(2))
#define LOGN(y)        (log ((double)y) / LOG2 )

// This next one is our error:
#define ILOGN(y) (int) (log ((double)y) / LOG2 )

main()
  {
 	double l;
	int i;
	printf("The log of 128 is %lf\n", log(128));
	printf("The log of 2   is %lf\n", log(2));
	printf("The log2 of 128 is %lf\n", log(128)/log(2));
	printf("\n");
	printf("Log2 as defined by a constant is %lf\n", LOG2);
	printf("just for info, double(y) = %lf\n", (double)128);
	printf("Log(base 2) 128 as defined is %lf\n", LOGN(128));
// Up to here, everything is fine.  The constants for Log(2) and Log(128)
// are calculated correctly, and dividing them yields 7.000, as expected.
// Even the LOGN #defined formula gets it right.  So how come this next
// line, which is identical to LOGN except for an (int) cast, gets it 
// wrong, but only on a 486?

	printf("Log(base 2) 128 as defined is %d\n", ILOGN(128));  // 6???

// Just for the record, if we do this in two steps, putting LOGN in a variable
// and then casting that variable into an integer, that works fine.

	l = LOGN(128);
	printf("l as a double = %lf\n", l);
	i = (int)l;
	printf("l as an int = %d\n", i);
	exit(1);
}

*PLEASE* tell me that this is an honest error and not some stupid mistake 
on our part.  I'm leaning against the possibility that it's our fault, since
the exact same code works on one machine and not another.

Help?  Anyone?

Duane Morin
 Walker Sonix, Inc
 Worcester, MA 01604
Technical Details
Message-ID: <1992Dec15.211516.1664@schunix.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!caen!nic.umass.edu!m2c!crackers!jjmhome!schunix!sonix

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!caen!nic.umass.edu!m2c!crackers!jjmhome!schunix!sonix
From: sonix@schunix.uucp (Duane Morin)
Newsgroups: comp.os.msdos.programmer
Subject: Math error in BC++?  Help!
Message-ID: <1992Dec15.211516.1664@schunix.uucp>
Date: 15 Dec 92 21:15:16 GMT
Distribution: na
Organization: SCHUNIX Public Access Unix for Worcester County, Massachusetts, USA
Lines: 59