Credence.Pattern.PreferErlangFloat
(credence v0.7.0)
Copy Markdown
Replaces float-coercion arithmetic tricks with explicit :erlang.float/1.
LLMs (and developers) use x * 1.0, x / 1.0, x + 0.0, or x - 0.0 to
coerce a number to a float. These are arithmetic idioms borrowed from Python;
:erlang.float/1 expresses the same intent explicitly and works whether the
input is an integer (converts) or already a float (returns it unchanged).
The rewrite preserves the float result — it does not delete the coercion.
Deleting * 1.0 (an earlier mistake of a now-merged sibling rule) would turn
6.0 back into 6, a value-kind change; wrapping in :erlang.float/1 keeps
the value identical for every number.
Applies to any operand shape — bare variables (n * 1.0) and compound
expressions alike (Enum.sum(list) * 1.0, (a + b) * 1.0).
Detected patterns
x * 1.0 1.0 * x
x / 1.0
x + 0.0 0.0 + x
x - 0.0Note: 0.0 - x is NOT flagged — it negates, not coerces.
Behaviour note
For every numeric operand the rewrite is exact (same float value). A
non-number operand raises in both forms — x * 1.0 raises ArithmeticError
while :erlang.float(x) raises ArgumentError — so the code crashes either
way and only the exception module differs. (A non-number operand at a
float-coercion site is already-broken code.)
Bad
defp to_float(n) when is_integer(n), do: n * 1.0
avg = total / count * 1.0Good
defp to_float(n) when is_integer(n), do: :erlang.float(n)
avg = :erlang.float(total / count)