Repository navigation
Match Erlang semantics for div/2 and round/1 - #500
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
Two functions in the erlang compatibility layer diverge from Erlang on negative operands: - div/2 used `/`, so div(-7, 2) returned -3.5 instead of -3. Erlang's div truncates toward zero. - round/1 used Math.round, which rounds half values toward +Infinity, so round(-2.5) returned -2 instead of -3, and round(-0.5) returned -0 instead of -1. Erlang rounds half values away from zero. Both are reachable from compiled Elixir: div(-7, 2) and round(-2.5) compile to Core.erlang.div and Core.erlang.round calls. Also drop a leftover console.log in nodes/1 that printed the accumulator on every call. Add tests for div, rem, round and trunc covering negative operands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two functions in the
erlangcompatibility layer disagree with Erlang on negative operands. Both are reachable from compiled Elixir —div(-7, 2),round(-2.5)andtrunc(-2.7)compile toCore.erlang.div,Core.erlang.roundandCore.erlang.trunccalls.div/2performed float divisionErlang's
divtruncates toward zero, soMath.trunc(left / right)is the matching implementation. Elixir'sdiv/2is a guard BIF that lowers to:erlang.div, which is why this is on the compiled path at all.round/1rounded half values toward +InfinityMath.roundbreaks ties toward+Infinity, but Erlang rounds them away from zero:Math.sign(number) * Math.round(Math.abs(number))rounds half values away from zero as Erlang does.Stray debug log
nodes/1printed its accumulator on every call, so any program reaching that branch wrote to stdout as a side effect. Removed.rem/1andtrunc/1already match Erlang; the new tests pin that so the four stay consistent as a set.Changes
src/javascript/lib/core/erlang_compat/erlang.js:div/2,round/1, dropconsole.loginnodes/1src/javascript/tests/core/erlang_compat/erlang_spec.js: tests fordiv,rem,round,truncwith negative operandsTesting
Each new test was confirmed to fail against the unfixed source.
Caveat on how this was verified:
npx avacannot start onmaster(see #499), so these runs were made with the changes from #499 applied locally, uncommitted. This PR touches only the runtime file and its spec, and does not depend on #499 to be correct — but its tests cannot run in CI until #499, or an equivalent fix, lands.