When both values are zero, then the change is zero.
If one of the values is zero, it's infinite (ambiguous), but I would set it to 100%.
Here is a C++ code (where v1
is the previous value (old), and v2
is new):
double result = 0;
if (v1 != 0 && v2 != 0) {
// If values are non-zero, use the standard formula.
result = (v2 / v1) - 1;
} else if (v1 == 0 || v2 == 0) {
// Change is zero when both values are zeros, otherwise it's 100%.
result = v1 == 0 && v2 == 0 ? 0 : 1;
}
result = v2 > v1 ? abs(result) : -abs(result);
// Note: To have format in hundreds, multiply the result by 100.