CodeSampleX

示例

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. contract 在 elixir 1 · linux alpine/x64 · docker…

sha256:d7487beb8b4c961e46ea9378c44a47b0be35429cc897b1fa62a486db8059d276

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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

契约

  1. assert Decimal arithmetic is exact where float arithmetic is not
  2. assert Decimal.new refuses a float and from_float is the explicit conversion
  3. assert == reports numerically equal Decimals as different while Decimal.equal? does not
  4. assert Enum.uniq and in inherit that, and sorting needs the Decimal module
  5. assert normalize produces a canonical form

文件

  • csx.json
  • mix.exs
  • mix.lock
  • test/contract_test.exs
  • test/test_helper.exs

下载源代码构件 (tar.gz)

源代码

csx.json
{"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"}
mix.exs
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
mix.lock
%{
  "decimal": {:hex, :decimal, "3.1.1", "430d87b04011ce6cbd4fd205be758311a81f87d552d40904abd00f015935b1d0", [:mix], [], "hexpm", "c5f25f2ced74a0587d03e6023f595db8e924c9d3922c8c8ffd9edfc4498cf1f6"},
}
test/contract_test.exs
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
test/test_helper.exs
ExUnit.start()

原始种子者

anonymous