Dear Statalist members,
I’ve written a small program, round_exact (now available on SSC), for cases where decimal‑exact rounding is required and Stata’s round() produces counter‑intuitive results due to binary floating‑point arithmetic. For example,
.round(2.675, .01)
.2.67
.round_exact 2.675, d(2)
.2.68
.round(1.005, .01)
.1
.round_exact 1.005, d(2)
.1.01
As has long been noted on Statalist (e.g., by Nick Cox and others), this behavior of round() is not a bug but a consequence of binary arithmetic; exact decimal rounding is fundamentally unattainable absent decimal arithmetic (or, rhetorically, a decimal‑based computer). The aim here is not to “fix” floating‑point arithmetic, but to provide stable, reporting‑oriented decimal rounding via decimal scaling.
Suggestions, critiques, and improvement ideas are very welcome.
Best regards,
Anne Shi
I’ve written a small program, round_exact (now available on SSC), for cases where decimal‑exact rounding is required and Stata’s round() produces counter‑intuitive results due to binary floating‑point arithmetic. For example,
.round(2.675, .01)
.2.67
.round_exact 2.675, d(2)
.2.68
.round(1.005, .01)
.1
.round_exact 1.005, d(2)
.1.01
As has long been noted on Statalist (e.g., by Nick Cox and others), this behavior of round() is not a bug but a consequence of binary arithmetic; exact decimal rounding is fundamentally unattainable absent decimal arithmetic (or, rhetorically, a decimal‑based computer). The aim here is not to “fix” floating‑point arithmetic, but to provide stable, reporting‑oriented decimal rounding via decimal scaling.
Suggestions, critiques, and improvement ideas are very welcome.
Best regards,
Anne Shi

Comment