Conversation
|
Anyway… I will work on adding more tests later… Btw. Take those explanations with a grain of salt. Obviously I wrote those tests (well Node generated them… but it is still my work), so subconsciously I will try to “make them correct” (maybe the better term would be “justify their shortcomings”). |
|
Hi @LiarPrincess , are you still planning to work on this PR? Is there any help needed? I could set up nodejs in the devcontainer and CI if needed. More tests are always better, and it looks like you have already made great progress here :) |
Those tests are 100% Swift. They were generated by Node because JavaScript also has a Code in
The test cases uncovered 2 potential problems:
The core team has to decide what to do with this. If we merge the tests we will have failures. Note that changing the rounding mode for
This PR was a part of a series of PRs that add tests from Violet - Python VM written in Swift. If you go to the Tests/BigIntTests you will see a few directories with "Violet" prefix. I am responsible for them (code ownership) if there are any problems (failures etc.) with them feel free to assign me. |
|
Thanks for the explanation :) |
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/BigIntsays it equals to0.Other engines
BitXor[-1, 18446744073709551615] -> -18446744073709551616linkWhy?
-1in two complement is represented as all1with the desired width.18446744073709551615is equal to2^64 − 1which is1111111111111111111111111111111111111111111111111111111111111111(basically1repeated 63 times)So, obviously if both
-1and18446744073709551615are all1thenxorwill be 0, just likeattaswift/BigIntreturns.Well… not exactly.
18446744073709551615is positive, so its binary representation has to have0prefix 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:
7vs8):(*) This is in
Intrange, so you can just-1932735284 >> 5to 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 1100With sign extension to
Int32:1111_1100_0110_0110_0110_0110_0110_0110, which is (according to Swift repl):I think that
attaswiftuses 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!).