Subject: math weirdness
To: None <port-mac68k@NetBSD.ORG>
From: Skeelo <skeelo@white-dwarf.dyn.ml.org>
List: port-mac68k
Date: 09/29/1997 19:09:25
I got these results from one of my computer science labs. I'm at a loss to
explain this... I get the correct answer on the college's Digital Unix
machine.


skeelo $ lab3a
To exit enter zero for the principle.
Enter a value for principle (in dollars): [500000] 100000
Enter a value for interest (in percent): [6.39] 8
Enter a value for time (in years): [30] 30

        The Monthly Mortgage Payment (MMP) is $Infinity

To exit enter zero for the principle.
Enter a value for principle (in dollars): [500000] 0
skeelo $ lab3b
Enter a value for principle (in dollars): [500000] 100000
Enter a value for interest (in percent): [6.39] 8
Enter a value for time (in years): [30] 30

        The Monthly Mortgage Payment (MMP) is $Infinity


Do another? [y/n] n

And the program listings....

FILE: lab3a.cxx
--------------------------------------------------------------
#include <stdiostream.h>
#include <iomanip.h>
#include <math.h>

float get_principle();
float get_interest();
float get_time();

int main()
{

/* set up variables for use later (p=principle r=rate/interest t=time
e=power pow_in=stuff to be raised pow_out=result of pow_in to the e) */

	float p, r, t, e, pow_in, pow_out, mmp;

	p = get_principle();

	while (p > 0)
	{
		r = get_interest();
		t = get_time();

		e = (t * 12.0); // figure out what to raise pow_in to
		pow_in = (1.0 / (1.0 + ( r / 12.0))); // what we raise to e
		pow_out = pow( pow_in , e ); // the result of pow_in to e power

		mmp = ((p * (r / 12.0)) / (1.0 - pow_out)); // compute mmp

		//  Copy out the mmp with $xxx.xx form.

		cout << setiosflags(ios::fixed) 
		<< "\n\tThe Monthly Mortgage Payment (MMP) is $" 
		<< setprecision(2) << mmp << "\n" <<endl;
		p = get_principle();
	}

return 0;
}

float get_principle()
{
float p;
        cout << "To exit enter zero for the principle.\n"
	<< "Enter a value for principle (in dollars): [500000] ";
	// prompt for p
        cin >> p; // read p
return p;
}

float get_interest()
{
float r;
	cout << "Enter a value for interest (in percent): [6.39] ";
	// prompt for r
	cin >> r; // read r
	r = (r / 100.0); // tranlate the interest to decimal form
return r;
}

float get_time()
{
float t;
	cout << "Enter a value for time (in years): [30] "; // prompt for t
	cin >> t; // read t
return t;
}

------------------------------------------------------------------

FILE: lab3b.cxx
------------------------------------------------------------------
#include <stdiostream.h>
#include <iomanip.h>
#include <math.h>

float get_principle();
float get_interest();
float get_time();

int main()
{

/* set up variables for use later (p=principle r=rate/interest t=time
e=power pow_in=stuff to be raised pow_out=result of pow_in to the e) */

	float p, r, t, e, pow_in, pow_out, mmp;
	char answer;

	do
	{
		p = get_principle();
		r = get_interest();
		t = get_time();

		e = (t * 12.0); // figure out what to raise pow_in to
		pow_in = (1.0 / (1.0 + ( r / 12.0))); // what we raise to e
		pow_out = pow( pow_in , e ); // the result of pow_in to e power

		mmp = ((p * (r / 12.0)) / (1.0 - pow_out)); // compute mmp

		//  Copy out the mmp with $xxx.xx form.

		cout << setiosflags(ios::fixed) 
		<< "\n\tThe Monthly Mortgage Payment (MMP) is $" 
		<< setprecision(2) << mmp << "\n" <<endl;

		cout << "\nDo another? [y/n] ";
		cin >> answer;
	}
	while ( (answer == 'y') || (answer == 'Y') );

return 0;
}

float get_principle()
{
float p;
        cout << "Enter a value for principle (in dollars): [500000] ";
	// prompt for p
        cin >> p; // read p
return p;
}

float get_interest()
{
float r;
	cout << "Enter a value for interest (in percent): [6.39] ";
	// prompt for r
	cin >> r; // read r
	r = (r / 100.0); // tranlate the interest to decimal form
return r;
}

float get_time()
{
float t;
	cout << "Enter a value for time (in years): [30] "; // prompt for t
	cin >> t; // read t
return t;
}