Sample
decimal 3.1.1: Parse decimal strings with 34 significant digits and leading zeros without exceeding default max_digits
Verified sample for hex decimal 3.1.1: Parse decimal strings with 34 significant digits and leading zeros without exceeding default max_digits. The contract…
sha256:415afd76d53201a314bf2d69d1ff4b9d5be593003f22a660bddfbb0a3d99ec4a
This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured.
How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people.
MIT-0
Execution evidence
The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.
- Evidence basis
- Signed contract pass
- Verification receipts
- 2
- Signing keys that built it
- 2
Declared environment
elixir linux x64 elixir elixir mix
Verification-run environments
| Environment | Contract | Stages | Run |
|---|---|---|---|
| elixir 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:PASS · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · hex@1 |
2026-08-16 |
| elixir 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:PASS · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · hex@1 |
2026-08-18 |
Case
HOW- Goal
- Parse decimal strings with 34 significant digits and leading zeros without exceeding default max_digits
- Packages
- Symbols
-
- Decimal.new
- Decimal.parse
- Decimal.to_string
- Environment
- elixir
- Created
- 2026-08-16T13:26:41Z
Contract
- assert Decimal.new parses 34 significant digits with leading zero without exceeding max_digits
- assert Decimal.parse returns parsed struct for 34-digit fractional string
- assert decimal128 full precision values roundtrip through to_string and Decimal.new
- assert leading fractional zeros do not count towards max_digits limit
- assert strings exceeding 34 significant digits are rejected with error under default limit
Files
- NOTES.md
- csx.json
- mix.exs
- mix.lock
- test/contract_test.exs
- test/test_helper.exs
Source
# Decimal Leading-Zero Max-Digits Parsing Bugfix
## What `search_known_solution` answered
`search_known_solution` returned existing sample `sha256:d7487beb8b4c961e46ea9378c44a47b0be35429cc897b1fa62a486db8059d276` ("Compare Decimal values in Elixir when == reports equal amounts as different"), which demonstrated struct equality traps with `==` versus `Decimal.equal?/2` across numbers with differing scales.
## What a model would have written instead
When parsing or roundtripping 34-digit decimal numbers formatted with leading zeros (such as `"0.3162277660168379331998893544432719"`), a model or developer expects `Decimal.new/1` and `Decimal.parse/1` to accept strings that fit within the default decimal128 context precision (`precision: 34`).
## How the wrong version fails
On Decimal versions prior to `3.1.1` (such as `3.1.0` and `3.0.0`), parsing strings at full decimal128 precision with leading zeros fails **loudly** with `Decimal.Error` (`"too many digits"`) or `:error` because the parser counted non-significant leading ASCII zero characters towards the default `:max_digits` limit of 34 (counting 35 total digits for `0.` + 34 significant digits), breaking string roundtrips for valid 34-digit values.
{"buildCommand":["mix","deps.compile","--no-deps-check"],"case":{"believed":"Decimal.new/1 and Decimal.parse/1 parse any decimal string containing up to the default decimal128 context precision of 34 significant digits regardless of leading zeros.","caseId":"case:sha256:5ded0407008032ea24bf07d4b61d2efa0f539a191b38e8805e4fffd7fbfe5b70","contract":["assert Decimal.new parses 34 significant digits with leading zero without exceeding max_digits","assert Decimal.parse returns parsed struct for 34-digit fractional string","assert decimal128 full precision values roundtrip through to_string and Decimal.new","assert leading fractional zeros do not count towards max_digits limit","assert strings exceeding 34 significant digits are rejected with error under default limit"],"goal":"Parse decimal strings with 34 significant digits and leading zeros without exceeding default max_digits","kind":"HOW","packages":["pkg:hex/decimal@3.1.1"],"schemaVersion":1,"symbols":["Decimal.new","Decimal.parse","Decimal.to_string"]},"contractCommand":["mix","test","--no-deps-check"],"environment":{"arch":"x64","ecosystem":"hex","executionContext":"elixir","language":"elixir","os":"linux","packageManager":"mix","runtime":"elixir","schemaVersion":1},"license":"MIT-0","packages":["pkg:hex/decimal@3.1.1"],"schemaVersion":1,"symbols":["Decimal.new","Decimal.parse","Decimal.to_string"],"verifierAdapter":"hex@1"}
defmodule CsxDecimalBugfix.MixProject do
use Mix.Project
def project do
[
app: :csx_decimal_bugfix,
version: "0.1.0",
elixir: "~> 1.18",
deps: deps()
]
end
def application, do: [extra_applications: []]
defp deps do
[{:decimal, "3.1.1"}]
end
end
%{
"decimal": {:hex, :decimal, "3.1.1", "430d87b04011ce6cbd4fd205be758311a81f87d552d40904abd00f015935b1d0", [:mix], [], "hexpm", "c5f25f2ced74a0587d03e6023f595db8e924c9d3922c8c8ffd9edfc4498cf1f6"},
}
defmodule CsxDecimalBugfixTest do
use ExUnit.Case
# Decimal 3.0.0 introduced decimal128 default limits (precision: 34, max_digits: 34).
# However, in 3.0.0 and 3.1.0, Decimal.parse/1 and Decimal.new/1 counted all ASCII digit
# characters including leading zeros against :max_digits.
# As a result, a 34-significant-digit number with a leading zero like
# "0.3162277660168379331998893544432719" had 35 counted digits and was rejected
# with Decimal.Error ("too many digits"), even though its significant digits equaled
# the default context precision (34).
#
# Decimal 3.1.1 fixed this by ignoring non-significant leading zeros in :max_digits.
test "Decimal.new parses 34 significant digits with leading zero without exceeding max_digits" do
raw = "0.3162277660168379331998893544432719"
dec = Decimal.new(raw)
assert Decimal.to_string(dec) == raw
assert dec.coef == 31_622_776_601_683_793_319_988_935_444_327_19
assert dec.exp == -34
end
test "Decimal.parse returns parsed struct for 34-digit fractional string" do
raw = "0.3162277660168379331998893544432719"
assert Decimal.parse(raw) ==
{%Decimal{sign: 1, coef: 31_622_776_601_683_793_319_988_935_444_327_19, exp: -34}, ""}
end
test "decimal128 full precision values roundtrip through to_string and Decimal.new" do
original = %Decimal{sign: 1, coef: 31_622_776_601_683_793_319_988_935_444_327_19, exp: -34}
stringified = Decimal.to_string(original)
assert stringified == "0.3162277660168379331998893544432719"
reparsed = Decimal.new(stringified)
assert Decimal.equal?(reparsed, original)
end
test "leading fractional zeros do not count towards max_digits limit" do
raw = "0.0001234567890123456789012345678901234"
assert Decimal.parse(raw) ==
{%Decimal{sign: 1, coef: 12_345_678_901_234_567_890_123_456_789_012_34, exp: -37}, ""}
end
test "strings exceeding 34 significant digits are rejected with error under default limit" do
overflow = "0.31622776601683793319988935444327195"
assert Decimal.parse(overflow) == :error
assert_raise Decimal.Error, fn -> Decimal.new(overflow) end
end
end
ExUnit.start()