Get difference from next number divisible by 10 - c#

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

Related

I need help understanding the modulo operator

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.

Rounding up an int divided by 10 to nearest int

I want to divide an int by 10 and get a whole number closest to rounded one, so
12 should give 1 and 17 should give 2
Heres my code:
int BonusValue;
int Str;
BonusValue = Convert.ToInt32(Math.Round(Convert.ToDouble(Str) / 10));
Will this work?
Yes, that should work.
It is however much easier if you add half the rounding step, in your case 5, to the integer. Then you can do simple integer division:
BonusValue = (Str+5) / 10
Use Mathf.RoundToInt:
Debug.Log(Mathf.RoundToInt(17f/10f));
That would work, but it is an inefficient way of doing it.
The best way in unity to do what you want is using the built in methods to do exactly that
BonusValue = Mathf.RountToInt(Str/10f);
It will work but what round type do you need? Check this:MidpointRounding Enumeration

how to calculate proportional ratio

First of all pardon me to raise this question here (not sure). Not good in maths so need help from others to understand how to calculate.
I have to calculate proportional ratio score. For doing that i am taking two input values
ValueA = 3
ValueB = 344.
To find the percentage of the proportional ratio ((ValueB-ValueA)/ValueA )*100)
that formula gives me the score 11366.6.
Now i have to match with proportional percentage against with following table,
no idea how to match with percentage
for example the score comes around 43.12 % then i will pick the value 5 (>40 -50)
% Ratio Score
0 0
≤10 1
>10 – 20 2
>20 – 30 3
>30 – 40 4
>40 – 50 5
>50 – 60 6
>60 – 70 7
>70 – 80 8
>80 – 90 9
>90 – 100 10
your formula is of (as you can see by the 11366.6 percentage) - it should be
100.0*(ValueB-ValueA)/(double)ValueB
this will give you values in between 0 and 100 percent if ValueB is always bigger than ValueA (if not use):
100.0*Math.Abs(ValueB - ValueA)/(double)Math.Max(ValueA, ValueB)
based on the table your score should than be simply:
var score = (int)Math.Ceiling(percentage / 10.0)
You should swap value a and value b of you get percentages bigger than 100. By the way, finding the proportional value is not unique and the formula you have provided is one way to do that. I guess Valuea/valueb is also a possibility for example.

random at min&max values , enlight me

While i'm trying to code basic lottery app for myself ( note that i'm really beginner on programming especially c#), a guy on StackOverflow said to me
rnd.Next(1, 50 * 7) % 50 // Randoming like that will be increase to chance of getting 1 and 49
rnd.Next(1, 50 ) // instead of this
I am really wondering how can we test it ? Can we rely on this tests ? Please enlight me
The last example will get a uniform distribution between 1 and 49 (inclusive). That is, the same chance for any number between (and including) 1 and 49.
The first example is much more tricky. It will first create any number between 1 and 349. The modulo 50 maps the number onto the interval 0-49 (including 0 and 49).
We now introduce the possibility to get 0 - if the random number is 50, 100, 150, 200, 250 or 300.
We can also get number 1-49 through N+0, N+50, N+100, N+150, N+200, N+250, N+300
That is, 6 chances to get 0 and 7 to get any other number.
The conclusion is that the first example will give a random number betwen 0-49 (inclusive) with slightly less chance of 0 than for the other numbers.

Specifically, what does % do in C#?

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.

Categories

Resources