Function percentDiff

Calculate the percentage difference between two numbers.

The result is how many percent the larger value exceeds the smaller one: percentDiff(100, 150) === 50, percentDiff(150, 100) === 50.

Edge cases are reported honestly instead of a sentinel value:

  • Both values equal (including 0, 0) — returns 0.
  • One value is 0 and the other is not — returns Infinity (the difference is unbounded; no finite percent can express it).
  • Parameters

    • a: number

      The first number.

    • b: number

      The second number.

    Returns number

    The percentage difference between the two numbers.