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.
First batch of tests from Violet - Python VM written in Swift (connected to #98 Using tests from “Violet - Python VM written in Swift”).
Test failures
Binary operations
Let's use the following test as an example (I chose this one at random, there are other tests that are failing, maybe for different reasons):
attaswift/BigInt
says it equals to0
.Other engines
BitXor[-1, 18446744073709551615] -> -18446744073709551616
linkWhy?
-1
in two complement is represented as all1
with the desired width.18446744073709551615
is equal to2^64 − 1
which is1111111111111111111111111111111111111111111111111111111111111111
(basically1
repeated 63 times)So, obviously if both
-1
and18446744073709551615
are all1
thenxor
will be 0, just likeattaswift/BigInt
returns.Well… not exactly.
18446744073709551615
is positive, so its binary representation has to have0
prefix in two complement (otherwise it would mean negative number).With this:
Shift right
Let's use the following test as an example (I chose this one at random, there are other tests that are failing, maybe for different reasons):
This is an interesting case (look at the last number:
7
vs8
):(*) This is in
Int
range, so you can just-1932735284 >> 5
to test it.Anyway… Wolfram and attaswift give
-60397977
, but Node, Python, Swift and Violet give-60397978
.Long story short, both answers are correct, it depends on how you round.
Doing this by hand:
1000 1100 1100 1100 1100 1100 1100 1100 >> 5 = 100 0110 0110 0110 0110 0110 0110 rem 0 1100
With sign extension to
Int32
:1111_1100_0110_0110_0110_0110_0110_0110
, which is (according to Swift repl):I think that
attaswift
uses sign + magnitude representation. If it was 2 complement then everything would be trivial, but it is not, so sometimes you need an adjustment: Swift uses what would be 'GMP_DIV_FLOOR' mode in 'GMP'. Which means that if we are negative and any of the removed bits is '1' then we have to round down.Wolfram does not round down (which is “more” mathematically correct!).