C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 8th, 2011, 06:20 AM
jbro jbro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 1 jbro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 8 sec
Reputation Power: 0
File IO - "Log" calculating incorrectly in my function

I'm trying to create a program that will calculate the different variables of the present value equation given four of the five variables. Everything works but the calculation for number of periods. (choice four) For some reason I'm getting incorrect values back when using the log function in the function. I've used a similar function in another program and it worked perfectly, for some reason it doesn't seem to work as part of this program. Any help would be appreciated. I included the function below the main body of the program.

/*Main Body*/

# include <stdio.h>
# include <math.h>
/* sets functions to double precision */
double getFv();
double getPv();
double getPmt();
double getNPer();
int getRate();
int main() {
/* sets input variables to double precision*/
double fv, pv, rate, pmt, per, fuval,pval,ppay,np,ir;
/* sets the conditional variable to an integer*/
int n;
do {
do {

/*Conditional variable selection here will determine which if function and which calculation function runs */
printf("Please enter the number that corresponds to the parameter you would like to calculate:\n");
printf("To quit: 0\n");
printf("To calculate future value: 1\n");
printf("To calculate present value: 2\n");
printf("To calculate the periodic payment: 3\n");
printf("To calculate the number of periods: 4\n");
printf("To calculate the interest rate: 5\n");
/* This will accept the input from user*/
scanf("%d",&n);

/* This if statements below will determine which calculation function runs */
/* This if statement will run the future value function if the condition is true*/
if (n==1) {
printf("Please input values separated by commas in the following order\n");
printf("(periodic payment, periodic interest rate in decimal form, number of periods, present value)\n");
/*accepts input from user for the getFv function*/
scanf("%lf,%lf,%lf,%lf",&pmt,&rate,&per,&pv );
fuval=getFv(pmt,rate,per,pv);
printf("The future value is $%.2f\n",fuval);
}
/* This if statement will run the present value function if the condition is true*/
if (n==2) { printf("Please input values separated by commas in the following order\n");
printf("(periodic payment, periodic interest rate in decimal form, number of periods, future value)\n");
/* Accepts user input to run the present value function*/
scanf("%lf,%lf,%lf,%lf",&pmt,&rate,&per,&fv );
pval=getPv(pmt,rate,per,fv);
printf("The present value is %.2f\n",pval);
}
/* Will run the periodic payment calculation function if the condition is true*/
if (n==3) { printf("Please input values separated by commas in the following order\n");
printf("(present value, periodic interest rate in decimal form, number of periods, future value)\n");
/*Accepts input from user to run the periodic payment function*/
scanf("%lf,%lf,%lf,%lf",&pv,&rate,&per,&fv );
ppay=getPmt(pv,rate,per,fv);
printf("The periodic payment is %.2f\n",ppay);
}
/* Will run the number of periods function if the condition is true*/
if (n==4) { printf("Please input values separated by commas in the following order\n");
printf("(future value, present value, periodic payment, periodic interest rate in decimal form)\n");
/* Accepts user input to run the number of periods function*/
scanf("%lf,%lf,%lf,%lf",&fv,&pv,&pmt,&rate );
np=getNPer(fv,pv,pmt,rate);
printf("The number of periods is %.2f\n",np);
} This will tell the user that the interest rate cannot yet be calculated and is not an option*/
if (n==5) { printf("This part of the program has not been updated yet\n");
ir=getRate(rate);
printf("The interest rate is 0\n",ir);
}
if (n>5) {printf("You must choose a number between 0 and 5\n");
}
if (n<0) {printf("You must choose a number between 0 and 5\n");
}
}while (n<0);
}while (n>5);

return 0;
}

/*Function that computes the number of periods*/

/* Function to get the number of periods in the present value equation*/
/* Doubles the precision of the function getNPer and all the elements of it. */
double getNPer (double fuval, double prval, double paymt, double rat) {
double nu, pnum, pden;
/* Calculates the numerator and denominator of the present value equation to determine the number of periods"
pnum= log((fuval-(paymt/rat))/(prval-(paymt/rat)));
pden= log(1+rat);
/* Divides the denominator into the numerator to obtain the number of periods */
nu= (pnum)/(pden);
/* returns the value of the number of periods to the main function */
return nu;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - "Log" calculating incorrectly in my function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap