{
  "id": 9680719,
  "title": "Small Calculator, Three Bugs: Input Parsing, Unit Drift, and Stale Results",
  "url": "https://urgent.news/2026/09/25/small-calculator-three-bugs-input-parsing-unit-drift-and-stale-results",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-25T02:39:16.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/ruixuan_jiang_663eb75f3fa/small-calculator-three-bugs-input-parsing-unit-drift-and-stale-results-41oj"
  },
  "original_language": "en",
  "account": "A tiny calculator with just two inputs and one output, the epitome of a \"hello world\" interaction, can be riddled with bugs. Three specific issues were encountered while developing bidirectional conversion between Cost Per Thousand Impressions (CPM) and Cost Per Click (CPC).\n\nBug 1: Percentage Drift\nThe core formula for calculating CPC from CPM and CTR is straightforward: divide CPM by CTR. However, the tricky part lies within the middle term. Click-Through Rate (CTR) is represented as a percentage (e.g., 2 means 2%). This requires treating the CTR as 2% rather than 2 clicks per 1000. To rectify this, the formula should multiply CTR by 10 (since CTR is per hundred impressions and CPM is per thousand). Writing the derivation as a comment beside the constant clarifies this for future developers.\n\nBug 2: Validation that Permits Empty Fields\nThe input fields for cost and CTR were validated using Number( ), which defaults to 0 if the input is empty. This design oversight is beneficial only when both fields are mandatory and cannot be zero. However, when optional fields allow zero values, an empty string should not be mistakenly treated as zero. Proper validation checks should be implemented to address this.\n\nBug 3: Stale Results After Invalid Submission\nA common user experience issue arises when submitting a form with invalid data: the old result remains displayed even after an error. For instance, if a user enters valid values, sees a $0.50 result, edits the cost field to an empty value, and then submits, the previous result ($0.50) should not persist on screen. Clearing the result and displaying a clear error message helps maintain consistency between the error state and the displayed result.\n\nThese bugs highlight the significance of correct calculations, thorough validation, and maintaining consistency in UI state. Implementing these fixes not only improves the functionality but also enhances the overall user experience of the calculator.",
  "summary": "A calculator with two inputs and one output is the \"hello world\" of interactive UI. It is also a small minefield. These are three defects I hit while building bidirectional CPM ↔ CPC conversion, and the fixes are portable to any numeric form. Bug 1: percentage drift The formula is simple once you see it. Cost per thousand impressions, divided by clicks per thousand impressions, gives cost per…",
  "key_points": [
    "Bug 1: Percentage drift in CTR calculation due to treating CTR as percentage",
    "Bug 2: Validation allowing empty fields treated as zero instead of leaving blank",
    "Bug 3: Stale results displayed after invalid form submission"
  ],
  "editors_take": "Fixing bugs in a simple calculator highlights the importance of correct calculations, thorough validation, and consistent UI state in improving both functionality and user experience.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}