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. Show FLOOR is defined as "the largest integer number smaller than the parameter", thus:
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. Created: May-31, 2021
This tutorial will discuss how to get the quotient and remainder of a division using the Get the Quotient and Remainder of an Integer Division Using the Math Library in JavaScriptIn 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
Output:
Note that the Get the Quotient and Remainder of an Integer Division Using the Bitwise Operators in JavaScriptIn 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
Output:
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
Output:
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 |