From 070d7ecfc11c954bbfcf3f9acd866d23d3337124 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 25 Oct 2025 06:09:08 +0000 Subject: [PATCH 1/2] fix: correct flush condition to use half() instead of quarter() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The encoder's flush function was using an incorrect condition for determining which final bit to emit. After normalization, the range [low, high] straddles the midpoint, so the flush logic should check if low < half() to decide whether to emit 0 or 1. Previously used: low <= quarter() Corrected to: low < half() This aligns with standard arithmetic coding flush logic and ensures correct encoding of the final bits. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/encoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encoder.rs b/src/encoder.rs index 8594407..d113b1d 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -239,7 +239,7 @@ where /// This method can fail if the output cannot be written to pub fn flush(mut self) -> io::Result<()> { self.pending += 1; - if self.state.low <= self.state.quarter() { + if self.state.low < self.state.half() { self.emit(false)?; } else { self.emit(true)?; From 012b50b8e8aaa0e4ff49693a9021a42a6b8a149e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 25 Oct 2025 07:15:48 +0000 Subject: [PATCH 2/2] fix: change flush condition from <= to < for quarter() comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous commit incorrectly changed the condition to use half() instead of quarter(). The actual bug was the comparison operator. After the E2/E3 normalization loop exits, we know: - low < quarter OR high >= three_quarter When low == quarter exactly, we must have high >= three_quarter (otherwise the loop would continue). This places us in the upper region, so we should emit 1, not 0. The fix: change <= to < to properly handle the boundary case. Before: if self.state.low <= self.state.quarter() After: if self.state.low < self.state.quarter() This reverts the half() change and fixes the actual comparison bug. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/encoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encoder.rs b/src/encoder.rs index d113b1d..d9fee46 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -239,7 +239,7 @@ where /// This method can fail if the output cannot be written to pub fn flush(mut self) -> io::Result<()> { self.pending += 1; - if self.state.low < self.state.half() { + if self.state.low < self.state.quarter() { self.emit(false)?; } else { self.emit(true)?;