Skip to content

Fix float round/2, floor/2, ceil/2 raises wrong exception for invalid precision - #15872

Merged
josevalim merged 1 commit into
elixir-lang:mainfrom
AlexGx:ag-float_precision_inv_exception-fix
Sep 8, 2026
Merged

josevalim merged 1 commit into
elixir-lang:mainfrom
AlexGx:ag-float_precision_inv_exception-fix

Conversation

@AlexGx

@AlexGx AlexGx commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Fixes Float.round/2, Float.floor/2, Float.ceil/2 raises wrong exception for invalid precision

iex> Float.round(1.1, %{})
** (Protocol.UndefinedError) protocol String.Chars not implemented for Map

expected:

** (ArgumentError) precision %{} is out of valid range of 0..15

I did not add tests because similar tests for other methods were recently removed during "warnings when running tests" cleanup.

@josevalim

Copy link
Copy Markdown
Member

Instead of changing this here, let's add guards around the precision in the callers to this function.

@AlexGx

AlexGx commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

All of them were already using guards. if remove the fallback clause, the error changes from ArgumentError to FunctionClauseError. Did I understand you correctly?

...
@precision_range 0..15
...

  def round(float, precision) when is_float(float) and precision in @precision_range do
    round(float, precision, :half_up)
  end

  def round(float, precision) when is_float(float) do
    raise ArgumentError, invalid_precision_message(precision)
  end

@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

@josevalim
josevalim merged commit 36351ae into elixir-lang:main Sep 8, 2026
@AlexGx
AlexGx deleted the ag-float_precision_inv_exception-fix branch September 8, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants