Thread: pow() problems

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    3

    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.
    Attached Files Attached Files

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    891
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,898
    > 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);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    The values I used for a pencil and paper calculation are: CashDemand=2000000, TransactionCost=150, Interest=.06, AmountCash=100000, DailyCashFlow=5000.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,898
    Perhaps it's something to do with raising the interest to the power 365.
    Code:
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (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
    
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,898
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with HW Problems!!!
    By NikeLover in forum C++ Programming
    Replies: 12
    Last Post: 02-23-2011, 05:17 AM
  2. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  3. help...2 C++ problems
    By mr_god_god in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2006, 02:59 AM
  4. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM
  5. Problems with cin.get()
    By Stevek in forum C++ Programming
    Replies: 5
    Last Post: 03-15-2003, 01:00 PM

Tags for this Thread