Hướng dẫn javascript divide get integer

JavaScript calculates right the floor of negative numbers and the remainder of non-integer numbers, following the mathematical definitions for them.

FLOOR is defined as "the largest integer number smaller than the parameter", thus:

  • positive numbers: FLOOR(X)=integer part of X;
  • negative numbers: FLOOR(X)=integer part of X minus 1 (because it must be SMALLER than the parameter, i.e., more negative!)

REMAINDER is defined as the "left over" of a division (Euclidean arithmetic). When the dividend is not an integer, the quotient is usually also not an integer, i.e., there is no remainder, but if the quotient is forced to be an integer (and that's what happens when someone tries to get the remainder or modulus of a floating-point number), there will be a non-integer "left over", obviously.

JavaScript does calculate everything as expected, so the programmer must be careful to ask the proper questions (and people should be careful to answer what is asked!) Yarin's first question was NOT "what is the integer division of X by Y", but, instead, "the WHOLE number of times a given integer GOES INTO another". For positive numbers, the answer is the same for both, but not for negative numbers, because the integer division (dividend by divisor) will be -1 smaller than the times a number (divisor) "goes into" another (dividend). In other words, FLOOR will return the correct answer for an integer division of a negative number, but Yarin didn't ask that!

gammax answered correctly, that code works as asked by Yarin. On the other hand, Samuel is wrong, he didn't do the maths, I guess, or he would have seen that it does work (also, he didn't say what was the divisor of his example, but I hope it was 3):

Remainder = X % Y = -100 % 3 = -1

GoesInto = (X - Remainder) / Y = (-100 - -1) / 3 = -99 / 3 = -33

By the way, I tested the code on Firefox 27.0.1, it worked as expected, with positive and negative numbers and also with non-integer values, both for dividend and divisor. Example:

-100.34 / 3.57: GoesInto = -28, Remainder = -0.3800000000000079

Yes, I noticed, there is a precision problem there, but I didn't had time to check it (I don't know if it's a problem with Firefox, Windows 7 or with my CPU's FPU). For Yarin's question, though, which only involves integers, the gammax's code works perfectly.

  1. HowTo
  2. JavaScript Howtos
  3. Integer Division in JavaScript

Created: May-31, 2021

  1. Get the Quotient and Remainder of an Integer Division Using the Math Library in JavaScript
  2. Get the Quotient and Remainder of an Integer Division Using the Bitwise Operators in JavaScript

This tutorial will discuss how to get the quotient and remainder of a division using the Math library and bitwise operators in JavaScript.

Get the Quotient and Remainder of an Integer Division Using the Math Library in JavaScript

In JavaScript, we can divide two variables easily, and the result is in floating-point numbers, but if we want to get the quotient and remainder of the division, we can use the Math library, providing us with a lot of functions. For example, we can get the quotient of a division using the Math.floor() or Math.trunc() function which converts the floating-point number to an integer, and to get the remainder, we can use the % character. For example, let’s find the quotient and remainder of 13 divided by 5. See the code below.

var a = 13;
var b = 5;
var quo = Math.floor(a/b);
var rem = a%b;
console.log('Quotient = ',quo,'Remainder = ',rem);

Output:

Quotient =  2 Remainder =  3

Note that the Math library will fail if the numbers are too large. You can also use the Math.trunc() function which can handle large numbers as compared to the Math.floor() function. The Math.floor() function will fail in the case of negative numbers, but Math.trunc() won’t fail in case of negative numbers.

Get the Quotient and Remainder of an Integer Division Using the Bitwise Operators in JavaScript

In JavaScript, we can get the quotient and remainder of a division using the bitwise operators. For example, we can get the quotient of a division using the bitwise NOT ~~ or bitwise OR |0, which converts the floating-point number to an integer. And to get the remainder, we can use the % character. For example, let’s find the quotient and remainder of 13 divided by 5. See the code below.

var a = 13;
var b = 5;
var quo = ~~(a/b);
var rem = a%b;
console.log('Quotient = ',quo,'Remainder = ',rem);

Output:

Quotient =  2 Remainder =  3

As you can see, the output is the same as of the above method. The performance of bitwise operators is greater as compared to the Math library, but the capacity to handle large numbers is less. So, if you have small numbers, you can use the bitwise operators; otherwise, use the Math library. The bitwise operators can also handle negative numbers. You can also use the parseInt() function to convert a floating-point number to an integer. If you don’t want to use any functions, you can use a simple formula with the remainder operator % as shown below.

var a = 13;
var b = 5;
var rem = a%b;
var quo = (a-rem)/b;
console.log('Quotient = ',quo,'Remainder = ',rem);

Output:

Quotient =  2 Remainder =  3

As you can see, the output is the same as of the above methods.

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - JavaScript Math

  • Convert Radians to Degrees in JavaScript
  • BigDecimal in JavaScript
  • Add Two Numbers in JavaScript
  • JavaScript Scientific Notation
  • Hướng dẫn javascript divide get integer