-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fixes to support 0TP27 #13351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes to support 0TP27 #13351
Changes from all commits
06cbefe
fff947b
0b7ff47
5edfdd6
ab31e6c
d59afeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,6 +275,8 @@ defmodule Float do | |
| -56.0 | ||
| iex> Float.ceil(34.251, 2) | ||
| 34.26 | ||
| iex> Float.ceil(-0.01) | ||
| -0.0 | ||
|
|
||
| """ | ||
| @spec ceil(float, precision_range) :: float | ||
|
|
@@ -332,6 +334,8 @@ defmodule Float do | |
| -6.0 | ||
| iex> Float.round(12.341444444444441, 15) | ||
| 12.341444444444441 | ||
| iex> Float.round(-0.01) | ||
| -0.0 | ||
|
|
||
| """ | ||
| @spec round(float, precision_range) :: float | ||
|
|
@@ -340,8 +344,13 @@ defmodule Float do | |
| # and could be implemented in the future. | ||
| def round(float, precision \\ 0) | ||
|
|
||
| def round(float, 0) when float == 0.0, do: float | ||
|
|
||
| def round(float, 0) when is_float(float) do | ||
| float |> :erlang.round() |> :erlang.float() | ||
| case float |> :erlang.round() |> :erlang.float() do | ||
| zero when zero == 0.0 and float < 0.0 -> -0.0 | ||
| rounded -> rounded | ||
| end | ||
| end | ||
|
|
||
| def round(float, precision) when is_float(float) and precision in @precision_range do | ||
|
|
@@ -365,6 +374,8 @@ defmodule Float do | |
| case rounding do | ||
| :ceil when sign === 0 -> 1 / power_of_10(precision) | ||
| :floor when sign === 1 -> -1 / power_of_10(precision) | ||
| :ceil when sign === 1 -> -0.0 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I'm not sure, we might want to rethink this whole rounding spec. Here is the current behavior: Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Interactive Elixir (1.16.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> for fun <- [:round, :ceil], num <- [-0.0, -0.01], prec <- 0..1, do: IO.puts "#{fun}(#{num}), #{prec}) = #{apply(Float, fun, [num, prec])}"
round(-0.0), 0) = 0.0
round(-0.0), 1) = -0.0
round(-0.01), 0) = 0.0
round(-0.01), 1) = 0.0
ceil(-0.0), 0) = -0.0
ceil(-0.0), 1) = -0.0
ceil(-0.01), 0) = -0.0
ceil(-0.01), 1) = 0.0It feels like we'd want to have to following invariants:
but none of these are currently respected. How about the following? round(-0.0), 0) = -0.0
round(-0.0), 1) = -0.0
round(-0.01), 0) = -0.0
round(-0.01), 1) = -0.0
ceil(-0.0), 0) = -0.0
ceil(-0.0), 1) = -0.0
ceil(-0.01), 0) = -0.0
ceil(-0.01), 1) = -0.0Seems consistent with what Javascript and Python are doing: > Math.round(-0.0, 0)
-0
> Math.round(-0.0, 1)
-0
> Math.ceil(-0.0, 1)
-0
> Math.ceil(-0.01, 1)
-0>>> round(-0.0, 0)
-0.0
>>> round(-0.0, 1)
-0.0
>>> round(-0.01, 1)
-0.0
>>> round(-0.01, 0)
-0.0
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks excellent to me, awesome summary and proposal.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK should be good! 5edfdd6 |
||
| :half_up when sign === 1 -> -0.0 | ||
| _ -> 0.0 | ||
| end | ||
|
|
||
|
|
@@ -394,6 +405,9 @@ defmodule Float do | |
| boundary = den <<< 52 | ||
|
|
||
| cond do | ||
| num == 0 and sign == 1 -> | ||
| -0.0 | ||
|
|
||
| num == 0 -> | ||
| 0.0 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1301,16 +1301,16 @@ defmodule Registry do | |
| iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest) | ||
| iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value) | ||
| iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value) | ||
| iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}]) | ||
| [{"world", self(), :value}, {"hello", self(), :value}] | ||
| iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}]) |> Enum.sort() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| [{"hello", self(), :value}, {"world", self(), :value}] | ||
|
|
||
| Get all keys in the registry: | ||
|
|
||
| iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest) | ||
| iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value) | ||
| iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value) | ||
| iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :_, :_}, [], [:"$1"]}]) | ||
| ["world", "hello"] | ||
| iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :_, :_}, [], [:"$1"]}]) |> Enum.sort() | ||
| ["hello", "world"] | ||
|
|
||
| """ | ||
| @doc since: "1.9.0" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,9 +104,9 @@ defmodule FloatTest do | |
| assert Float.ceil(7.5432e3) === 7544.0 | ||
| assert Float.ceil(7.5e-3) === 1.0 | ||
| assert Float.ceil(-12.32453e4) === -123_245.0 | ||
| assert Float.ceil(-12.32453e-10) === 0.0 | ||
| assert Float.ceil(-12.32453e-10) === -0.0 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was already the case on OTP26, the test was misleading precisely because |
||
| assert Float.ceil(0.32453e-10) === 1.0 | ||
| assert Float.ceil(-0.32453e-10) === 0.0 | ||
| assert Float.ceil(-0.32453e-10) === -0.0 | ||
| assert Float.ceil(1.32453e-10) === 1.0 | ||
| assert Float.ceil(0.0) === 0.0 | ||
| end | ||
|
|
@@ -130,7 +130,7 @@ defmodule FloatTest do | |
| assert Float.ceil(-12.524235, 3) === -12.524 | ||
|
|
||
| assert Float.ceil(12.32453e-20, 2) === 0.01 | ||
| assert Float.ceil(-12.32453e-20, 2) === 0.0 | ||
| assert Float.ceil(-12.32453e-20, 2) === -0.0 | ||
|
|
||
| assert Float.ceil(0.0, 2) === 0.0 | ||
|
|
||
|
|
@@ -139,6 +139,11 @@ defmodule FloatTest do | |
| end | ||
| end | ||
|
|
||
| test "with small floats rounded up to -0.0" do | ||
| assert Float.ceil(-0.1, 0) === -0.0 | ||
| assert Float.ceil(-0.01, 1) === -0.0 | ||
| end | ||
|
|
||
| test "with subnormal floats" do | ||
| assert Float.ceil(5.0e-324, 0) === 1.0 | ||
| assert Float.ceil(5.0e-324, 1) === 0.1 | ||
|
|
@@ -172,6 +177,17 @@ defmodule FloatTest do | |
| end | ||
| end | ||
|
|
||
| test "with small floats rounded to +0.0 / -0.0" do | ||
| assert Float.round(0.01, 0) === 0.0 | ||
| assert Float.round(0.01, 1) === 0.0 | ||
|
|
||
| assert Float.round(-0.01, 0) === -0.0 | ||
| assert Float.round(-0.01, 1) === -0.0 | ||
|
|
||
| assert Float.round(-0.49999, 0) === -0.0 | ||
| assert Float.round(-0.049999, 1) === -0.0 | ||
| end | ||
|
|
||
| test "with subnormal floats" do | ||
| for precision <- 0..15 do | ||
| assert Float.round(5.0e-324, precision) === 0.0 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see a relevant test to be added, a one-size map would hide the fact that the keys are misordered, we already have a keyword above...