{"issue":{"title":"Mod/intdiv Mathematical Operators to Work Around JavaScript Oddities","user":{"login":"mitsuhiko","id":7396,"url":"https://api.github.com/users/mitsuhiko","gravatar_id":"181de1fb11dffe39774f3e2e23cda3b6","avatar_url":"https://secure.gravatar.com/avatar/181de1fb11dffe39774f3e2e23cda3b6?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"created_at":"2011-12-25T03:03:52Z","milestone":null,"labels":[{"name":"enhancement","url":"https://api.github.com/repos/jashkenas/coffee-script/labels/enhancement","color":"84b6eb"}],"state":"open","updated_at":"2011-12-27T22:31:26Z","id":2654269,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"closed_at":null,"html_url":"https://github.com/jashkenas/coffee-script/issues/1971","body":"One of the weird aspects of JavaScript is how it defines modulo:\r\n\r\n```coffeescript\r\n> (-3) % 4\r\n-3\r\n```\r\n\r\nIn comparison Python/Ruby define them to always have the sign of the right side:\r\n\r\n```python\r\n>>> -3 % 4\r\n1\r\n```\r\n\r\nLikewise it's very common to have to divide and require an integer result. As such I would propose two new operators '%%' for right handside signed mod and '//' for integer division:\r\n\r\n```coffeescript\r\na %% b\r\n```\r\n\r\nwould compile to\r\n\r\n```coffeescript\r\n(a % b + b) % b\r\n```\r\n\r\nAnd\r\n\r\n```coffeescript\r\na // b\r\n```\r\n\r\nwould compile to\r\n\r\n```javascript\r\nMath.floor(a / b)\r\n```\r\n\r\nWhen is this useful? For instance consider wanting to move a player on a grid with the intent of mirroring over on edges:\r\n\r\n```coffeescript\r\ngoRight: ->\r\n  @x = (@x + 1) %% @boardWidth\r\n\r\ngoLeft: ->\r\n  @x = (@x - 1) %% @boardWidth\r\n```\r\n\r\nAnother use case (where also the integer division comes in handy) is converting for instance things like global coordinates to locals:\r\n\r\n```coffeescript\r\ngetTile: (x, y) ->\r\n   sectionX = x // SECTION_WIDTH\r\n   sectionY = y // SECTION_HEIGHT\r\n   localX = x %% SECTION_WIDTH\r\n   localY = y %% SECTION_HEIGHT\r\n   section = @sections[sectionX][sectionY]\r\n   section[localX + localY * SECTION_WIDTH]\r\n```","number":1971,"url":"https://api.github.com/repos/jashkenas/coffee-script/issues/1971","comments":11,"assignee":null},"comment":{"user":{"login":"STRd6","id":18894,"url":"https://api.github.com/users/STRd6","gravatar_id":"33117162fff8a9cf50544a604f60c045","avatar_url":"https://secure.gravatar.com/avatar/33117162fff8a9cf50544a604f60c045?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"created_at":"2011-12-27T22:31:26Z","updated_at":"2011-12-27T22:31:26Z","id":3286102,"body":"> Aside from the fact that patching prototype is literally the worst way to make this work\r\n\r\n@mitsuhiko Why is patching Number.prototype the worst way? What's so bad about it?\r\n","url":"https://api.github.com/repos/jashkenas/coffee-script/issues/comments/3286102"},"action":"created"}