Twenty modulus six is equal to two, which is the remainder but how to know the the modulus is using the 3 to perform the operation?
20 % 6 = 2
You can use
Math.floor(20 / 6);
for this. It rounds down so you have the largest number possible without decimals.
If you want the quotient, you just have to truncate the division
Math.trunc(20/6) //Return 3
Reference
Modulus is not using the 3, it simply knows that:
20 / 6 = 3, with a remainder of 2
=> 20 % 6 = 2
If you want the 3, you just need the expression:
Math.floor(20 / 6)
(or possibly Math.trunc depending on how you want negative numbers handled - floor rounds towards negative infinity, trunc rounds toward zero).
Related
(Sorry in advance if this has been asked before, I searched and couldn't find a similar question)
So I believe modulo (%) gives me the remainder of a long division equation. so 2%4 =0r.
So in simple terms a modulo equation that equals zero should be an even number. and a modulo equation that equals 1, should be an odd number? Is that correct?
Here's where I begin to confuse myself.
What about equations that equal an even or odd remainder, would that still output an equal or odd number.
For instance. 5%149 equals 4r.. the remainder is an even number, so is the output all even numbers.. or does the very fact that there is any remainder at all mean that the output will be odd numbers??
TLDR, is modulo as simple as 0r outputs even numbers. And anything with 1 or more remainder outputs odd numbers.
Modulo (or modulus) is used to see the remainder of a division.
You can flip it on it's head and use multiplication to help you out as well if necessary. I've provided some examples.
Try doing something like this:
From the equation you posted in your example: 149 % 5 would give you the remainder of 4. Reason for this: The last multiple of 5 you can get before 149 is 145, and your modulus equation is telling you that you have 4 left over.
Now, if you were to do something like 150 % 5, your remainder would be 0 because 150 is a safe multiple of 5.
Some documentation should hopefully help you understand this a bit better as well: https://docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus
Some examples to help you understand the remainder:
10 % 5 = 0 Since 5 x 2 = 10
9 % 3 = 0 Since 9 x 3 = 9
6 % 2 = 0 Since 2 x 3 = 6
7 % 2 = 1 Since you can only multiply 2 three times to get 6, you are left with a remainder of 1.
a modulo equation that equals zero should be an even number. and a
modulo equation that equals 1, should be an odd number?
You've probably seen modulo as a way of testing for evenness but this is not right. It should read
a modulo by 2 operation that equals zero is an even number
ie. x % 2 == 0 implies x is even. Because x is divisible by 2. x % 3 == 0 means x is divisible by 3.
Here's one way I learned to look at it. Consider an analogue clock with n hours on in (perhaps n=12 or n=24 or some other funny clock). The first number in the modulo operation tells you how many hours forward to traverse, going round and round the circle. The second number (n) tells you how many hours is built into the clock.
Here are some examples:
You go forward 5 on a 12 hour clock and land on 5 o'clock.
5 % 12 == 5
You go forward 13, completing one full loop plus one more hour, landing on 1 o'clock.
13 % 12 == 1
You go forward 24, completing 2 full loops but landing at the starting point, 0. (ok most clocks have 12 at the top but its the same as 0.)
24 % 12 == 0
Consider a clock or spinning wheel with 4 categories.
Start at the base and take 7 steps forward. That give you one full traversal (4 steps) then 3 more steps lands you on the 3rd item.
7 % 4 == 3
You've just stepped forward 2. Because the wheel has 4 slots, the counting hasn't reset yet.
2 % 4 == 2
So to recap, the first number is the number of steps to take, the second number is the size of the clock.
The pseudocode for incrementing natural numbers using a recursive algorithm is like this (example from the Algorithm Design Manual by Steven S. Skiena):
Increment(y)
if y = 0 then return(1) else
if (y mod 2) = 1 then
return(2 * Increment(y/2))
else return(y + 1)
I implemented it with JavaScript here: https://repl.it/#danielmai/IncrementalNaturalNumbers
function increment(y) {
if(y == 0) return 1
if(y % 2 == 1) {
return 2 * increment(y / 2)
}
else return y + 1
}
It doesn't work for odd numbers. I found out that JavaScript rounds up numbers with 0.5 or higher, so if y is odd, it will increment twice, i.e 5 -> 7.
I can use Math.floor(y/2) to make it round down, but I assume this function should work regardless of rounding up or down. So my question is, is there a way to correct this recursive function in JS without using Math.floor?
This has nothing to do with javascript rounding numbers up with 0.5 or higher.
Your increment function is asuming x / 2 will return an integer, but in javascript this will give a decimal number when odd. So when doing increment(3), you are recursively calling increment(1.5). As 1.5 % 2 = 1.5, its not == 1 so it ends up returning 2.5. So in the end, you end up returning 2.5 * 2 = 5.
This funcion would indeed work on c++ where if you are working with integers, division will trim trailing decimals. However, in javascript addition +, subtraction -, multiplication *, division /, power **, and modulo % all treat numbers in JavaScript as a double. Only binary operators treat numbers in JavaScript as a signed 32 bit integer.
Javascript doesn't have integer division, so you can't just do 5/2 and expect it to give you 2.
If you're looking for an alternative to Math.floor, this website has you covered. It has all of the alternatives you could ever want, and can check which will be the fastest on your browser.
This question already has answers here:
What number does 8e3 evaluate to?
(4 answers)
Closed 6 years ago.
var time_arr = process.hrtime(start);
var time = (time_arr[0] * 1e9 + time_arr[1]) / 1e6;
What does it mean when the calculation has to multiply by 1e9 and divide by 1e6 ?
What does it mean when the calculation has to multiply by 1e9 and divide by 1e6 ?
It means that it's multiplied by 1 billion and divided by 1 million.
1e9 means 1 * 10 to the 9th power, which is 1 billion (1000000000).
1e6 means 1 * 10 to the 6th power, which is 1 million (1000000).
You can think of it as 1 and 9 zeroes, 1 and 6 zeroes etc.
It doesn't have to be 1, it can be:
3e4 meaning 30000
or:
1.2e6 meaning 1200000
You can also use negative powers of ten for decimal fractions:
1e-1 is 0.1
1e-2 is 0.01
etc.
It's useful for very large or very small numbers where you don't want to count zeroes.
See the scientific notation on Wikipedia:
https://en.wikipedia.org/wiki/Scientific_notation
Javascript evaluates the following code snippet to -1.
-5 % 4
I understand that the remainder theorem states a = bq + r such that 0 ≤ r < b.
Given the definition above should the answer not be 3? Why does JavaScript return -1?
Because it's a remainder operator, not a modulo. But there's a proposal for a proper one.
A quote from Ecma 5.1
remainder r from a dividend n and a divisor d is defined by the
mathematical relation r = n − (d × q)
where q is an integer that is negative only if n/d is negative and
positive only if n/d is positive
Most programming languages use a symmetric modulo which is different than the mathematical one for negative values.
The mathematical modulo can be computed using the symmetric modulo like this:
a mod b = ((a % b) + b) % b
mod mathematical modulo
% symmetric modulo
The reason is that % is not a modulus but a remainder operator. See here
If you're using % to do modular arithmetic, it doesn't matter (conceptually, at least) whether -5 % 4 evaluates to –1 or 3, because those two numbers are congruent modulo 4: for the purposes of modular arithmetic, they're the same.
...if the remainder is nonzero, there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a and n. (http://en.wikipedia.org/wiki/Modulo_operation)
in python, which takes the sign of divisor:
-5 % 4 == 3 # -5 = (-2) * 4 + 3
in javascript, which takes the sign of divident:
-5 % 4 == -1 # -5 = (-1) * 4 - 1
Is there a simple way to find how many times a number goes into another number evenly in JavaScript?
Say 11 divided by 4 --- I know 4 goes into 11 2 times evenly
I have this code but I thought there was a simpler way that I maybe forgetting?
<script>
a = 11;
b = 4;
i = a % b;
i2 = a - i;
solution = i2 / b;
document.write(solution); // 2
</script>
What about...
Math.floor(11 / 4);
If you're wanting to handle negative numbers (thanks Ted Hopp), you could use ~~, |0 or any other bitwise trick that will treat its operand as a 32 bit signed integer. Keep in mind, besides this being confusing, it won't handle a number over 32bits.
~~(11 / 4);
You can use this trick:
(a / b) >> 0
Shifting by 0 truncates the fractional part. This will always round toward 0, which Math.floor will not do with negative numbers.