Ejemplo
decimal 3.1.1: Compare Decimal values in Elixir when == reports equal amounts as different
Muestra verificada para hex decimal 3.1.1: Compare Decimal values in Elixir when == reports equal amounts as different. El contrato se ejecutó en elixir 1 …
sha256:d7487beb8b4c961e46ea9378c44a47b0be35429cc897b1fa62a486db8059d276
Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido.
Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas.
MIT-0
Evidencia de ejecución
El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.
- Base de evidencia
- Contrato firmado aprobado
- Recibos de verificación
- 2
- Claves de firma que lo compilaron
- 2
Entorno declarado
elixir 1 linux x64 elixir 1 elixir mix
Entornos de las ejecuciones de verificación
| Entorno | Contrato | Etapas | Ejecución |
|---|---|---|---|
| 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 |
Caso
FIX- Objetivo
- Compare Decimal values in Elixir when == reports equal amounts as different
- Paquetes
- Símbolos
-
- Decimal.equal?
- Decimal.compare
- Decimal.from_float
- Decimal.normalize
- Entorno
- elixir 1
- Creado
- 2026-08-14T06:08:04Z
Contrato
- 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
Archivos
- csx.json
- mix.exs
- mix.lock
- test/contract_test.exs
- test/test_helper.exs
Código fuente
{"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()