I need help with how this works. Microsoft's site doesn't have an answer so I was hoping there is a way to deal with this.
What would
stopwatch.ElapsedMilliseconds % 120000 == 0
mean?
It is in an if statement but I need to understand what is meant by that. Does it mean every 120 seconds (2 minutes), it will equal 0 and restart?
1000 milliseconds = 1 second and therefore 120 000 milliseconds = 2 minutes
% (Mod operator), means it returns the remainder of stopwatch.ElapsedMilliseconds divided by 120000.
== 0 means you're checking for a remainder of zero.
So therefor your statement is true when elapsed time is exactly divisible by 2 minutes (i.e. exactly 2 mins, 4 mins, 6 mins etc.), otherwise it will return false
The Modulus Operator (%) operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.
stopwatch.ElapsedMilliseconds % 120000 == 0 means, ElapsedMilliseconds is multiplications of 120000 (which is 2 minutes). So, your if condition hits for every 2 mins.
stopwatch.ElapsedMilliseconds % 120000 == 0
this statement is checking condition on milisecond elapsed in stopwatch and is it completely divisible by 120000, if it is then ok else there might be some other condition also
Related
Question: Assume integer variables x=10, y=5, z=3, w=2. The value of x+y%w is?
Possible answers: A: 11 B: 1 C: 12 D: none of the above
X+Y simple concatenation or addition operator with % which is remainder operator. Would this not be 10 + 5 =15 with a remainder 2? or 10 + 5 with a remainder of two?
x+y%w -> 10+5%2 = 11 because 5%2 (=1) has the highest priority
(x+y)%w -> (10+5)%2 = 1 because the parenthesis modify the priority of operator
% operator basically means divide and only use the remainder.
I was trying to recreate my C++ factor program from a few years ago in my new language C#. All I could remember is that it possibly involved a modulo, and possibly didn't. I knew that it involved at least one for and if statement. However, when I started trying to recreate it I kept getting nothing near what should be. I thought it had something to do with me not understanding loops, but it turns out I understand loops just fine. What I don't understand is how to use the modulo when performing math operations.
for instance what am I doing when I say something like:
(ignore that it might not actually work, it's just an example)
if(12 % 2 == 0)
{
Console.WriteLine("I don't understand.");
}
This kind of thing I don't quite have a grasp of yet. I realize that it is taking the remainder, and that's all I can grasp, not how it's actually used in real programming. I managed to get my factor program to work in C# after a bit of thinking and tinkering, it again doesn't mean I understand this operator or its uses. I no longer have access to the old C++ file.
The % (modulo) operator yields the remainder from the division. In your example the remainder is equal to 0 and the if evaluates to true (0 == 0). A classic example is when it's used to see if a number is even or not.
if (number % 2 == 0) {
// even
} else {
// odd
}
Think of modulo like a circle with a pointer (spinner), easiest example is a clock.
Notice how at the top it is zero.
The modulo function maps any value to one of those values on the spinner, think of the value to the left of the % as the number of steps around the spinner, and the second value as the number of total steps in the spinner, so we have the following.
0 % 12 = 0
1 % 12 = 1
12 % 12 = 0
13 % 12 = 1
We always start at 0.
So if we go 0 steps around a 12 step spinner we are still at 0, if we go 1 step from zero we are on 1, if we go 12 steps we are back at 0. If we go 13 we go all the way around and end at 1 again.
I hope this helps you visualize it.
It helps when you are using structures like an array, and you want to cycle through them. Imagine you have an array of the days of the week, 7 elements (mon-sunday). You want to always display the day 3 days from the current day. well Today is tuesday, so the array element is days[1], if we want to get the day 3 days from now we do days[1+3]; now this is alright, but what if we are at saturday (days[5]) and want to get 3 days from there? well we have days[5+3] which is an index out of bounds error as our array has only 7 elements (max index of 6) and we tried to access the 8th element.
However, knowing what you know about modulos and spinners now you can do the following:
string threeDaysFromNow = days[(currentDay + 3)%7]; When it goes over the bounds of the array, it wraps around and starts at the beginning again. There are many applications for this. Just remember the visualization of spinners, that is when it clicked in my head.
The modulo operator % returns the remainder of a division operation. For example, where 13 / 5 = 2, 13 % 5 = 3 (using integer math).
It's a common tactic to check a value against % 2 to see if it is even. If it is even, the remainder will be 0, otherwise it will be 1.
As for your specific use of it, you are doing 12 % 2 which is not only 0, but will always be 0. That will always make the if condition 12 % 2 == 0 true, which makes the if rather redundant.
as mentioned, it's commonly used for checking even/odd but also can use it to iterate loops at intervals, or split files into mod chunks. i personally use mod for clock face type problems as my data often navigates a circle.
the register is in mod for example an 8 bit register rolls over at 2^8 so so can force compliance into a register size var = mod(var, 256)
and the last thing i know about mod is that it is used in checksum and random number generation, but i haven't gone into the why for those. at all
An example where you could use this is in indexing arrays in certain for loops. For example, take the simple equation that defines the new pixel value of a resampled image using bicubic interpolation:
where
Don't worry what bicubic interpolation exactly is for the moment, we're just concerned about executing what seems to be two simple for loops: one for index i and one for index j. Note that the vector 'a' is 16 numbers long.
A simple for loop someone would try could be:
int n= 0;
for(int i = 0; i < 4; ++i)
{
for(int j = 0; i < 4; ++j)
{
pxy += a[n] * pow(x,i) * pow(y,j); // p(x,y)
n++; // n = 15 when finished
}
}
Or you could do it in one for loop:
for(int i = 0; i < 16; ++i)
{
int i_new = floor(i / 4.0); // i_new provides indices 0-3 incrementing every 4 iterations of loop
int j_new = i % 4; // j_new is reset to 0 when i is a multiple of 4
pxy += a[i] * pow(x,i_new) * pow(y,j_new); // p(x,y)
}
Printing i_new and j_new in the loop:
i_new j_new
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
3 0
3 1
3 2
3 3
As you can see, % can be very useful.
I have two times in Ticks like so:
//2016-01-22T17:34:52.648Z
var tick1 = 635890808926480754;
//2016-01-22T17:34:52.000Z
var tick2 = 635890808920000000;
Now as you can see comparing these two numbers tick1 == tick2 returns false
although the dates are the same (apart from milliseconds).
I would like to truncate the milliseconds off these numbers without converting it to a datetime (because this would reduce efficiency)
I have looked at Math.Round which says:
Rounds a value to the nearest integer or to the specified number of fractional digits.
and also Math.Truncate neither of which I think do what I need.
Looking at Datetime.Ticks it says:
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
Therefore I need to round the number down to the nearest ten million.
Is this possible?
You could use integer division:
if (tick1 / TimeSpan.TicksPerSecond == tick2 / TimeSpan.TicksPerSecond)
This works because if you divide a long/int by a long/int the result is also a long/int therefore truncating the decimal portion.
You can use this:
if(Math.Abs(tick1 - tick2) < TimeSpan.TicksPerSecond)
Which avoid doing divisions.
You may adjust the precision you need with any of the following:
TimeSpan.TicksPerDay
TimeSpan.TicksPerHour
TimeSpan.TicksPerMinute
TimeSpan.TicksPerSecond
TimeSpan.TicksPerMillisecond
Divide it by 1000 like this:
Long Seconds = 635890808926480754/1000
//Seconds = 635890808926480
Hello everyone How can I get the difference between a number and the next number divisible by 10 in c#?
Example.
15 should give 5. (20-15 =5)
21 should give 9. (30-21=9)
30 should give 0. (30-30=0)
Sorry for my english.
Thanks in advance.
Mod won't work, that will give you the opposite of what you want. 21%10=1 not 9 like you want. What you should do is 10-(num%10). This will get you much closer. The only result that will be wrong in this case is 30, which will give a result of 10.
so:
(10-(num%10))%10
This will give you the correct result everytime, although a bit cumbersome.
you should use modulus
15 % 10 will give 5
21 % 10 will give 1
30 % 10 will give 0
You are looking for the "mod" operator.
x=15;
y=x%10; //y=5
http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
The answer is not that stright forward. I dont know exactly the syntax for C#, but here is my approach. You'll need to adapt it to your code
x = 15;
y = x % 10;
if y > 0 return 10 - y
return 0
I have never came across anything that requires me to use it, and when I google what it does nothing comes up.
So, can someone please explain in detail, what does it do?
Its the modulus operator.
See the MSDN Link, although it doesn't have a great example.
It basically gets the remainder, when the first number is divided by the second.
Like 7 % 3 = 1. You can play with this on google.
As MSDN Example says, modding different types (doubles,decimals) results those types.
The most common use is in programs that need to check for an even number:
n % 2 == 0;// if the mod of n by 2 (remainder) is zero then n is even
Specifically like #BenVoigt says modulus actually takes the sign of the dividend.(unlike remainder which takes the sign of the divisor) It seems some languages implement it this way, there is a list here on wikipedia. So C# takes the sign of the dividend.
-7 % 3 = -1//in C#
-6 % 2 = 0// so even checks work ok with negative numbers in C#
But the result from google is 2?
It is the Modulo Operation. Returns the remainder when one integer is divided by another.
It is the modulus operator
http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
It lets you know what is left over once the first number has been divided by the second as many times as it can. For example:
5 % 2 = 1
Because 2 can only go into 5, 2 times (4), then all you have is 1 left over.
5 % 2.2 = 0.6
Because 2.2*2 is 4.4 and 5-4.4 is 0.6.