{
  "schemaVersion": 1,
  "slug": "npv-calculator",
  "category": "finance",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "b9e828670e645508da30b892c70b925b419e4fd007cf5118a5a800563e14a4e4",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/npv-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "NPV = −I₀ + Σ_{t=1}^{n} C×(1+g)^(t−1)/(1+r)^t",
    "vars": [
      [
        "NPV",
        "net present value"
      ],
      [
        "I₀",
        "initial investment"
      ],
      [
        "C",
        "first-year cash flow"
      ],
      [
        "g",
        "annual growth rate"
      ],
      [
        "r",
        "discount rate"
      ],
      [
        "n",
        "number of years"
      ]
    ],
    "note": "g and r are entered as percentages and divided by 100. Cash flows are assumed at each year-end; IRR is the rate x where NPV(x)=0. Bisection starts with [−0.9999, 1] and expands the upper bound until NPV changes sign; it runs only when a sign change exists. Profitability index = present value of inflows / I₀."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "if(!Number.isInteger(years)) return {npv:null,irr:null,profitabilityIndex:null,pvInflows:null,totalInflows:null,__error:'inputError'}; const r=discountRate/100,g=growthRate/100; const flows=[-initialInvestment]; let pv=0,undisc=0; for(let t=1;t<=years;t++){const cf=annualCashFlow*Math.pow(1+g,t-1);flows.push(cf);undisc+=cf;pv+=cf/Math.pow(1+r,t);} const npvAt=x=>flows.reduce((a,c,i)=>a+c/Math.pow(1+x,i),0); let irr=null,lo=-0.9999,hi=1; if(initialInvestment>0&&annualCashFlow>0){while(npvAt(hi)>0&&hi<Number.MAX_SAFE_INTEGER/2)hi*=2;if(npvAt(lo)*npvAt(hi)<0){for(let i=0;i<200;i++){const mid=(lo+hi)/2;if(npvAt(lo)*npvAt(mid)<=0)hi=mid;else lo=mid;}irr=((lo+hi)/2)*100;}} return {npv:pv-initialInvestment,irr,profitabilityIndex:initialInvestment>0?pv/initialInvestment:null,pvInflows:pv,totalInflows:undisc};",
    "inputs": [
      {
        "name": "initialInvestment",
        "type": "number",
        "default": 50000,
        "min": 0,
        "max": 1000000000,
        "step": 1000
      },
      {
        "name": "annualCashFlow",
        "type": "number",
        "default": 12000,
        "min": 0,
        "max": 1000000000,
        "step": 500
      },
      {
        "name": "growthRate",
        "type": "number",
        "default": 2,
        "min": -20,
        "max": 40,
        "step": 0.1,
        "slider": true
      },
      {
        "name": "discountRate",
        "type": "number",
        "default": 8,
        "min": 0,
        "max": 50,
        "step": 0.1,
        "slider": true
      },
      {
        "name": "years",
        "type": "number",
        "default": 10,
        "min": 1,
        "max": 40,
        "step": 1,
        "slider": true
      }
    ],
    "outputs": [
      {
        "name": "npv",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "irr",
        "precision": 2,
        "unit": "%",
        "optional": true
      },
      {
        "name": "profitabilityIndex",
        "precision": 3,
        "unit": "",
        "optional": true
      },
      {
        "name": "pvInflows",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "totalInflows",
        "precision": 2,
        "unit": "$"
      }
    ],
    "interpret": null
  }
}
