Skip to content

Fix encodings for zero valued big.Floats with non-zero precisions. - #118

Merged
phiryll merged 8 commits into
mainfrom
fix-bigfloat-zero-ordering
Jul 22, 2026
Merged

Fix encodings for zero valued big.Floats with non-zero precisions.#118
phiryll merged 8 commits into
mainfrom
fix-bigfloat-zero-ordering

Conversation

@phiryll

@phiryll phiryll commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Fix encodings for zero-valued big.Floats with non-zero precisions.

Previously zero values were encoded without precision or mode.

Claude did identify that there was a problem in how the code detected
a value was zero (using big.Float.Prec() instead of big.Float.Sign()),
but got the nature of the fix wrong. It only corrected the detection,
but did not change the encoding, so the encoding is not reversible.
Claude's fix is in the first commit.

  • *Fix big.Float zero detection to use value, not precision
  • Comment out some unused constants.
  • Disable timeout when fuzz testing.
  • Remove claude's test, as the fix is not correct.
  • Remove commented out code.
  • Add test functions to create zeroes with non-zero precision.
  • Add failing test cases for zero big.Floats with non-zero precision.
  • Fix encoding for big.Float zero values with non-zero precision.

phiryll and others added 8 commits July 21, 2026 17:25
bigFloatCodec classified a value as zero using `prec == 0`, but a
big.Float's precision is independent of its value. Only new(big.Float)
and var-declared zeros have precision 0; zeros from big.NewFloat(0) or
from arithmetic carry a nonzero precision (53 for NewFloat). Those zeros
fell through to the finite-number path and were encoded with exponent 0,
sorting them as though they were large finite values.

Concretely, big.NewFloat(0) encoded greater than 0.25 and 1e-300, and
-1e-300 encoded greater than -0.0, breaking the codec's ordering
guarantee. Detect zero with value.Sign() == 0 instead.

The existing tests missed this because every zero they construct uses
var/new(big.Float), which has precision 0. Add TestBigFloatZeroPrecision
covering precision-bearing signed zeros against small finite neighbors.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
It is a partial fix, but zeroes with different precisions need to be
encoded differently. The existing tests should be modified to include
new test cases instead of adding a new test here.
@phiryll
phiryll merged commit 06a6b21 into main Jul 22, 2026
9 checks passed
@phiryll
phiryll deleted the fix-bigfloat-zero-ordering branch July 22, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant