1 #include "profitability.hpp"
2 #include "secant.cpp"
3 using namespace std;
4 
run(double * y)5 bool profitability::run ( double * y )
6 {
7    OK=true;
8 
9    // cout<<setiosflags(ios::fixed);
10    // cout<<endl<<endl<<"   PROFITABILITY ANALYSIS"<<endl;
11    // cout<<endl<<setprecision(1)<<"      Return on investment   (%)= "<<ROI()*100.0;
12    // cout<<endl<<"      Rate of return         (%)= "<<RR()*100.0; // y[10]
13 
14    y[10] = RR()*100.0;  // y[10]
15 
16    ROI();
17    RR();
18 
19    // cout<<endl<<"      Discounted flow rate   (%)= "<<DFR()*100.0;
20 
21    DFR();
22 
23    // cout<<endl<<endl<<"      Payout time            (y)= "<<PT();              // y[ 9]
24    y[9] = PT();
25 
26    // cout<<endl<<setprecision(0)<<"      Annual equivalent cost ($)= "<<AEC();  // y[12]
27    y[12] = AEC();
28 
29    // cout<<endl<<"      Net present value      ($)= "<<NPV()<<endl;             // y[13]
30    y[13] = NPV();
31 
32    return OK;
33 }
34 
ROI()35 double profitability::ROI()
36 {
37   // if(!MUTE)cout<<endl<<"        return on investment...";
38    num=den=0.0;
39    for(i=0;i<C->N;i++)
40    {
41       if(C->Inv[i]>EPS) den+=C->Inv[i];
42       num+=(C->Rev[i]-C->Coper[i]-C->Amort[i]);
43    }
44    if (num>EPS && den>EPS && C->N>0) {
45      // if(!MUTE)cout<<" OK";
46      return num/C->N/den;
47    }
48    else return 0.0;
49 }
50 
RR()51 double profitability::RR()
52 {
53    // if(!MUTE)cout<<endl<<"        rate of return...";
54    num=den=0.0;
55    for(i=0;i<C->N;i++)
56    {
57       num+=(C->Rev[i]-C->Coper[i])/pow(1.0+C->i_rate, i);
58       den+=C->Inv[i]/pow(1.0+C->i_rate, i);
59    }
60    if(num>EPS && den>EPS) {
61      // if(!MUTE)cout<<" OK";
62      return num/den;
63    }
64    else return 0.0;
65 }
66 
DFR()67 double profitability::DFR()
68 {
69   //if(!MUTE)cout<<endl<<"        discounted cash flow rate...";
70    solver = new secant<profitability>();
71    solver->set(this, 0.0, 0.01);
72    OK = solver->run();
73 
74    if ( OK && num>EPS && num < 1e20 ) {
75      // if(!MUTE)cout<<" OK";
76      return num;
77    }
78    else return 0.0;
79 }
80 
f(double x)81 double profitability::f(double x)
82 {
83    num=x;
84    sum=0.0;
85    for(i=0;i<C->N;i++)
86      sum += C->Flow[i]/pow(1.0+x, i);
87    return sum;
88 }
89 
PT()90 double profitability::PT()
91 {
92   // if(!MUTE)cout<<endl<<"        payout time...";
93    sum=0.0;
94    for(i=0;i<C->N;i++)
95    {
96       if((sum+C->Flow[i])>0.0)
97       {
98          den=0.0;
99          while(sum+den*C->Flow[i]<=0.0) den+=0.001;
100          den+=double(i-1);
101          i=C->N;
102       }
103       else sum+=C->Flow[i];
104    }
105 
106    if(den>EPS) {
107      // if(!MUTE)cout<<" OK";
108      return den;
109    }
110    else return 0.0;
111 }
112 
AEC()113 double profitability::AEC()
114 {
115    //if(!MUTE)cout<<endl<<"        annual equivalent cost...";
116    sum=0.0;
117    for(i=0;i<C->N;i++) sum+=(C->Coper[i]+C->Inv[i])/pow(1.0+C->i_rate, i);
118    if (sum>EPS) {
119 //      if(!MUTE)
120 //        cout<<" OK";
121      return sum*(C->i_rate*pow(1.0+C->i_rate,C->N))/(pow(1.0+C->i_rate,C->N)-1.0);
122    }
123    else return 0.0;
124 }
125 
NPV()126 double profitability::NPV()
127 {
128   // if(!MUTE)cout<<endl<<"        net present value...";
129    sum=0.0;
130    for ( i = 0 ; i < C->N ; i++ )
131      sum += C->Flowact[i];
132    if ( sum > EPS ) {
133 //      if(!MUTE)
134 //        cout<<" OK";
135      return sum;
136    }
137    return 0.0;
138 }
139