サンプル
decimal 3.1.1: Compare Decimal values in Elixir when == reports equal amounts as different
検証済みサンプル — hex decimal 3.1.1: Compare Decimal values in Elixir when == reports equal amounts as different. elixir 1 · linux alpine/x64 · docker で contract…
sha256:d7487beb8b4c961e46ea9378c44a47b0be35429cc897b1fa62a486db8059d276
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 2
- ビルドした署名鍵
- 2
宣言された環境
elixir 1 linux x64 elixir 1 elixir mix
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| elixir 1 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 | PASS | compile:PASS · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · hex@1 |
2026-08-14 |
| elixir 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:PASS · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · hex@1 |
2026-08-17 |
ケース
FIX- ゴール
- Compare Decimal values in Elixir when == reports equal amounts as different
- パッケージ
- シンボル
-
- Decimal.equal?
- Decimal.compare
- Decimal.from_float
- Decimal.normalize
- 環境
- elixir 1
- 作成日
- 2026-08-14T06:08:04Z
コントラクト
- assert Decimal arithmetic is exact where float arithmetic is not
- assert Decimal.new refuses a float and from_float is the explicit conversion
- assert == reports numerically equal Decimals as different while Decimal.equal? does not
- assert Enum.uniq and in inherit that, and sorting needs the Decimal module
- assert normalize produces a canonical form
ファイル
- csx.json
- mix.exs
- mix.lock
- test/contract_test.exs
- test/test_helper.exs
ソース
{"buildCommand":["mix","deps.compile","--no-deps-check"],"case":{"caseId":"case:sha256:c2acd06179e0edd2e87a05b95f3c83e2d18148950c37885eebaeff5a9513a504","constraints":{"runtime":"elixir"},"contract":["assert Decimal arithmetic is exact where float arithmetic is not","assert Decimal.new refuses a float and from_float is the explicit conversion","assert == reports numerically equal Decimals as different while Decimal.equal? does not","assert Enum.uniq and in inherit that, and sorting needs the Decimal module","assert normalize produces a canonical form"],"goal":"Compare Decimal values in Elixir when == reports equal amounts as different","kind":"FIX","packages":["pkg:hex/decimal@3.1.1"],"schemaVersion":1,"symbols":["Decimal.equal?","Decimal.compare","Decimal.from_float","Decimal.normalize"]},"contractCommand":["mix","test","--no-deps-check"],"environment":{"arch":"x64","ecosystem":"hex","executionContext":"elixir","language":"elixir","os":"linux","packageManager":"mix","runtime":"elixir","runtimeVersion":"1","schemaVersion":1},"license":"MIT-0","packages":["pkg:hex/decimal@3.1.1"],"schemaVersion":1,"symbols":["Decimal.equal?","Decimal.compare","Decimal.from_float","Decimal.normalize"],"verifierAdapter":"hex@1"}
defmodule CsxDecimal.MixProject do
use Mix.Project
def project do
[
app: :csx_decimal,
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 CsxDecimalTest do
use ExUnit.Case
# Decimal fixes float arithmetic and then hands you a subtler problem:
# two Decimals that are numerically equal are different structs, because
# the scale is part of the value. Every construct that uses Elixir's
# built-in equality — ==, in, Enum.uniq, pattern matching, sorting — is
# therefore wrong on Decimals, and wrong quietly.
test "the arithmetic is the easy part" do
refute 0.1 + 0.2 == 0.3
assert Decimal.add(Decimal.new("0.1"), Decimal.new("0.2")) |> Decimal.to_string() == "0.3"
end
test "Decimal.new refuses a float on purpose" do
# Taking the float would import the error Decimal exists to avoid, so
# the conversion has to be asked for by name.
#
# apply/3 keeps this an assertion about runtime behaviour. Written as a
# literal, Elixir 1.20's type checker rejects it at compile time — the
# same answer, arriving earlier, which is worth knowing on its own.
assert_raise FunctionClauseError, fn -> apply(Decimal, :new, [0.1]) end
assert Decimal.from_float(0.1) |> Decimal.to_string() == "0.1"
assert Decimal.from_float(0.1 + 0.2) |> Decimal.to_string() == "0.30000000000000004"
end
test "== compares the struct, so equal values are not equal" do
a = Decimal.new("1.0")
b = Decimal.new("1.00")
refute a == b
assert Decimal.equal?(a, b)
assert Decimal.compare(a, b) == :eq
end
test "everything built on == inherits the bug" do
a = Decimal.new("1.0")
b = Decimal.new("1.00")
assert length(Enum.uniq([a, b])) == 2
refute b in [a]
end
test "sorting needs the module, not the default term order" do
values = [Decimal.new("2"), Decimal.new("1.0")]
# Structs sort by their fields, and coef comes before exp, so 2 sorts
# before 1.0 — a wrong order that looks right on plenty of inputs.
assert Enum.map(Enum.sort(values), &Decimal.to_string/1) == ["2", "1.0"]
assert Enum.map(Enum.sort(values, Decimal), &Decimal.to_string/1) == ["1.0", "2"]
end
test "normalize is how you get a canonical form" do
assert Decimal.normalize(Decimal.new("1.00")) |> Decimal.to_string() == "1"
assert Decimal.normalize(Decimal.new("1.0")) == Decimal.normalize(Decimal.new("1.00"))
end
end
ExUnit.start()