Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/mod-loop
A workaround for using negative numbers with JS's modulo operator
https://github.com/hughsk/mod-loop
Last synced: 12 days ago
JSON representation
A workaround for using negative numbers with JS's modulo operator
- Host: GitHub
- URL: https://github.com/hughsk/mod-loop
- Owner: hughsk
- License: other
- Created: 2013-07-07T03:45:24.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-07-07T04:41:46.000Z (over 11 years ago)
- Last Synced: 2024-10-17T16:41:26.982Z (22 days ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# mod-loop #
JavaScript's modulo operator (`%`) doesn't handle negative numbers nicely -
here's something general to work around the issue.Benchmarks on jsperf can be found
[here](http://jsperf.com/modulo-for-negative-numbers). Thanks to
[shama](http://github.com/shama) and
[mikolalysenko](http://github.com/mikolalysenko) for finding a faster method.## Installation ##
``` bash
npm install mod-loop
```## Usage ##
### `require('mod-loop')(x, y)` ###
Returns `x % y`, for both positive and negative and negative numbers.
``` javascript
var mod = require('mod-loop')mod(+100, 10) // 0
mod(-100, 10) // 0
mod(-105, 10) // 5
mod(-102, 10) // 8
```