測試會四捨五入至 100% 的評測百分比
Test a Benchmark Percentage That Rounds to 100%
評測報告以整數百分比顯示時,999/1,000 會四捨五入至 100%,容易令人誤以為沒有例外;示例改以「<100% (999/1000)」顯示,與「100% (1000/1000)」區分。報告亦應列出樣本數並説明統計項目,測試則須按所選格式規則涵蓋特殊樣本及最終輸出標籤。
When I summarize a local comparison inspired by CaptchaTheCat, I’d test the tie between the number on the slide and the exact sample it describes. For example, 999 successes out of 1,000 rounds to 100% at whole-percent precision. A reader can see that number and reasonably interpret it as no exceptions.
Written with AI assistance. The Python example uses synthetic data and was checked locally.
Separate the presentation rule from the measured rate
I keep the integer counts in the report and format a short label for display. This example uses a whole percentage except when the rounded label would imply a perfect result. When that happens it shows a qualifier beside the rejected count, so 1,000/1,000 remains visually distinct from 999/1,000.
The formatting choice is independent of the confidence warranted by the sample. Ten of ten is a perfect observed result for those ten observations. It still might come from a small or unrepresentative group. A report meant for decisions should publish the sample size and define what was counted.
from decimal import Decimal, ROUND_HALF_UP
def label(success, total):
if not 0 <= success <= total or total == 0:
raise ValueError("invalid observations")
pct = Decimal(success) * 100 / total
rounded = pct.quantize(Decimal("1"), rounding=ROUND_HALF_UP)
if rounded == 100 and success < total:
return f"<100% ({success}/{total})"
return f"{rounded}% ({success}/{total})"
assert label(1000, 1000) == "100% (1000/1000)"
assert label(995, 1000) == "<100% (995/1000)"
assert label(994, 1000) == "99% (994/1000)"
print(label(999, 1000))
The local result was <100% (999/1000). A display could instead show 99.9% for a fixed audience that reads one decimal place. The regression test should follow the chosen rule and keep the exceptional sample in its own fixture.
I also check exported labels in the final chart or digest. An external library may apply its own formatting after the application emits a numeric fraction. A test at the presentation boundary guards against losing the qualifier during a dashboard migration.
Further reading
Validate a reordered benchmark export.
Keep ties visible in a comparison table.
Find Duplicate JSON Keys Before They Overwrite an Amazon Price.
來源:DEV Community · dev.to