1. pow() problems

I'm trying to write a program that calculates the optimal cash return and cash balance upper limit using the Miller-Orr Model. Several of the equations require multiple variables under a root sign. I think the problem is that pow() can't use variables? I've attached the program. IntDay is suppose to be .000211, OptPoint 126101.72 and UpperLimit 178305.16. Any help on getting this to work will be greatly appreciated. I'm a novice at this at best. 2. First of all, please read the Forum Rules, and post your code using Code Tags, not as an attachment.

Then you should read the man page for pow().

Yes, you can pass variables to pow() as you would for any other function, providing you use the correct data type.

I would change your floats to doubles.

I have not compiled your code as is. 3. > IntDay is suppose to be .000211, OptPoint 126101.72 and UpperLimit 178305.16
And what are we supposed to type in for these variables
- CashDemand
- TransactionCost
- Interest
- AmountCash
- DailyCashFlow

This is the original code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
float CashDemand, TransactionCost, Interest, CashReplenishB, AmountCash, DailyCashFlow, IntDay, OptPoint, UpperLimit;

printf("Enter annual cash demand in dollars:\n"); scanf("%f", &CashDemand);
printf("Enter transaction cost for marketable securities sales:\n"); scanf("%f", &TransactionCost);
printf("Enter the interest that securities can earn as a decimal:\n"); scanf("%f", &Interest);
CashReplenishB=sqrt((2*TransactionCost*CashDemand)/Interest);
printf("The optimal cash replenishment level from the Baumol Model is \$ %.2f.\n\n\n", CashReplenishB);

printf("What amount of cash do you want kept on hand:\n"); scanf("%f", &AmountCash);
printf("What is the standard deviation of expected daily cash flows:\n"); scanf("%f", &DailyCashFlow);
IntDay=pow(1+Interest,365);
OptPoint=pow((3*TransactionCost*pow(DailyCashFlow,2))/(4*IntDay),3)+AmountCash;
UpperLimit=(3*OptPoint)-(2*AmountCash);
printf("The daily securities interest rate is \$ %.2f.", IntDay);
printf("The optimal cash return point using the Miller-Orr Model is \$ %.2f.\n", OptPoint);
printf("The upper limit for the cash balance using the Miller-Orr Model is \$ %.2f.\n", UpperLimit);
}
This is the code with some sane formatting applied.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
float CashDemand, TransactionCost, Interest, CashReplenishB, AmountCash,
DailyCashFlow, IntDay, OptPoint, UpperLimit;

printf("Enter annual cash demand in dollars:\n");
scanf("%f", &CashDemand);

printf("Enter transaction cost for marketable securities sales:\n");
scanf("%f", &TransactionCost);

printf("Enter the interest that securities can earn as a decimal:\n");
scanf("%f", &Interest);

CashReplenishB = sqrt((2 * TransactionCost * CashDemand) / Interest);

printf("The optimal cash replenishment level from the Baumol Model is \$ %.2f.\n\n\n",
CashReplenishB);

printf("What amount of cash do you want kept on hand:\n");
scanf("%f", &AmountCash);

printf("What is the standard deviation of expected daily cash flows:\n");
scanf("%f", &DailyCashFlow);

IntDay = pow(1 + Interest, 365);
OptPoint = pow( (3 * TransactionCost * pow(DailyCashFlow, 2)) /
(4 * IntDay),3 )
+ AmountCash;

UpperLimit = (3 * OptPoint) - (2 * AmountCash);
printf("The daily securities interest rate is \$ %.2f.", IntDay);

printf("The optimal cash return point using the Miller-Orr Model is \$ %.2f.\n",
OptPoint);

printf("The upper limit for the cash balance using the Miller-Orr Model is \$ %.2f.\n",
UpperLimit);
} 4. The values I used for a pencil and paper calculation are: CashDemand=2000000, TransactionCost=150, Interest=.06, AmountCash=100000, DailyCashFlow=5000. 5. Perhaps it's something to do with raising the interest to the power 365.
Code:
\$ gdb -q ./a.out
(gdb) b 30
Breakpoint 1 at 0x40088e: file foo.c, line 30.
(gdb) run
Starting program: /home/sc/Documents/a.out
Enter annual cash demand in dollars:
2000000
Enter transaction cost for marketable securities sales:
150
Enter the interest that securities can earn as a decimal:
.06
The optimal cash replenishment level from the Baumol Model is \$ 100000.00.

What amount of cash do you want kept on hand:
100000
What is the standard deviation of expected daily cash flows:
5000

Breakpoint 1, main () at foo.c:30
30	  IntDay = pow(1 + Interest, 365);
(gdb) p Interest
\$1 = 0.0599999987
(gdb) n
31	  OptPoint = pow( (3 * TransactionCost * pow(DailyCashFlow, 2)) /
(gdb) p IntDay
\$2 = 1.72437722e+09 6. I just changed the exponents to a decimal form and it works. I also corrected a formula error that came from turning roots into an exponent. Does anyone know why the decimal form worked? Thanks for the idea Salem.
Code:
int main()
{
float CashDemand, TransactionCost, Interest, CashReplenishB, AmountCash, DailyCashFlow, IntDay, OptPoint, UpperLimit;

printf("Enter annual cash demand in dollars:\n"); scanf("%f", &CashDemand);
printf("Enter transaction cost for marketable securities sales:\n"); scanf("%f", &TransactionCost);
printf("Enter the interest that securities can earn as a decimal:\n"); scanf("%f", &Interest);
CashReplenishB=sqrt((2*TransactionCost*CashDemand)/Interest);
printf("The optimal cash replenishment level from the Baumol Model is \$ %.2f.\n\n\n", CashReplenishB);

printf("What amount of cash do you want kept on hand:\n"); scanf("%f", &AmountCash);
printf("What is the standard deviation of expected daily cash flows:\n"); scanf("%f", &DailyCashFlow);
IntDay=pow(1+Interest,.002739726)-1;
OptPoint=pow((3*TransactionCost*pow(DailyCashFlow,2))/(4*IntDay),.333333333)+AmountCash;
UpperLimit=(3*OptPoint)-(2*AmountCash);
printf("The daily securities interest rate is %f.\n", IntDay);
printf("The optimal cash return point using the Miller-Orr Model is \$ %.2f.\n", OptPoint);
printf("The upper limit for the cash balance using the Miller-Orr Model is \$ %.2f.\n", UpperLimit);
} 7. I noticed you managed to mush all the code into a compressed hair-ball again.

Compressing the code like that has a completely negligible effect on compilation time, and absolutely no effect on run-time speed.

All you're doing is alienating anyone else who might care to look at your code. Keep posting code like that, and you'll soon be ignored.

> IntDay=pow(1+Interest,.002739726)-1;
So why did you write this in the first place?
> IntDay = pow(1 + Interest, 365);

I mean, why didn't you write this in the first place if it's what you wanted.
IntDay = pow(1 + Interest, 1/365.0);

The only tricky bit is that 1/365 is done in integer maths, so you get zero, whereas adding the .0 forces the division into floating point mode and you get what you want. Popular pages Recent additions cash, novice, pow, program, variables 