Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A workaround for using negative numbers with JS's modulo operator

License

Notifications You must be signed in to change notification settings

hughsk/mod-loop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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. Thanks to shama and mikolalysenko for finding a faster method.

Installation

npm install mod-loop

Usage

require('mod-loop')(x, y)

Returns x % y, for both positive and negative and negative numbers.

var mod = require('mod-loop')

mod(+100, 10) // 0
mod(-100, 10) // 0
mod(-105, 10) // 5
mod(-102, 10) // 8

About

A workaround for using negative numbers with JS's modulo operator

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors