Find bug in code to get $50 USD bounty
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I believe the code that was written by "Vyacheslav Sokolov" in Python (File: xyz2code.py) at cubicpostcode.com has some serious bugs. The file was converted to javascript at: https://cubicpostcode.github.io/calc/3xyz2cube.html may have some serious bugs. Test it and find some serious bugs or limitations and you will be eligible to win this bounty. Also your name and github account will be featured on this project website, if you wish so.

Only for the serious geek that wants to help this project to be 100% to trust and free of any bugs.
You may win the bounty if you are a serious geek that discovers serious bugs and problems with the project and website.

awarded to TheOsch

Crowdsource coding tasks.

1 Solution

Winning solution

Hello!

The first bug at https://cubicpostcode.github.io/calc/3xyz2cube.html is in function max(...): in the script it takes 3 arguments but in fact it's called with four. So if you'll remove

            function max(v1, v2, v3) {
                let m = v1;
                if (v2 > m)
                    m = v2;
                if (v3 > m)
                    m = v3;
                return m;
            }

and replace

var square_number = max(y + 1, z + 1, -y, -z)

with

var square_number = Math.max(y + 1, z + 1, -y, -z)

the bug will be fixed.

Another two problems are in the end of the script where "code" is calculated. First, the precision of a Number in Javascript is not enough to hold 22 exact decimal digits, so the last digits in the resulting code are likely to be wrong (this problem doesn't exist in Python); second, there should be exactly 22 digits in the code, i.e. smaller code should be left-p-added with zeros which the script currently doesn't do. The first problem can be solved by dividing the code into two parts that contain much less than 22 digits; the second one is solved by the follopwing function.

All the end of the script starting from the line I commented can be replaced with this fragment:

            // var code = slice_number * 441000000000000 + cube_p
            function pad(x, n) {
                var s = "000000000000" + x;
                return s.substr(s.length - n);
            }
            var c1 = pad(Math.floor(slice_number * 441 + cube_p * 1e-12), 10)
            var c2 = pad(cube_p % 1e12, 12)
            var code = c1 + "" + c2
            document.getElementById('cube-code').innerHTML = code;
Is the Python code (File: xyz2code.py) with the same bug?
danalex 3 months ago
No. It uses the standard Python's "max" function which is the same as "Math.max" in Javascript. The code in Python had been tested many times on different examples, and I still think it's correct
TheOsch 3 months ago
I will probably award you the bounty unless someone finds a serious bug that you are missing.
danalex 3 months ago
I found another two errors. They are definitely the last. I'll update the solution in a few minutes.
TheOsch 3 months ago
Can you please post the javascript code for the second problem: there should be exactly 22 digits in the code, i.e. smaller code should be left-p-added with zeros which the script currently doesn't do.
danalex 3 months ago
Can you please post the javascript code for the second problem: there should be exactly 22 digits in the code, i.e. smaller code should be left-p-added with zeros which the script currently doesn't do.
danalex 3 months ago
Can you please post the javascript code for the second problem: there should be exactly 22 digits in the code, i.e. smaller code should be left-p-added with zeros which the script currently doesn't do.
danalex 3 months ago
The last fragment solves both problems. The pad(x, n) function pads the given number x to n digits in total, including zeros if needed. c1 is padded to 10 digits, c2 to 12, so the total length of the result is 22.
TheOsch 3 months ago
I did commit the change. Can you please have a look to make sure now it is working fine?
danalex 3 months ago
One more detail. in var c1 = pad(parseInt(slice_number * 441 + cube_p * 1e-12), 10) parseInt must be replaced with Math.floor.
TheOsch 3 months ago
By the way, the body of the coord_round(x) function can be replaced with one line: return Math.floor(x) though the current function works too.
TheOsch 3 months ago
With the last fix (Math.floor instead of parseInt) the script is working correctly in any case.
TheOsch 3 months ago
To be honest I'm confused. I did commit one of the proposed changes. Is it working fine now?
danalex 3 months ago
Yes, it's alright now.
TheOsch 3 months ago
Just one question, when I choose the first option with my iPhone and put the iPhone in a table without moving it at all, the cubes change every minute or so. Is it due to the GPS accuracy? Maybe in the future we will have more accurate GPS devices, because like this it is not relieable. You are never sure about the cube your iphone is on. What do you think?
danalex 3 months ago
I've looked at that page on my Mac, yes, it changes coordinates every dozen of seconds. As far as I know the guaranteed precision of GPS is 50 meters while the resolution of these cubic codes is 1 meter. And by the nature of these codes two points which are 1-2 m from each other can have significantly different codes. So it's alright.
TheOsch 3 months ago
How long is it expected to take to make sure that the script is correct now?
TheOsch 3 months ago
Thank you!
TheOsch 3 months ago