admin管理员组文章数量:1023848
I was wondering how JavaScript handles modulo. For example, what would JavaScript evaluate 47 % 8
as? I can’t seem to find any documentation on it, and my skills on modulo aren’t the best.
I was wondering how JavaScript handles modulo. For example, what would JavaScript evaluate 47 % 8
as? I can’t seem to find any documentation on it, and my skills on modulo aren’t the best.
4 Answers
Reset to default 3Exactly as every language handles modulo: The remainder of X / Y
.
47 % 8 == 7
Also if you use a browser like Firefox + Firebug, Safari, Chrome, or even IE8+ you could test such an operation as quickly as hitting F12.
Javascript's modulo operator returns a negative number when given a negative number as input (on the left side).
14 % 5 // 4
15 % 5 // 0
-15 % 5 // -0
-14 % 5 // -4
(Note: negative zero is a distinct value in JavaScript and other languages with IEEE floats, but -0 === 0
so you usually don't have to worry about it.)
If you want a number that is always between 0 and a positive number that you specify, you can define a function like so:
function mod(n, m) {
return ((n % m) + m) % m;
}
mod(14, 5) // 4
mod(15, 5) // 4
mod(-15, 5) // 0
mod(-14, 5) // 1
Modulo should behave like you expect. I expect.
47 % 8 == 7
Fiddle Link
TO better understand modulo here is how its built;
function modulo(num1, num2) {
if (typeof num1 != "number" || typeof num2 != "number"){
return NaN
}
var num1isneg=false
if (num1.toString().includes("-")){
num1isneg=true
}
num1=parseFloat(num1.toString().replace("-",""))
var leftover =parseFloat( ( parseFloat(num1/num2) - parseInt(num1/num2)) *num2)
console.log(leftover)
if (num1isneg){
var z = leftover.toString().split("")
z= ["-", ...z]
leftover = parseFloat(z.join(""))
}
return leftover
}
I was wondering how JavaScript handles modulo. For example, what would JavaScript evaluate 47 % 8
as? I can’t seem to find any documentation on it, and my skills on modulo aren’t the best.
I was wondering how JavaScript handles modulo. For example, what would JavaScript evaluate 47 % 8
as? I can’t seem to find any documentation on it, and my skills on modulo aren’t the best.
4 Answers
Reset to default 3Exactly as every language handles modulo: The remainder of X / Y
.
47 % 8 == 7
Also if you use a browser like Firefox + Firebug, Safari, Chrome, or even IE8+ you could test such an operation as quickly as hitting F12.
Javascript's modulo operator returns a negative number when given a negative number as input (on the left side).
14 % 5 // 4
15 % 5 // 0
-15 % 5 // -0
-14 % 5 // -4
(Note: negative zero is a distinct value in JavaScript and other languages with IEEE floats, but -0 === 0
so you usually don't have to worry about it.)
If you want a number that is always between 0 and a positive number that you specify, you can define a function like so:
function mod(n, m) {
return ((n % m) + m) % m;
}
mod(14, 5) // 4
mod(15, 5) // 4
mod(-15, 5) // 0
mod(-14, 5) // 1
Modulo should behave like you expect. I expect.
47 % 8 == 7
Fiddle Link
TO better understand modulo here is how its built;
function modulo(num1, num2) {
if (typeof num1 != "number" || typeof num2 != "number"){
return NaN
}
var num1isneg=false
if (num1.toString().includes("-")){
num1isneg=true
}
num1=parseFloat(num1.toString().replace("-",""))
var leftover =parseFloat( ( parseFloat(num1/num2) - parseInt(num1/num2)) *num2)
console.log(leftover)
if (num1isneg){
var z = leftover.toString().split("")
z= ["-", ...z]
leftover = parseFloat(z.join(""))
}
return leftover
}
本文标签: How does JavaScript handle moduloStack Overflow
版权声明:本文标题:How does JavaScript handle modulo? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565193a2156405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论