negative modulo (v16)

Revision 16 of this benchmark created by Endro on


Preparation HTML

<script>
function mod_a(n, d) {
    return n - (d * Math.floor(n / d));
}

function mod_b(n, d) {
    var q = ~~(n / d);
    if (n < 0) q = q - 1;
    return n - (d * q);
}

function mod_c(n, d) {
    var q = ~~(n / d);
    (n < 0) ? --q : q;
    return n - (d * q);
}

function mod_d(n, d) {
    var remain = n % d;
    return remain >= 0 ? remain : remain + d;
};

function mod_e(n, d) {
    return ((n % d) + d) % d;
}
function modlk (num, mod) {
	if (mod & (mod - 1) === 0 && mod !== 0) {
		return num & (mod - 1);
	}
	return num < 0 ? ((num % mod) + mod) % mod : num % mod;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
mod_a
mod_a(-7,9);
ready
mod_b
mod_b(-7,9);
ready
mod_c
mod_c(-7,9);
ready
mod_d
mod_d(-7,9);
ready
mod_e
mod_e(-7,9);
ready
mod_a (non-int)
mod_a(-7.2,9);
ready
mod_b (non-int)
mod_b(-7.2,9);
ready
mod_c (non-int)
mod_c(-7.2,9);
ready
mod_d (non-int)
mod_d(-7.2,9);
ready
mod_e (non-int)
mod_e(-7.2,9);
ready
modlk (non-int)
modlk(-7.2,9);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.