Skip to content

Conversation

@sabiwara
Copy link
Contributor

This PR fixes most of the small breakage in the CI due to the -0.0 change and non-deterministic order of keys.

There is still a breaking test for Registry, and I haven't looked into IEx/mix etc yet, will look into it later.

Comment on lines -3812 to -3813
iex> Enum.unzip(%{a: 1, b: 2})
{[:a, :b], [1, 2]}
Copy link
Contributor Author

@sabiwara sabiwara Feb 15, 2024

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...

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was already the case on OTP26, the test was misleading precisely because 0.0 === -0.0 was true

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing Float.ceil/2 to match Float.ceil/1 and :math.ceil/1

Copy link
Contributor Author

@sabiwara sabiwara Feb 15, 2024

Choose a reason for hiding this comment

The 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.0

It feels like we'd want to have to following invariants:

  • ceil(x) >= round(x)
  • fun(-0.01) <= fun(-0.0)
  • fun(-0.01, 0) === fun(-0.01, 1)

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.0

Seems 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks excellent to me, awesome summary and proposal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK should be good! 5edfdd6

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably this change in :ets, adding the sort in the doctest makes it obvious that we should not rely on the order? (unit tests are already doing this)

@sabiwara sabiwara merged commit 91af017 into elixir-lang:main Feb 16, 2024
@sabiwara sabiwara deleted the otp27-fixes branch February 16, 2024 07:47
josevalim pushed a commit that referenced this pull request Feb 24, 2024
* Fix non-deterministic key-value tests

* Fix non-deterministic Enum tests

* Fix :only option when deriving Inspect

* Float.ceil and Float.floor return -0.0 for negative numbers

* Fix non-deterministic Registry doctest

* Simplify check
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